views:

750

answers:

3

I have a mesh data structure with polygons that can be either triangles and quads. What is the fastest way to draw this using OpenGL?

The slow way is to iterate over the structure and for each polygon to make the appropriate glBegin() .. glEnd() with either GL_QUADS or GL_TRIANGLES. I would like to avoid having to do glBegin() .. glEnd() for each and every polygon.

Another option is to divide the structure into two structures, one containing the triangles and one containing the quads and then go over them separately. This is also something I'd like to avoid since I really want to keep them all in a single structure.

Unfortunately triangulating the quads into triangles is not an option at this time.

Is there a good solution to this?

+2  A: 

You may want to create two indice table, one for quads one for triangles and then write all the Quad, followed by triangles (or the inverse). To draw using an array of indices, use glDrawElements with a set of glVertexPointer and glEnableClientState to make it works.

Moreover if you really want to gain speed, you may place all of your vertices in a VBO, and your indice. This way you get all your vertices and indices on the GPU's RAM.

Raoul Supercopter
As a corollary: With OpenGL reducing the number of glXXX calls will generally provide a speedup. If possible, you should also use the _STRIP drawing modes.
Dolphin
+2  A: 

You can always convert them all to triangles, quite easily, too.

Basically the pseudocode would look something like this:

glBegin(GL_TRIANGLES);
for each (object *a in objectList)
  if (a->type == TRIANGLE)
    glVertex3fv(a->vertices);
  if (a->type == QUAD)
  {
    glVertex3f(a->vertices[0]);
    glVertex3f(a->vertices[1]);
    glVertex3f(a->vertices[2]);

    glVertex3f(a->vertices[2]);
    glVertex3f(a->vertices[1]);
    glVertex3f(a->vertices[3]);
  }
glEnd();

You'd have to be careful about keeping them oriented the same way (clockwise or counterclockwise), but it should work!

Andrei Krotkov
The problem with this is that if I'm drawing it with glPolygonMode(GL_LINE) there would be a diagonal line. I think I knew once how to avoid this..
shoosh
There's a technique of not drawing internal edges, but it completely slips my mind...
Andrei Krotkov
using `glBegin()` and `glEnd()` will gaurantee this is *not* going to be the fastest way.
Xavier Ho
+1  A: 

As far as glPolygonMode(GL_LINE) and decomposed GL_QUADS go, glEdgeFlagPointer() should be the functionality you're looking for. GL_TRUE for the perimeter segments, GL_FALSE on the quad diagonals.

Don't forget to glEnableClientState(GL_EDGE_FLAG_ARRAY).

genpfault