1

I am creating a 2d game engine as a side project. I've been doing some experimenting and research and have come across a lot of people suggesting that you batch (store multiple objects to draw) in the same VBO. For example, if I my scene had a lot of trees I could put all the trees in the same VBO because they have the same memory footprint and then use a single glDrawArrays to draw all of them.

This is fine and it makes total sense... but then I started to wonder how I can send the different transforms for each tree? How do I get that to the shader? Or is this approach assuming I do the calculations on CPU and send the entire VBO each draw?

Here are two of the main questions i've been looking at:

OpenGL VAO best practices

OpenGL How Many VAOs

Community
  • 1
  • 1
TomShar
  • 129
  • 1
  • 2
  • 12

1 Answers1

1

The term you're looking for is Instancing.

You create a VAO that contains the model of the tree, and then instead of passing the model matrix through the uniforms, you put it as an instanced vertex attribute in the VAO. Then each instance of your tree will get drawn with a different model matrix.

You can utilize compute shaders or transform feedback to update and store the model-view product in the VAO once per instance per frame (rather than calculating it per each vertex of each instance).

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • Ah, I might have been slightly unclear in my question. I was aware of instancing but the models for each tree might be slightly different, but use the same texture... perhaps cardboard boxes might be a better example. If you look at the response to the second link I gave his answer to the first question is what im talking about. – TomShar Dec 12 '16 at 21:21
  • It's unclear then what you're asking. He is talking about static geometry, whether you ask about animation. As per cardboards it sounds that they could be actually drawn with instancing..? – Yakov Galka Dec 13 '16 at 08:35
  • I understood what he was saying as having multiple objects one after another in a single VBO and use glDrawMulti*() to draw all (say 5) objects which are different shapes but use the same texture. What i'm asking is how you give them different transforms as you would just be drawing all of the items in the VBO in the same position which is kinda useless? Instancing like you suggested only works if you're trying to render the same thing multiple times. I'm asking about rendering different items using one glDraw call because all the objects are in a single VBO. – TomShar Dec 13 '16 at 17:25
  • @TomShar: if you pack multiple objects into the same `glDraw` call then their vertex coordinates are to be already transformed. – Yakov Galka Dec 13 '16 at 17:28