Difference between revisions of "Vertex Array Object"

From OpenGL Wiki
Jump to navigation Jump to search
(New page: You might also want to read glVertexAttribPointer which is at http://www.opengl.org/wiki/GlVertexAttribPointer<br> <br> GL 3.0 introduces VAO (Vertex Array Object). It also introduces Gene...)
 
(→‎The Downside: Removing nonsense.)
Line 35: Line 35:
 
Could you use glVertexPointer and the others with VAO? Probably. If you make a GL 3.0 forward compatible context, you can't use glVertexPointer and the others.<br>
 
Could you use glVertexPointer and the others with VAO? Probably. If you make a GL 3.0 forward compatible context, you can't use glVertexPointer and the others.<br>
  
== The Downside ==
+
 
You would have to make one VAO for every object you will be drawing unless if the VBO offsets are identical for some of the objects. In order for the VBO offsets to be identical, you would have to make a large VBO and put lots of objects in it. This is something you should already be doing to avoid redundent glBindBuffer calls.<br>
 
  
 
== Delete ==
 
== Delete ==
 
Don't forget to deallocate on shutdown. If you don't deallocate, the driver will automatically deallocate for you.
 
Don't forget to deallocate on shutdown. If you don't deallocate, the driver will automatically deallocate for you.
 
   glDeleteVertexArrays(1, &VAOID);
 
   glDeleteVertexArrays(1, &VAOID);

Revision as of 08:11, 15 August 2009

You might also want to read glVertexAttribPointer which is at http://www.opengl.org/wiki/GlVertexAttribPointer

GL 3.0 introduces VAO (Vertex Array Object). It also introduces Generic Vertex Attributes (glVertexAttribPointer).
GL 1.5 introduced VBO (Vertex Buffer Object) which you can read about http://www.opengl.org/wiki/General_OpenGL
They have created GL_ARB_vertex_array_object so that GL 2.0 implementations can also have support for VAO (if the IHV driver programmers choose to offer it)

Introduction

One of the problems of OpenGL is validation. Whenever you make a GL call, the driver has to validate all sorts of things to make sure the GL state is ok and that actually executing the command won't hang the system. VAOs help in this area.
Instead of making a lot of glVertexAttribPointer calls, you just call glBindVertexArray.

This is how you prepare a VAO. According to the spec, a VBO must be created and bound before calling glBindVertexArray and glVertexAttribPointer otherwise GL_INVALID_OPERATION will be generated

 glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);   //A previously created VBO
 uint VAOID;
 glGenVertexArrays(1, &VAOID);
 glBindVertexArray(VAOID);
 //Now we must define the VAO format
 //Vertices, XYZ, FLOAT. We give GL_FALSE since we don't want normalization
 glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);
 //Normals, XYZ, FLOAT.
 glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);
 //TexCoord0, XY (Also called ST. Also called UV), FLOAT. 
 glVertexAttribPointer(texCoord0Loc, 2, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);

The XXX are offsets into the VBO.
Now the VAO is ready. To use it.

 glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);   //Necessary?
 glBindVertexArray(VAOID);
 glDrawRangeElements(...);  //or glDrawElements, or glDrawArrays

The spec doesn't make clear if calling glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID) is necessary for drawing.

Calling glBindVertexArray(0) unbinds the VAO.
Could you use glVertexPointer and the others with VAO? Probably. If you make a GL 3.0 forward compatible context, you can't use glVertexPointer and the others.


Delete

Don't forget to deallocate on shutdown. If you don't deallocate, the driver will automatically deallocate for you.

 glDeleteVertexArrays(1, &VAOID);