Search code examples
c++openglterraingl-triangle-strip

How does OpenGL actually read indices?


I trying to figure out how to draw a flat terrain with triangle_strips, and while I was writing the loop for creating indices array I thought how does OpenGL know to parse the indices because so far I have seen everybody when creating indices write them like that:

0 3 1 4 2 5 0xFFFF 3 6 4 7 5 8

(here being 0xFFFF primitive restart that marks the end of the strip)

and so technically as I understand this should be parsed with offset +1 for every triangle...so the first triangle would use first three indices (0 3 1) and the next with offset +1 (3 1 4)...next (1 4 2) and so on. Here's the picture in case I didn't explain well: First way

But other way that I have seen is creating indices for every triangle separately .... so: 0 3 1 3 1 4 1 4 2 4 2 5 Second way

So my question is how to specify the layout...does OpenGL do this automatically as I set draw call to GL_TRIANGLE_STRIP or else?


Solution

  • There are three kinds of triangle primitives:

    • GL_TRIANGLES: Vertices 0, 1, and 2 form a triangle. Vertices 3, 4, and 5 form a triangle. And so on.

    • GL_TRIANGLE_STRIP: Every group of 3 adjacent vertices forms a triangle. A vertex stream of n length will generate n-2 triangles.

    • GL_TRIANGLE_FAN: The first vertex is always held fixed. From there on, every group of 2 adjacent vertices form a triangle with the first. So with a vertex stream, you get a list of triangles like so: (0, 1, 2) (0, 2, 3), (0, 3, 4), etc. A vertex stream of n length will generate n-2 triangles.

    Your first scenario describes a GL_TRIANGLE_STRIP, while your second scenario describes GL_TRIANGLES.