Graphics / Reference / Vertex data, VAO and VBO

Vertex data, VAO and VBO

Vertex buffers store uploaded vertex bytes, while a VAO records how those bytes are exposed as vertex attributes. Drawing reuses this prepared state rather than uploading geometry again.

Example CPU-side vertex data in pixel coordinates:

float triangleVerts[] = {100,100, 300,100, 200,300}; float lineVerts[] = {50,50, 350,350};

Triangle VAO/VBO setup:

glGenVertexArrays(1,&vaoTri); glGenBuffers(1,&vboTri); glBindVertexArray(vaoTri); glBindBuffer(GL_ARRAY_BUFFER,vboTri); glBufferData(GL_ARRAY_BUFFER,sizeof(triangleVerts),triangleVerts,GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,2*sizeof(float),(void*)0); glBindVertexArray(0);

glBufferData uploads the triangle array into the currently bound array buffer. glEnableVertexAttribArray(0) enables vertex attribute location 0.

glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,2*sizeof(float),(void*)0) declares attribute 0 as two floating-point components per vertex, with successive vertices separated by two floats and with offset 0.

The line endpoints receive equivalent VAO/VBO setup using lineVerts.

By draw time, the vertex data has already been uploaded. The application binds the VAO corresponding to the geometry it wants to draw, and the recorded attribute state tells the vertex stage how to interpret the associated buffer data.

The associated pipeline diagram places the vertex arrays at the Vertex Data stage before the uploaded-buffer stage.

Triangle and line coordinate arrays are attached to the Vertex Data stage, with an arrow leading from those CPU-side values into the VBO and later pipeline stages.
The CPU-side coordinate arrays are the pipeline's input data; they must be uploaded before later stages can consume them.

Builds on

Read