views:

554

answers:

1

Hello,

About half of my meshes are using triangles, another half using triangle fans. I'd like to offload these into a vertex buffer object but I'm not quite sure how to do this. The triangle fans all have a different number of vertices... for example, one might have 5 and another 7.

VBO's are fairly straight forward using plain triangles, however I'm not sure how to use them with triangle fans or with different triangle types. I'm fairly sure I need an index buffer, but I'm not quite sure what I need to do this.

I know how many vertices make up each fan during run time... I'm thinking I can use that to call something like glArrayElement

Any help here would be much appreciated!

+1  A: 

VBOs and index buffers are an orthogonal things. If you're not using index buffers yet, maybe it is wiser to move one step at a time.

So... regarding your question. If you put all your triangle fans in a vbo, the only thing you need to draw them is to setup your vbo and pass the index in it for your fan start

   glBindBuffer(GL_VERTEX_BUFFER, buffer);
   glVertexPointer(3, GL_FLOAT, 0, NULL); // 3 floats per vertex
   for each i in fans
       glDrawArrays(GL_TRIANGLE_FAN, indef_of_first_vertex_for_fan[i], fan_vertex_count[i])

Edit: I'd have to say that you're probably better off transforming your fans to a regular triangle set, and use glDrawArrays(GL_TRIANGLES) for all your triangles. A call per primitive is rarely efficient.

Bahbar
Bahbar, Thanks for the tip! I had read about transforming the fans into triangles... However I might have 200,000 triangle fan primitives. Granted, I'd only need to do this once, so in your opinion is it cheaper to transform them and then do glDrawArrays or should I just do the call for each prim? Thanks
Polaris878
Well, if you have 200K triangle fans, then you have that many calls **per frame**. Think about the cost compared to some load-time (or better off-line process time) that's required to transform the fans to triangles. sun proposed an extension because this was too much of an overhead *10 years ago*. The overhead/intersting work ratio only increased since.http://www.opengl.org/registry/specs/SUN/triangle_list.txt
Bahbar
To be complete, I should add that [primitive restart](http://www.opengl.org/registry/specs/NV/primitive_restart.txt) is an extension that you could be using as well (but requires indexing of your primitives, which you should look at anyways). [glMultiDrawArrays](http://www.opengl.org/sdk/docs/man/xhtml/glMultiDrawArrays.xml) is yet another attempt at reducing the overhead, but is rarely actually optimized in the drivers.
Bahbar
Good stuff, thanks man. It is so hard to find good examples/documentation of this kind of stuff
Polaris878