Just want to make sure I understand this correctly (I’d ask on SO Chat, but it’s dead in there!):
We’ve got a Vertex Array, which we make “current” by binding it
then we’ve got a Buffer, which we bind to a Target
then we fill that Target via glBufferData
which essentially populates whatever was bound to that target, i.e. our Buffer
and then we call glVertexAttribPointer which describes how the data is laid out — the data being whatever is bound to GL_ARRAY_BUFFER
and this descriptor is saved to our original Vertex Array
(1) Is my understanding correct?
The documentation is a little sparse about how everything correlates.
(2) Is there some kind of default Vertex Array? Because I forgot/omitted glGenVertexArrays and glBindVertexArray and my program worked fine without it.
Edit: I missed a step… glEnableVertexAttribArray.
(3) Is the Vertex Attrib tied to the Vertex Array at the time glVertexAttribPointer is called, and then we can enable/disable that attrib via glEnableVertexAttribArray at any time, regardless of which Vertex Array is currently bound?
Or (3b) Is the Vertex Attrib tied to the Vertex Array at the time glEnableVertexAttribArray is called, and thus we can add the same Vertex Attrib to multiple Vertex Arrays by calling glEnableVertexAttribArray at different times, when different Vertex Arrays are bound?
Some of the terminology is a bit off:
Vertex Arrayis just an array (typically afloat[]) that contains vertex data. It doesn’t need to be bound to anything. Not to be confused with aVertex Array Objector VAO, which I will go over laterBuffer Object, commonly referred to as aVertex Buffer Objectwhen storing vertices, or VBO for short, is what you’re calling just aBuffer.glVertexAttribPointerworks exactly likeglVertexPointerorglTexCoordPointerwork, just instead of named attributes, you get to provide a number that specifies your own attribute. You pass this value asindex. All yourglVertexAttribPointercalls get queued up for the next time you callglDrawArraysorglDrawElements. If you have a VAO bound, the VAO will store the settings for all your attributes.The main issue here is that you’re confusing vertex attributes with VAOs. Vertex attributes are just the new way of defining vertices, texcoords, normals, etc. for drawing. VAOs store state. I’m first going to explain how drawing works with vertex attributes, then explain how you can cut down the number of method calls with VAOs:
glEnableVertexAttribArray(0);.glBindBuffer(GL_ARRAY_BUFFER, myBuffer);.glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);. In order of parameter: 0 is the attribute you’re defining, 3 is the size of each vertex,GL_FLOATis the type,GL_FALSEmeans to not normalize each vertex, the last 2 zeros mean that there’s no stride or offset on the vertices.glDrawArrays(GL_TRIANGLES, 0, 6);glDisableVertexAttribArray(0);Wrap that in
glUseProgram()calls and you have a rendering system that works with shaders properly. But let’s say you have 5 different attributes, vertices, texcoords, normals, color, and lightmap coordinates. First of all, you would be making a singleglVertexAttribPointercall for each of these attributes, and you’d have to enable all the attributes beforehand. Let’s say you define the attributes 0-4 as I have them listed. You would enable all of them like so:And then you would have to bind different VBOs for each attribute (unless you store them all in one VBO and use offsets/stride), then you need to make 5 different
glVertexAttribPointercalls, fromglVertexAttribPointer(0,...);toglVertexAttribPointer(4,...);for vertices to lightmap coordinates respectively.Hopefully that system alone makes sense. Now I’m going to move on to VAOs to explain how to use them to cut down on the number of method calls when doing this type of rendering. Note that using a VAO is not necessary.
A
Vertex Array Objector VAO is used to store the state of all theglVertexAttribPointercalls and the VBOs that were targeted when each of theglVertexAttribPointercalls were made.You generate one with a call to
glGenVertexArrays. To store everything you need in a VAO, bind it withglBindVertexArray, then do a full draw call. All thedrawbind calls get intercepted and stored by the VAO. You can unbind the VAO withglBindVertexArray(0);Now when you want to draw the object, you don’t need to re-call all the VBO binds or the
glVertexAttribPointercalls, you just need to bind the VAO withglBindVertexArraythen callglDrawArraysorglDrawElementsand you’ll be drawing the exact same thing as though you were making all those method calls. You probably want to unbind the VAO afterwards too.Once you unbind the VAO, all the state returns to how it was before you bound the VAO. I’m not sure if any changes you make while the VAO is bound is kept, but that can easily be figured out with a test program. I guess you can think of
glBindVertexArray(0);as binding to the “default” VAO…Update: Someone brought to my attention the need for the actual draw call. As it turns out, you don’t actually need to do a FULL draw call when setting up the VAO, just all the binding stuff. Don’t know why I thought it was necessary earlier, but it’s fixed now.