From d7efcff2c8a31bbbb2764702d639ff7739a001b4 Mon Sep 17 00:00:00 2001 From: miaurizius Date: Fri, 24 Apr 2026 13:23:15 +0200 Subject: [PATCH] added camera movement --- CMakeLists.txt | 3 ++- include/Camera.hpp | 31 +++++++++++++++++++++++ src/main.cpp | 63 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 83 insertions(+), 14 deletions(-) create mode 100644 include/Camera.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 02a17ac..54f33a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,8 @@ add_executable(${PROJECT_NAME} src/main.cpp src/Shader.cpp include/Shader.hpp include/IndexBuffer.hpp - libs/stb_image.hpp) + libs/stb_image.hpp + include/Camera.hpp) target_compile_definitions(${PROJECT_NAME} PRIVATE $<$:_DEBUG>) diff --git a/include/Camera.hpp b/include/Camera.hpp new file mode 100644 index 0000000..d7ca7a4 --- /dev/null +++ b/include/Camera.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include "glm/glm.hpp" +#include "glm/ext/matrix_transform.hpp" +#include "glm/gtc/matrix_transform.hpp" + +class Camera { +private: + glm::mat4 projection; + glm::mat4 view; + glm::mat4 viewProj; + +public: + Camera(float fov, float width, float height) { + projection = glm::perspective(fov/2.0f, width/height, 0.1f, 1000.f); + view = glm::mat4(1.0f); + update(); + } + + glm::mat4 getViewProj() { + return viewProj; + } + + void update() { + viewProj = projection * view; + } + + void translate(glm::vec3 v) { + view = glm::translate(view, v*-1.0f); + } +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 553a4ad..8002fbf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ #include "VertexBuffer.hpp" #include "Shader.hpp" #define STB_IMAGE_IMPLEMENTATION +#include "Camera.hpp" #include "stb_image.hpp" #include "glm/glm.hpp" #include "glm/ext/matrix_transform.hpp" @@ -80,7 +81,7 @@ int main() { #ifdef _DEBUG SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); //remove for stable release #endif - + SDL_GL_SetSwapInterval(1); uint32 flags = SDL_WINDOW_OPENGL; //| SDL_WINDOW_FULLSCREEN; @@ -169,12 +170,11 @@ int main() { auto model = glm::mat4(1.0f); model = glm::scale(model, glm::vec3(1.2f)); - auto projection = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.0f, 100.0f); - projection = glm::perspective(glm::radians(45.0f), 4.0f/3.0f, 0.1f, 100.0f); + Camera camera(90.0f, 800.0f, 600.0f); + camera.translate(glm::vec3(0.0f, 0.0f, 5.0f)); + camera.update(); - auto view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -3.0f)); - - auto modelViewProj = projection * view * model; + auto modelViewProj = camera.getViewProj() * model; int modelViewProjMatrixLocation = glGetUniformLocation(shader.getShaderId(), "u_modelViewProj"); @@ -185,13 +185,56 @@ int main() { bool running = true; SDL_Event e; + + bool btnW; + bool btnS; + bool btnA; + bool btnD; + while (running) { + while (SDL_PollEvent(&e)) { + if (e.type == SDL_EVENT_QUIT) { + running = false; + } else if (e.type == SDL_EVENT_KEY_DOWN) { + switch (e.key.key) { + case SDLK_W: btnW = true; break; + case SDLK_S: btnS = true; break; + case SDLK_A: btnA = true; break; + case SDLK_D: btnD = true; break; + default: break; + } + } else if (e.type == SDL_EVENT_KEY_UP) { + switch (e.key.key) { + case SDLK_W: btnW = false; break; + case SDLK_S: btnS = false; break; + case SDLK_A: btnA = false; break; + case SDLK_D: btnD = false; break; + default: break; + } + } + } + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); time += delta; + if (btnW) { + camera.translate(glm::vec3(0.0f, 0.0f, -2.0f*delta)); + } + if (btnS) { + camera.translate(glm::vec3(0.0f, 0.0f, 2.0f*delta)); + } + if (btnA) { + camera.translate(glm::vec3(-1.0f*delta, 0.0f, 0.0f)); + } + if (btnD) { + camera.translate(glm::vec3(1.0f*delta, 0.0f, 0.0f)); + } + + camera.update(); + model = glm::rotate(model, 1.0f*delta, glm::vec3(0.0f, 1.0f, 0.0f)); - modelViewProj = projection * view * model; + modelViewProj = camera.getViewProj() * model; if (colorUniformLocation != 1) { glUniform4f(colorUniformLocation, sinf(time)*sinf(time), 0.0f, 1.0f, 1.0f); @@ -209,12 +252,6 @@ int main() { SDL_GL_SwapWindow(window); - while (SDL_PollEvent(&e)) { - if (e.type == SDL_EVENT_QUIT) { - running = false; - } - } - const uint64 endCounter = SDL_GetPerformanceCounter(); const uint64 counterElapsed = endCounter - lastCounter; delta = (static_cast(counterElapsed) / static_cast(perfCounterFrequency));