Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL - why is GL_ELEMENT_ARRAY_BUFFER for indices?

I'm currently new to the OpenGL ES 2.0 realm and would like to understand as much as I can regarding binding, buffers, shaders, etc.

As of now, I'm just trying to understand the differences between GL_ELEMENT_ARRAY_BUFFER and GL_ARRAY_BUFFER and when to use each of the noted presets.

My current understanding leads me to believe that GL_ELEMENT_ARRAY_BUFFER is specifically for indices for the said triangles while the other is for everything else.

Could someone please elaborate on why and if this is correct? How is GL_ELEMENT_ARRAY_BUFFER handled differently?

like image 943
TheCodingArt Avatar asked Feb 26 '13 16:02

TheCodingArt


2 Answers

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

like image 59
goatherder Avatar answered Sep 28 '22 17:09

goatherder


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.

like image 27
datenwolf Avatar answered Sep 28 '22 17:09

datenwolf