Is it possible to associate a VBO with multiple VAOs?

I'm trying to display a UV map model by treating its texture coordinates as an array of vertex positions. I installed VAO for a model that looks great, and then tried to add a second VAO and bind a texture coordinate buffer to it. Unfortunately, this does nothing.

I wrote a second set of vertex and fragment shaders for the UV map, which compiled just fine. The buffer is connected in the same way as with a set of VAO models and a set of vertices. The only difference I see is not re-specifying the buffer data.

This is my code for setting up the VAO model:

// 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 installed the VAO UV map as follows:

 // 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 ); 

Can I use the same VBO with more than one VAO?

+4
source share
1 answer

Yes VAOs simply store references to VBOs with related data for format, offset, etc., As indicated by glVertexAttribPointer . The VBOs index has slightly different semantics.

+6
source

All Articles