diff options
| author | hachem <im@hachem.wtf> | 2025-08-15 03:18:29 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2025-08-15 03:18:29 +0200 |
| commit | a6655eebe2a69ee427264bd43df5747851ed96df (patch) | |
| tree | fa8c6ef862898a3d8e19250698dca0d9aaf4f9c2 /src/core | |
| parent | 3d8d3259e1cf5a7d88e5d1e530cc8c9bf9140734 (diff) | |
feat: complete first geodesic 3D raytracing
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/camera.cpp | 78 | ||||
| -rw-r--r-- | src/core/camera.h | 75 |
2 files changed, 72 insertions, 81 deletions
diff --git a/src/core/camera.cpp b/src/core/camera.cpp index 43cb1ee..bba6ee2 100644 --- a/src/core/camera.cpp +++ b/src/core/camera.cpp @@ -1,6 +1,7 @@ #include "camera.h" #include <glm/gtc/quaternion.hpp> +#include <GLFW/glfw3.h> namespace Donut { @@ -50,8 +51,10 @@ namespace Donut else if (m_camera_mode == CameraMode::Orbital) { glm::vec3 position = get_orbital_position(); - glm::vec3 world_up(0.0f, 1.0f, 0.0f); - m_view_matrix = glm::lookAt(position, m_target, world_up); + glm::vec3 target = m_orbital_target; + glm::vec3 up(0.0f, 1.0f, 0.0f); + + m_view_matrix = glm::lookAt(position, target, up); m_view_projection_matrix = m_projection_matrix * m_view_matrix; } } @@ -158,76 +161,65 @@ namespace Donut } } - void Camera::set_orbital_mode(bool enabled) - { - m_camera_mode = enabled ? CameraMode::Orbital : CameraMode::FPS; - recalculate_view_matrix(); - } - glm::vec3 Camera::get_orbital_position() const { float clamped_elevation = glm::clamp(m_elevation, 0.01f, float(M_PI) - 0.01f); - return glm::vec3( - m_radius * sin(clamped_elevation) * cos(m_azimuth), - m_radius * cos(clamped_elevation), - m_radius * sin(clamped_elevation) * sin(m_azimuth) + return glm::vec3 + ( + m_orbital_radius * sin(clamped_elevation) * cos(m_azimuth), + m_orbital_radius * cos(clamped_elevation), + m_orbital_radius * sin(clamped_elevation) * sin(m_azimuth) ); } + void Camera::update_orbital() + { + m_orbital_target = glm::vec3(0.0f, 0.0f, 0.0f); + if (m_dragging || m_panning) + m_moving = true; + else + m_moving = false; + recalculate_view_matrix(); + } + void Camera::process_orbital_mouse_move(double x, double y) { - if (m_dragging && !m_panning) + if (m_dragging && !m_panning) { - float dx = float(x - m_orbital_last_x); - float dy = float(y - m_orbital_last_y); + float dx = float(x - m_last_x_orbital); + float dy = float(y - m_last_y_orbital); - m_azimuth += dx * m_orbit_speed; - m_elevation -= dy * m_orbit_speed; + m_azimuth += dx * m_orbital_speed; + m_elevation -= dy * m_orbital_speed; m_elevation = glm::clamp(m_elevation, 0.01f, float(M_PI) - 0.01f); } - m_orbital_last_x = x; - m_orbital_last_y = y; - update_orbital_state(); - recalculate_view_matrix(); + m_last_x_orbital = x; + m_last_y_orbital = y; + update_orbital(); } void Camera::process_orbital_mouse_button(int button, int action, int mods) { - if (button == GLFW_MOUSE_BUTTON_LEFT) + if (button == GLFW_MOUSE_BUTTON_LEFT) { - if (action == GLFW_PRESS) + if (action == GLFW_PRESS) { m_dragging = true; m_panning = false; - } - else if (action == GLFW_RELEASE) + } + else if (action == GLFW_RELEASE) { m_dragging = false; m_panning = false; } } - update_orbital_state(); } void Camera::process_orbital_scroll(double x_offset, double y_offset) { - m_radius -= y_offset * m_zoom_speed; - m_radius = glm::clamp(m_radius, m_min_radius, m_max_radius); - update_orbital_state(); - recalculate_view_matrix(); - } - - void Camera::update_orbital_state() - { - m_target = glm::vec3(0.0f, 0.0f, 0.0f); - if (m_dragging || m_panning) - { - m_moving = true; - } - else - { - m_moving = false; - } + m_orbital_radius -= y_offset * m_zoom_speed; + m_orbital_radius = glm::clamp(m_orbital_radius, m_orbital_min_radius, m_orbital_max_radius); + update_orbital(); } } diff --git a/src/core/camera.h b/src/core/camera.h index 7ea5472..809be48 100644 --- a/src/core/camera.h +++ b/src/core/camera.h @@ -2,7 +2,6 @@ #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> -#include <GLFW/glfw3.h> #define _USE_MATH_DEFINES #ifndef M_PI @@ -54,20 +53,23 @@ namespace Donut void set_movement_speed(float speed) { m_movement_speed = speed; } float get_movement_speed() const { return m_movement_speed; } - void set_orbital_mode(bool enabled); - bool is_orbital_mode() const { return m_camera_mode == CameraMode::Orbital; } + void set_orbital_target(const glm::vec3& target) { m_orbital_target = target; } + const glm::vec3& get_orbital_target() const { return m_orbital_target; } - void set_target(const glm::vec3& target) { m_target = target; } - const glm::vec3& get_target() const { return m_target; } + void set_orbital_radius(double radius) { m_orbital_radius = radius; } + double get_orbital_radius() const { return m_orbital_radius; } - void set_radius(double radius) { m_radius = radius; } - double get_radius() const { return m_radius; } + void set_orbital_limits(double min_radius, double max_radius) + { + m_orbital_min_radius = min_radius; + m_orbital_max_radius = max_radius; + } - void set_min_radius(double min_radius) { m_min_radius = min_radius; } - double get_min_radius() const { return m_min_radius; } + void set_orbital_speed(float speed) { m_orbital_speed = speed; } + float get_orbital_speed() const { return m_orbital_speed; } - void set_max_radius(double max_radius) { m_max_radius = max_radius; } - double get_max_radius() const { return m_max_radius; } + void set_zoom_speed(double speed) { m_zoom_speed = speed; } + double get_zoom_speed() const { return m_zoom_speed; } void set_azimuth(float azimuth) { m_azimuth = azimuth; } float get_azimuth() const { return m_azimuth; } @@ -75,30 +77,27 @@ namespace Donut void set_elevation(float elevation) { m_elevation = elevation; } float get_elevation() const { return m_elevation; } - void set_orbit_speed(float orbit_speed) { m_orbit_speed = orbit_speed; } - float get_orbit_speed() const { return m_orbit_speed; } - - void set_zoom_speed(double zoom_speed) { m_zoom_speed = zoom_speed; } - double get_zoom_speed() const { return m_zoom_speed; } - glm::vec3 get_orbital_position() const; - + void update_orbital(); void process_orbital_mouse_move(double x, double y); void process_orbital_mouse_button(int button, int action, int mods); void process_orbital_scroll(double x_offset, double y_offset); - bool is_dragging() const { return m_dragging; } - bool is_panning() const { return m_panning; } - bool is_moving() const { return m_moving; } + void set_camera_mode(CameraMode mode) { m_camera_mode = mode; } + CameraMode get_camera_mode() const { return m_camera_mode; } + + bool is_dragging() const { return m_dragging; } + bool is_panning() const { return m_panning; } + bool is_moving() const { return m_moving; } + double get_last_x() const { return m_last_x; } + double get_last_y() const { return m_last_y; } private: void recalculate_view_matrix(); void recalculate_projection_matrix(); - void update_orbital_state(); - private: CameraMode m_camera_mode = CameraMode::FPS; - + glm::mat4 m_projection_matrix; glm::mat4 m_view_matrix; glm::mat4 m_view_projection_matrix; @@ -116,19 +115,19 @@ namespace Donut bool m_first_mouse = true; float m_last_x = 0.0f; float m_last_y = 0.0f; - - glm::vec3 m_target = glm::vec3(0.0f, 0.0f, 0.0f); - double m_radius = 6.34194e10; - double m_min_radius = 1e10; - double m_max_radius = 1e12; - float m_azimuth = 0.0f; - float m_elevation = static_cast<float>(M_PI) / 2.0f; - float m_orbit_speed = 0.01f; - double m_zoom_speed = 25e9f; - bool m_dragging = false; - bool m_panning = false; - bool m_moving = false; - double m_orbital_last_x = 0.0; - double m_orbital_last_y = 0.0; + + glm::vec3 m_orbital_target = glm::vec3(0.0f, 0.0f, 0.0f); + double m_orbital_radius = 6.34194e10; + double m_orbital_min_radius = 1e10; + double m_orbital_max_radius = 1e12; + float m_azimuth = 0.0f; + float m_elevation = static_cast<float>(M_PI) / 2.0f; + float m_orbital_speed = 0.01f; + double m_zoom_speed = 25e9f; + bool m_dragging = false; + bool m_panning = false; + bool m_moving = false; + double m_last_x_orbital = 0.0; + double m_last_y_orbital = 0.0; }; } |
