XNA 3D 图形 - 我的代码的含义?
我想将模型绘制到屏幕上。这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主要区别在于,在使用变换部件的情况下,您可以完全控制机械部件变换,在简单的情况下,您不能应用额外的变换矩阵。
简单地说,您可以将网格作为一个整体旋转,也可以将机甲作为玩偶旋转,例如在玩偶动画中。
但我仍然没有看到你的样本中通过骨骼索引访问相同矩阵的原因。它看起来仍然毫无用处,直到您在代码中对它们执行某些操作,但每次在一个地方创建它看起来像是对 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.
父骨骼变换),
在屏幕上旋转),
世界空间。
我猜测在步骤 1 中没有任何转换,因为它是一个简单的盒子或其他东西,而在步骤 2 中 modelPosition 是 0,0,0 所以它实际上并没有被移动到任何地方。
更改 modelPosition 的值,您将看到模型在屏幕上移动,但不会随着您的更改而移动。
parent bone transforms),
spinning around on the screen),
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.