diff options
Diffstat (limited to 'src/Core')
| -rw-r--r-- | src/Core/Camera.cpp | 79 | ||||
| -rw-r--r-- | src/Core/Camera.h | 83 |
2 files changed, 72 insertions, 90 deletions
diff --git a/src/Core/Camera.cpp b/src/Core/Camera.cpp index 6f86124..eeb62a4 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_CameraMode == CameraMode::Orbital) { glm::vec3 position = GetOrbitalPosition(); - glm::vec3 worldUp(0.0f, 1.0f, 0.0f); - m_ViewMatrix = glm::lookAt(position, m_Target, worldUp); + glm::vec3 target = m_OrbitalTarget; + glm::vec3 up(0.0f, 1.0f, 0.0f); + + m_ViewMatrix = glm::lookAt(position, target, up); m_ViewProjectionMatrix = m_ProjectionMatrix * m_ViewMatrix; } } @@ -158,77 +161,65 @@ namespace Donut } } - // Orbital Camera Methods - void Camera::SetOrbitalMode(bool enabled) - { - m_CameraMode = enabled ? CameraMode::Orbital : CameraMode::FPS; - RecalculateViewMatrix(); - } - glm::vec3 Camera::GetOrbitalPosition() const { float clampedElevation = glm::clamp(m_Elevation, 0.01f, float(M_PI) - 0.01f); - return glm::vec3( - m_Radius * sin(clampedElevation) * cos(m_Azimuth), - m_Radius * cos(clampedElevation), - m_Radius * sin(clampedElevation) * sin(m_Azimuth) + return glm::vec3 + ( + m_OrbitalRadius * sin(clampedElevation) * cos(m_Azimuth), + m_OrbitalRadius * cos(clampedElevation), + m_OrbitalRadius * sin(clampedElevation) * sin(m_Azimuth) ); } + void Camera::UpdateOrbital() + { + m_OrbitalTarget = glm::vec3(0.0f, 0.0f, 0.0f); + if (m_Dragging || m_Panning) + m_Moving = true; + else + m_Moving = false; + RecalculateViewMatrix(); + } + void Camera::ProcessOrbitalMouseMove(double x, double y) { - if (m_Dragging && !m_Panning) + if (m_Dragging && !m_Panning) { - float dx = float(x - m_OrbitalLastX); - float dy = float(y - m_OrbitalLastY); + float dx = float(x - m_LastX_Orbital); + float dy = float(y - m_LastY_Orbital); - m_Azimuth += dx * m_OrbitSpeed; - m_Elevation -= dy * m_OrbitSpeed; + m_Azimuth += dx * m_OrbitalSpeed; + m_Elevation -= dy * m_OrbitalSpeed; m_Elevation = glm::clamp(m_Elevation, 0.01f, float(M_PI) - 0.01f); } - m_OrbitalLastX = x; - m_OrbitalLastY = y; - UpdateOrbitalState(); - RecalculateViewMatrix(); + m_LastX_Orbital = x; + m_LastY_Orbital = y; + UpdateOrbital(); } void Camera::ProcessOrbitalMouseButton(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; } } - UpdateOrbitalState(); } void Camera::ProcessOrbitalScroll(double xoffset, double yoffset) { - m_Radius -= yoffset * m_ZoomSpeed; - m_Radius = glm::clamp(m_Radius, m_MinRadius, m_MaxRadius); - UpdateOrbitalState(); - RecalculateViewMatrix(); - } - - void Camera::UpdateOrbitalState() - { - m_Target = glm::vec3(0.0f, 0.0f, 0.0f); - if (m_Dragging || m_Panning) - { - m_Moving = true; - } - else - { - m_Moving = false; - } + m_OrbitalRadius -= yoffset * m_ZoomSpeed; + m_OrbitalRadius = glm::clamp(m_OrbitalRadius, m_OrbitalMinRadius, m_OrbitalMaxRadius); + UpdateOrbital(); } } diff --git a/src/Core/Camera.h b/src/Core/Camera.h index 7fe3bd9..a049d06 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 @@ -24,7 +23,6 @@ namespace Donut float nearPlane = 0.1f, float farPlane = 100.0f); ~Camera() = default; - // FPS Camera Methods void SetPosition(const glm::vec3& position) { m_Position = position; RecalculateViewMatrix(); } void SetRotation(const glm::vec3& rotation) { m_Rotation = rotation; RecalculateViewMatrix(); } @@ -55,21 +53,23 @@ namespace Donut void SetMovementSpeed(float speed) { m_MovementSpeed = speed; } float GetMovementSpeed() const { return m_MovementSpeed; } - // Orbital Camera Methods - void SetOrbitalMode(bool enabled); - bool IsOrbitalMode() const { return m_CameraMode == CameraMode::Orbital; } + void SetOrbitalTarget(const glm::vec3& target) { m_OrbitalTarget = target; } + const glm::vec3& GetOrbitalTarget() const { return m_OrbitalTarget; } - void SetTarget(const glm::vec3& target) { m_Target = target; } - const glm::vec3& GetTarget() const { return m_Target; } + void SetOrbitalRadius(double radius) { m_OrbitalRadius = radius; } + double GetOrbitalRadius() const { return m_OrbitalRadius; } - void SetRadius(double radius) { m_Radius = radius; } - double GetRadius() const { return m_Radius; } + void SetOrbitalLimits(double minRadius, double maxRadius) + { + m_OrbitalMinRadius = minRadius; + m_OrbitalMaxRadius = maxRadius; + } - void SetMinRadius(double minRadius) { m_MinRadius = minRadius; } - double GetMinRadius() const { return m_MinRadius; } + void SetOrbitalSpeed(float speed) { m_OrbitalSpeed = speed; } + float GetOrbitalSpeed() const { return m_OrbitalSpeed; } - void SetMaxRadius(double maxRadius) { m_MaxRadius = maxRadius; } - double GetMaxRadius() const { return m_MaxRadius; } + void SetZoomSpeed(double speed) { m_ZoomSpeed = speed; } + double GetZoomSpeed() const { return m_ZoomSpeed; } void SetAzimuth(float azimuth) { m_Azimuth = azimuth; } float GetAzimuth() const { return m_Azimuth; } @@ -77,35 +77,27 @@ namespace Donut void SetElevation(float elevation) { m_Elevation = elevation; } float GetElevation() const { return m_Elevation; } - void SetOrbitSpeed(float orbitSpeed) { m_OrbitSpeed = orbitSpeed; } - float GetOrbitSpeed() const { return m_OrbitSpeed; } - - void SetZoomSpeed(double zoomSpeed) { m_ZoomSpeed = zoomSpeed; } - double GetZoomSpeed() const { return m_ZoomSpeed; } - - // Orbital camera position calculation glm::vec3 GetOrbitalPosition() const; - - // Input handling for orbital camera + void UpdateOrbital(); void ProcessOrbitalMouseMove(double x, double y); void ProcessOrbitalMouseButton(int button, int action, int mods); void ProcessOrbitalScroll(double xoffset, double yoffset); - // State management - bool IsDragging() const { return m_Dragging; } - bool IsPanning() const { return m_Panning; } - bool IsMoving() const { return m_Moving; } + void SetCameraMode(CameraMode mode) { m_CameraMode = mode; } + CameraMode GetCameraMode() const { return m_CameraMode; } + + bool IsDragging() const { return m_Dragging; } + bool IsPanning() const { return m_Panning; } + bool IsMoving() const { return m_Moving; } + double GetLastX() const { return m_LastX; } + double GetLastY() const { return m_LastY; } private: void RecalculateViewMatrix(); void RecalculateProjectionMatrix(); - void UpdateOrbitalState(); - private: - // Camera mode CameraMode m_CameraMode = CameraMode::FPS; - - // FPS Camera members + glm::mat4 m_ProjectionMatrix; glm::mat4 m_ViewMatrix; glm::mat4 m_ViewProjectionMatrix; @@ -123,20 +115,19 @@ namespace Donut bool m_FirstMouse = true; float m_LastX = 0.0f; float m_LastY = 0.0f; - - // Orbital Camera members - glm::vec3 m_Target = glm::vec3(0.0f, 0.0f, 0.0f); - double m_Radius = 6.34194e10; - double m_MinRadius = 1e10; - double m_MaxRadius = 1e12; - float m_Azimuth = 0.0f; - float m_Elevation = static_cast<float>(M_PI) / 2.0f; - float m_OrbitSpeed = 0.01f; - double m_ZoomSpeed = 25e9f; - bool m_Dragging = false; - bool m_Panning = false; - bool m_Moving = false; - double m_OrbitalLastX = 0.0; - double m_OrbitalLastY = 0.0; + + glm::vec3 m_OrbitalTarget = glm::vec3(0.0f, 0.0f, 0.0f); + double m_OrbitalRadius = 6.34194e10; + double m_OrbitalMinRadius = 1e10; + double m_OrbitalMaxRadius = 1e12; + float m_Azimuth = 0.0f; + float m_Elevation = static_cast<float>(M_PI) / 2.0f; + float m_OrbitalSpeed = 0.01f; + double m_ZoomSpeed = 25e9f; + bool m_Dragging = false; + bool m_Panning = false; + bool m_Moving = false; + double m_LastX_Orbital = 0.0; + double m_LastY_Orbital = 0.0; }; } |
