【发布时间】:2011-08-08 19:28:31
【问题描述】:
本周我开始与 XNA 合作,目前正在构建一个可以用于未来游戏的良好内核。
我无法完成我的渲染树,因为我不知道如何编写以下代码:
(我经常使用 OpenGL,所以我正在寻找与此代码等效的最快)
public void DrawRecursively( GameTime deltaTime )
{
glPushMatrix();
/* these 3 lines are what i didn't figure out with XNA */
glTranslate3f( position[0], position[1], position[2] );
glRotatef( theta, 0.0f, 1.0f, 0.0f );
glRotatef( phi, 1.0f, 0.0f, 0.0f );
this.Draw( deltaTime );
foreach ( ComponentInterface child in childs )
{
child.DrawRecursively( deltaTime );
}
glPopMatrix();
}
我目前的尝试是这样的:
public void DrawRecursively( GameTime deltaTime, Matrix worldMatrix )
{
Matrix backup = worldMatrix;
// TRANSLATE HERE.
// ROTATE HERE.
this.Draw( deltaTime );
foreach ( ComponentInterface child in childs )
{
child.DrawRecursively( deltaTime, worldMatrix );
}
worldMatrix = backup;
}
我了解不能从任何地方隐式访问 worldMatrix,您必须携带对它的引用。
那我该如何翻译和旋转呢?
我的worldMatrix 备份是相当于glPushMatrix/PopMatrix 块的正确方法吗?
谢谢,
尼克
编辑:
我想我设法向前迈出了几步,话虽如此,但它仍然无法正常工作。 天哪,我想念 openGL 和所有详细的文档,MSDN 不会给我太多信息... 这是我的最新方法:
public void DrawRecursively( GameTime deltaTime, BasicEffect effect )
{
Matrix worldMatrix = effect.World;
Matrix backup = worldMatrix;
worldMatrix = worldMatrix * Matrix.CreateTranslation( position.ToVector3() );
worldMatrix = worldMatrix * Matrix.CreateRotationY( theta );
worldMatrix = worldMatrix * Matrix.CreateRotationX( phi );
effect.Parameters["xWorld"].SetValue( worldMatrix );
this.Draw( deltaTime );
foreach ( ComponentInterface child in childs )
{
child.DrawRecursively( deltaTime, effect );
}
effect.Parameters["xWorld"].SetValue( backup );
}
effect.Parameters["xWorld"] 返回一个空指针,所以SetValue 显然会引发访问冲突错误。根据调试器,我已经仔细检查并且效果实例已正确初始化。
这是正确的做法吗?
再次编辑:
多亏了你的帮助,我离成功有点近了,但是三角形仍然是静止的,即使增加它的方向角也不会旋转。
public void DrawRecursively( GameTime deltaTime, BasicEffect effect )
{
Matrix worldMatrix = effect.World;
Matrix backup = worldMatrix;
effect.World = worldMatrix * Matrix.CreateScale( scale )
* Matrix.CreateRotationX( orientation.X )
* Matrix.CreateRotationX( orientation.Y )
* Matrix.CreateRotationX( orientation.Z )
* Matrix.CreateTranslation( position.ToVector3() );
this.Draw( deltaTime );
foreach ( ComponentInterface child in childs )
{
child.DrawRecursively( deltaTime, effect );
}
effect.World = backup;
}
【问题讨论】:
标签: c# tree xna rotation render