Sign in to follow this  
rocklobster

Integrating VAO to work with my VBO's

Recommended Posts

Hi guys,

I've just done some reading about VAO's and it looks like they can be a decent improvement on my current rendering routine. Currently all of my rendering is done using simple interleaved VBO's with indices (position, normal, uv). My model class has a vector of VBO's (each one relates to a specific sub object in my model format) and i was thinking i could just use a single VAO to describe all of them? I'm not sure however if a VAO can be used for multiple VBO's though.

Currently my drawing routine looks like (don't have code with me):

[CODE]
void drawModel(Model* model)
{
for (int i = 0; i < model->getVBOList(); i++)
{
VBO vbo = model->getVBOList()[i];

glBindBuffer(GL_ARRAY_BUFFER, vbo.id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.ibo.id)
// do vertex attrib pointers
glDrawElements(..)
}
}
[/CODE]

If i had a vertex array object bound to all of these VBO's would it be possible to simply draw them all by binding the VAO and calling drawElements again?

Thanks guys,

- rocklobster

Share this post


Link to post
Share on other sites
That's what VAO's are for, yes. They store all vertex array bindings in one object, so as long as the bindings (pointers, enabled attributes, attribute locations and such) remain the same, you only need to bind the VAO and draw.

Share this post


Link to post
Share on other sites
Great thanks!

One more question, should i still be using glDrawElements()? The parameter takes the size of my index buffer, but i'm going to be drawing multiple VBO's each with their own index buffer?

Share this post


Link to post
Share on other sites
You still have to use glDrawElements, it is not stored in the VAO. The element buffer object is a binding stored in the VAO though, so you don't have to rebind the index buffer every time.

The parameter you're talking about is not the size of the index buffer, but how many indices you want to draw. There is a distict difference between the meaning of the two, especially if the index buffer stores more indices than you want to draw in one single draw call.

Share this post


Link to post
Share on other sites
Hrm, it only seems to be drawing the first VBO. Hate to post a bit of a code dump, but someone might be able to spot an error?

First i'm creating a vertex array object and binding it. Then i iterate over the model data in each VBO and create the opengl VBO from it.

[CODE]
void loadModel(Model* model, bool withIBO)
{
unsigned int indexCount = 0;
GLuint vaoID;
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
model->setVAOHandle(vaoID);
for (int i = 0; i < (signed)model->getVBOs()->size(); i++)
{
VBO vbo = model->getVBOs()->at(i);
GLuint vboID;
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, vbo.getArray().size() * sizeof(Vertex), &vbo.getArray()[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(12));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), BUFFER_OFFSET(24));
vbo.setVBOID(vboID);
GLuint iboID;
glGenBuffers(1, &iboID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, vbo.getIBO().getArray().size() * sizeof(unsigned int), &vbo.getIBO().getArray()[0], GL_STATIC_DRAW);
vbo.setIBOID(iboID);
indexCount += vbo.getIBO().getArray().size() * sizeof(unsigned int);
}
model->setTotalIndices(indexCount);
}
...
void drawModel(Model* model)
{
glBindVertexArray(model->getVAOHandle());
glDrawElements(GL_TRIANGLES, model->getTotalIndices(), GL_UNSIGNED_INT, BUFFER_OFFSET(0));

}
[/CODE] Edited by rocklobster

Share this post


Link to post
Share on other sites
Small misunderstanding here.

[quote name='Brother Bob' timestamp='1339088949' post='4947094']
That's what VAO's are for, yes. They store all vertex array bindings in one object...
[/quote]
That is, one VAO for every object, and you have many objects. So you will need a different VAO for every iteration in the loop.

Notice also that the use of VAO is not optional, it is now mandatory.

Share this post


Link to post
Share on other sites
The index count parameter is the number of indices to draw from the currently bound index array, not the total amount from all index arrays you have ever bound. Besides, there is only one index array that can be bound anyway, so your for-loop is overwriting the binding each loop, and then you're trying to draw more than what it contains.

You need one VAO per object in your code. And by object, I mean whatever goes in one iteration of your for-loop. Likewise, you need a for-loop to loop over all VAOs.

Share this post


Link to post
Share on other sites
Ah cool, thanks for clearing that up, yeah i was sorta trying to ask that in my initial post. Thanks once again guys.

I'd assume if i had a separate buffer for each normal and uv coordinates it would then be useful because i would have an[left]glEnableVertexAttribArray call [/left]for each buffer Edited by rocklobster

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this