From 02b0f6a67aee1e7b306d6d79f617045319fc7a53 Mon Sep 17 00:00:00 2001 From: karl Date: Tue, 7 Jan 2020 15:35:12 +0100 Subject: [PATCH] Nonfunctional new rendering --- ECS/Components/Mesh.h | 6 +-- ECS/Systems/RenderSystem.h | 9 +++- Rendering/Shader.cpp | 2 +- Shaders/default-fragment.fs | 3 +- Shaders/default-vertex.vs | 11 ++-- main.cpp | 105 +++++++++++++++++------------------- 6 files changed, 67 insertions(+), 69 deletions(-) diff --git a/ECS/Components/Mesh.h b/ECS/Components/Mesh.h index eb28920..e5c0715 100644 --- a/ECS/Components/Mesh.h +++ b/ECS/Components/Mesh.h @@ -10,14 +10,14 @@ #include struct Mesh { - explicit Mesh(std::vector vertices_vector) : vertices(&vertices_vector[0]), vertex_count(vertices_vector.size()) { + explicit Mesh(std::vector vertices) : vertices(vertices), vertex_count(vertices.size() / 5) { glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(&vertices), &vertices, GL_STATIC_DRAW); // position attribute glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0); @@ -39,7 +39,7 @@ private: unsigned int vertex_count; /// Vertices in the format x, y, z, u, v - float *vertices; + std::vector vertices; }; #endif //ECSGAME_MESH_H diff --git a/ECS/Systems/RenderSystem.h b/ECS/Systems/RenderSystem.h index f36c714..dcb03e5 100644 --- a/ECS/Systems/RenderSystem.h +++ b/ECS/Systems/RenderSystem.h @@ -16,9 +16,14 @@ using namespace ECS; class RenderSystem : public EntitySystem { public: void render(World *pWorld, Shader shader) { - pWorld->each([&](Entity *ent, ComponentHandle camera, ComponentHandle camera_transform) { + pWorld->each([&](Entity *ent, ComponentHandle camera, ComponentHandle cameraTransform) { + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + shader.use(); + shader.setMat4("projection", camera->projection); - shader.setMat4("view", camera_transform->matrix); + shader.setMat4("view", cameraTransform->matrix); pWorld->each([&](Entity *ent, ComponentHandle mesh, ComponentHandle transform) { shader.setMat4("model", transform->matrix); diff --git a/Rendering/Shader.cpp b/Rendering/Shader.cpp index 7350bc3..d957299 100644 --- a/Rendering/Shader.cpp +++ b/Rendering/Shader.cpp @@ -89,7 +89,7 @@ void Shader::setFloat(const std::string &name, float value) const { } void Shader::setMat4(const std::string &name, glm::mat4 mat) const { - glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 10, GL_FALSE, glm::value_ptr(mat)); + glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, glm::value_ptr(mat)); } void Shader::checkCompileErrors(unsigned int shader, const std::string &type) { diff --git a/Shaders/default-fragment.fs b/Shaders/default-fragment.fs index 98fbdbe..acc3521 100644 --- a/Shaders/default-fragment.fs +++ b/Shaders/default-fragment.fs @@ -1,8 +1,7 @@ #version 320 es out mediump vec4 FragColor; -in mediump vec3 COLOR; void main() { - FragColor = vec4(COLOR, 1.0); + FragColor = vec4(1.0, 1.0, 1.0, 1.0); } \ No newline at end of file diff --git a/Shaders/default-vertex.vs b/Shaders/default-vertex.vs index 7b1b9df..661a5f9 100644 --- a/Shaders/default-vertex.vs +++ b/Shaders/default-vertex.vs @@ -1,11 +1,12 @@ #version 320 es -layout (location = 0) in vec3 aPos; // the position variable has attribute position 0 -layout (location = 1) in vec3 aColor; // the color variable has attribute position 1 +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 uvPos; -out vec3 COLOR; // output a color to the fragment shader +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; void main() { - gl_Position = vec4(aPos, 1.0); - COLOR = aColor; + gl_Position = projection * view * model * vec4(aPos, 1.0); } \ No newline at end of file diff --git a/main.cpp b/main.cpp index 4e8764e..71aed38 100644 --- a/main.cpp +++ b/main.cpp @@ -51,11 +51,15 @@ int main() { return -1; } + glEnable(GL_DEPTH_TEST); + // TODO: Could be automated by getting all classes within 'ECS/Systems' // world->registerSystem(new GravitySystem(-9.8f)); world->registerSystem(new PositionDebugOutputSystem()); world->registerSystem(new KeyboardMovementSystem()); - world->registerSystem(new RenderSystem()); + + RenderSystem* renderSystem = new RenderSystem(); + world->registerSystem(renderSystem); Entity *player = world->create(); player->assign(); @@ -64,53 +68,53 @@ int main() { Entity *box = world->create(); box->assign(); + box->assign(std::vector{ + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, + + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + + -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, + + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, + -0.5f, 0.5f, -0.5f, 0.0f, 1.0f + }); box->get()->translate(glm::vec3(0.0f, 0.0f, -10.0f)); Shader defaultShader("Shaders/default-vertex.vs", "Shaders/default-fragment.fs"); - // set up vertex data (and buffer(s)) and configure vertex attributes - // ------------------------------------------------------------------ - float vertices[] = { - // positions // colors - 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left - 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top - }; - unsigned int indices[] = { // note that we start from 0! - 0, 1, 2 // first Triangle - }; - unsigned int VBO, VAO, EBO; - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - glGenBuffers(1, &EBO); - // bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s). - glBindVertexArray(VAO); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - - // position attribute - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); - glEnableVertexAttribArray(0); - - // color attribute - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3* sizeof(float))); - glEnableVertexAttribArray(1); - glEnableVertexAttribArray(0); - - // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind - glBindBuffer(GL_ARRAY_BUFFER, 0); - - // remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound. - //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - - // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other - // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary. - glBindVertexArray(0); - double timeInLastFrame = glfwGetTime(); double elapsed_time = 0.0; @@ -124,18 +128,7 @@ int main() { std::cout << "Elapsed time: " << elapsed_time << std::endl; world->tick(delta); - - /* Render here */ - // render - // ------ - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - - // draw our first triangle - defaultShader.use(); - glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized - glDrawArrays(GL_TRIANGLES, 0, 3); - // glBindVertexArray(0); // no need to unbind it every time + renderSystem->render(world, defaultShader); /* Swap front and back buffers */ glfwSwapBuffers(window);