# Sharing VBO with multiple objects and fixed size buffer data

I'm just messing around with OpenGL and getting some basic structures in place and my first attempt resulted in each SceneObject class (just contains vertex information right now) having it's own VBO inside it, however I've read that it might be better to share VBOs across multiple objects.

Also, I read that you should avoid resizing a VBO (repeated calls to glBufferData with different size parameters), and instead choose a fixed size for a VBO, and just try a range from the buffer. I don't think changing the size of the buffer data would happen too often, but surely it would be better to only allocate the data you need? Choosing an arbitrary value seems risky.

I'm looking for some advice on working with individual objects in a scene and their associated buffer data.

Don't create a new VBO for each SceneObject. Instead create a new VBO for each mesh and have multiple SceneObjects reference the same VBO if they are using the same mesh. If you are using shaders you will never be modifying your data within your VBOs since you will be instead sending a set of matricies to the shader which will transform the 'static' verticies.

Your VBOs should almost always be considered static with the exception being streamed geometry such as terrain.

• So if I have 2 cubes, would each side of a cube be a mesh, and those meshes be shared amongst both cubes? Then when drawing via the shader, use a matrix to transform from the position of cube1 to cube2? Sep 3 '12 at 20:51
• @MarkIngram you would have 1 'cube' mesh and you would have 2 transform matricies. It would basically be like SetVBO(Cube); SetMatrix("transform", Cube1Transform); Render(); SetMatrix("transform", Cube2Transform); Render(); Sep 3 '12 at 20:59
• Ah OK, so each object would contain a reference to a potentially shared mesh object. Thanks. Sep 3 '12 at 21:12
• And what about objects that does change vertices, for example, an arc that is rendered/generated each frame with differents values for startingAngle and sweepAngle? Mar 31 '13 at 16:39
• @JCM If you can perform all of the calculations for the arc on the GPU in shaders then you can still use a shared static VBO. If not then yeah you will need a separate VBO per object as well as making it dynamic so you can modify the vertices. You will see a significant performance loss if you do this with a large amount of objects or are updating a large amount of vertices every frame. Apr 1 '13 at 15:29

Sharing a VBO applies for very particular cases, the usual way to go is to have one VBO per mesh. If you have many instances of the same mesh, look at instanced rendering.

It should be noted that glBufferData() allocates memory (on the GPU for almost every modern hardware), which is why it shouldn't be called too often.