diff options
Diffstat (limited to 'gfx/frustum.cpp')
-rw-r--r-- | gfx/frustum.cpp | 43 |
1 files changed, 40 insertions, 3 deletions
diff --git a/gfx/frustum.cpp b/gfx/frustum.cpp index 9e046f6..faa676d 100644 --- a/gfx/frustum.cpp +++ b/gfx/frustum.cpp @@ -3,10 +3,10 @@ #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 glm::mat4 & view, const glm::mat4 & projection) : - view {view}, projection {projection}, viewProjection {}, inverseViewProjection {}, planes {} +Frustum::Frustum(const GlobalPosition3D & pos, const glm::mat4 & view, const glm::mat4 & projection) : + position {pos}, view {view}, projection {projection}, viewProjection {}, inverseViewProjection {}, planes {} { updateCache(); } @@ -18,6 +18,43 @@ Frustum::updateView(const glm::mat4 & newView) updateCache(); } +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}; + std::array<glm::vec<3, Extent>, 2ZU * 2ZU * 2ZU> out {}; + std::ranges::copy(std::views::cartesian_product(EXTENTS, EXTENTS, EXTENTS) + | std::views::transform( + std::make_from_tuple<glm::vec<3, Extent>, std::tuple<Extent, Extent, Extent>>), + out.begin()); + return out; + }(); + + const std::array<RelativePosition4D, 8> corners + = 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(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; + })); + }); +} + void Frustum::updateCache() { |