From c310240881e9d1b474db6ef8f8f2891ce1646795 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 7 Mar 2025 00:11:07 +0000 Subject: Position is moved to Frustum --- gfx/frustum.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'gfx/frustum.cpp') diff --git a/gfx/frustum.cpp b/gfx/frustum.cpp index 9e046f6..a9dbdf0 100644 --- a/gfx/frustum.cpp +++ b/gfx/frustum.cpp @@ -5,8 +5,8 @@ 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(); } -- cgit v1.2.3 From d0aceb54752078200bc75a96888ffaf7483678cb Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 7 Mar 2025 00:43:25 +0000 Subject: Add function to test if an AABB is visible in a frustum --- gfx/aabb.cpp | 6 ++++++ gfx/aabb.h | 2 ++ gfx/frustum.cpp | 26 ++++++++++++++++++++++++++ gfx/frustum.h | 4 ++++ 4 files changed, 38 insertions(+) (limited to 'gfx/frustum.cpp') diff --git a/gfx/aabb.cpp b/gfx/aabb.cpp index 862dacb..0a04ef4 100644 --- a/gfx/aabb.cpp +++ b/gfx/aabb.cpp @@ -22,3 +22,9 @@ AxisAlignedBoundingBox::operator+=(const GlobalPosition3D & point) max = glm::max(max, point); return *this; } + +AxisAlignedBoundingBox +AxisAlignedBoundingBox::operator-(const GlobalPosition3D & viewPoint) const +{ + return {min - viewPoint, max - viewPoint}; +} diff --git a/gfx/aabb.h b/gfx/aabb.h index 568e91a..db3a215 100644 --- a/gfx/aabb.h +++ b/gfx/aabb.h @@ -11,6 +11,8 @@ public: AxisAlignedBoundingBox & operator+=(const GlobalPosition3D & point); + AxisAlignedBoundingBox operator-(const GlobalPosition3D & viewPoint) const; + [[nodiscard]] static AxisAlignedBoundingBox fromPoints(std::span points); GlobalPosition3D min, max; diff --git a/gfx/frustum.cpp b/gfx/frustum.cpp index a9dbdf0..8031d85 100644 --- a/gfx/frustum.cpp +++ b/gfx/frustum.cpp @@ -1,4 +1,5 @@ #include "frustum.h" +#include "aabb.h" #include #include #include @@ -18,6 +19,31 @@ Frustum::updateView(const glm::mat4 & newView) updateCache(); } +bool +Frustum::contains(const AxisAlignedBoundingBox & aabb) const +{ + static constexpr auto EXTENT_CORNER_IDXS = [] { + using Extent = GlobalPosition3D AxisAlignedBoundingBox::*; + constexpr auto EXTENTS = std::array {&AxisAlignedBoundingBox::min, &AxisAlignedBoundingBox::max}; + std::array, 2ZU * 2ZU * 2ZU> out {}; + std::ranges::copy(std::views::cartesian_product(EXTENTS, EXTENTS, EXTENTS) + | std::views::transform( + std::make_from_tuple, std::tuple>), + out.begin()); + return out; + }(); + + const std::array 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(planes, [&corners](const auto & frustumPlane) { + return (std::ranges::all_of(corners, [&frustumPlane](const auto & corner) { + return glm::dot(frustumPlane, corner) < 0.F; + })); + }); +} + void Frustum::updateCache() { diff --git a/gfx/frustum.h b/gfx/frustum.h index 46f4108..5ee7d2c 100644 --- a/gfx/frustum.h +++ b/gfx/frustum.h @@ -4,6 +4,8 @@ #include #include +class AxisAlignedBoundingBox; + class Frustum { public: Frustum(const GlobalPosition3D & pos, const glm::mat4 & view, const glm::mat4 & projection); @@ -28,6 +30,8 @@ public: void updateView(const glm::mat4 & view); + [[nodiscard]] bool contains(const AxisAlignedBoundingBox &) const; + protected: static constexpr size_t FACES = 6; void updateCache(); -- cgit v1.2.3 From a6cec1f8eeb54a12fb2ee058f07a451d3b549958 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 8 Mar 2025 19:54:10 +0000 Subject: Template AxisAlignedBoundingBox on unit type --- game/geoData.h | 2 +- game/terrain.cpp | 2 +- game/terrain.h | 2 +- gfx/aabb.cpp | 15 --------------- gfx/aabb.h | 30 ++++++++++++++++++++---------- gfx/frustum.cpp | 7 +++---- gfx/frustum.h | 7 ++++--- test/test-lib.cpp | 2 +- 8 files changed, 31 insertions(+), 36 deletions(-) delete mode 100644 gfx/aabb.cpp (limited to 'gfx/frustum.cpp') diff --git a/game/geoData.h b/game/geoData.h index a504f9b..0ff0c42 100644 --- a/game/geoData.h +++ b/game/geoData.h @@ -81,5 +81,5 @@ protected: virtual void afterChange(); private: - AxisAlignedBoundingBox extents; + AxisAlignedBoundingBox extents; }; diff --git a/game/terrain.cpp b/game/terrain.cpp index 530b373..577d9e5 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -82,7 +82,7 @@ Terrain::generateMeshes() {(surfaceKey.basePosition + 1) * TILE_SIZE || getExtents().max.z}}; } else { - meshItr->second.aabb = AxisAlignedBoundingBox::fromPoints( + meshItr->second.aabb = AxisAlignedBoundingBox::fromPoints( indices | std::views::transform([this](const auto vertex) -> GlobalPosition3D { return this->point(VertexHandle {static_cast(vertex)}); })); diff --git a/game/terrain.h b/game/terrain.h index f38fe84..5f03634 100644 --- a/game/terrain.h +++ b/game/terrain.h @@ -35,7 +35,7 @@ private: glVertexArray vertexArray; glBuffer indicesBuffer; GLsizei count; - AxisAlignedBoundingBox aabb; + AxisAlignedBoundingBox aabb; }; struct SurfaceKey { diff --git a/gfx/aabb.cpp b/gfx/aabb.cpp deleted file mode 100644 index 19c2217..0000000 --- a/gfx/aabb.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "aabb.h" - -AxisAlignedBoundingBox & -AxisAlignedBoundingBox::operator+=(const GlobalPosition3D & point) -{ - min = glm::min(min, point); - max = glm::max(max, point); - return *this; -} - -AxisAlignedBoundingBox -AxisAlignedBoundingBox::operator-(const GlobalPosition3D & viewPoint) const -{ - return {min - viewPoint, max - viewPoint}; -} diff --git a/gfx/aabb.h b/gfx/aabb.h index 229d516..a661f66 100644 --- a/gfx/aabb.h +++ b/gfx/aabb.h @@ -1,31 +1,41 @@ #pragma once -#include "config/types.h" +#include "maths.h" #include #include -class AxisAlignedBoundingBox { +template class AxisAlignedBoundingBox { public: + using V = glm::vec<3, T, Q>; AxisAlignedBoundingBox() = default; - AxisAlignedBoundingBox(const GlobalPosition3D & min, const GlobalPosition3D & max) : min {min}, max {max} { } + AxisAlignedBoundingBox(const V & min, const V & max) : min {min}, max {max} { } - AxisAlignedBoundingBox & operator+=(const GlobalPosition3D & point); + AxisAlignedBoundingBox & + operator+=(const V & point) + { + min = glm::min(min, point); + max = glm::max(max, point); + return *this; + } - AxisAlignedBoundingBox operator-(const GlobalPosition3D & viewPoint) const; + AxisAlignedBoundingBox + operator-(const V & viewPoint) const + { + return {min - viewPoint, max - viewPoint}; + } [[nodiscard]] static AxisAlignedBoundingBox fromPoints(auto && points) { - using Limits = std::numeric_limits; - static constexpr const auto INITIAL - = std::make_pair(GlobalPosition3D {Limits::max()}, GlobalPosition3D {Limits::min()}); - return std::make_from_tuple( + using Limits = std::numeric_limits; + static constexpr const auto INITIAL = std::make_pair(V {Limits::max()}, V {Limits::min()}); + return std::make_from_tuple>( std::ranges::fold_left(points, INITIAL, [](const auto & prev, const auto & point) { auto & [min, max] = prev; return std::make_pair(glm::min(min, point), glm::max(max, point)); })); } - GlobalPosition3D min, max; + V min, max; }; diff --git a/gfx/frustum.cpp b/gfx/frustum.cpp index 8031d85..865dcde 100644 --- a/gfx/frustum.cpp +++ b/gfx/frustum.cpp @@ -1,5 +1,4 @@ #include "frustum.h" -#include "aabb.h" #include #include #include @@ -20,11 +19,11 @@ Frustum::updateView(const glm::mat4 & newView) } bool -Frustum::contains(const AxisAlignedBoundingBox & aabb) const +Frustum::contains(const BoundingBox & aabb) const { static constexpr auto EXTENT_CORNER_IDXS = [] { - using Extent = GlobalPosition3D AxisAlignedBoundingBox::*; - constexpr auto EXTENTS = std::array {&AxisAlignedBoundingBox::min, &AxisAlignedBoundingBox::max}; + using Extent = GlobalPosition3D BoundingBox::*; + constexpr auto EXTENTS = std::array {&BoundingBox::min, &BoundingBox::max}; std::array, 2ZU * 2ZU * 2ZU> out {}; std::ranges::copy(std::views::cartesian_product(EXTENTS, EXTENTS, EXTENTS) | std::views::transform( diff --git a/gfx/frustum.h b/gfx/frustum.h index 5ee7d2c..2624ba1 100644 --- a/gfx/frustum.h +++ b/gfx/frustum.h @@ -1,11 +1,10 @@ #pragma once +#include "aabb.h" #include "config/types.h" #include #include -class AxisAlignedBoundingBox; - class Frustum { public: Frustum(const GlobalPosition3D & pos, const glm::mat4 & view, const glm::mat4 & projection); @@ -30,11 +29,13 @@ public: void updateView(const glm::mat4 & view); - [[nodiscard]] bool contains(const AxisAlignedBoundingBox &) const; + using BoundingBox = AxisAlignedBoundingBox; + [[nodiscard]] bool contains(const BoundingBox &) const; protected: static constexpr size_t FACES = 6; void updateCache(); + [[nodiscard]] bool boundByPlanes(const BoundingBox &, size_t nplanes) const; GlobalPosition3D position; glm::mat4 view, projection; diff --git a/test/test-lib.cpp b/test/test-lib.cpp index 8dadc70..ec91f6e 100644 --- a/test/test-lib.cpp +++ b/test/test-lib.cpp @@ -114,7 +114,7 @@ BOOST_DATA_TEST_CASE(mergeCloseInts, BOOST_AUTO_TEST_CASE(aabb_from_points) { - const auto aabb = AxisAlignedBoundingBox::fromPoints(std::vector { + const auto aabb = AxisAlignedBoundingBox::fromPoints(std::vector { {1, 2, 3}, {4, 2, 1}, {9, 1, 7}, -- cgit v1.2.3 From fc74736b9eaa1f3e033ded9103b69d2a39a3e263 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 11 Mar 2025 00:40:35 +0000 Subject: 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. --- gfx/frustum.cpp | 16 ++++++++++++++-- gfx/frustum.h | 1 + 2 files changed, 15 insertions(+), 2 deletions(-) (limited to 'gfx/frustum.cpp') 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 #include -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 {} @@ -20,6 +20,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::*; @@ -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; [[nodiscard]] bool contains(const BoundingBox &) const; + [[nodiscard]] bool shadedBy(const BoundingBox &) const; protected: static constexpr size_t FACES = 6; -- cgit v1.2.3