On all the tutorials I can find about VAO's (Vertex Array Objects), they show on how to use them by configuring vertex attributes and binding a VBO (Vertex Buffer Object). But I want to create a VAO that will be configured for a set of VBO's in combination with a fixed shader, where each buffer uses the same data pattern (vertex, uv, color, etc). So, I want to create one VAO for multiple VBO's that will be drawn using one shader.
I couldn't find any demo on this, so I decided to just give it a try. But it doesn't work and crashes on the glDrawArray
call. It looks like the VBO isn't bound. Here is the code I'm using:
Rendering:
/* Prepare vertex attributes */
glBindVertexArrayOES(vao);
/* Upload the buffer to the GPU */
glBindBuffer(GL_ARRAY_BUFFER, pool->next());
glBufferSubData(GL_ARRAY_BUFFER, 0, parts * buffer.stride() * 6, buffer.getBuffer());
/* Draw the triangles */
glDrawArrays(GL_TRIANGLES, 0, parts * 6);
glBindVertexArrayOES(0);
VAO Creation:
glBindVertexArrayOES(vao);
glEnableVertexAttribArray(ls.position);
glVertexAttribPointer(ls.position, 2, GL_FLOAT, GL_FALSE, buffer.stride(), 0);
glEnableVertexAttribArray(ls.color);
glVertexAttribPointer(ls.color, 3, GL_FLOAT, GL_FALSE, buffer.stride(), GL_BUFFER_OFFSET(buffer.dataTypeSize * 2));
glBindVertexArrayOES(0);
Where ls
is a simple struct
that holds the attribute locations.
In the Rendering part, swapping the glBindBuffer
and the glBindVertexArrayOES
around didn't work either.
So, the question is: Is it even possible to do so, or will I have to create for each buffer a VAO? And if I have to create a VAO for each VBO, is it possible to update the VBO's data using glBufferSubData
in combination with a VAO?