OpenGL – why is GL_ELEMENT_ARRAY_BUFFER for indices?

GL_ELEMENT_ARRAY_BUFFER is used to indicate the buffer you’re presenting contains the indices of each element in the “other” (GL_ARRAY_BUFFER) buffer.

So, as a very basic example with vertices only (no other data), if you have an index buffer:

{0, 1, 2} {0, 2, 3}

and the data buffer contains:

{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 1, 0}}

Then, when you call glDrawElements, it knows to pick out the vertices 0, 1 & 2 for the first triangle, then 0, 2, 3 for the second (ie: basically a square).

This becomes more useful when you have more complicated models with a lots of vertices & faces – as many of the faces will share the same vertices (hence you don’t need to “resend” the same data).

Note: The above example only shows vertices – you can interleave as much data as you like in there (vertex colours, normals, texture coordinates… etc).

Leave a Comment