Animating multiple objects

Started by
5 comments, last by Buckeye 13 years, 1 month ago
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?
Non est ad astra mollis e terris via
Advertisement
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:

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()
};


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
There's no "hard", and "the impossible" takes just a little time.
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.



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





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.
[color=#1C2837][size=2][/quote]
[color=#1C2837][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?
Non est ad astra mollis e terris via
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.

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

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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.
Non est ad astra mollis e terris via
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement