Sign in to follow this  

Many static objects in a vertex buffer. How to remove objects from the VB?

This topic is 4819 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 have a lot of small objects in my game that are static geometry (they have no transformation but the initial one), so I want to put all of them into a big VB, sorted by their texture, so I can render them efficiently. The player, however, can destroy those objects, so they must be removed from the VB. How should I do that? Should I use a dynamic VB?

Share this post


Link to post
Share on other sites
I suggest you use indexed primitives, such as triangle lists (which are as fast as tristrips these days). Then, use the "base vertex" offset for each of the instances, per instance per index buffer. When an object is deleted, just don't draw that particular instance / index buffer. The cool thing about this is that you can cull per instance / index buffer this way.

If you want to batch more than that, I'd suggest dynamically creating the index buffer each frame, so you get one dynamic index buffer per texture for the things of that texture that are visible.

Share this post


Link to post
Share on other sites
How exactly could you achieve that and sort all of the poly's as per texture? If you're scanning the objects in the scene to find out if they're on or off, you're ignoring the texture sort. If you add an on off trigger to the poly's when an object is turned on or off(and render the polygons in order of texture), you're not able to draw more then one of those objects (say you want to make 5 of an object).

Share this post


Link to post
Share on other sites
You use a single vertex buffer.
Each object instance has a bit of state:
- the texture it's drawn with, as a texture bin pointer
- the index buffer it's drawn with and number of indices
- the base vertex for the object
- the world center point and radius of the instance, for culling

The texture bin looks something like:


struct RenderingInstance {
IDirect3D9IndexBuffer * indexBuffer_;
DWORD firstVertex_;
};
struct TextureBin {
IDirect3D9Texture * texture_;
char * debugInfo_;
RenderingInstance * instances_;
size_t instancesUsed_;
size_t instancesAllocated_;
};


When you visit all the object instances, for the instances that intersect the frustum, you fill out the next free RenderingInstance with the index buffer and vertex start offset (re-sizing the array if it's not big enough), for the TextureBin it's rendered with.

When you then go to actually draw, you visit the TextureBins, and for each bin that has more than 0 instances, you issue the appropiate index buffer (or accumulate into a bigger index buffer before rendering), then set instancesUsed_ back to 0.

Note that I'm not suggesting a linked list for the RenderingInstances, because that will cause more cache and TLB misses; arrays of small elements are usually much more efficient -- although it's unlikely to really matter until you get thousands of instances, and then the batching overhead will be much bigger anyway.

Note that this assumes that the vertices all live in world space in a large vertex buffer, like the original poster said, which is a fine way to do it for many small- to medium-sized game levels.

With a little extra control data in the various structures, you can also support objects that use transforms, but still sort by texture; you can also support the "max vertex index" value; you can accumulate index buffers until you get a certain size (say, 30,000 indices) and then issue, and keep walking; and many other nice optimizations.

Share this post


Link to post
Share on other sites

This topic is 4819 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