From 21e1a24ebe3b4a4679702314ee77dc5259d13070 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 14 Mar 2025 01:52:02 +0000 Subject: Add support for changing the aspect ratio of a camera --- gfx/camera.cpp | 15 +++++++++++---- gfx/camera.h | 7 +++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/gfx/camera.cpp b/gfx/camera.cpp index f01054a..3bb785d 100644 --- a/gfx/camera.cpp +++ b/gfx/camera.cpp @@ -5,17 +5,24 @@ #include Camera::Camera(GlobalPosition3D pos, Angle fov, Angle aspect, GlobalDistance near, GlobalDistance far) : - Camera {pos, near, far, glm::lookAt({}, ::north, ::up), + Camera {pos, fov, aspect, near, far, glm::lookAt({}, ::north, ::up), glm::perspective(fov, aspect, static_cast(near), static_cast(far))} { } -Camera::Camera(GlobalPosition3D pos, GlobalDistance near, GlobalDistance far, const glm::mat4 & view, - const glm::mat4 & projection) : - Frustum {pos, view, projection}, forward {::north}, up {::up}, near {near}, far {far} +Camera::Camera(GlobalPosition3D pos, Angle fov, Angle aspect, GlobalDistance near, GlobalDistance far, + const glm::mat4 & view, const glm::mat4 & projection) : + Frustum {pos, view, projection}, fov {fov}, aspect {aspect}, forward {::north}, up {::up}, near {near}, far {far} { } +void +Camera::setAspect(Angle aspect) +{ + projection = glm::perspective(fov, aspect, static_cast(near), static_cast(far)); + Frustum::updateCache(); +} + Ray Camera::unProject(const ScreenRelCoord & mouse) const { diff --git a/gfx/camera.h b/gfx/camera.h index d1bfc82..b17bcbb 100644 --- a/gfx/camera.h +++ b/gfx/camera.h @@ -12,6 +12,8 @@ public: [[nodiscard]] Ray unProject(const ScreenRelCoord &) const; + void setAspect(Angle aspect); + void setPosition(const GlobalPosition3D & p) { @@ -64,10 +66,11 @@ public: [[nodiscard]] static Direction3D upFromForward(const Direction3D & forward); private: - Camera(GlobalPosition3D position, GlobalDistance near, GlobalDistance far, const glm::mat4 & view, - const glm::mat4 & projection); + Camera(GlobalPosition3D position, Angle fov, Angle aspect, GlobalDistance near, GlobalDistance far, + const glm::mat4 & view, const glm::mat4 & projection); void updateView(); + Angle fov, aspect; Direction3D forward; Direction3D up; GlobalDistance near, far; -- cgit v1.2.3