hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/src/core/camera.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/camera.cpp')
-rw-r--r--src/core/camera.cpp78
1 files changed, 35 insertions, 43 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();
}
}