diff --git a/src/engine/EngineContext.cpp b/src/engine/EngineContext.cpp index 7627a5a..45ff8f7 100644 --- a/src/engine/EngineContext.cpp +++ b/src/engine/EngineContext.cpp @@ -505,7 +505,7 @@ namespace fgl::engine model->syncBuffers( command_buffer ); - constexpr int val { 16 }; + constexpr int val { 1 }; constexpr float x_offset { -( static_cast< float >( val ) * 30.0f ) / 2.0f }; constexpr float y_offset { -( static_cast< float >( val ) * 20.0f ) / 2.0f }; diff --git a/src/engine/debug/drawers.cpp b/src/engine/debug/drawers.cpp index 96546d8..2fa20b7 100644 --- a/src/engine/debug/drawers.cpp +++ b/src/engine/debug/drawers.cpp @@ -133,28 +133,33 @@ namespace fgl::engine::debug inline void drawLine( const LineSegment< CoordinateSpace::World > line, const glm::vec3 color, const float thickness ) { - const Coordinate< CoordinateSpace::Screen > start_screen { toScreenSpace( line.getPosition() ) }; - const Coordinate< CoordinateSpace::Screen > end_screen { toScreenSpace( line.getEnd() ) }; + Coordinate< CoordinateSpace::Screen > start_screen { toScreenSpace( line.getPosition() ) }; + Coordinate< CoordinateSpace::Screen > end_screen { toScreenSpace( line.getEnd() ) }; - if ( !inView( start_screen.vec() ) && !inView( end_screen.vec() ) ) return; + if ( isBehind( start_screen.vec() ) && isBehind( end_screen.vec() ) ) return; + const auto frustum { getDebugDrawingCamera().getFrustumBounds() }; + + if ( !frustum.intersects( line ) ) return; + + //Check if either point is behind the camera if ( isBehind( start_screen.vec() ) ) { - const auto frustum { getDebugDrawingCamera().getFrustumBounds() }; const auto new_line { line.flip() }; const auto new_point { frustum.intersection( new_line ) }; + + start_screen = toScreenSpace( new_point ); } else if ( isBehind( end_screen.vec() ) ) - {} - else { - ImGui::GetBackgroundDrawList()->AddLine( - glmToImgui( start_screen ), - glmToImgui( end_screen ), - ImColor( color.x, color.y, color.z ), - thickness ); + const auto new_point { frustum.intersection( line ) }; + + end_screen = toScreenSpace( new_point ); } + + ImGui::GetBackgroundDrawList()->AddLine( + glmToImgui( start_screen ), glmToImgui( end_screen ), ImColor( color.x, color.y, color.z ), thickness ); } void drawLine( diff --git a/src/engine/primitives/Frustum.cpp b/src/engine/primitives/Frustum.cpp index 21c5251..083d0d4 100644 --- a/src/engine/primitives/Frustum.cpp +++ b/src/engine/primitives/Frustum.cpp @@ -325,16 +325,24 @@ namespace fgl::engine CoordinateSpace::World >::intersection( const Line< CoordinateSpace::World >& line ) const { Coordinate< CoordinateSpace::World > coordinate { line.getEnd() }; + const float max_dist { signedDistance( line.getDirection(), coordinate, line.getPosition() ) }; //Test each. Whatever the line enters exists first is the point to return auto testPlane = [ & ]( const Plane< CoordinateSpace::World >& plane ) { - const auto old_distance { signedDistance( line.getDirection(), coordinate, line.getPosition() ) }; + const float old_distance { signedDistance( line.getDirection(), coordinate, line.getPosition() ) }; const auto intersection { line.intersection( plane ) }; + + if ( std::isnan( intersection.x ) || std::isnan( intersection.y ) || std::isnan( intersection.z ) ) return; + const auto intersection_distance { signedDistance( line.getDirection(), intersection, line.getPosition() ) }; - if ( old_distance < intersection_distance ) coordinate = intersection; + + if ( intersection_distance < 0.0f ) return; + if ( intersection_distance > max_dist ) return; + + if ( old_distance > intersection_distance ) coordinate = intersection; }; testPlane( this->right ); @@ -344,6 +352,10 @@ namespace fgl::engine testPlane( this->top ); testPlane( this->bottom ); + const auto distance { signedDistance( line.getDirection(), coordinate, line.getPosition() ) }; + + assert( distance > 0.0f ); + return coordinate; } diff --git a/src/engine/primitives/lines/LineSegment.hpp b/src/engine/primitives/lines/LineSegment.hpp index 9f9f235..fd995a5 100644 --- a/src/engine/primitives/lines/LineSegment.hpp +++ b/src/engine/primitives/lines/LineSegment.hpp @@ -47,6 +47,9 @@ namespace fgl::engine return plane.isForward( start ) != plane.isForward( end ); } + template < typename T > + bool intersets( const T t ) const; + template < typename T > requires is_plane< T > Coordinate< CType > FGL_FLATTEN intersection( const T plane ) const diff --git a/src/engine/primitives/points/Coordinate.hpp b/src/engine/primitives/points/Coordinate.hpp index ff6108c..10c31c5 100644 --- a/src/engine/primitives/points/Coordinate.hpp +++ b/src/engine/primitives/points/Coordinate.hpp @@ -85,4 +85,10 @@ namespace fgl::engine static_assert( sizeof( glm::vec3 ) == sizeof( ModelCoordinate ) ); -} // namespace fgl::engine \ No newline at end of file + template < fgl::engine::CoordinateSpace CType > + ::std::ostream& operator<<( ::std::ostream& os, const fgl::engine::Coordinate< CType > coordinate ) + { + return os << "(" << coordinate.x << ", " << coordinate.y << ", " << coordinate.z << ")"; + } + +} // namespace fgl::engine