7

(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?

MintGrowth
  • 4,831
  • 9
  • 58
  • 88
  • Quite unrelated comment: How can one be positive about VBOs and not about IBOs, as they are even simpler, if you understood the former. And in practical applications you will often use `glDrawElements` instead of `glDrawArrays`. – Christian Rau Aug 08 '11 at 14:56
  • @Christian Rau: I may not have made my question clear enough, I am curious as to whether or not I have to create multiple VBO's instead of one global one. – MintGrowth Aug 08 '11 at 15:04
  • I understood (and answered) your question. It's just that IBOs aren't that different or harder than VBOs, like Ken said. – Christian Rau Aug 08 '11 at 15:08

2 Answers2

5

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

Christian Rau
  • 43,206
  • 10
  • 106
  • 177
  • Would I be able to use one VBO and IBO per Quad? I am setting up a sprite object. – MintGrowth Aug 08 '11 at 15:13
  • @Storm You can use one VBO per quad, but then you can only render one quad per draw call, which might be intended for your sprite object (which consists only of a single quad). But if you got many, quite identical, sprites (like in a particle system), this is surely a case, where you should pack all those sprites/particles into a single VBO, so you can render all of them in one go. Like I said, it depends on the concrete application and particle systems are surely one in favor of the one-VBO approach. – Christian Rau Aug 08 '11 at 15:16
  • That makes sense. Would there be a way to batch the sprite draw calls for non similar sprites? I think XNA does this with their SpriteBatch. – MintGrowth Aug 08 '11 at 15:52
  • @Storm The problem is, that you cannot change the texture (or other such render parameters) for each sprite this way. What you can do is sort your sprites by texture/material/shader/... and then batch them accordingly. Otherwise you will have to employ more advanced batching techniques, like texture atlases, texture arrays or instanced rendering. – Christian Rau Aug 08 '11 at 16:04
  • @Storm And by the way, the XNA `SpriteBatch` seems to contain only a single sprite image, but I only took a short glimpse, don't have any XNA experience. – Christian Rau Aug 08 '11 at 16:11
  • I could pack all of my sprite frames into one texture. I could then just make sure that no state changes occur in the same draw call. Is it possible to vary the tex coords from quad to quad? So that they can have diff. images, even though they all share a texture? – MintGrowth Aug 08 '11 at 17:38
  • @Storm You cannot make them vary from quad to quad, but from vertex to vertex, of course. Making a large texture atlas out of small sub-textures is a common technique to put objects into larger batches. – Christian Rau Aug 08 '11 at 19:19
2

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.

Ken Wayne VanderLinde
  • 17,085
  • 2
  • 42
  • 66