Integrating VAO to work with my VBO's

Started by
7 comments, last by rocklobster 11 years, 10 months ago
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):


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

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


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
Advertisement
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.
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?
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.
Ah right thanks, so i'll just get the total sum of indices from all the vbo's and use that.
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.


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

}
Small misunderstanding here.


That's what VAO's are for, yes. They store all vertex array bindings in one object...

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.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
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.
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

glEnableVertexAttribArray call

for each buffer

This topic is closed to new replies.

Advertisement