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/gl | |
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/gl')
-rw-r--r-- | gfx/gl/camera.cpp | 67 | ||||
-rw-r--r-- | gfx/gl/camera.h | 94 | ||||
-rw-r--r-- | gfx/gl/sceneRenderer.h | 2 | ||||
-rw-r--r-- | gfx/gl/shadowMapper.cpp | 2 |
4 files changed, 2 insertions, 163 deletions
diff --git a/gfx/gl/camera.cpp b/gfx/gl/camera.cpp deleted file mode 100644 index 9e165fa..0000000 --- a/gfx/gl/camera.cpp +++ /dev/null @@ -1,67 +0,0 @@ -#include "camera.h" -#include <collections.h> -#include <glm/gtx/transform.hpp> -#include <maths.h> -#include <ray.h> - -Camera::Camera(GlobalPosition3D pos, Angle fov, Angle aspect, GlobalDistance zNear, GlobalDistance zFar) : - position {pos}, forward {::north}, up {::up}, near {zNear}, far {zFar}, view {}, - projection { - glm::perspective(fov, aspect, static_cast<RelativeDistance>(zNear), static_cast<RelativeDistance>(zFar))}, - viewProjection {}, inverseViewProjection {} -{ - updateView(); -} - -Ray<GlobalPosition3D> -Camera::unProject(const ScreenRelCoord & mouse) const -{ - static constexpr const glm::vec4 SCREEN {0, 0, 1, 1}; - return { - .start = position, - .direction = glm::normalize(glm::unProject(mouse || 1.F, view, projection, SCREEN)), - }; -} - -void -Camera::updateView() -{ - view = glm::lookAt({}, forward, up); - viewProjection = projection * view; - inverseViewProjection = glm::inverse(viewProjection); - static constexpr auto PLANES = std::array {0, 1, 2} * std::array {1.F, -1.F}; - std::ranges::transform(PLANES, frustumPlanes.begin(), [vpt = glm::transpose(viewProjection)](const auto & idxs) { - const auto [idx, sgn] = idxs; - return vpt[3] + (vpt[idx] * sgn); - }); -} - -Direction3D -Camera::upFromForward(const Direction3D & forward) -{ - const auto right = crossProduct(forward, ::down); - return crossProduct(forward, right); -} - -std::array<GlobalPosition4D, 4> -Camera::extentsAtDist(const GlobalDistance dist) const -{ - const auto clampToSeaFloor = [this, dist](GlobalPosition3D target) -> GlobalPosition4D { - target += position; - if (target.z < -1500) { - const CalcPosition3D diff = target - position; - const CalcDistance limit = -1500 - position.z; - return {position + ((limit * diff) / diff.z), (limit * dist) / diff.z}; - } - return {target, dist}; - }; - const auto depth = -(2.F * (static_cast<float>(dist - near)) * static_cast<float>(far)) - / (static_cast<float>(dist) * (static_cast<float>(near - far))) - - 1.F; - static constexpr const std::array extents {-1.F, 1.F}; - static constexpr const auto cartesianExtents = extents * extents; - return cartesianExtents * [&depth, this, &clampToSeaFloor](const auto & extent) { - const glm::vec4 in {extent.first, extent.second, depth, 1.F}; - return clampToSeaFloor(perspective_divide(inverseViewProjection * in)); - }; -} diff --git a/gfx/gl/camera.h b/gfx/gl/camera.h deleted file mode 100644 index d4fe6de..0000000 --- a/gfx/gl/camera.h +++ /dev/null @@ -1,94 +0,0 @@ -#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; -}; diff --git a/gfx/gl/sceneRenderer.h b/gfx/gl/sceneRenderer.h index 93470f5..4649a68 100644 --- a/gfx/gl/sceneRenderer.h +++ b/gfx/gl/sceneRenderer.h @@ -1,12 +1,12 @@ #pragma once -#include "camera.h" #include "gfx/lightDirection.h" #include "glArrays.h" #include "program.h" #include "sceneProvider.h" #include "sceneShader.h" #include "shadowMapper.h" +#include <gfx/camera.h> #include <glm/fwd.hpp> class SceneRenderer { diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index 1b95aa3..908dbdb 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -1,5 +1,4 @@ #include "shadowMapper.h" -#include "camera.h" #include "collections.h" #include "game/gamestate.h" #include "gfx/gl/shaders/fs-shadowDynamicPointInstWithTextures.h" @@ -21,6 +20,7 @@ #include "sceneProvider.h" #include "sceneShader.h" #include "sorting.h" +#include <gfx/camera.h> #include <glm/gtc/type_ptr.hpp> #include <glm/gtx/transform.hpp> #include <glm/matrix.hpp> |