From 4967b334799025392c5d3c71d1e3c79bda5c47ba Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 28 Sep 2024 21:21:26 +0100 Subject: [PATCH] Calculate view_direction from camera rotation --- src/main.cc | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/main.cc b/src/main.cc index bebb200..c321091 100644 --- a/src/main.cc +++ b/src/main.cc @@ -49,6 +49,9 @@ struct Camera { // The up vector in world coordinates glm::vec3 up; + + // pitch, yaw + glm::vec2 rotation; }; struct App { @@ -99,16 +102,17 @@ struct App { bool running; }; -int init (App &app); -void create_vertex_spec (App &app); -void create_graphics_pipeline(App &app); -void run_main_loop (App &app); -void cleanup (App &app); -GLuint create_shader_program (const std::string &vertex_shader_source, const std::string &fragment_shader_source); -GLuint compile_shader (GLuint type, const std::string &source); -std::string load_shader (const std::string &filepath); -void handle_object_movement (App &app); -void handle_camera_movement (App &app); +int init (App &app); +void create_vertex_spec (App &app); +void create_graphics_pipeline (App &app); +void run_main_loop (App &app); +void cleanup (App &app); +GLuint create_shader_program (const std::string &vertex_shader_source, const std::string &fragment_shader_source); +GLuint compile_shader (GLuint type, const std::string &source); +std::string load_shader (const std::string &filepath); +void handle_object_movement (App &app); +void handle_camera_movement (App &app); +glm::vec3 rotation_to_view_direction(const Camera &camera); int main() { App app = {}; @@ -176,8 +180,9 @@ int init(App &app) { app.translation = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 1.5f)); app.rotation = glm::mat4(1.0f); app.scale = glm::mat4(1.0f); + app.camera.rotation = glm::vec2(0.0f, -90.0f); app.camera.position = glm::vec3(0.0f, 0.0f, 3.0f); - app.camera.view_direction = glm::vec3(0.0f, 0.0f, -1.0f); + app.camera.view_direction = rotation_to_view_direction(app.camera); app.camera.up = glm::vec3(0.0f, 1.0f, 0.0f); app.model_mat = app.translation * app.rotation * app.scale; app.view_mat = glm::lookAt(app.camera.position, app.camera.position + app.camera.view_direction, app.camera.up); @@ -446,9 +451,11 @@ void handle_object_movement(App &app) { void handle_camera_movement(App &app) { if (app.event.type == SDL_MOUSEMOTION) { - glm::vec2 offset = glm::vec2(app.event.motion.xrel, 0) * 0.1f; + glm::vec2 offset = glm::vec2(-app.event.motion.yrel, app.event.motion.xrel) * 0.1f; app.prev_mouse += offset; - app.camera.view_direction = glm::rotate(app.camera.view_direction, glm::radians(offset.x), app.camera.up); + app.camera.rotation += offset; + app.camera.rotation.x = glm::clamp(app.camera.rotation.x, -89.0f, 89.0f); + app.camera.view_direction = rotation_to_view_direction(app.camera); } else { switch (app.event.key.keysym.sym) { case SDLK_w: @@ -470,3 +477,12 @@ void handle_camera_movement(App &app) { app.view_mat = glm::lookAt(app.camera.position, app.camera.position + app.camera.view_direction, app.camera.up); } + +glm::vec3 rotation_to_view_direction(const Camera &camera) { + glm::vec3 direction; + direction.x = cos(glm::radians(camera.rotation.y)) * cos(glm::radians(camera.rotation.x)); + direction.y = sin(glm::radians(camera.rotation.x)); + direction.z = sin(glm::radians(camera.rotation.y)) * cos(glm::radians(camera.rotation.x)); + + return glm::normalize(direction); +}