liveBook · Manning

Vertex array object

As explained in Systems Programming with Zig

A vertex array object is an OpenGL object that stores vertex attribute configuration and associated buffer bindings.

A vertex array object (VAO) keeps vertex-attribute configuration and buffer bindings together. OpenGL uses this state to manage different attribute layouts for shader programs. section 7.4.4

Create a VAO with glGenVertexArrays and delete it with glDeleteVertexArrays. To configure a triangle, bind its vertex buffer object (VBO) and the VAO, describe attribute 0 as two floats with glVertexAttribPointer, then enable the attribute. Bindings are implicit state, so keep them brief and reset them to 0 to avoid invisible side effects. section 7.4.4 section 7.4.5

Listing 7.12 An abstract object
var vao: c_uint = undefined;
c.glGenVertexArrays(1, &vao); #1
defer c.glDeleteVertexArrays(1, &vao); #2

The minimal Zig snippet creates a VAO and schedules its deletion. section 7.4.4

Listing 7.14 Explaining our vertices to the VAO
{ #1
    c.glBindBuffer(c.GL_ARRAY_BUFFER, triangle_vbo); #2
    defer c.glBindBuffer(c.GL_ARRAY_BUFFER, 0); #2

    c.glBindVertexArray(vao); #3
    defer c.glBindVertexArray(0); #3

    c.glVertexAttribPointer(
        0, #4
        2, #5
        c.GL_FLOAT, #6
        c.GL_FALSE, #7
        2 * @sizeOf(f32), #8
        @ptrFromInt(0), #9
    );
    c.glEnableVertexAttribArray(0); #10
}

The triangle setup binds a VBO and VAO, defines an attribute, and enables it. section 7.4.5

How Systems Programming with Zig covers it

How deep it goes
Defines VAO state and demonstrates creation, binding, attribute-pointer setup, and enabling an attribute.
Examples
Configures a triangle with attribute 0 set to two floating-point values per vertex.
What it assumes
Assumes a triangle, a shader, and vertex data in a vertex buffer object.
What you can do after
Apply the OpenGL API to create a VAO and configure a triangle’s vertex attribute from a VBO.
How it connects
Connects vertex array objects to vertex attributes, vertex buffer objects, and vertex shaders.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.