Render multiple objects in OpenGL

652 Views Asked by At

So I read somewhere that I should use fewer VBOs as possible and render many objects from a single VBO. I get the point, but isn't that less organized? I planned on creating 3d shapes classes where each instance of the class has its own VBO that will be bound when it needs to be drawn. This comes in contrast to what I wrote earlier.

How do 3D applications (video games etc.) get this done?

1

There are 1 best solutions below

2
On

Firstly a VBO is just a collection of vertices that is stored in your video card memory. You can use a single VBO and model matrix to transform it, you can also apply different shaders and textures on top of it.

This is best explained with a 2D game in mind where every graphic is basically a quad; 4 vertices. enter image description here

Would you really need to flood your video card memory with repeated allocations for a VBO of 4 vertices?

Instead we can have a pointer to the VBO and model matrix that transforms it. The VBO is usually stored in a resource manager so it stays in memory for as long as we like. So now we're reusing a single VBO for everything, we're applying different transformations and textures to it. It looks like there are lots and lots of VBO's but actually we're being very efficient with our video card memory.

Here's some pseudo code:

class Sprite {
    mat4 transform;
    GLuint VBO = ResourceManager.quadVBO;
}