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?
Each vertex array object (“VAO”) can contain multiple VBOs. Although not required, VAOs help you to organize and isolate the data in your VBOs.
All the articles on the internet on VAO's show that binding a VAO automatically binds the VBO. So, I think a VAO is a configuration that holds both glVertexAttribPointer states and the glBindBuffer state.
What you need to do is bind a vao (say vao_cube), then bind the cube vbo and everything. Once done, you bind the other vao (vao_plane), an bind ITS plane vbo. Now when drawing, you bind a vao and draw, bind the next vao and draw that. The vao stores the vbo and vertex attributes and stuff in it.
A VBO is a buffer of memory which the gpu can access. That's all it is. A VAO is an object that stores vertex bindings. This means that when you call glVertexAttribPointer and friends to describe your vertex format that format information gets stored into the currently bound VAO.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With