What are Vertex Array Objects?
I always think about VAO as an array of data buffers used by OpenGL. Using modern OpenGL you will create a VAO and Vertex Buffer Objects.
//vaoB is a buffer
glGenVertexArrays(1, vaoB); //creates one VAO
glBindVertexArray(vao.get(0));
glGenBuffers(vbo.length, vbo, 0); //vbo is a buffer
glBindVertexArray(vao.get(1));
glGenBuffers(vbo1.length, vbo1, 0); //vbo1 is a buffer
glBindVertexArray(vao.get(2));
glGenBuffers(vbo2.length, vbo2, 0); //vbo2 is a buffer
The next step is to bind data to a buffer:
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER,vertBuf.limit()*4, vertBuf, GL_STATIC_DRAW); //vertf buf is a floatbuffer of vertices
At this point OpenGL Sees:
Now we can use glVertexAttribPointer to tell OpenGL what the data in the buffer represents:
glBindBuffer(GL_ARRAY_BUFFER, 0); //bind VBO at 0
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0); //each vertex has 3 components of size GL_FLOAT with 0 stride (space) between them and the first component starts at 0 (start of data)
OpenGL now has the data in the buffer and knows how the data is organized into vertices. The same process can be applied to texture coordinates etc but for texture coordinates there would be two values.
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER,coordBuf.limit()*4, coordBuf, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);
Next you can bind texture and draw arrays, you will want to create a Vert and Frag shader, compile and attach it to a program (not included here).
glActiveTexture(textureID); //bind our texture
glBindTexture(GL_TEXTURE_2D, textureID);
glDrawArrays(GL_TRIANGLES,0,6); //in this case 6 indices are used for two triangles forming a square
"Vertex Array Object" is brought to you by the OpenGL ARB Subcommittee for Silly Names.
Think of it as a geometry object. (As an old time SGI Performer programmer, I call them geosets.) The instance variables/members of the object are your vertex pointer, normal pointer, color pointer, attrib N pointer, ...
When a VAO is first bound, you assign these members by calling
glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer...;
glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer...;
and so on. Which attributes are enabled and the pointers you supply are stored in the VAO.
After that when you bind the VAO again, all the those attributes and pointers also become current. So one glBindVertexArray
call is equivalent to all the code previously needed to set up all the attributes. It's handy for passing geometry around between functions or methods without having to create your own structs or objects.
(One time setup, multiple use is the easiest way to use VAOs, but you can also change attributes just by binding it and doing more enable/pointer calls. VAOs are not constants.)
More info in response to Patrick's questions:
The default for a newly created VAO is that it's empty (AFAIK). No geometry at all, not even vertexes, so if you try to draw it, you'll get an OpenGL error. This is reasonably sane, as in "initialize everything to False/NULL/zero".
You only need to glEnableClientState
when you set things up. The VAO remembers the enable/disable state for each pointer.
Yes the VAO will store glEnableVertexAttribArray
and glVertexAttrib
. The old vertex, normal, color, ... arrays are the same as attribute arrays, vertex == #0 and so on.