Reputation: 279
I want to draw model into screen. Here is my code:
protected override void Draw(GameTime gameTime) {
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
Matrix[] transforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index] *
Matrix.CreateRotationY(modelRotation) *
Matrix.CreateTranslation(modelPosition);
effect.View = Matrix.CreateLookAt(cameraPosition,
Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f), aspectRatio,
1.0f, 10000.0f);
}
mesh.Draw();
}
base.Draw(gameTime);
}
But, if I remove:
Matrix[] transforms = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(transforms);
and change code in loop:
effect.World = Matrix.CreateRotationY(modelRotation);
It still work fine. So why I must add above code into my project ? What is benefit of it ? Thanks very much!
Upvotes: 1
Views: 505
Reputation: 6466
effect.World = transforms[mesh.ParentBone.Index] *
Matrix.CreateRotationY(modelRotation) *
Matrix.CreateTranslation(modelPosition);
I'm guessing that in step 1 there aren't any transforms because it's a simple box or something and in step 2 the modelPosition is 0,0,0 so it wasn't actually being moved anywhere anyway.
alter the value of modelPosition and you'll see that the model moves around the screen but won't with your changes.
Upvotes: 0
Reputation: 2566
The primary difference is that at case where the transform parts is using, you have full control of the mech parts transformations, at simple case, you can not apply additional transformation matrices.
Simply, you can rotate your mesh as one piece, or you can rotate a mech as a doll, in doll animation, for example.
But i still do not see the reason in your sample to muitiply the identical matrices, accessed via bone index. It still looks useless, until you do something with them in code, but create it every time on one place looks like inproper use of martices.
Upvotes: 1