开发者

XNA 3D Graphics - Meaning of my code?

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!


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.


effect.World = transforms[mesh.ParentBone.Index] *
                Matrix.CreateRotationY(modelRotation) *
                Matrix.CreateTranslation(modelPosition);
  1. transforms each mesh bone by it's own transform (which includes the parent bone transforms),
  2. then rotates it around the Y axis (has it spinning around on the screen),
  3. then moves it to another location in world space.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜