Skip to content
systemcoding.dev
GPU

Create a VAO and VBO with glGenVertexArrays and glGenBuffers

Create a VAO and VBO with glGenVertexArrays and glGenBuffers in OpenGL 3.3 core, bind the VAO first, then fill the buffer with a triangle.

1 min read
Summarize in AI

This is how a core-profile OpenGL 3.3 program stores its first mesh. The core profile has no default vertex array object, so drawing without one fails; create the VAO first and bind it, because the buffer binding made next is recorded inside it.

triangle.cc
GLuint vao, vbo;
float verts[] = {
    -0.5f, -0.5f, 0.0f,
     0.5f, -0.5f, 0.0f,
     0.0f,  0.5f, 0.0f,
};

glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);

glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof verts, verts, GL_STATIC_DRAW);
printf("vao=%u vbo=%u\n", vao, vbo);
Text
vao=1 vbo=1

The two names are numbered per object type, so both are 1 on a fresh context; the exact values depend on the driver. glGenVertexArrays and glGenBuffers only reserve names, and the object is not created until the first bind, as the glGenBuffers reference page describes. GL_ARRAY_BUFFER itself is global state, not part of the VAO, so the attribute pointers set up afterwards are what the VAO remembers.

Nothing here draws yet: you still need glVertexAttribPointer and a shader program. Before OpenGL 3.0 there is no glGenVertexArrays at all.

Taggedcopengl
Share

More snippets

GPU

List platforms with clGetPlatformIDs

Call clGetPlatformIDs twice, once for the count and once for the handles, then print each platform name with clGetPlatformInfo in OpenCL 3.0 C.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.