XNA 3D 图形 - 我的代码的含义?

发布于 2024-12-04 16:45:09 字数 1426 浏览 1 评论 0原文

我想将模型绘制到屏幕上。这是我的代码:

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);
    }

但是,如果我删除:

Matrix[] 变换 = new Matrix[model.Bones.Count]; model.CopyAbsoluteBoneTransformsTo(transforms);

并更改循环中的代码:

effect.World = Matrix.CreateRotationY(modelRotation);

它仍然工作正常。那么为什么我必须将上述代码添加到我的项目中?它有什么好处呢?非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み格子的夏天 2024-12-11 16:45:09

主要区别在于,在使用变换部件的情况下,您可以完全控制机械部件变换,在简单的情况下,您不能应用额外的变换矩阵。

简单地说,您可以将网格作为一个整体旋转,也可以将机甲作为玩偶旋转,例如在玩偶动画中。

但我仍然没有看到你的样本中通过骨骼索引访问相同矩阵的原因。它看起来仍然毫无用处,直到您在代码中对它们执行某些操作,但每次在一个地方创建它看起来像是对 martices 的不正确使用。

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.

静待花开 2024-12-11 16:45:09
effect.World = transforms[mesh.ParentBone.Index] *
                Matrix.CreateRotationY(modelRotation) *
                Matrix.CreateTranslation(modelPosition);
  1. 通过它自己的变换来变换每个网格骨骼(其中包括
    父骨骼变换),
  2. 然后绕 Y 轴旋转(有吗
    在屏幕上旋转),
  3. 然后将其移动到另一个位置
    世界空间。

我猜测在步骤 1 中没有任何转换,因为它是一个简单的盒子或其他东西,而在步骤 2 中 modelPosition 是 0,0,0 所以它实际上并没有被移动到任何地方。

更改 modelPosition 的值,您将看到模型在屏幕上移动,但不会随着您的更改而移动。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文