From 2454b5449bbda815eb15327476e376d0d3a39617 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 5 Mar 2025 22:16:56 +0000 Subject: Move camera out of gl folder, it's not OpenGL specific --- gfx/camera.h | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 gfx/camera.h (limited to 'gfx/camera.h') diff --git a/gfx/camera.h b/gfx/camera.h new file mode 100644 index 0000000..d4fe6de --- /dev/null +++ b/gfx/camera.h @@ -0,0 +1,94 @@ +#pragma once + +#include "config/types.h" +#include +#include +#include + +class Camera { +public: + Camera(GlobalPosition3D, Angle fov, Angle aspect, GlobalDistance zNear, GlobalDistance zFar); + + [[nodiscard]] glm::mat4 + getViewProjection() const + { + return viewProjection; + } + + [[nodiscard]] Ray unProject(const ScreenRelCoord &) const; + + void + setPosition(const GlobalPosition3D & p) + { + position = p; + updateView(); + } + + void + setForward(const Direction3D & f) + { + setForward(f, upFromForward(f)); + } + + void + setForward(const Direction3D & f, const Direction3D & u) + { + forward = f; + up = u; + updateView(); + } + + void + setView(const GlobalPosition3D & p, const Direction3D & f) + { + position = p; + setForward(f); + } + + void + setView(const GlobalPosition3D & p, const Direction3D & f, const Direction3D & u) + { + position = p; + setView(f, u); + } + + void + lookAt(const GlobalPosition3D & target) + { + setForward(glm::normalize(RelativePosition3D(target - position))); + } + + [[nodiscard]] auto + getForward() const + { + return forward; + } + + [[nodiscard]] auto + getPosition() const + { + return position; + } + + [[nodiscard]] auto & + getFrustumPlanes() const + { + return frustumPlanes; + } + + [[nodiscard]] std::array extentsAtDist(GlobalDistance) const; + + [[nodiscard]] static Direction3D upFromForward(const Direction3D & forward); + +private: + void updateView(); + + GlobalPosition3D position; + Direction3D forward; + Direction3D up; + + GlobalDistance near, far; + glm::mat4 view, projection; + glm::mat4 viewProjection, inverseViewProjection; + std::array frustumPlanes; +}; -- cgit v1.2.3