Difference between revisions of "Vertex Array Object"

From OpenGL Wiki
Jump to navigation Jump to search
m (Fixing link)
(Fixing the formatting of the code blocks.)
Line 1: Line 1:
You might also want to read glVertexAttribPointer which is at [[GlVertexAttribPointer]]
+
You might also want to read glVertexAttribPointer which is at [[GLVertexAttribPointer]]
  
 
GL 3.0 introduces VAO (Vertex Array Object).
 
GL 3.0 introduces VAO (Vertex Array Object).
Line 13: Line 13:
  
 
<code>
 
<code>
uint VAOID;
+
  uint VAOID;
glGenVertexArrays(1, &VAOID);
+
  glGenVertexArrays(1, &VAOID);
glBindVertexArray(VAOID);
+
  glBindVertexArray(VAOID);
//Now we must define the VAO format
+
  //Now we must define the VAO format
//Vertices, XYZ, FLOAT. We give GL_FALSE since we don't want normalization
+
  //Vertices, XYZ, FLOAT. We give GL_FALSE since we don't want normalization
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);  //A previously created VBO
+
  glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);  //A previously created VBO
glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);
+
  glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);
//Normals, XYZ, FLOAT.
+
  //Normals, XYZ, FLOAT.
glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);
+
  glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);
//TexCoord0, XY (Also called ST. Also called UV), FLOAT.  
+
  //TexCoord0, XY (Also called ST. Also called UV), FLOAT.  
glVertexAttribPointer(texCoord0Loc, 2, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);
+
  glVertexAttribPointer(texCoord0Loc, 2, GL_FLOAT, GL_FALSE, sizeof(MyVertex), XXX);
 
</code>
 
</code>
  
Line 31: Line 31:
  
 
<code>
 
<code>
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);  //Necessary?
+
  glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);  //Necessary?
glBindVertexArray(VAOID);
+
  glBindVertexArray(VAOID);
glDrawRangeElements(...);  //or glDrawElements, or glDrawArrays
+
  glDrawRangeElements(...);  //or glDrawElements, or glDrawArrays
 
</code>
 
</code>
  

Revision as of 21:07, 17 August 2009

You might also want to read glVertexAttribPointer which is at GLVertexAttribPointer

GL 3.0 introduces VAO (Vertex Array Object).

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.

 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
 glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);   //A previously created VBO
 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