From 9a229cf771c7572a37f2d1054c5397e361b4ee0b Mon Sep 17 00:00:00 2001 From: maelstrom Date: Fri, 27 Sep 2024 22:34:32 +0200 Subject: [PATCH] LOOK AT IT. ADMIIIIIRE IT. --- src/main.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++- src/renderer.cpp | 4 ++-- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index fa69765..315e15e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,14 +4,25 @@ #include #include "renderer.h" +#include "camera.h" void errorCatcher(int id, const char* str); +Camera camera(glm::vec3(0.0, 0.0, 3.0)); + +// void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); +void processInput(GLFWwindow* window); +void mouseCallback(GLFWwindow* window, double xpos, double ypos); +// void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); +void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods); + int main() { glfwSetErrorCallback(errorCatcher); glfwInit(); GLFWwindow *window = glfwCreateWindow(1200, 900, "GLTest", NULL, NULL); + glfwSetMouseButtonCallback(window, mouseButtonCallback); + glfwSetCursorPosCallback(window, mouseCallback); glfwMakeContextCurrent(window); glewInit(); @@ -19,13 +30,60 @@ int main() { renderInit(window); do { + processInput(window); render(window); glfwSwapBuffers(window); glfwPollEvents(); } while(!glfwWindowShouldClose(window)); + + glfwTerminate(); + return 0; } void errorCatcher(int id, const char* str) { printf("Something *terrible* happened. Here's the briefing: [%d] %s\n", id, str); -} \ No newline at end of file +} + +float lastTime; +void processInput(GLFWwindow* window) { + float deltaTime = glfwGetTime() - lastTime; + lastTime = glfwGetTime(); + + if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) + camera.processMovement(DIRECTION_FORWARD, deltaTime); + if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) + camera.processMovement(DIRECTION_BACKWARDS, deltaTime); + if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) + camera.processMovement(DIRECTION_LEFT, deltaTime); + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) + camera.processMovement(DIRECTION_RIGHT, deltaTime); +} + +bool mouseCapturing = false; +float lastX = 0, lastY = 0; +bool mouseFirst = true; +void mouseCallback(GLFWwindow* window, double xpos, double ypos) { + if (!mouseCapturing) return; + + if (mouseFirst) { + lastX = xpos, lastY = ypos; + mouseFirst = false; + } + + float deltaX = xpos - lastX, deltaY = ypos - lastY; + lastX = xpos, lastY = ypos; + + camera.processRotation(deltaX, deltaY); +} + +void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) { + if (button == GLFW_MOUSE_BUTTON_2) + mouseCapturing = action == GLFW_PRESS; + if (mouseCapturing) { + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + } else { + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + mouseFirst = true; + } +} diff --git a/src/renderer.cpp b/src/renderer.cpp index a547fb3..ad2990e 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -12,10 +12,10 @@ #include "renderer.h" Shader *shader = NULL; -Camera camera(glm::vec3(0.0, 0.0, 3.0)); +extern Camera camera; void renderInit(GLFWwindow* window) { - glViewport(0, 0, 500, 500); + glViewport(0, 0, 1200, 900); initMeshes();