From 7b6a9c7b45c1725008b30cb66fb16f5c395e8475 Mon Sep 17 00:00:00 2001 From: karl Date: Sun, 21 Mar 2021 19:31:49 +0100 Subject: [PATCH] Add Input system for keyboard movement --- Input.h | 15 +++++++++++++++ MCRenderer.cpp | 25 +++++++++++++++++++++---- MCRenderer.h | 2 ++ main.cpp | 22 +++++++++++++++++++--- 4 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 Input.h diff --git a/Input.h b/Input.h new file mode 100644 index 0000000..6935a75 --- /dev/null +++ b/Input.h @@ -0,0 +1,15 @@ +#include + +class Input { + + public: + inline static std::map keys_down; + + static void set_key_down(int key, bool down) { + keys_down[key] = down; + } + + static bool is_key_down(int key) { + return keys_down[key]; + } +}; \ No newline at end of file diff --git a/MCRenderer.cpp b/MCRenderer.cpp index f59a747..d9c81c7 100644 --- a/MCRenderer.cpp +++ b/MCRenderer.cpp @@ -1,5 +1,6 @@ #include "MCRenderer.h" #include "Framebuffer3D.h" +#include "Input.h" #include "VertexBuffer.h" #include @@ -25,7 +26,26 @@ void MCRenderer::render(float delta) { noise_shader.use(); noise.bind_and_clear(); - noise_shader.setFloat("height", 0); + if (Input::is_key_down(GLFW_KEY_E)) { + height += delta; + } + if (Input::is_key_down(GLFW_KEY_Q)) { + height -= delta; + } + if (Input::is_key_down(GLFW_KEY_W)) { + camera.translate(glm::vec3(0.0, 0.0, -delta * 20.0)); + } + if (Input::is_key_down(GLFW_KEY_S)) { + camera.translate(glm::vec3(0.0, 0.0, delta * 20.0)); + } + if (Input::is_key_down(GLFW_KEY_D)) { + camera.rotate(-delta * 90.0, glm::vec3(0.0, 1.0, 0.0)); + } + if (Input::is_key_down(GLFW_KEY_A)) { + camera.rotate(delta * 90.0, glm::vec3(0.0, 1.0, 0.0)); + } + + noise_shader.setFloat("height", height); for (int i = 0; i < size_z; i++) { // Create one layer @@ -47,9 +67,6 @@ void MCRenderer::render(float delta) { glClearColor(0.6f, 0.9f, 0.9f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - // Set the camera position - camera.rotate(delta, glm::vec3(0.0, 1.0, 0.0)); - // Bind the camera to the rendering shader render_shader.setMat4("proj", camera.get_projection()); render_shader.setMat4("view", camera.get_view()); diff --git a/MCRenderer.h b/MCRenderer.h index 979da5d..571a4d1 100644 --- a/MCRenderer.h +++ b/MCRenderer.h @@ -16,6 +16,8 @@ class MCRenderer { int size_y; int size_z; + float height = 0.0; + Shader render_shader; Shader noise_shader; diff --git a/main.cpp b/main.cpp index 2d8e250..fa1de86 100644 --- a/main.cpp +++ b/main.cpp @@ -6,11 +6,18 @@ #include #include "Framebuffer3D.h" +#include "Input.h" #include "MCRenderer.h" #include "Shader.h" void framebuffer_size_callback(GLFWwindow *window, int width, int height); +static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { + if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE); + + Input::set_key_down(key, action == GLFW_RELEASE ? false : true); +} + // settings const unsigned int SCR_WIDTH = 1920; const unsigned int SCR_HEIGHT = 1080; @@ -44,6 +51,9 @@ int main() { return -1; } + // Keyboard input processing function + glfwSetKeyCallback(window, key_callback); + // configure global opengl state // ----------------------------- glEnable(GL_DEPTH_TEST); @@ -52,16 +62,22 @@ int main() { MCRenderer renderer = MCRenderer(128, 128, 128); // render loop - // ----------- + double timeInLastFrame = glfwGetTime(); + double elapsed_time = 0.0; + while (!glfwWindowShouldClose(window)) { - renderer.render(0.1); // TODO: Proper delta + // Delta time handling + double delta = glfwGetTime() - timeInLastFrame; + timeInLastFrame = glfwGetTime(); + elapsed_time += delta; + + renderer.render(delta); // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) glfwSwapBuffers(window); glfwPollEvents(); } - glfwTerminate(); return 0; }