From 53810dd99fcd17391e318d68aa11d06236548562 Mon Sep 17 00:00:00 2001 From: karl Date: Sun, 13 Dec 2020 00:12:49 +0100 Subject: [PATCH] Toggle-able AA button Q toggles anti-aliasing (handled in RenderSystem) --- ECS/Components/Texture.h | 2 +- ECS/Systems/RenderSystem.h | 27 ++++++++++++++++++++++++++- main.cpp | 5 ++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/ECS/Components/Texture.h b/ECS/Components/Texture.h index c9ab2d8..611b24d 100644 --- a/ECS/Components/Texture.h +++ b/ECS/Components/Texture.h @@ -62,7 +62,7 @@ struct Texture { explicit Texture(const std::string& path, Settings settings, bool transparent) : id(loadTexture(path, settings)), render_transparent(transparent) {} void addNormalmap(const std::string& path, Settings settings) { - normal_id = loadTexture(path, settings); + normal_id = loadTexture(path, settings); } }; diff --git a/ECS/Systems/RenderSystem.h b/ECS/Systems/RenderSystem.h index ef0bf0a..2e42b13 100644 --- a/ECS/Systems/RenderSystem.h +++ b/ECS/Systems/RenderSystem.h @@ -48,7 +48,7 @@ void renderQuad() glBindVertexArray(0); } -class RenderSystem : public EntitySystem { +class RenderSystem : public EntitySystem, public EventSubscriber { public: struct RenderObject { RenderObject(const glm::mat4 &matrix, const glm::vec3 &origin, unsigned int textureId, unsigned int normalId, const Mesh &mesh, float distance, const Material &material) @@ -63,6 +63,7 @@ public: void render(Shader shader) const { glm::mat4 model_matrix = matrix; model_matrix[3] = glm::vec4(origin, 1.0); + shader.setMat4("model", model_matrix); // 0 can't be a valid texture name, so we use it for meshes without textures here @@ -167,6 +168,28 @@ public: } RenderSystem() { + setup(); + } + + void configure(World *pWorld) override { + myWorld = pWorld; + + myWorld->subscribe(this); + } + + void receive(World *pWorld, const InputEvent &event) override { + if (event.key == GLFW_KEY_Q) { + if (event.action == GLFW_PRESS) { + if (glIsEnabled(GL_MULTISAMPLE)) { + glDisable(GL_MULTISAMPLE); + } else { + glEnable(GL_MULTISAMPLE); + } + } + } + } + + void setup() { // Configure depth map glGenFramebuffers(1, &depthMapFBO); @@ -274,6 +297,8 @@ public: unsigned int depthMap; unsigned int depthMapFBO; + + World *myWorld; }; #endif //ECSGAME_RENDERSYSTEM_H diff --git a/main.cpp b/main.cpp index 82c80d0..5a491b1 100644 --- a/main.cpp +++ b/main.cpp @@ -45,6 +45,9 @@ int main() { /* Initialize the library */ if (!glfwInit()) return -1; + + // Anti Aliasing + glfwWindowHint(GLFW_SAMPLES, 4); /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(1280, 720, "ECSGame", NULL, NULL); @@ -204,7 +207,7 @@ int main() { renderSystem->render(world, defaultShader, shadowShader, debugShader); ring->get()->rotate(delta * 100.0, glm::vec3(0.0, 1.0, 0.0)); - sun->get()->direction = glm::normalize(glm::vec3(glm::rotate(glm::mat4(1), (float)elapsed_time * 0.3f, glm::vec3(0.0, 1.0, 0.0)) * glm::vec4(1.0, 1.0, 1.0, 0.0))); + sun->get()->direction = glm::normalize(glm::vec3(glm::rotate(glm::mat4(1), (float)elapsed_time * 0.5f, glm::vec3(0.0, 1.0, 0.0)) * glm::vec4(1.0, 1.0, 1.0, 0.0))); /* Swap front and back buffers */ glfwSwapBuffers(window);