diff options
| author | hachem <im@hachem.wtf> | 2025-08-12 18:41:26 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-08-12 18:41:26 +0200 |
| commit | 7e06a916e6a385c37075abcb83d14b8c1fef7bc0 (patch) | |
| tree | 8054d96e89d43b0f98b75af726079d723a70ca2a /src/core | |
| parent | 50cdd344a3ed87c75a67739e2ffd34762e9b0895 (diff) | |
feat: camera
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/application.cpp | 149 | ||||
| -rw-r--r-- | src/core/application.h | 11 | ||||
| -rw-r--r-- | src/core/camera.cpp | 132 | ||||
| -rw-r--r-- | src/core/camera.h | 69 | ||||
| -rw-r--r-- | src/core/window.cpp | 18 | ||||
| -rw-r--r-- | src/core/window.h | 17 |
6 files changed, 377 insertions, 19 deletions
diff --git a/src/core/application.cpp b/src/core/application.cpp index 92bf211..b80ad84 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -9,6 +9,7 @@ #include <iostream> #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> +#include <GLFW/glfw3.h> namespace Donut { @@ -70,14 +71,78 @@ namespace Donut else m_minimized = false; + if (m_camera) + m_camera->set_projection(45.0f, (float)e.get_width() / (float)e.get_height(), 0.1f, 100.0f); + + event.handled = true; + return true; + }); + + dispatcher.dispatch<WindowFocusEvent>([this, &event](WindowFocusEvent& e) + { + m_first_mouse = true; + m_mouse_dragging = false; + event.handled = true; + return true; + }); + + dispatcher.dispatch<WindowLostFocusEvent>([this, &event](WindowLostFocusEvent& e) + { + m_mouse_dragging = false; event.handled = true; return true; }); dispatcher.dispatch<KeyPressedEvent>([this, &event](KeyPressedEvent& e) { - if (e.get_key_code() == 256) - close(); + m_keys[e.get_key_code()] = true; + return true; + }); + + dispatcher.dispatch<KeyReleasedEvent>([this, &event](KeyReleasedEvent& e) + { + m_keys[e.get_key_code()] = false; + return true; + }); + + dispatcher.dispatch<MouseMovedEvent>([this, &event](MouseMovedEvent& e) + { + if (!m_mouse_dragging) + return true; + + if (m_first_mouse) + { + m_last_x = e.get_x(); + m_last_y = e.get_y(); + m_first_mouse = false; + } + + float x_offset = m_last_x - e.get_x(); + float y_offset = e.get_y() - m_last_y; + + m_last_x = e.get_x(); + m_last_y = e.get_y(); + + if (m_camera) + m_camera->on_mouse_move(x_offset, y_offset); + + return true; + }); + + dispatcher.dispatch<MouseButtonPressedEvent>([this, &event](MouseButtonPressedEvent& e) + { + if (e.get_mouse_button() == GLFW_MOUSE_BUTTON_LEFT) + { + m_mouse_dragging = true; + m_first_mouse = true; + } + return true; + }); + + dispatcher.dispatch<MouseButtonReleasedEvent>([this, &event](MouseButtonReleasedEvent& e) + { + if (e.get_mouse_button() == GLFW_MOUSE_BUTTON_LEFT) + m_mouse_dragging = false; return true; }); } @@ -86,14 +151,32 @@ namespace Donut { Renderer::init(); - float vertices[3 * 7] = + m_camera = std::make_unique<Camera>(45.0f, 1280.0f / 720.0f, 0.1f, 100.0f); + m_camera->set_position({ 0.0f, 0.0f, 3.0f }); + m_camera->set_mouse_sensitivity(0.1f); + m_camera->set_movement_speed(5.0f); + + float vertices[] = { - -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, - 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, - 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f + -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f, + -0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 1.0f, + -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 1.0f }; - uint32_t indices[3] = { 0, 1, 2 }; + uint32_t indices[] = + { + 0, 1, 2, 2, 3, 0, + 1, 5, 6, 6, 2, 1, + 5, 4, 7, 7, 6, 5, + 4, 0, 3, 3, 7, 4, + 3, 2, 6, 6, 7, 3, + 4, 5, 1, 1, 0, 4 + }; m_vertex_array = std::shared_ptr<VertexArray>(VertexArray::create()); m_vertex_buffer = std::shared_ptr<VertexBuffer>(VertexBuffer::create(vertices, sizeof(vertices))); @@ -105,7 +188,7 @@ namespace Donut m_vertex_array->add_vertex_buffer(m_vertex_buffer); - m_index_buffer = std::shared_ptr<IndexBuffer>(IndexBuffer::create(indices, 3)); + m_index_buffer = std::shared_ptr<IndexBuffer>(IndexBuffer::create(indices, 36)); m_vertex_array->set_index_buffer(m_index_buffer); m_shader = std::shared_ptr<Shader>(Shader::create("assets/basic.glsl")); @@ -118,19 +201,55 @@ namespace Donut void Application::on_update() { + float current_frame = (float)glfwGetTime(); + m_delta_time = current_frame - m_last_frame; + m_last_frame = current_frame; + + if (m_camera) + { + if (m_keys[GLFW_KEY_W]) + m_camera->move_forward(m_delta_time); + if (m_keys[GLFW_KEY_S]) + m_camera->move_backward(m_delta_time); + if (m_keys[GLFW_KEY_A]) + m_camera->move_left(m_delta_time); + if (m_keys[GLFW_KEY_D]) + m_camera->move_right(m_delta_time); + if (m_keys[GLFW_KEY_SPACE]) + m_camera->move_up(m_delta_time); + if (m_keys[GLFW_KEY_LEFT_SHIFT]) + m_camera->move_down(m_delta_time); + } } void Application::on_render() { - Renderer::set_clear_color({ 0.1f, 0.1f, 0.1f, 1.0f }); + Renderer::set_clear_color({ 0.0f, 0.0f, 0.0f, 1.0f }); Renderer::clear(); - Renderer::begin_scene(); - - glm::mat4 view_projection = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f); + glm::mat4 view_projection = m_camera ? m_camera->get_view_projection_matrix() : glm::mat4(1.0f); - Renderer::submit(m_shader, m_vertex_array, glm::mat4(1.0f)); - - Renderer::end_scene(); + glm::vec3 center_pos(0.0f, 0.0f, 0.0f); + glm::vec3 right_pos(2.0f, 0.0f, 0.0f); + glm::vec3 left_pos(-2.0f, 0.0f, 0.0f); + glm::vec3 up_pos(0.0f, 2.0f, 0.0f); + glm::vec3 down_pos(0.0f, -2.0f, 0.0f); + glm::vec3 forward_pos(0.0f, 0.0f, 2.0f); + glm::vec3 back_pos(0.0f, 0.0f, -2.0f); + + glm::mat4 transform = glm::translate(glm::mat4(1.0f), center_pos); + Renderer::submit(m_shader, m_vertex_array, transform, view_projection); + transform = glm::translate(glm::mat4(1.0f), right_pos); + Renderer::submit(m_shader, m_vertex_array, transform, view_projection); + transform = glm::translate(glm::mat4(1.0f), left_pos); + Renderer::submit(m_shader, m_vertex_array, transform, view_projection); + transform = glm::translate(glm::mat4(1.0f), up_pos); + Renderer::submit(m_shader, m_vertex_array, transform, view_projection); + transform = glm::translate(glm::mat4(1.0f), down_pos); + Renderer::submit(m_shader, m_vertex_array, transform, view_projection); + transform = glm::translate(glm::mat4(1.0f), forward_pos); + Renderer::submit(m_shader, m_vertex_array, transform, view_projection); + transform = glm::translate(glm::mat4(1.0f), back_pos); + Renderer::submit(m_shader, m_vertex_array, transform, view_projection); } } diff --git a/src/core/application.h b/src/core/application.h index a860e1c..f78f107 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -2,6 +2,7 @@ #include "window.h" #include "event.h" +#include "camera.h" #include <memory> #include <functional> @@ -42,6 +43,16 @@ namespace Donut std::shared_ptr<VertexBuffer> m_vertex_buffer; std::shared_ptr<IndexBuffer> m_index_buffer; std::shared_ptr<Shader> m_shader; + + std::unique_ptr<Camera> m_camera; + + bool m_keys[1024] = { false }; + bool m_first_mouse = true; + float m_last_x = 0.0f; + float m_last_y = 0.0f; + float m_delta_time = 0.0f; + float m_last_frame = 0.0f; + bool m_mouse_dragging = false; static Application* s_instance; }; diff --git a/src/core/camera.cpp b/src/core/camera.cpp new file mode 100644 index 0000000..bad2919 --- /dev/null +++ b/src/core/camera.cpp @@ -0,0 +1,132 @@ +#include "camera.h" +#include <glm/gtc/quaternion.hpp> + +namespace Donut +{ + Camera::Camera(float fov, float aspect_ratio, float near_plane, float far_plane) + : m_fov(fov), m_aspect_ratio(aspect_ratio), m_near_plane(near_plane), m_far_plane(far_plane) + { + recalculate_projection_matrix(); + recalculate_view_matrix(); + } + + void Camera::set_projection(float fov, float aspect_ratio, float near_plane, float far_plane) + { + m_fov = fov; + m_aspect_ratio = aspect_ratio; + m_near_plane = near_plane; + m_far_plane = far_plane; + recalculate_projection_matrix(); + } + + void Camera::recalculate_projection_matrix() + { + m_projection_matrix = glm::perspective(glm::radians(m_fov), m_aspect_ratio, m_near_plane, m_far_plane); + m_view_projection_matrix = m_projection_matrix * m_view_matrix; + } + + void Camera::recalculate_view_matrix() + { + float pitch = glm::radians(m_rotation.x); + float yaw = glm::radians(m_rotation.y); + float roll = glm::radians(m_rotation.z); + + glm::vec3 direction; + direction.x = cos(yaw) * cos(pitch); + direction.y = sin(pitch); + direction.z = sin(yaw) * cos(pitch); + + glm::vec3 front = glm::normalize(direction); + glm::vec3 world_up(0.0f, 1.0f, 0.0f); + glm::vec3 right = glm::normalize(glm::cross(front, world_up)); + glm::vec3 up = glm::normalize(glm::cross(right, front)); + + m_view_matrix = glm::lookAt(m_position, m_position + front, up); + m_view_projection_matrix = m_projection_matrix * m_view_matrix; + } + + glm::vec3 Camera::get_forward_direction() const + { + float pitch = glm::radians(m_rotation.x); + float yaw = glm::radians(m_rotation.y); + + glm::vec3 direction; + direction.x = cos(yaw) * cos(pitch); + direction.y = sin(pitch); + direction.z = sin(yaw) * cos(pitch); + + return glm::normalize(direction); + } + + glm::vec3 Camera::get_right_direction() const + { + glm::vec3 world_up(0.0f, 1.0f, 0.0f); + return glm::normalize(glm::cross(get_forward_direction(), world_up)); + } + + glm::vec3 Camera::get_up_direction() const + { + return glm::normalize(glm::cross(get_right_direction(), get_forward_direction())); + } + + void Camera::on_update(float delta_time) + { + } + + void Camera::on_mouse_move(float x_offset, float y_offset, bool constrain_pitch) + { + x_offset *= m_mouse_sensitivity; + y_offset *= m_mouse_sensitivity; + + m_rotation.y += x_offset; + m_rotation.x += y_offset; + + if (constrain_pitch) + { + if (m_rotation.x > 89.0f) + m_rotation.x = 89.0f; + if (m_rotation.x < -89.0f) + m_rotation.x = -89.0f; + } + + recalculate_view_matrix(); + } + + void Camera::move_forward(float delta_time) + { + m_position += get_forward_direction() * m_movement_speed * delta_time; + recalculate_view_matrix(); + } + + void Camera::move_backward(float delta_time) + { + m_position -= get_forward_direction() * m_movement_speed * delta_time; + recalculate_view_matrix(); + } + + void Camera::move_right(float delta_time) + { + m_position += get_right_direction() * m_movement_speed * delta_time; + recalculate_view_matrix(); + } + + void Camera::move_left(float delta_time) + { + m_position -= get_right_direction() * m_movement_speed * delta_time; + recalculate_view_matrix(); + } + + void Camera::move_up(float delta_time) + { + glm::vec3 world_up(0.0f, 1.0f, 0.0f); + m_position += world_up * m_movement_speed * delta_time; + recalculate_view_matrix(); + } + + void Camera::move_down(float delta_time) + { + glm::vec3 world_up(0.0f, 1.0f, 0.0f); + m_position -= world_up * m_movement_speed * delta_time; + recalculate_view_matrix(); + } +} diff --git a/src/core/camera.h b/src/core/camera.h new file mode 100644 index 0000000..200c4b0 --- /dev/null +++ b/src/core/camera.h @@ -0,0 +1,69 @@ +#pragma once + +#include <glm/glm.hpp> +#include <glm/gtc/matrix_transform.hpp> + +namespace Donut +{ + class Camera + { + public: + Camera(float fov = 45.0f, float aspect_ratio = 16.0f / 9.0f, + float near_plane = 0.1f, float far_plane = 100.0f); + ~Camera() = default; + + void set_position(const glm::vec3& position) { m_position = position; recalculate_view_matrix(); } + void set_rotation(const glm::vec3& rotation) { m_rotation = rotation; recalculate_view_matrix(); } + + const glm::vec3& get_position() const { return m_position; } + const glm::vec3& get_rotation() const { return m_rotation; } + + glm::vec3 get_forward_direction() const; + glm::vec3 get_right_direction() const; + glm::vec3 get_up_direction() const; + + const glm::mat4& get_projection_matrix() const { return m_projection_matrix; } + const glm::mat4& get_view_matrix() const { return m_view_matrix; } + const glm::mat4& get_view_projection_matrix() const { return m_view_projection_matrix; } + + void set_projection(float fov, float aspect_ratio, float near_plane, float far_plane); + + void on_update(float delta_time); + void on_mouse_move(float x_offset, float y_offset, bool constrain_pitch = true); + + void move_forward(float delta_time); + void move_backward(float delta_time); + void move_right(float delta_time); + void move_left(float delta_time); + void move_up(float delta_time); + void move_down(float delta_time); + + void set_mouse_sensitivity(float sensitivity) { m_mouse_sensitivity = sensitivity; } + float get_mouse_sensitivity() const { return m_mouse_sensitivity; } + + void set_movement_speed(float speed) { m_movement_speed = speed; } + float get_movement_speed() const { return m_movement_speed; } + + private: + void recalculate_view_matrix(); + void recalculate_projection_matrix(); + private: + glm::mat4 m_projection_matrix; + glm::mat4 m_view_matrix; + glm::mat4 m_view_projection_matrix; + + glm::vec3 m_position = { 0.0f, 0.0f, 3.0f }; + glm::vec3 m_rotation = { 0.0f, 0.0f, 0.0f }; + + float m_fov = 45.0f; + float m_aspect_ratio = 16.0f / 9.0f; + float m_near_plane = 0.1f; + float m_far_plane = 100.0f; + + float m_mouse_sensitivity = 0.1f; + float m_movement_speed = 5.0f; + bool m_first_mouse = true; + float m_last_x = 0.0f; + float m_last_y = 0.0f; + }; +} diff --git a/src/core/window.cpp b/src/core/window.cpp index 77ef885..26c134c 100644 --- a/src/core/window.cpp +++ b/src/core/window.cpp @@ -90,6 +90,24 @@ namespace Donut return glfwWindowShouldClose(m_window) || m_is_closed; } + void Window::set_cursor_locked(bool locked) + { + m_cursor_locked = locked; + if (locked) + glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + else + glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + } + + void Window::set_cursor_visible(bool visible) + { + m_cursor_visible = visible; + if (visible) + glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + else + glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + } + void Window::glfw_error_callback(int error, const char* description) { std::cout << "GLFW Error (" << error << "): " << description << std::endl; diff --git a/src/core/window.h b/src/core/window.h index 8e9778e..d541b54 100644 --- a/src/core/window.h +++ b/src/core/window.h @@ -15,16 +15,24 @@ namespace Donut ~Window(); bool should_close() const; - void on_update() const; + void on_update() const; + + void set_event_callback(const EventCallbackFn& callback) + { + m_event_handler.set_event_callback(callback); + } - void set_event_callback(const EventCallbackFn& callback) { m_event_handler.set_event_callback(callback); } EventHandler& get_event_handler() { return m_event_handler; } - unsigned int get_width() const { return m_width; } + unsigned int get_width() const { return m_width; } unsigned int get_height() const { return m_height; } void* get_native_window() const { return m_window; } + void set_cursor_locked(bool locked); + void set_cursor_visible(bool visible); + bool is_cursor_locked() const { return m_cursor_locked; } + bool is_cursor_visible() const { return m_cursor_visible; } private: void init(); void shutdown(); @@ -39,7 +47,6 @@ namespace Donut static void glfw_mouse_button_callback(GLFWwindow* window, int button, int action, int mods); static void glfw_mouse_scroll_callback(GLFWwindow* window, double x_offset, double y_offset); static void glfw_cursor_pos_callback(GLFWwindow* window, double x_pos, double y_pos); - private: GLFWwindow* m_window; EventHandler m_event_handler; @@ -48,5 +55,7 @@ namespace Donut unsigned int m_width; unsigned int m_height; bool m_is_closed; + bool m_cursor_locked = false; + bool m_cursor_visible = true; }; } |
