From 4e10d57f83f719e02005fe41e70fe1a4721a9439 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 5 Mar 2025 23:52:18 +0000 Subject: Split core view definition out of Camera into Frustum --- gfx/frustum.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 gfx/frustum.cpp (limited to 'gfx/frustum.cpp') diff --git a/gfx/frustum.cpp b/gfx/frustum.cpp new file mode 100644 index 0000000..9e046f6 --- /dev/null +++ b/gfx/frustum.cpp @@ -0,0 +1,30 @@ +#include "frustum.h" +#include +#include +#include + +static constexpr auto PLANES = std::array {0, 1, 2} * std::array {1.F, -1.F}; + +Frustum::Frustum(const glm::mat4 & view, const glm::mat4 & projection) : + view {view}, projection {projection}, viewProjection {}, inverseViewProjection {}, planes {} +{ + updateCache(); +} + +void +Frustum::updateView(const glm::mat4 & newView) +{ + view = newView; + updateCache(); +} + +void +Frustum::updateCache() +{ + viewProjection = projection * view; + inverseViewProjection = glm::inverse(viewProjection); + std::ranges::transform(PLANES, planes.begin(), [vpt = glm::transpose(viewProjection)](const auto & idxs) { + const auto [idx, sgn] = idxs; + return vpt[3] + (vpt[idx] * sgn); + }); +} -- cgit v1.2.3