added camera movement

This commit is contained in:
2026-04-24 13:23:15 +02:00
parent af1e96ce6d
commit d7efcff2c8
3 changed files with 83 additions and 14 deletions

View File

@@ -13,7 +13,8 @@ add_executable(${PROJECT_NAME} src/main.cpp
src/Shader.cpp src/Shader.cpp
include/Shader.hpp include/Shader.hpp
include/IndexBuffer.hpp include/IndexBuffer.hpp
libs/stb_image.hpp) libs/stb_image.hpp
include/Camera.hpp)
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:Debug>:_DEBUG>) target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<CONFIG:Debug>:_DEBUG>)

31
include/Camera.hpp Normal file
View File

@@ -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);
}
};

View File

@@ -9,6 +9,7 @@
#include "VertexBuffer.hpp" #include "VertexBuffer.hpp"
#include "Shader.hpp" #include "Shader.hpp"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "Camera.hpp"
#include "stb_image.hpp" #include "stb_image.hpp"
#include "glm/glm.hpp" #include "glm/glm.hpp"
#include "glm/ext/matrix_transform.hpp" #include "glm/ext/matrix_transform.hpp"
@@ -80,7 +81,7 @@ int main() {
#ifdef _DEBUG #ifdef _DEBUG
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); //remove for stable release SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); //remove for stable release
#endif #endif
SDL_GL_SetSwapInterval(1);
uint32 flags = SDL_WINDOW_OPENGL; //| SDL_WINDOW_FULLSCREEN; uint32 flags = SDL_WINDOW_OPENGL; //| SDL_WINDOW_FULLSCREEN;
@@ -169,12 +170,11 @@ int main() {
auto model = glm::mat4(1.0f); auto model = glm::mat4(1.0f);
model = glm::scale(model, glm::vec3(1.2f)); model = glm::scale(model, glm::vec3(1.2f));
auto projection = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, -10.0f, 100.0f); Camera camera(90.0f, 800.0f, 600.0f);
projection = glm::perspective(glm::radians(45.0f), 4.0f/3.0f, 0.1f, 100.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 = camera.getViewProj() * model;
auto modelViewProj = projection * view * model;
int modelViewProjMatrixLocation = glGetUniformLocation(shader.getShaderId(), "u_modelViewProj"); int modelViewProjMatrixLocation = glGetUniformLocation(shader.getShaderId(), "u_modelViewProj");
@@ -185,13 +185,56 @@ int main() {
bool running = true; bool running = true;
SDL_Event e; SDL_Event e;
bool btnW;
bool btnS;
bool btnA;
bool btnD;
while (running) { 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); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
time += delta; 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)); 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) { if (colorUniformLocation != 1) {
glUniform4f(colorUniformLocation, sinf(time)*sinf(time), 0.0f, 1.0f, 1.0f); glUniform4f(colorUniformLocation, sinf(time)*sinf(time), 0.0f, 1.0f, 1.0f);
@@ -209,12 +252,6 @@ int main() {
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
while (SDL_PollEvent(&e)) {
if (e.type == SDL_EVENT_QUIT) {
running = false;
}
}
const uint64 endCounter = SDL_GetPerformanceCounter(); const uint64 endCounter = SDL_GetPerformanceCounter();
const uint64 counterElapsed = endCounter - lastCounter; const uint64 counterElapsed = endCounter - lastCounter;
delta = (static_cast<float32>(counterElapsed) / static_cast<float32>(perfCounterFrequency)); delta = (static_cast<float32>(counterElapsed) / static_cast<float32>(perfCounterFrequency));