Thursday, September 18, 2008

FBX and XNA Part 4 - Drawing the model.

Now that we know what all the parts of the model are in the FBX, lets combine them.

well,

for each Mesh in the Model
make a copy of the Vertices
make an array of Matrices, one for each point in the Vertices.
They all get initialized to all zeros (not Identity)
for each SubDeformer
M = SubDeformer.Transform
if it has any Indexes (some do not)
find the animation in the current Take
for this Deformer.
It is the one in Connections that has the
same name as the SubDeformer
M = M * the animation current rotation * the PreRotation *
the animation current translation
if there is a parent LimbNode recursively do the
M = M * rot * prerot * trans
remember m for this deformer.

Now for all the remembered Ms influence the array of Matrices by
doing weighted sums of the elements in the matrices. Note that
this is not a matrix multiply, it is an element
by element weighted sum.
vMats[Indexes[i]] += Matrix.Multiply(Transform, Weights[i])

Now for every element in the copy of Vertices, do a matrix multiply by
the weighted sum matrices
to arrive at the position. Also do a TransformNormal to get the normals right.

Phew, that is how its done.

Now take the copy of Vertices that is now in world coordinates, and draw it on the screen.

foreach Polygon in the list of polygon indices, call
GraphicsDevice.DrawUserPrimitives. Like this
gd.DrawUserPrimitives(PrimitiveType.TriangleFan, drawVerticesXformed, polys[i].StartIdx, polys[i].PrimCount);

It's easy as that.

TF

2 comments:

KD said...

Nice work.. I am looking for this thing friend.. Actually i was doing the FBX SDK samples ..

BTW do you know how to get these matrices using FBX SDK api ? So that we can avoid parsing the FBX ascii file.


( if you are not aware of FBX SDK , it is provided by autodesk.. it can read fbx,3ds, collada formats )

brad said...

yea but the sdk is only in c++ not c# for xna. big problem