Managing Multiple Objects In A VBO

Started by
2 comments, last by Sharlin 17 years, 1 month ago
I need some suggestions on the best way to manage my scene geometry using VBOs. The objects in my scene are somewhat simple... cones, spheres, ellipsoids, etc. I want to store one copy of each type of object in a VBO and later use glDrawElements/glDrawRangeElements to render the type of object I desire at the desired location. For example if I had 1 cone and 1 sphere I would do something like this.... //Create Data VBO GLuint id = 0; GLsizeiptr dataSize = coneVerticesSize + coneNormalsSize + shereVerticeSize + sphereNormalsSize; glGenBuffers(1, &id); glBindBuffer(GL_ARRAY_BUFFER, id); glBufferData(GL_ARRAY_BUFFER, dataSize, 0, GL_STREAM_DRAW_ARB); // Add cone glBufferSubData(GL_ARRAY_BUFFER, coneVerticesOffset, coneVerticesSize, coneVertices); glBufferSubData(GL_ARRAY_BUFFER, coneNormalsOffset, coneNormalsSize, coneNormals); // Add sphere glBufferSubData(GL_ARRAY_BUFFER, sphereVerticesOffset, coneVerticesSize, coneVertices); glBufferSubData(GL_ARRAY_BUFFER, sphereNormalsOffset, sphereNormalsSize, sphereNormals); glGenBuffers(1, &id2); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id2); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize, 0, GL_STREAM_DRAW_ARB); glBufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, coneIndicesOffset, coneIndicesSize, coneIndices); glBufferSubDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sphereIndicesOffset, sphereIndicesSize, sphereIndices); Then to render the cone... glBindBufferARB(GL_ARRAY_BUFFER_ARB, id); glNormalPointer( GL_FLOAT, 0, (void*)(coneNormalOffset)); glVertexPointer( 3, GL_FLOAT, 0, (void*)(coneVertexOffset)); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, id2); glIndexPointer(GL_INT, 0, (void coneIndicesOffset)); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); glDrawElements(GL_TRIANGLE_STRIP, 6, GL_UNSIGNED_SHORT, (GLushort*)0+0); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); Then similarly for the sphere.... Is this a common method? It seems very tedious to manage all the offsets, sizes etc. required to do this. Does anyone have any suggestions on how to streamline this? Any online articles available on this type of topic? One of the most, or more, important things I need to be able to do is add and delete objects during run time. BTW, other forums I have posted to have the feature to embed code, by placing it between
... Is that not possible here? Thanks Ed
Advertisement
The faq describes how to embed code.

Personally I think trying to fit multiple objects into a VBO is more hassle than it's worth.

For something like more complex primitives such as cones and spheres, just use a VBO for each one. If you're concerned about potential performance implications of repeatedly binding and unbinding VBOs, you could always batch them up in a display list.

[Edited by - gharen2 on March 7, 2007 3:52:16 AM]
Hi CelticGamer,

First of all glIndexPointer isn't used for triangle indices. If the code you posted works, then i suspect it's because you don't enable the index array through glEnableClientState(). You don't have to call this. For indices, just bind an ELEMENT_ARRAY_BUFFER VBO and call glDrawElements/glDrawRangeElements with an offset inside the buffer, instead of the actual data pointer (as you already doing).

AFAIK glBindBuffer() isn't a slow operation. What is slow though, is the pointer re-binding (glXXXPointer()) functions. The problem is that each time you bind a new buffer, you have to set the pointers again (even if they are the same as the previous buffer). I think so at least.
So if you don't want to alter object indices so they can fit into the same glXXXPointer group, i think there is no real performance gain from packing multiple objects into a single buffer. And because (as you said) you want to create/destroy objects at runtime, i'd suggest you create one buffer per object as gharen2 said.

Just a silly question. Can two spheres be different (from the vertices/indices point of view)? If most of your spheres/cones/ellipsoids differ only in their transformation, then you shouldn't create a VBO for each one of them. You can simply use the same data with different transformations. But you already know that. I'm just checking :)

Hope that helps.

HellRaiZer

PS. Forgive my English.
HellRaiZer
How objects are mapped into VBOs (if at all) is a low-level implementation detail of your renderer, and shouldn't be too much of a hassle to implement if you properly abstract it away. On the level of abstract "shapes" such as spheres and cones, you shouldn't need to worry about how they are stored and how they are rendered.

This topic is closed to new replies.

Advertisement