Franco.Chou's Space

Franco's Tech Blog

Monthly Archives: 7月 2010

A Closer Look into “Model”


OK,this time i’ll try to describe my work in English.

I have a lot of stuff in hand recently, forgive me to update this space so late.

 

I think we have neccessarity to have a closer look into “Model” Class —  one of the basic elements in XNA.

Simple? maybe, but must be very important!

 

First let’s shoot the framework of a “Model”!

In the XNA Framework, the Model class represents the whole model. The Model contains a ModelMesh for each separate mesh in the model. Each ModelMesh contains a ParentBone, which controls the mesh’s position and orientation relative to the model. The Model has a Root bone, which determines the model’s position and orientation. Every ModelBone can have one parent and many children. The Root bone on the Model object is the ultimate parent. Its children are bones on ModelMesh objects—objects which might have other ModelMesh bones as their children, and so on. In any given family of bones, rotating the parent bone also rotates the children, and their children, and so on.

Every bone has a transformation matrix (called Transform) that defines its position and rotation relative to the position of the parent bone. This rotation and translation applies to all the vertices in the ModelMesh (for example, all the vertices that connect to that bone). To animate a bone, you multiply the default bone transform by a new matrix. When you draw the ModelMesh, you then base your world matrix on the bone’s transform.

The easiest way to incorporate transformed bones into drawing is to use the CopyAbsoluteBoneTransformsTo method. This method takes the bone transforms, which are relative to each other, and iterates over them to make them relative to the Root bone of the Model. Then it returns a copy of these transforms. When you draw each ModelMesh, you can use the absolute bone transform as the first part of your world matrix. This way you won’t have to worry about parent bones and their relationships.

———————————————————————————————-

Now let’s have a look at this code segement:

Model myModel;
Matrix[] modelTransforms;

…………

myModel.CopyAbsoluteBoneTransformsTo(modelTransforms);[1]
                foreach (ModelMesh mesh in myModel.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.EnableDefaultLighting();
                        effect.World = modelTransforms[mesh.ParentBone.Index] * worldMatrix; [2]
                        effect.View = fpsCam.ViewMatrix;
                        effect.Projection = fpsCam.ProjectionMatrix;
                    }
                    mesh.Draw();
                }

 

Now we can understand the function of “Model.CopyAbsoluteBoneTransformsTo(modelTransforms)” [1] first.

modelTransforms is an array of Matrixes which records the information of the bone of each mesh(The information contains “the mesh’s position and orientation”.)  So when we render a Model(actually what we do is to render each mesh of a Model)we need to use the data of matched bone whose information is convayed in modelTransforms.

Model.CopyAbsoluteBoneTransformsTo(modelTransforms)[1]  Copies the information of bones to modelTransforms.   And use it with modelTransforms[mesh.ParentBone.Index]  [2] in the code segement.

For more,plz search the difference between “bone” and “mesh”!………

— 非常唯美的国产动画。 虽然没有《秒速五厘米》那样细腻,但是对于国产动画来说已经是难能可贵的了!