Vertex Array Object
- Tags
- opengl
Is a VBO like construct to store both VBOs and vertex-attributes in a single construct.
Note: A VAO can have share VBOs between multiple VAOs.
This greatly shortens the amount of boilerplate needed to draw something because you don't need to skeep switching VBO bindings and vertex-attrib declarations within the render loop. You can just bind a single VAO.
Creating a VAO
As with VBOs, we first need to declare a vertex array object and reference it through an id.
unsigned int VAO;
glGenVertexArrays(1, &VAO);
Then we have to bind the vertex array to let OpenGL know we're currently working with it.
glBindVertexArray(VAO);
Setup any VBOs and vertex-attributes.
// 2. copy our vertices array in a buffer for OpenGL to use
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// 3. then set our vertex attributes pointers
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
Using a VAO
We can trigger the use of our VAO by using this in the render loop.
glBindVertexArray(VAO);