summaryrefslogtreecommitdiff
path: root/gfx/frustum.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2025-03-05 23:52:18 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2025-03-05 23:52:18 +0000
commit4e10d57f83f719e02005fe41e70fe1a4721a9439 (patch)
tree44a7300f97e1a7c09ad560f2176e1c089da6fe20 /gfx/frustum.cpp
parentMove camera out of gl folder, it's not OpenGL specific (diff)
downloadilt-4e10d57f83f719e02005fe41e70fe1a4721a9439.tar.bz2
ilt-4e10d57f83f719e02005fe41e70fe1a4721a9439.tar.xz
ilt-4e10d57f83f719e02005fe41e70fe1a4721a9439.zip
Split core view definition out of Camera into Frustum
Diffstat (limited to 'gfx/frustum.cpp')
-rw-r--r--gfx/frustum.cpp30
1 files changed, 30 insertions, 0 deletions
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 <algorithm>
+#include <collections.h>
+#include <glm/ext/matrix_transform.hpp>
+
+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);
+ });
+}