From 40cbffc450b3593a5edc45548d4451defbfb251a Mon Sep 17 00:00:00 2001 From: kj16609 Date: Fri, 23 Feb 2024 23:25:50 -0500 Subject: [PATCH] Fixes BoundingBox lines not being correct --- src/engine/model/BoundingBox.cpp | 23 +++++++++++++++++++---- src/engine/primitives/Frustum.cpp | 26 ++++++++++++++++++-------- src/engine/primitives/Plane.hpp | 5 ----- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/engine/model/BoundingBox.cpp b/src/engine/model/BoundingBox.cpp index ac4c325..e3564ed 100644 --- a/src/engine/model/BoundingBox.cpp +++ b/src/engine/model/BoundingBox.cpp @@ -199,11 +199,26 @@ namespace fgl::engine std::vector< Line< CType > > BoundingBox< CType >::lines() const { const auto points { this->points() }; + std::vector< Line< CType > > lines; - for ( std::uint32_t i = 0; i < points.size() - 1; ++i ) - { - lines.emplace_back( points[ i ], points[ i + 1 ] ); - } + + //Top + lines.emplace_back( points[ 0 ], points[ 1 ] ); + lines.emplace_back( points[ 1 ], points[ 2 ] ); + lines.emplace_back( points[ 2 ], points[ 3 ] ); + lines.emplace_back( points[ 3 ], points[ 0 ] ); + + //Bottom + lines.emplace_back( points[ 4 ], points[ 5 ] ); + lines.emplace_back( points[ 5 ], points[ 6 ] ); + lines.emplace_back( points[ 6 ], points[ 7 ] ); + lines.emplace_back( points[ 7 ], points[ 4 ] ); + + //Sides + lines.emplace_back( points[ 0 ], points[ 4 ] ); + lines.emplace_back( points[ 1 ], points[ 5 ] ); + lines.emplace_back( points[ 2 ], points[ 6 ] ); + lines.emplace_back( points[ 3 ], points[ 7 ] ); return lines; } diff --git a/src/engine/primitives/Frustum.cpp b/src/engine/primitives/Frustum.cpp index f644f92..d62d485 100644 --- a/src/engine/primitives/Frustum.cpp +++ b/src/engine/primitives/Frustum.cpp @@ -13,17 +13,27 @@ namespace fgl::engine template <> bool Frustum< CoordinateSpace::World >::intersects( const Line< CoordinateSpace::World > line ) const { + const bool top_intersects { top.intersects( line ) }; + const bool bottom_intersects { bottom.intersects( line ) }; + + const bool left_intersects { left.intersects( line ) }; + const bool right_intersects { right.intersects( line ) }; + + const bool near_intersects { near.intersects( line ) }; + const bool far_intersects { far.intersects( line ) }; + //Check if the line passes through the frustum - const bool intersects_forward_back { near.intersects( line ) && far.intersects( line ) }; - const bool intersects_left_right { left.intersects( line ) && right.intersects( line ) }; - const bool intersects_top_bottom { top.intersects( line ) && bottom.intersects( line ) }; + const bool intersects_left_right { left_intersects && right_intersects }; + const bool intersects_top_bottom { top_intersects && bottom_intersects }; - //const bool line_within_top_bottom { top.isForward( line ) && bottom.isForward( line ) }; - //const bool line_within_left_right { left.isForward( line ) && right.isForward( line ) }; - //At least one point of the line is within the near and far planes - const bool line_within_near_far { near.isForward( line ) || far.isForward( line ) }; + const bool line_within_near_far { !near_intersects && !far_intersects }; - return line_within_near_far && ( intersects_forward_back || intersects_top_bottom || intersects_left_right ); + const bool line_outside_top_bottom { !top_intersects && !bottom_intersects }; + const bool line_outside_left_right { !left_intersects && !right_intersects }; + + const bool line_outside_range { line_outside_top_bottom && line_outside_left_right }; + + return line_within_near_far && !( line_outside_range ) && ( intersects_top_bottom || intersects_left_right ); } template <> diff --git a/src/engine/primitives/Plane.hpp b/src/engine/primitives/Plane.hpp index d3e6024..495375d 100644 --- a/src/engine/primitives/Plane.hpp +++ b/src/engine/primitives/Plane.hpp @@ -48,11 +48,6 @@ namespace fgl::engine bool isForward( const WorldCoordinate coord ) const { return distanceFrom( coord ) > 0.0; } - bool isForward( const Line< CoordinateSpace::World > line ) const - { - return isForward( line.start ) && isForward( line.end ); - } - bool isBehind( const WorldCoordinate coord ) const { return !isForward( coord ); } //! Returns a normalized Vector