Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a VBO be bound to multiple VAOs?

Tags:

c++

c

opengl

glsl

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?

like image 527
PeddleSpam Avatar asked Mar 15 '13 17:03

PeddleSpam


People also ask

Can a VAO have multiple VBO?

Each vertex array object (“VAO”) can contain multiple VBOs. Although not required, VAOs help you to organize and isolate the data in your VBOs.

Does binding VAO bind VBO?

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.

How do I use multiple VBOs?

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.

What is VBO and Vao?

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.


1 Answers

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.

like image 92
Brett Hale Avatar answered Sep 29 '22 21:09

Brett Hale