Sign in to follow this  

Animating multiple objects

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

Hi, I posted this question a few days ago in game programming forum, but I'm stuck again so hopefully some of you guys here can help.
I need to animate many objects at the same time, I'm talking like 100+ objects on screen each being transformed (rotated, scaled, moved) with it's own transform matrix. Each object is just a simple indexed polygon with a texture on it. The way I have it set up now is: SetTransform(polygon1Matrix); DrawIndexedPrimitive(); repeat with polygon2Matrix for all the polygons. So in effect I have 1 DIP call per polygon. So not surprisingly after 10 polygons were added the framerate dropped noticably (I am using the debug libraries, but I still don't think 27 fps for 10-15 polys is acceptable on gtx 260). My question is, how can I animate each polygon without having to call a DIP for each matrix I set. Now the reason I'm reposting this question is because the answer I got in another forum was to squeeze a matrix that contains the transform into the vertex definition, then in the vertex shader multiply that matrix by the vertex position.. But I have spent the past 3 hours trying to do that, I realized that it's no easy task. There's no matrix flag in the FVF declaration, so I must fool the shader somehow by encoding my matrix into other components like the color, or texture, which I'm not exactly sure how to do. But this seems like a really awkward approach for something so common. I have seen this done in many many games, so there must be some way to do this without jumping through these hoops. Any thoughts?

Share this post


Link to post
Share on other sites
FVFs don't contain anything related to matrices; so you have to do that on your own.

Don't play with FVFs, play with vertex declarations instead. Here's an example:
[code]
D3DVERTEXELEMENT9 vPNT [ ] =
{
{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
{ 0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 },
{ 0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 }, //world matrix, 1st row
{ 0, 32, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2 }, //2nd row
{ 0, 44, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3 } //3rd row
{ 0, 56, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4 } //4th row
D3DDECL_END()
}; [/code]

Each frame, you may have to Lock & Unlock the corresponding vertex buffer to embed matrix information to a vertex.

I recommend you to google for Instancing. SDK has a great example about it as well.

Btw, using debug runtimes causes performance hit. Why don't you use Release?

hth.
-R

Share this post


Link to post
Share on other sites
Another approach may be to set an array of matrices in your shader. Depending on the complexity of your shader, even with version 2, you should be able to size that array to 30-40 matrices. In your vertex structure, assign a unique index for each object. In the shader, use that index to retrieve the appropriate matrix to transform your vertices - a method similar to skinned animation.

Then batch your object rendering into groups of 30-40 objects. Set the matrix array, render the objects, set another matrix array, etc.

Share this post


Link to post
Share on other sites
[quote name='programci_84' timestamp='1300259012' post='4786383']

Don't play with FVFs, play with vertex declarations instead. Here's an example:
...

[/quote]



Perfect, thanks programci, I wasn't even aware that they switched away from using FVFs because every example on the net still uses them. :)


[quote name = 'Buckeye']
[color=#1C2837][size=2]Another approach may be to set an array of matrices in your shader.[/size][/color]
[color=#1C2837][size=2][/quote][/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[size="2"][color="#1C2837"]Thats also a great solution. This would save me the valuble Lock/Unlock and allows my vertices to be light, I'm going to try programci's approach first and then I'll compare it to this one. What does cause some inconvenience is that I guess I would have to somehow check the graphics card on each machine to find out what the max array size it allows, right? Or is that something that is determined by the version of shader?[/color][/size]

Share this post


Link to post
Share on other sites
[quote]What does cause some inconvenience is that I guess I would have to somehow check the graphics card on each machine to find out what the max array size it allows, right? Or is that something that is determined by the version of shader? [/quote]
You should check the capabilities of the graphics card capability in any case, but, yes, you'll have to check. You can use GetDeviceCaps and (if I remember) check the MaxVertexShaderConst value*. The SDK has a DirectX Caps Viewer you can use to check values. I'm not positive, but I believe a V2_0 card guarantees** 256 constant registers (can someone confirm that?). At 4 floats per register ( 1 matrix row per register ) that's 64 matrices. However, as you'll probably want to load some other variables (light direction, view/projection matrices, etc.), a modest vertex shader should handle 50 or so matrices pretty easily. E.g., I use ~52 bone matrices as a limit for my skinned mesh animation vertex shader.

EDIT: *MaxVertexShaderConst shows 256 slots on my V2 machine.

**If that guarantee is true, then you only need to check for >= Version 2 for the card to ensure your register limit.

[quote]the capabilities of the graphics card capability[/quote]
Sorry. That's from my Department of Redundancy Department. Edited by Buckeye

Share this post


Link to post
Share on other sites
Ok, great, thanks for pointing me in the right direction Buckeye :]. I actually just noticed an awesome DX9 reference stickied to this forum written by legalize which contains in depth info that you mentioned (like the number of register that are available in each shader version, available instructions etc), so I'll be checking it out. Thanks for all the help guys.

Share this post


Link to post
Share on other sites
[quote]an awesome DX9 reference stickied to this forum written by legalize which contains in depth info that you mentioned (like the number of register that are available in each shader version[/quote]
Good work. I'd forgotten those stickies. As you mentioned, his reference confirms that, if you check for version >= 2, then you can count on a minimum of 256 constant registers.

Share this post


Link to post
Share on other sites
Sign in to follow this