Sign in to follow this  

Managing Multiple Objects In A VBO

This topic is 3934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this