From c64b7b4bdebae42ab1ecd9f856e14722e694b710 Mon Sep 17 00:00:00 2001 From: connellpaxton Date: Tue, 30 Jan 2024 11:41:47 -0500 Subject: [PATCH] Created camera movement in the Input class, next step: cursor control --- Input/Input.cpp | 64 +++++++++++++++++++++++++++++++++++++++++------ Input/Input.hpp | 20 +++++++++------ Window/Window.cpp | 1 - pléascach.cpp | 16 ++++++------ 4 files changed, 77 insertions(+), 24 deletions(-) diff --git a/Input/Input.cpp b/Input/Input.cpp index 086efec..4310043 100644 --- a/Input/Input.cpp +++ b/Input/Input.cpp @@ -5,6 +5,9 @@ #include +#include + +#include bool Input::shouldClose() { return glfwWindowShouldClose(in); @@ -18,10 +21,10 @@ Input::Input(INPUT_PTR in) : in(in) { glfwSetWindowUserPointer(in, this); glfwSetWindowSizeCallback(in, [](GLFWwindow* in, int width, int height) { - Log::info("Event Received: Resize to %dx%d\n", width, height); + Log::info("Event Received:Resize to %dx%d\n", width, height); Input* input = reinterpret_cast(glfwGetWindowUserPointer(in)); input->queue.push(InputEvent{ - .tag = InputEvent::Tag::RESIZE, + .tag = InputEvent::Tag::eRESIZE, .resize = { .width = width, .height = height, @@ -32,17 +35,17 @@ Input::Input(INPUT_PTR in) : in(in) { glfwSetKeyCallback(in, [](GLFWwindow* in, int key, int scancode, int action, int mods) { int state = (action == GLFW_PRESS || action == GLFW_REPEAT); static const char* action_strings[] = { "RELEASE", "PRESS", "REPEAT" }; - Log::info("Event Received: Key: \"%s\" %s\n", glfwGetKeyName(key, scancode), action_strings[action]); + Log::info("Event Received:Key:\"%s\" %s\n", glfwGetKeyName(key, scancode), action_strings[action]); Input* input = reinterpret_cast(glfwGetWindowUserPointer(in)); - InputModifierBit i_mods = NONE; + InputModifierBit i_mods = eNONE; if(mods &= GLFW_MOD_SHIFT) - i_mods |= SHIFT; + i_mods |= eSHIFT; if (mods &= GLFW_MOD_CONTROL) - i_mods |= CNTRL; + i_mods |= eCNTRL; input->queue.push(InputEvent{ - .tag = InputEvent::Tag::KEY, + .tag = InputEvent::Tag::eKEY, .key = { .key = key, .state = state, @@ -55,7 +58,7 @@ Input::Input(INPUT_PTR in) : in(in) { Input* input = reinterpret_cast(glfwGetWindowUserPointer(in)); input->queue.push(InputEvent{ - .tag = InputEvent::Tag::CURSOR, + .tag = InputEvent::Tag::eCURSOR, .pos = { .x = x, .y = y, @@ -63,3 +66,48 @@ Input::Input(INPUT_PTR in) : in(in) { }); }); } + +void Input::handleMovementKeys(Renderer& ren) { + const auto forward = glm::vec3(glm::cos(ren.cam.phi), 0.0, glm::sin(ren.cam.phi)); + const auto right = glm::cross(forward, glm::vec3(0.0, 1.0, 0.0)); + const auto speed = glfwGetKey(in, GLFW_KEY_LEFT_SHIFT)? 2.0f : 1.0f; + + if(glfwGetKey(in, GLFW_KEY_UP)) { + ren.cam.theta -= 0.01; + } + + if(glfwGetKey(in, GLFW_KEY_UP)) { + ren.cam.theta -= 0.01; + } + if(glfwGetKey(in, GLFW_KEY_DOWN)) { + ren.cam.theta += 0.01; + } + + if(glfwGetKey(in, GLFW_KEY_LEFT)) { + ren.cam.phi -= 0.03; + } + + if(glfwGetKey(in, GLFW_KEY_RIGHT)) { + ren.cam.phi += 0.03; + } + /* move "forward" or "backward" */ + if (glfwGetKey(in, GLFW_KEY_W)) { + ren.cam.pos += forward * 0.1f * speed; + } + + if (glfwGetKey(in, GLFW_KEY_S)) { + ren.cam.pos += forward * -0.1f * speed; + } + + /* move "left" or "right" */ + if (glfwGetKey(in, GLFW_KEY_A)) { + ren.cam.pos -= right * 0.1f * speed; + } + + if (glfwGetKey(in, GLFW_KEY_D)) { + ren.cam.pos += right * 0.1f * speed; + } + + ren.cam.theta = glm::clamp(ren.cam.theta, 0.01f, glm::pi() - 0.01f); + ren.cam.phi = glm::mod(ren.cam.phi, glm::two_pi()); +} diff --git a/Input/Input.hpp b/Input/Input.hpp index 35a84fd..273186c 100644 --- a/Input/Input.hpp +++ b/Input/Input.hpp @@ -8,9 +8,9 @@ #include enum InputModifierBit { - NONE = 0x00, - SHIFT = 0x01, - CNTRL = 0x02, + eNONE = 0x00, + eSHIFT = 0x01, + eCNTRL = 0x02, }; constexpr InputModifierBit operator & (const InputModifierBit a, const InputModifierBit b) { @@ -32,11 +32,11 @@ constexpr u32 operator ~ (InputModifierBit a) { struct InputEvent { enum Tag { - EXIT, - RESIZE, - CURSOR, - KEY, - BUTTON, + eEXIT, + eRESIZE, + eCURSOR, + eKEY, + eBUTTON, } tag; union { @@ -54,6 +54,8 @@ struct InputEvent { }; }; +struct Renderer; + struct Input { Input(INPUT_PTR in); @@ -63,5 +65,7 @@ struct Input { std::queue queue; + void handleMovementKeys(Renderer& ren); + bool shouldClose(); }; \ No newline at end of file diff --git a/Window/Window.cpp b/Window/Window.cpp index c3ba769..b71f9cd 100644 --- a/Window/Window.cpp +++ b/Window/Window.cpp @@ -16,7 +16,6 @@ static void win_err_callback(int code, const char* msg) { Window::Window(const std::string& title, u32 width, u32 height) : width(width), height(height) { glfwSetErrorCallback(win_err_callback); - if (!glfwInit()) { Log::error("Failed to initialize GLFW\n"); return; diff --git a/pléascach.cpp b/pléascach.cpp index 32dd45a..d58ffa1 100644 --- a/pléascach.cpp +++ b/pléascach.cpp @@ -7,6 +7,9 @@ #include +#include + + int main(int argc, char* argv[]) { try { Window win(argv[0], 256, 512); @@ -16,26 +19,25 @@ int main(int argc, char* argv[]) { while (!in->shouldClose()) { Timer frame_timer; in->poll(); + in->handleMovementKeys(ren); while (in->queue.size()) { /* take event from front of queue, then process it */ const auto& event = in->queue.front(); in->queue.pop(); switch (event.tag) { - case InputEvent::Tag::RESIZE: + case InputEvent::Tag::eRESIZE: Log::info("Event Processed: Resized to %dx%d\n", event.resize.width, event.resize.height); /* no need to have a resize() function in the renderer, b/c swapchain images will be * automatically marked out-of-date, and recreation will be triggered in our code */ break; - case InputEvent::Tag::KEY: - Log::info("Event Processed: Button 0x%x %d\n", event.key.key, event.key.state); - break; - case InputEvent::Tag::EXIT: + case InputEvent::Tag::eEXIT: win.close(); break; - case InputEvent::Tag::CURSOR: - case InputEvent::Tag::BUTTON: + case InputEvent::Tag::eCURSOR: + break; + case InputEvent::Tag::eBUTTON: break; } }