Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertex Buffer Objects in OpenGL 2.1

Tags:

c

opengl

vbo

(I specified 2.1 because my laptop won't go past that version. I would have probably done this anyway since 3.x and on introduces shaders as mandatory?).

Thanks to Wikipedia: http://en.wikipedia.org/wiki/Vertex_Buffer_Object I am starting to grasp how simple it can be to use VBO's (I am still not positive about IBO's?). What I have understood so far is the primary reason to use them is a performance boost gained due to the fact that data is now stored in video memory.

What I would like to know is how I am supposed to use them in a practical context. For instance, all of what I have seen has been setting up one Vertex Buffer Object and drawing one triangle or one cube, etc. What if I want to draw 2 or more? Do I set up a new VBO for each entity that I want to draw? Or do I magically append to some static VBO that I setup early on?

like image 217
LunchMarble Avatar asked Aug 08 '11 14:08

LunchMarble


2 Answers

It depends, as the question is quite broad.

Using one VBO for the vertex attributes and one for the indices for each model/object/entity/mesh is the straight forward approach. You may benefit from storing all models in a single VBO, so you don't have to bind buffers too often, but this brings in other problems when those models are not that static. Also, you may use multiple VBOs per object, maybe one for dynamic data and one for static data, or one for geometry data (position...) and one for material related data (texCoords...).

Using one VBO per object may have it's (dis)advantages, as well as using a single huge VBO may not be that good an idea. It just depends on the concrete application and how well those concepts fit into it. But for the beginning, the straight-forward one-VBO-per-object approach is not that bad an idea. Just don't use one VBO per triangle ;)

like image 120
Christian Rau Avatar answered Oct 15 '22 03:10

Christian Rau


It should be really easy - just add more data to the VBO.

So, for example, if you wanted to render 3 triangles instead of just 1, make sure that you send 9 vertices to glBufferData. Then, to render all of them, use the call glDrawArrays(GL_TRIANGLES, 0, 9); It really should be that simple.

However, a point about IBOs is that they're really not much different from VBOs at all, and I would really get used to using them. That is really the practical way of using VBOs. Since multiple triangles are usually meant to cover the same surface (e.g. a cube needs twelve triangles), using only VBOs causes a lot of repetition in specifying vertex data. By using index buffer as well, things are even more efficient, since any repetition is removed from the VBO. I found this which provides a concise example of moving from VBO-only to VBO/IBO and also gives a good explanation of the change.

like image 2
Ken Wayne VanderLinde Avatar answered Oct 15 '22 05:10

Ken Wayne VanderLinde