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).
This has mostly historic reasons. Back when there were no VBOs, the pointers specified with glVertexPointer and similar were not "associated" with a OpenGL object of any kind. When VBOs got introduced this behavior carried over into the semantics of VBOs, which required a different buffer target for indices and attributes.
With the introduction of generic vertex attributes such an association functionality has been added.
Today it's mostly of a hint to the OpenGL implementation to know, in which way the data is going to be addressed, to optimize the data flow accordingly. But it also functions well as a mental reminder to the programmer, what's currently dealt with.