Fixes lines not drawing if one point is outside the camera

This commit is contained in:
2024-03-11 18:33:09 -04:00
parent df6891e6ab
commit 7f633922be
5 changed files with 41 additions and 15 deletions

View File

@@ -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 };

View File

@@ -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(

View File

@@ -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;
}

View File

@@ -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

View File

@@ -85,4 +85,10 @@ namespace fgl::engine
static_assert( sizeof( glm::vec3 ) == sizeof( ModelCoordinate ) );
} // namespace fgl::engine
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