SInce someone else asked a similar question. Here is some example code for SH-clipping.
//--------------------------------------------------------------------------------------------------
RnArray< RnVector3 > rnClipPolygon( const RnArray< RnVector3 >& Polygon, const RnPlane& Plane, int Edge )
{
RN_ASSERT( Polygon.Size() >= 3 );
RnArray< RnVector3 > Out;
RnVector3 Vertex1 = Polygon.Back();
float Distance1 = rnDistance( Plane, Vertex1 );
for ( int Index = 0; Index < Polygon.Size(); ++Index )
{
RnVector3 Vertex2 = Polygon[ Index ];
float Distance2 = rnDistance( Plane, Vertex2 );
if ( Distance1 <= 0.0f && Distance2 <= 0.0f )
{
// Both vertices are behind the plane - keep vertex2
Out.PushBack( Vertex2 );
}
else if ( Distance1 <= 0.0f && Distance2 > 0.0f )
{
// Vertex1 is behind of the plane, vertex2 is in front -> intersection point
float Fraction = Distance1 / ( Distance1 - Distance2 );
RnVector3 IntersectionPoint = Vertex1.Position + Fraction * ( Vertex2.Position - Vertex1.Position );
// Keep intersection point
Out.PushBack( IntersectionPoint );
}
else if ( Distance2 <= 0.0f && Distance1 > 0 )
{
// Vertex2 is behind of the plane, vertex1 is in front -> intersection point
float Fraction = Distance1 / ( Distance1 - Distance2 );
RnVector3 IntersectionPoint = Vertex1.Position + Fraction * ( Vertex2.Position - Vertex1.Position );
// Keep intersection point
Out.PushBack( IntersectionPoint );
// And also keep vertex2
Out.PushBack( Vertex2 );
}
// Keep vertex2 as starting vertex for next edge
Vertex1 = Vertex2;
Distance1 = Distance2;
}
return Out;
}
HTH,
-Dirk