Search code examples
c++openglgraphics3dvertex-buffer

Rendering different triangle types and triangle fans using vertex buffer objects? (OpenGL)


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!


Solution

  • 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.