Attempt to fix first person camera implementation

This commit is contained in:
Abdelrahman Said 2024-09-24 00:40:34 +01:00
parent 229b039617
commit 69cca0d308

View File

@ -25,8 +25,10 @@
#include <string>
#include <vector>
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
#define WINDOW_HALF_WIDTH 640
#define WINDOW_HALF_HEIGHT 360
enum exit_codes : int {
EXIT_CODE_SUCCESS,
@ -84,6 +86,9 @@ struct App {
glm::mat4 projection_mat;
GLint u_projection_idx;
glm::vec2 current_mouse;
glm::vec2 prev_mouse;
bool running;
};
@ -132,6 +137,7 @@ int init(App &app) {
return EXIT_CODE_WINDOW_CREATION_FAILED;
}
SDL_WarpMouseInWindow(app.window, WINDOW_HALF_WIDTH, WINDOW_HALF_HEIGHT);
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
@ -173,6 +179,8 @@ int init(App &app) {
0.1f,
20.0f
);
app.current_mouse = glm::vec2(WINDOW_HALF_WIDTH, WINDOW_HALF_HEIGHT);
app.prev_mouse = glm::vec2(WINDOW_HALF_WIDTH, WINDOW_HALF_HEIGHT);
return EXIT_CODE_SUCCESS;
}
@ -431,9 +439,12 @@ void handle_object_movement(App &app) {
void handle_camera_movement(App &app) {
if (app.event.type == SDL_MOUSEMOTION) {
app.current_mouse += glm::vec2(app.event.motion.xrel, app.event.motion.yrel);
glm::vec2 mouse_delta = app.prev_mouse - app.current_mouse;
app.camera.view_direction = glm::rotate(app.camera.view_direction,
glm::radians((float)app.event.motion.xrel * -0.5f),
glm::radians(mouse_delta.x),
app.camera.up_vector);
app.prev_mouse = app.current_mouse;
} else {
switch (app.event.key.keysym.sym) {
case SDLK_w: