summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan.goodliffe@octal.co.uk>2022-12-13 12:14:43 +0000
committerDan Goodliffe <dan.goodliffe@octal.co.uk>2022-12-13 12:14:43 +0000
commitc20c5167510758353d5a62cfff24fd5494b9c5a4 (patch)
tree2b249e1c94fe26593ea22972b486553da8b57bda
parentCalculate an accurate up vector for the camera (diff)
downloadilt-c20c5167510758353d5a62cfff24fd5494b9c5a4.tar.bz2
ilt-c20c5167510758353d5a62cfff24fd5494b9c5a4.tar.xz
ilt-c20c5167510758353d5a62cfff24fd5494b9c5a4.zip
Add Camera method to get the extents of the view frustrum at some distance
-rw-r--r--gfx/gl/camera.cpp18
-rw-r--r--gfx/gl/camera.h2
-rw-r--r--test/test-render.cpp17
3 files changed, 37 insertions, 0 deletions
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<glm::vec3, 4>
+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<glm::vec3, 4> 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<glm::vec3, 4> 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 <gfx/models/texture.h>
#include <lib/glArrays.h>
#include <maths.h>
+#include <stream_support.hpp>
#include <ui/applicationBase.h>
#include <ui/window.h>
@@ -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)