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 Array
is just an array (typically a float[]
) that contains vertex data. It doesn't need to be bound to anything. Not to be confused with a Vertex Array Object
or VAO, which I will go over laterBuffer Object
, commonly referred to as a Vertex Buffer Object
when storing vertices, or VBO for short, is what you're calling just a Buffer
.glVertexAttribPointer
works exactly like glVertexPointer
or glTexCoordPointer
work, just instead of named attributes, you get to provide a number that specifies your own attribute. You pass this value as index
. All your glVertexAttribPointer
calls get queued up for the next time you call glDrawArrays
or glDrawElements
. 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_FLOAT
is the type, GL_FALSE
means 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 single glVertexAttribPointer
call 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:
for (int i = 0; i < 5; i++) glEnableVertexAttribArray(i);
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 glVertexAttribPointer
calls, from glVertexAttribPointer(0,...);
to glVertexAttribPointer(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 Object
or VAO is used to store the state of all the glVertexAttribPointer
calls and the VBOs that were targeted when each of the glVertexAttribPointer
calls were made.
You generate one with a call to glGenVertexArrays
. To store everything you need in a VAO, bind it with glBindVertexArray
, then do a full draw call. All the draw bind calls get intercepted and stored by the VAO. You can unbind the VAO with glBindVertexArray(0);
Now when you want to draw the object, you don't need to re-call all the VBO binds or the glVertexAttribPointer
calls, you just need to bind the VAO with glBindVertexArray
then call glDrawArrays
or glDrawElements
and 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.
The terminology and sequence of APIs to be called is quite confusing indeed. What's even more confusing is how the various aspects - buffer, generic vertex attribute and shader attribute variable get associated. See OpenGL-Terminology for a pretty good explanation.
Further, the link OpenGL-VBO,shader,VAO shows a simple example with the necessary API calls. It's particularly good for those transitioning from immediate mode to the programmable pipeline.
Hope it helps.
Edit: As you can see from the comments below, people can make assumptions and jump to conclusions. The reality is that it's quite confusing for beginners.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With