Jump to content

  • Log In with Google      Sign In   
  • Create Account

Integrating VAO to work with my VBO's


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 07 June 2012 - 10:42 AM

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()[i];
	   
	    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

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8202

Like
2Likes
Like

Posted 07 June 2012 - 11:09 AM

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.

#3 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 07 June 2012 - 11:30 AM

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?

#4 Brother Bob   Moderators   -  Reputation: 8202

Like
2Likes
Like

Posted 07 June 2012 - 11:41 AM

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.

#5 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 07 June 2012 - 11:43 AM

Ah right thanks, so i'll just get the total sum of indices from all the vbo's and use that.

#6 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 07 June 2012 - 12:03 PM

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

}

Edited by rocklobster, 07 June 2012 - 12:04 PM.


#7 larspensjo   Members   -  Reputation: 1540

Like
1Likes
Like

Posted 07 June 2012 - 12:16 PM

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

#8 Brother Bob   Moderators   -  Reputation: 8202

Like
2Likes
Like

Posted 07 June 2012 - 12:17 PM

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.

#9 rocklobster   Members   -  Reputation: 415

Like
0Likes
Like

Posted 07 June 2012 - 12:23 PM

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

Edited by rocklobster, 07 June 2012 - 12:25 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS