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 ++ test/test-render.cpp | 17 +++++++++++++++++ 3 files changed, 37 insertions(+) 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: diff --git a/test/test-render.cpp b/test/test-render.cpp index 77501ac..1150a67 100644 --- a/test/test-render.cpp +++ b/test/test-render.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -95,6 +96,22 @@ class TestScene : public SceneProvider { BOOST_GLOBAL_FIXTURE(ApplicationBase); BOOST_GLOBAL_FIXTURE(TestMainWindow); +BOOST_DATA_TEST_CASE(cam, + boost::unit_test::data::xrange(0.5F, 30.F, 1.3F) * boost::unit_test::data::xrange(0.5F, 10.F, 0.3F) + * boost::unit_test::data::xrange(50.F, 500.F, 70.F), + dist, near, far) +{ + static constexpr glm::vec3 pos {-10, -10, 60}; + Camera cam {pos, half_pi, 1.f, near, far}; + + const auto e = cam.extentsAtDist(dist); + + BOOST_CHECK_CLOSE_VEC(e[0], pos + glm::vec3(-dist, dist, -dist)); + BOOST_CHECK_CLOSE_VEC(e[1], pos + glm::vec3(-dist, dist, dist)); + BOOST_CHECK_CLOSE_VEC(e[2], pos + glm::vec3(dist, dist, -dist)); + BOOST_CHECK_CLOSE_VEC(e[3], pos + glm::vec3(dist, dist, dist)); +} + BOOST_FIXTURE_TEST_SUITE(w, TestRenderOutput); BOOST_AUTO_TEST_CASE(basic) -- cgit v1.2.3