How OpenGL reads vertex data

A triangle can start as nine numbers in a C++ array. OpenGL needs both those numbers and a description of how to read them. A vertex buffer object (VBO) stores the bytes; a vertex array object (VAO) stores the vertex input configuration.

This example comes from Lesson 1.6: Vertex Buffers + Rendering. You can explore the complete lesson without creating an account.

First, the three positions

Each vertex has an x, y and z coordinate. These three positions describe a triangle:

std::vector<float> triangleVertices = {
     0.0f,  0.5f, 0.0f,
    -0.5f, -0.5f, 0.0f,
     0.5f, -0.5f, 0.0f
};

In memory, this is one sequence of nine floats. The array does not carry a separate instruction saying that every three values form one position.

The VBO holds the bytes

After creating a buffer name with glGenBuffers, the program binds that buffer to GL_ARRAY_BUFFER. The call to glBufferData copies the array's bytes into the buffer's data store. At this point, the data has been uploaded, but the vertex input still needs a layout.

The VAO remembers the layout

With a VAO and the VBO bound, glVertexAttribPointer specifies how attribute zero reads from that buffer. The buffer association and the attribute settings become part of the VAO's state.

glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

glVertexAttribPointer(
    0, 3, GL_FLOAT, GL_FALSE,
    3 * sizeof(float), (void*)0
);
glEnableVertexAttribArray(0);

Here, attribute zero reads three GL_FLOAT components. The matching vertex shader input is declared with layout (location = 0). The location connects the attribute configuration to that input.

Stride and offset answer different questions

Stride is the number of bytes from one position to the next. In this example, three 32-bit floats occupy twelve bytes. 3 * sizeof(float) expresses that spacing in the code.

Offset is where this attribute begins in the buffer. The first position starts at byte zero, so the last argument is (void*)0.

Nine floats grouped into Vertex 0, Vertex 1 and Vertex 2. Each vertex has x, y and z components. The stride between vertices is 12 bytes.
Three positions in one buffer. Every group of three floats is another vertex. Open the diagram for a larger view.

When a vertex contains more attributes, such as both position and color, the stride describes the spacing between complete vertices, while each attribute's offset identifies its starting position within that layout.

From the layout to the draw call

The rendering loop uses the shader program, binds the configured VAO, and calls glDrawArrays with GL_TRIANGLES and a count of three. OpenGL reads the enabled attribute through the stored layout. The full lesson shows the setup, the draw call, cleanup and checks for understanding.

Follow the code in the interactive lesson

See the project change alongside the explanation, inspect the files, and revisit the diagram. The browser player is read-only; download the project to edit and run it in your own IDE.

The complete lesson is open without signing up.

By Alex Kulbitsky. Technical reference: Khronos OpenGL reference for glVertexAttribPointer.