# How do I structure code with multiple VBOs?

As per my previous question, it seems it would be desirable to use more than a single VBO in my OpenGL program. But I've hit a wall when trying to do this. Currently my main function looks something like this in pseudocode

setup "main" VBO and VAO
load shaders
setup shader attributes

while true
update geometry into "main" VBO
glBufferData(... "main" VBO)
glClear(GL_COLOR_BUFFER_BIT)
glDrawArrays(...)


Now what I thought I could do is effectively move the setup "main" VBO inside the game loop itself

setup VAO
load shaders
setup shader attributes

while true
setup "main" VBO
update geometry into "main" VBO
glBufferData(... "main" VBO)
glClear(GL_COLOR_BUFFER_BIT)
glDrawArrays(...)


which would then allow me to split the loop into multiple chunks, each creating its own VBO and drawing. But this doens't seem to work, and I get a blank white screen. I can only guess this is because the shaders are not only bound to the VAO, but also to the currently active VBO? I've tried glUseProgram() after the "main" VBO setup, but it doesn't seem to help.

I've found it quite difficult to find information regarding which phase exactly depends on which, and in what order things need to be executed.

• Shaders are not bound to the VBO. – user253751 Mar 16 '16 at 8:54
• Are you making a new VBO on every step? Are you calling glBindBuffer on every step? Your pseudocode seems OK, so we'd have to see more realistic code to diagnose. I would recommend you check out the OpenGL Superbible ("blue book") – Nox Mar 16 '16 at 12:36

## 1 Answer

glBufferData reallocates the VBO in VRAM, use glBufferSubData when updating data.

Generally each mesh has its own VBO(s); and own VAO if not batched. There are optimizations where you use a single VBO for all meshes with the same attribs, etc, but don't worry about that.

The relation of these things is

1. VBO created and loaded with data
2. VAO is bound
3. Vertex attribs are set, vertexAttribPointer points into the current ARRAY_BUFFER, attribs are part of the state of the VAO
4. Program gets vertex attribs from the current VAO (which may be the default VAO 0 if you never bound one)
• A difference between BufferData and BufferSubData is that BufferData may reshape the buffer, while BufferSubData is stuck with the initial configuration. This matters even more if you use BufferStorage to make buffers of immutable shape, upon which it's an error to BufferData even if you specify identical sizes. – Lars Viklund Mar 22 '16 at 22:04