From c20c5167510758353d5a62cfff24fd5494b9c5a4 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 13 Dec 2022 12:14:43 +0000 Subject: Add Camera method to get the extents of the view frustrum at some distance --- gfx/gl/camera.cpp | 18 ++++++++++++++++++ gfx/gl/camera.h | 2 ++ 2 files changed, 20 insertions(+) (limited to 'gfx/gl') diff --git a/gfx/gl/camera.cpp b/gfx/gl/camera.cpp index 69cfe35..31be9a6 100644 --- a/gfx/gl/camera.cpp +++ b/gfx/gl/camera.cpp @@ -32,3 +32,21 @@ Camera::upFromForward(const glm::vec3 & forward) const auto right = glm::cross(forward, ::down); return glm::cross(forward, right); } + +std::array +Camera::extentsAtDist(const float dist) const +{ + const auto depth = -(2.f * (dist - near) * far) / (dist * (near - far)) - 1.f; + static constexpr const std::array extents {-1.F, 1.F}; + std::array out {}; + auto outitr = out.begin(); + for (auto x : extents) { + for (auto y : extents) { + const glm::vec4 in {x, y, depth, 1.f}; + + const auto out = inverseViewProjection * in; + *outitr++ = out / out.w; + } + } + return out; +} diff --git a/gfx/gl/camera.h b/gfx/gl/camera.h index 2d937a0..1ded3e0 100644 --- a/gfx/gl/camera.h +++ b/gfx/gl/camera.h @@ -61,6 +61,8 @@ public: return position; } + std::array extentsAtDist(float) const; + static glm::vec3 upFromForward(const glm::vec3 & forward); private: -- cgit v1.2.3