summaryrefslogtreecommitdiff
path: root/gfx/frustum.cpp
diff options
context:
space:
mode:
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);
+ });
+}