Sign in to follow this  
CombatWombat

OpenGL Buffers, Drawing many of the same object

Recommended Posts

Hello,

I am a convert from opengl...

I am having some trouble developing an overall strategy for how to implement Index and Vertex Buffers for a particular application. All the tutorials I find focus on one or two objects in a scene.

Say, for the sake of argument, that my scene consists or 1000 cubes, all of identical size, but which will all be at different locations and orientations. These locations and orientations change every frame. Texture and lighting will also likely vary between cubes. I gather that many calls to drawPrimitive is a bad thing. Therefor it sounds like all the triangles that make up these many cubes should be batched together into a buffer. Additionally, cubes may be "joined together" such that drawing their adjoining faces is unnecessary.

So option 1, looks like:
Make a Vertex/Index Buffer to store the dimensions of a single cube. Write this to the GPU, since this will not change.

for (loop through cubes)
Call Translate/Orient Matrix Transforms
Draw A cube

Option 1 sounds terrible since it makes a ton of draw calls, but how else can I handle the transformations since they occur every frame?

Option 2
Transform all model-space verticies into worldspace.
Cull faces that are touching each other, as these are not necessary to draw.
Batch verticies based on texture.
Write them to vertex buffers.
Draw cubes, only stopping to switch texture groups.

This seems better, except now the CPU is doing a lot of work for the transforms ahead of time.

I'm not really sure how to proceed, as the architecture of the problem seems like it could become very convoluted if I do not have a clear plan ahead of time.

Thank you for any assistance!

Share this post


Link to post
Share on other sites
Look into instancing. [url="http://msdn.microsoft.com/en-us/library/ee418269%28v=vs.85%29.aspx"]This[/url] has an overview of both hardware and software instancing and the benefits and limitations of both.

Share this post


Link to post
Share on other sites
Both options you've listed will probably CPU issues. With 1000 draw calls, your FPS will drop significantly. And if you're doing CPU transforms on 1000 you're also going to be running slow. If you're only moving a small number of the cubes each frame then your best bet is to use one vertex buffer that contains pre-transformed vertices. You can then set just the vertices that change each frame.

Share this post


Link to post
Share on other sites
[quote name='NewtonsBit' timestamp='1306714327' post='4817289']
Both options you've listed will probably CPU issues. With 1000 draw calls, your FPS will drop significantly. And if you're doing CPU transforms on 1000 you're also going to be running slow. If you're only moving a small number of the cubes each frame then your best bet is to use one vertex buffer that contains pre-transformed vertices. You can then set just the vertices that change each frame.
[/quote]

Thank you both for your replies.

I am looking into Instancing, as the SDK sample is actually exactly what I need. Now I just need to understand it. I still do not really understand how I encode the data that is changing (translate and rotate transforms), into a FVF structure. I think that is what is required for this approach to work, correct? I will research this more.

It is entirely possible for [i]all[/i] of my cubes to be moving at any given frame, so I think keeping a separate buffer of static objects may not really help me.

Share this post


Link to post
Share on other sites
[quote name='CombatWombat' timestamp='1306726462' post='4817349']
[quote name='NewtonsBit' timestamp='1306714327' post='4817289']
Both options you've listed will probably CPU issues. With 1000 draw calls, your FPS will drop significantly. And if you're doing CPU transforms on 1000 you're also going to be running slow. If you're only moving a small number of the cubes each frame then your best bet is to use one vertex buffer that contains pre-transformed vertices. You can then set just the vertices that change each frame.
[/quote]

Thank you both for your replies.

I am looking into Instancing, as the SDK sample is actually exactly what I need. Now I just need to understand it. I still do not really understand how I encode the data that is changing (translate and rotate transforms), into a FVF structure. I think that is what is required for this approach to work, correct? I will research this more.

It is entirely possible for [i]all[/i] of my cubes to be moving at any given frame, so I think keeping a separate buffer of static objects may not really help me.
[/quote]


Yes, you have to encode the instance data into your vertex buffer. Usually you would store the position and the forward and up vectors for each instance. As far as the FVF goes just put them as texcoords. Also I would look into using vertex declarations instead of FVF since it gives you much more control over how you store your data.

Share this post


Link to post
Share on other sites

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