24

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?

Rami Chasygov
  • 2,714
  • 7
  • 25
  • 37
TheCodingArt
  • 3,436
  • 4
  • 30
  • 53

2 Answers2

36

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

TheCodingArt
  • 3,436
  • 4
  • 30
  • 53
goatherder
  • 559
  • 3
  • 4
  • 4
    This is the understanding I have come to know. The only real question I have is why the word ELEMENT in the buffer tag? That's what throws me off every single time because ... it's just an ARRAY. I wouldn't quite call it an elemental array. – TheCodingArt Feb 27 '13 at 17:52
  • 5
    Personally the term "element" in that name to me means it is an array of "element" references - as the data buffer can contain arbitrary data - but each grouping of data (vertex + normal + colour + tex coord) can be basically termed an "element". – goatherder Feb 28 '13 at 02:01
  • Well, the element array just contains the order of the array containing the elements... that's the only logical reason I can think of, which makes no sense to me. Why isn't the GL_ARRAY_BUFFER called the element buffer if it contains the elements? That's where I'm a little lost lol. – TheCodingArt Feb 28 '13 at 22:22
18

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.

datenwolf
  • 159,371
  • 13
  • 185
  • 298