Skip to Content.
Sympa Menu

cgal-discuss - Re: [cgal-discuss] Polyhedron rendering in OpenGL

Subject: CGAL users discussion list

List archive

Re: [cgal-discuss] Polyhedron rendering in OpenGL


Chronological Thread 
  • From: gilles <>
  • To:
  • Subject: Re: [cgal-discuss] Polyhedron rendering in OpenGL
  • Date: Wed, 30 Sep 2009 21:21:36 +0200

Amy Tabb a écrit :
Hello,

I recently started using CGAL (but have been using OpenGL for a while) and here's some functions that I use. I tried to simplify my functions so that you could use them as a template or incorporate the CGAL-specific information easily into your work. Good luck.


Best, Amy


The first function draws the points and edges of the polyhedron, assuming that somewhere you have something resembling these typedefs:

typedef Kernel::Point_3 Point;


typedef CGAL::Polyhedron_3<Kernel, My_items, CGAL::HalfedgeDS_default> Polyhedron;
typedef CGAL::Tag_true Supports_removal;

typedef Polyhedron::Vertex Vertex;
typedef Polyhedron::Vertex_iterator Vertex_iterator;
typedef Polyhedron::Halfedge_iterator Halfedge_iterator;
typedef Polyhedron::Halfedge_handle Halfedge_handle;
typedef Polyhedron::Edge_iterator Edge_iterator;

***************************************
The function:


void drawPolyhedronPointsAndLines(Polyhedron P){
Facet_iterator f;
Vertex_iterator v;
Edge_iterator eit;
Halfedge_handle h_handle;
double x, y, z;

// this just walks through all of your vertices .... the 3D space data is in vertex_handle->point().x(), .y(), .z() (if a 3D point versus 2D)
glBegin(GL_POINTS);
for (v = P.vertices_begin(); v != P.vertices_end(); ++v){
cout << "Vertex " << v->point() << endl;
x = v->point().x();
y = v->point().y();
z = v->point().z();
glVertex3f(x, y, z);
}
glEnd();

Point_3_exact pv;

// then I walk through the edges (not halfedges, though you could do this too) of the Polyhedron.
for (eit = P.edges_begin(); eit != P.edges_end(); ++eit){
h_handle = eit;
glColor3f(r, g, b);

glBegin( GL_LINES);
pv = h_handle->opposite()->vertex()->point();
glVertex3f(pv.x(), pv.y(), pv.z());
pv = h_handle->vertex()->point();
glVertex3f(pv.x(), pv.y(), pv.z());
glEnd();
}
}


**********************************************************************
Depending on your goal, you may want to walk around the edges of one facet so that you can create the faces of the polyhedron.
I'll include that here as well.


Again, the function below assumes another typedef:
typedef Polyhedron::Halfedge_around_facet_circulator HF_circulator;


void drawPolyhedronFacets(Polyhedron P){

Facet_iterator f;
Vertex_iterator v;
HF_circulator j;
double x, y, z;

Point pv
Point p1; Point p2;

// I use the GLUtesselator because some of the faces may be concave or have holes. The documentation for that is with the OpenGL community.
// I didn't include all of the GLU tess functions because this is just an example of how to use CGAL to walk around the faces of a Polyhedron.
GLUtesselator *tobj;
tobj = gluNewTess();
gluTessCallback(tobj, GLU_TESS_VERTEX,
(void(*)())glVertex3dv);
gluTessCallback(tobj, GLU_TESS_BEGIN,
(void(*)())beginCallback);
gluTessCallback(tobj, GLU_TESS_END,
endCallback);
gluTessCallback(tobj, GLU_TESS_ERROR,
(void(*)())errorCallback);
gluTessCallback(tobj, GLU_TESS_COMBINE,
(void(*)())combineCallback);
// End GLUtesselator declarations

list<GLdouble*> coords_list;
GLdouble* arr_ptr;

// prepare to tesselate for facets...
for(f = P.facets_begin(); f != P.facets_end(); f++){ // create new center vertices
j = f->facet_begin();

// normal computation - there are many ways to do this -
// I store the normal of each facet through the use of
// My_items (extended dcel records - see above in the typedef declaration of Polyhedron,
// but this is another CGAL issue that if interested you'll have to read up on),
// though you could compute it as you walk around the facet.
n = f->normal/sqrt(f->normal.squared_length());
cout << " n " << n << endl;

// for GLUtesselation gluTessBeginPolygon(tobj, NULL);
gluTessBeginContour(tobj);
// for GLUtesselation end glNormal3f(n.x(),n.y(),n.z());
do {
cout << j->vertex()->point() << endl;
pv = j->vertex()->point();
x = pv.x();
y = pv.y();
z = pv.z();
// GLU tess specific actions start
arr_ptr = new GLdouble[3];
arr_ptr[0] = x;
arr_ptr[1] = y;
arr_ptr[2] = z;
gluTessVertex(tobj, arr_ptr, arr_ptr);
coords_list.push_back(arr_ptr);
// end GLUtess specific actions
} while ( ++j != f->facet_begin());
gluTessEndContour(tobj);
gluTessEndPolygon(tobj);

}


// more gluTess specific actions
gluDeleteTess(tobj);
while (coords_list.size() > 0){
arr_ptr = coords_list.front(); coords_list.pop_front();
delete arr_ptr;
}

}

Laurent Rineau (GeometryFactory) wrote:
Le mardi 29 septembre 2009 20:55:05,

a écrit :
Hello,

I'm a (very) beginner and I do need help (to start) !!!

I'm wondering how can get I from HDS (it's a Polyhedron) the proper vertex
to use afterwards
for rendering in OpenGL.

For example, I want to convert HDS into triplet of point (forming a
triangle)

-> Can anybody helps ?

That you so much for this little "coup de pouce"

You should have a look at the source code of the polyhedron demo, in demo/Polyhedron/, with CGAL>=3.4.

In CGAL-3.5, see the "draw" methods in Scene_polyhedron_item.cpp, and Scene_nef_polyhedron_item.cpp, which use code from demo/Polyhedron/include/CGAL/gl_render.h and demo/Polyhedron/Scene_nef_rendering.cpp.

The nef case shows the non-triangular case, which is far more complicated, as one has to triangulate each face of the polyhedron.

I hope that can help.



Hello Amy,

Thank you very much : Life will be a bit better for me now ;)

Gilles



Archive powered by MHonArc 2.6.16.

Top of Page