summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2025-03-11 00:40:35 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2025-03-11 01:13:59 +0000
commitfc74736b9eaa1f3e033ded9103b69d2a39a3e263 (patch)
treec3f725a614b5aab57dd8476de7e2410d579f3094
parentPass a Frustum to shadow renderers (diff)
downloadilt-fc74736b9eaa1f3e033ded9103b69d2a39a3e263.tar.bz2
ilt-fc74736b9eaa1f3e033ded9103b69d2a39a3e263.tar.xz
ilt-fc74736b9eaa1f3e033ded9103b69d2a39a3e263.zip
Extend Frustum for testing for shaded by
Like contains, but doesn't test the back plane as shadow caster can be anywhere behind the view point and still cast into it.
-rw-r--r--gfx/frustum.cpp16
-rw-r--r--gfx/frustum.h1
2 files changed, 15 insertions, 2 deletions
diff --git a/gfx/frustum.cpp b/gfx/frustum.cpp
index 865dcde..faa676d 100644
--- a/gfx/frustum.cpp
+++ b/gfx/frustum.cpp
@@ -3,7 +3,7 @@
#include <collections.h>
#include <glm/ext/matrix_transform.hpp>
-static constexpr auto PLANES = std::array {0, 1, 2} * std::array {1.F, -1.F};
+static constexpr auto PLANES = std::array {0, 1, 2} * std::array {-1.F, 1.F};
Frustum::Frustum(const GlobalPosition3D & pos, const glm::mat4 & view, const glm::mat4 & projection) :
position {pos}, view {view}, projection {projection}, viewProjection {}, inverseViewProjection {}, planes {}
@@ -21,6 +21,18 @@ Frustum::updateView(const glm::mat4 & newView)
bool
Frustum::contains(const BoundingBox & aabb) const
{
+ return boundByPlanes(aabb, FACES);
+}
+
+bool
+Frustum::shadedBy(const BoundingBox & aabb) const
+{
+ return boundByPlanes(aabb, FACES - 1);
+}
+
+bool
+Frustum::boundByPlanes(const BoundingBox & aabb, size_t nplanes) const
+{
static constexpr auto EXTENT_CORNER_IDXS = [] {
using Extent = GlobalPosition3D BoundingBox::*;
constexpr auto EXTENTS = std::array {&BoundingBox::min, &BoundingBox::max};
@@ -36,7 +48,7 @@ Frustum::contains(const BoundingBox & aabb) const
= EXTENT_CORNER_IDXS * [relativeAabb = aabb - position](auto idxs) -> glm::vec4 {
return {(relativeAabb.*(idxs.x)).x, (relativeAabb.*(idxs.y)).y, (relativeAabb.*(idxs.z)).z, 1.F};
};
- return std::ranges::none_of(planes, [&corners](const auto & frustumPlane) {
+ return std::ranges::none_of(std::span(planes).subspan(0, nplanes), [&corners](const auto & frustumPlane) {
return (std::ranges::all_of(corners, [&frustumPlane](const auto & corner) {
return glm::dot(frustumPlane, corner) < 0.F;
}));
diff --git a/gfx/frustum.h b/gfx/frustum.h
index 2624ba1..a2d90e9 100644
--- a/gfx/frustum.h
+++ b/gfx/frustum.h
@@ -31,6 +31,7 @@ public:
using BoundingBox = AxisAlignedBoundingBox<GlobalDistance>;
[[nodiscard]] bool contains(const BoundingBox &) const;
+ [[nodiscard]] bool shadedBy(const BoundingBox &) const;
protected:
static constexpr size_t FACES = 6;