Can a VBO be bound to multiple VAOs?

c, c++, glsl, opengl

Solution

Yes. VAOs just store references to VBOs, with the associated data for format, offsets, etc., as specified by glVertexAttribPointer. Index VBOs have slightly different semantics.

Problem

I'm trying to render a model's UV map by treating its texture coordinates as an array of vertex positions. I set up a VAO for the model which renders perfectly, then tried adding a second VAO and binding the texture coordinate buffer to it. Unfortunately it doesn't render anything. I've written a second set of vertex and fragment shaders for the UV map which compile just fine. The buffer is bound in the same way as with the model VAO and the vertex attributes set. The only difference I can see is I'm not re-specifying the buffer data. This is my code for setting up the model VAO: ``` // Create model VAO glGenVertexArrays( 1, &modelVAO ); glBindVertexArray( modelVAO ); // Create position buffer glGenBuffers( 1, &positionBuffer ); glBindBuffer( GL_ARRAY_BUFFER, positionBuffer ); glBufferData( GL_ARRAY_BUFFER, sizeof( GLfloat ) * vertexCount * 4, positions, GL_STATIC_DRAW ); glVertexAttribPointer( 0, 4, GL_FLOAT, GL_FALSE, 0, 0 ); glEnableVertexAttribArray( 0 ); // Create normal buffer glGenBuffers( 1, &normalBuffer ); glBindBuffer( GL_ARRAY_BUFFER, normalBuffer ); glBufferData( GL_ARRAY_BUFFER, sizeof( GLfloat ) * vertexCount * 3, normals, GL_STATIC_DRAW ); glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, 0 ); glEnableVertexAttribArray( 1 ); // Create texture coordinate buffer glGenBuffers( 1, &textureCoordinateBuffer ); glBindBuffer( GL_ARRAY_BUFFER, textureCoordinateBuffer ); glBufferData( GL_ARRAY_BUFFER, sizeof( GLfloat ) * vertexCount * 2, textureCoordinates, GL_DYNAMIC_DRAW ); glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE, 0, 0 ); glEnableVertexAttribArray( 2 ); // Unbind model VAO glBindVertexArray( 0 ); ``` Then I set up the UV map VAO like this: ``` // Create new UV map VAO glGenVertexArrays( 1, &uvMapVAO ); glBindVertexArray( uvMapVAO ); // Bind texture coordinate buffer glBindBuffer( GL_ARRAY_BUFFER, textureCoordinateBuffer ); glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 0, 0 ); glEnableVertexAttribArray( 0 ); // Unbind UV map VAO glBindVertexArray( 0 ); ``` Is it possible to use the same VBO with more than one VAO like this?

Original source