iosopengl-esvertex-buffergl-triangle-strip

opengl es, ios and triangle fans


I am currently rendering a scene using triangles with the following code:

        glBindVertexArrayOES(_mVertexArrayObjectTriangles);
        glBindBuffer(GL_ARRAY_BUFFER, _mVertexPositionNormalTriangles);
        glDrawElements(GL_TRIANGLES, _mCRBuffer->GetIndexTriangleData()->size(), GL_UNSIGNED_INT, 0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArrayOES(0);

_mVertexArrayObjectTriangles is my vertex array object holding elements to be rendered via triangles

_mVertexPositionNormalTriangles is my array of vertices and vertex normals

_mCRBuffer->GetIndexTriangleData() is my array of indices into the vertex array. This is a simple array of integers that encode each triangle (a,b,c,a,b,d would encode two triangles abc and abd).

All works just fine, but I would like to render my primitives using triangle fans instead of triangles. How do I set up an array of triangle fans (i.e. more that one) to be drawn using something like

glDrawElements(GL_TRIANGLE_FAN, ....

How do set up my index array to index a set of triangle fans for rendering (jnstead of triangles). The vertices themselves need not change, just the indices to render them using triangle fans instead of triangles.

I can find good examples using triangle strips (here), including how to set up the index array but nothing on triangle fans.


Solution

  • Changing from a strip to a fan is non-trivial, as your data needs to be setup accordingly. You have to have a central vertex from which all the other triangles emanate

    enter image description here

    If you choose to do it, you just need to order your vertices in the manner shown in the diagram, and make sure you do it in chunks of vertices that have a common vertex.