diff --git a/core/Screen.cpp b/core/Screen.cpp new file mode 100644 index 0000000..ef12cab --- /dev/null +++ b/core/Screen.cpp @@ -0,0 +1,80 @@ +#include "Screen.h" + +const Vertex vertices[] = { + Vertex(-1.0f, -1.0f, 0.0f, 0.0f, 0.0f), + Vertex(-1.0f, 1.0f, 0.0f, 0.0f, 1.0f), + Vertex(1.0f, -1.0f, 0.0f, 1.0f, 0.0f), + + Vertex(-1.0f, 1.0f, 0.0f, 0.0f, 1.0f), + Vertex(1.0f, -1.0f, 0.0f, 1.0f, 0.0f), + Vertex(1.0f, 1.0f, 0.0f, 1.0f, 1.0f) +}; + +Screen::Screen() { + // Setup quad + glGenVertexArrays(1, &quad); + glBindVertexArray(quad); + + Id vbo; + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + // Vertex positions + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) 0); + //Vertex UV + glEnableVertexAttribArray(1); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*) (3 * sizeof(float))); + + glBindVertexArray(0); + + // Setup frame buffers + glGenFramebuffers(1, &framebuffer); + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); + + tex.width = width; + tex.height = height; + glGenTextures(1, &tex.id); + glBindTexture(GL_TEXTURE_2D, tex.id); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glBindTexture(GL_TEXTURE_2D, 0); + + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex.id, 0); + + // Setup render buffer + glGenRenderbuffers(1, &renderbuffer); + glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderbuffer); + + if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { + std::cerr << "Error setting up screen framebuffer." << std::endl; + exit(1); + } + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +void Screen::Enable() { + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); + glClearColor(0.5f, 0.5f, 0.5f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_DEPTH_TEST); +} + +void Screen::Disable() { + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glClearColor(1.0f, 1.0f, 1.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + glDisable(GL_DEPTH_TEST); +} + +void Screen::Draw() { + glBindVertexArray(quad); + glDrawArrays(GL_TRIANGLES, 0, 6); + glBindVertexArray(0); +} diff --git a/core/Screen.h b/core/Screen.h new file mode 100644 index 0000000..eeb95af --- /dev/null +++ b/core/Screen.h @@ -0,0 +1,28 @@ +#ifndef SCREEN_H +#define SCREEN_H + +#include +#include + +#include + +#include "types.h" +#include "Vertex.h" +#include "Material.h" + +class Screen { +public: + Screen(); + void Enable(); + void Disable(); + void Draw(); + const int width = 640; + const int height = 480; + Texture tex; +private: + Id quad; + Id framebuffer; + Id renderbuffer; +}; + +#endif /* SCREEN_H */ diff --git a/core/Shaders/ScreenShader.cpp b/core/Shaders/ScreenShader.cpp new file mode 100644 index 0000000..5e7116d --- /dev/null +++ b/core/Shaders/ScreenShader.cpp @@ -0,0 +1,5 @@ +#include "ScreenShader.h" +#include "screen.vert.h" +#include "screen.frag.h" + +ScreenShader::ScreenShader() : Shader(screen_vert, screen_frag) {} diff --git a/core/Shaders/ScreenShader.h b/core/Shaders/ScreenShader.h new file mode 100644 index 0000000..a3026ed --- /dev/null +++ b/core/Shaders/ScreenShader.h @@ -0,0 +1,6 @@ +#include "Shader.h" + +class ScreenShader : public Shader { +public: + ScreenShader(); +}; diff --git a/core/couch.cpp b/core/couch.cpp index bc15a34..15903c9 100644 --- a/core/couch.cpp +++ b/core/couch.cpp @@ -9,6 +9,10 @@ #include "types.h" #include "Shaders/FlatShader.h" +#include "Shaders/ScreenShader.h" + +#include "Screen.h" + #include "Ball.h" #include "Camera.h" #include "Input.h" @@ -80,7 +84,6 @@ int main() { } glViewport(0, 0, width, height); - glEnable(GL_DEPTH_TEST); root = Node::GetRoot(); @@ -88,12 +91,13 @@ int main() { input->Use(window); Camera defaultCamera; + + Screen screen; + ScreenShader *screenShader = new ScreenShader(); FlatShader *shader = new FlatShader(); - shader->Use(); Matrix projection = glm::perspective(glm::radians(45.0f), 800.0f/600.0f, 0.1f, 100.0f); - shader->UpdateProjection(projection); // TODO Allow multiple scripting languages Lua *lua = new Lua(); @@ -103,11 +107,13 @@ int main() { double delta = 0.0; while(!glfwWindowShouldClose(window)) { - glClearColor(0.5f, 0.5f, 0.5f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + // Start rendering to texture; + screen.Enable(); lua->Update(delta); + shader->Use(); + shader->UpdateProjection(projection); Matrix view(1.0f); Camera *camera = Camera::GetCurrentCamera(); view = glm::rotate(view, -camera->transform.rotation.x, Vector3(1.0f, 0.0f, 0.0f)); @@ -118,6 +124,14 @@ int main() { // Render the scene tree render(root, shader, Matrix(1.0f)); + + // Stop rendering to texture + screen.Disable(); + // // Render the screen + screenShader->Use(); + screenShader->UpdateTex(true, screen.tex); + glViewport(0, 0, width, height); + screen.Draw(); glfwSwapBuffers(window); glfwPollEvents(); diff --git a/roadmap.md b/roadmap.md index 0262779..5ec425c 100644 --- a/roadmap.md +++ b/roadmap.md @@ -36,3 +36,19 @@ For now, what we've got is good ### Better error messages Okay, now I want a message other than "main.lua not found!" if the lua file fucks up. Also, I want Lua errors to kill the program with an error message and a stack trace. + +### Added model importing +I did this without writing about it in the roadmap. I still want to add a way to get multiple meshes on the same model, but +I'll save skeletal animation until we have a working physics system + +### Refactoring pass +I want to clean things up and see if some of the code can be combined into multiple files. +Also, I want a better user experience, so I might wind up removeing the %includes from the +swig files and customizing the interface by hand. I also want to add a whole mess of constants +so that I don't have to test for keypresses. Lastly, I want to test if a function exists before +calling it from the lua file, so stubs aren't necessary to prevent a crash + +- [ ] Combine files +- [ ] Better interface +- [ ] Constants for keys and the like +- [ ] No crash on calling non-existant scripting hooks diff --git a/shaders/CMakeLists.txt b/shaders/CMakeLists.txt index 9946efe..09ee23a 100644 --- a/shaders/CMakeLists.txt +++ b/shaders/CMakeLists.txt @@ -10,7 +10,9 @@ endmacro() add_shader(flat.vert) add_shader(flat.frag) +add_shader(screen.vert) +add_shader(screen.frag) add_custom_target(shader_headers - DEPENDS flat.vert.h flat.frag.h + DEPENDS flat.vert.h flat.frag.h screen.vert.h screen.frag.h COMMENT "Generated shaders headers.") diff --git a/shaders/screen.frag b/shaders/screen.frag new file mode 100644 index 0000000..2194903 --- /dev/null +++ b/shaders/screen.frag @@ -0,0 +1,18 @@ +#version 330 core + +in vec2 UV; + +out vec4 FragColor; + +struct Material { + vec3 color; + bool usesColor; + sampler2D tex; + bool usesTex; +}; + +uniform Material material; + +void main() { + FragColor = texture(material.tex, UV); +} diff --git a/shaders/screen.vert b/shaders/screen.vert new file mode 100644 index 0000000..0f1317d --- /dev/null +++ b/shaders/screen.vert @@ -0,0 +1,11 @@ +#version 330 core + +layout (location = 0) in vec3 pos; +layout (location = 1) in vec2 uv; + +out vec2 UV; + +void main() { + gl_Position = vec4(pos, 1.0); + UV = uv; +} diff --git a/thirdparty/assimp/code/.#CMakeLists.txt b/thirdparty/assimp/code/.#CMakeLists.txt deleted file mode 120000 index 78a3133..0000000 --- a/thirdparty/assimp/code/.#CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -dane@sweetdee.1202:1610989317 \ No newline at end of file