Animating multiple objects
#1 Members - Reputation: 102
Posted 16 March 2011 - 12:13 AM
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?
#2 Members - Reputation: 333
Posted 16 March 2011 - 01:03 AM
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
#3 Members - Reputation: 1189
Posted 16 March 2011 - 09:24 AM
Then batch your object rendering into groups of 30-40 objects. Set the matrix array, render the objects, set another matrix array, etc.
#4 Members - Reputation: 102
Posted 16 March 2011 - 10:28 AM
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.
Another approach may be to set an array of matrices in your shader.
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?
#5 Members - Reputation: 1189
Posted 16 March 2011 - 11:09 AM
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.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?
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.
Sorry. That's from my Department of Redundancy Department.the capabilities of the graphics card capability
Edited by Buckeye, 16 March 2011 - 11:20 AM.
#6 Members - Reputation: 102
Posted 16 March 2011 - 03:28 PM
#7 Members - Reputation: 1189
Posted 16 March 2011 - 06:29 PM
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.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






