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

@@ -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<float32>(counterElapsed) / static_cast<float32>(perfCounterFrequency));