Simple Triangulation

Hello all,

I'm new to Open Cascade and I'm just fiddling around to try and get an idea of how everything is supposed to be used.

I have my own OpenGL engine in which I would like to render a cube generated via Open Cascade.

I used to bottle tutorial to figure out how to make the cube. Now I want to triangulated it so I have something to that I can give to OpenGL.

Here is my code:

double dSize = 10.0;

//Create points
gp_Pnt oPoint1( dSize, 0.0, dSize );
gp_Pnt oPoint2( dSize, 0.0, -dSize );
gp_Pnt oPoint3( -dSize, 0.0, -dSize );
gp_Pnt oPoint4( -dSize, 0.0, dSize );

//Create segments
Handle( Geom_TrimmedCurve ) hSegment1 = GC_MakeSegment( oPoint1, oPoint2 );
Handle( Geom_TrimmedCurve ) hSegment2 = GC_MakeSegment( oPoint2, oPoint3 );
Handle( Geom_TrimmedCurve ) hSegment3 = GC_MakeSegment( oPoint3, oPoint4 );
Handle( Geom_TrimmedCurve ) hSegment4 = GC_MakeSegment( oPoint4, oPoint1 );

//Create edges
TopoDS_Edge oEdge1 = BRepBuilderAPI_MakeEdge( hSegment1 );
TopoDS_Edge oEdge2 = BRepBuilderAPI_MakeEdge( hSegment2 );
TopoDS_Edge oEdge3 = BRepBuilderAPI_MakeEdge( hSegment3 );
TopoDS_Edge oEdge4 = BRepBuilderAPI_MakeEdge( hSegment4 );

//Create wire profile
TopoDS_Wire oWireProfile = BRepBuilderAPI_MakeWire( oEdge1, oEdge2, oEdge3, oEdge4 );

//Create face profile
TopoDS_Face oFaceProfile = BRepBuilderAPI_MakeFace( oWireProfile );

//Extrude profile
gp_Vec oPrismVec( 0, dSize, 0 );
TopoDS_Shape oBody = BRepPrimAPI_MakePrism( oFaceProfile, oPrismVec );

int iTriangleCount = 0;
//Explore body to extract and triangulate faces.
for( TopExp_Explorer oBodyExplorer( oBody, TopAbs_FACE ) ; oBodyExplorer.More() ; oBodyExplorer.Next() )
TopoDS_Face oCurrentFace = TopoDS::Face( oBodyExplorer.Current() );
TopLoc_Location oLocation;
Handle_Poly_Triangulation hCurrentTriangulation = BRep_Tool::Triangulation( oCurrentFace, oLocation );

if( !hCurrentTriangulation.IsNull() )
iTriangleCount += hCurrentTriangulation->NbTriangles();

BRep_Tool::Triangulation always returns a null handle. The documentation for BRep_Tool::Triangulation says that it means that the triangulation is impossible.

Am I missing something?


Göran Barz's picture

Hi Ian,

the triangulation isn't created automatically, you have to create it with the help of the BRepMesh-class.
BRepMesh::Mesh(oCurrentFace, 0.5);
before accessing the triangulation


Ian Clévy's picture

Thank you Göran, that fixed it!