diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-03-05 22:16:56 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2025-03-05 22:16:56 +0000 |
commit | 2454b5449bbda815eb15327476e376d0d3a39617 (patch) | |
tree | 405643f93a6e8eb2cd3f8b38a17649fd2f85f5a6 /gfx/camera.h | |
parent | Have Camera keep an array of frustum plane definitions (diff) | |
download | ilt-2454b5449bbda815eb15327476e376d0d3a39617.tar.bz2 ilt-2454b5449bbda815eb15327476e376d0d3a39617.tar.xz ilt-2454b5449bbda815eb15327476e376d0d3a39617.zip |
Move camera out of gl folder, it's not OpenGL specific
Diffstat (limited to 'gfx/camera.h')
-rw-r--r-- | gfx/camera.h | 94 |
1 files changed, 94 insertions, 0 deletions
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 <glm/glm.hpp> +#include <maths.h> +#include <ray.h> + +class Camera { +public: + Camera(GlobalPosition3D, Angle fov, Angle aspect, GlobalDistance zNear, GlobalDistance zFar); + + [[nodiscard]] glm::mat4 + getViewProjection() const + { + return viewProjection; + } + + [[nodiscard]] Ray<GlobalPosition3D> 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<GlobalPosition4D, 4> 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<glm::vec4, 6> frustumPlanes; +}; |