C# XNA-渲染树旋转和平移

C# XNA-渲染树旋转和平移,c#,tree,xna,rotation,render,C#,Tree,Xna,Rotation,Render,本周我开始与XNA合作,目前正在构建一个良好的核心,我将能够在未来的游戏中使用 我无法完成渲染树,因为我不知道如何编写以下代码: 我经常使用OpenGL,所以我正在寻找与此代码相当的最快版本 public void DrawRecursively( GameTime deltaTime ) { glPushMatrix(); /* these 3 lines are what i didn't figure out with XNA */ glTranslate3f(

本周我开始与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 );
}
Parameters[xWorld]返回一个空指针,因此SetValue显然会抛出访问冲突错误。根据调试器,我已仔细检查并正确初始化了effect实例

这是正确的方法吗

再次编辑:

多亏了你的帮助,我有点接近成功,但三角形仍然是静态的,即使在增加其方向角时也不会旋转

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

BasicFect有用于世界、视图和投影矩阵的getter和setter,但着色器本身保留一个WorldViewProj矩阵,这就是effect.Parameters[xWorld]失败的原因。坚持做定位球,剩下的就交给最基本的运动员了

Matrix worldMatrix = effect.World;
Matrix backup = worldMatrix;

worldMatrix *= Matrix.CreateTranslation( position.ToVector3() );
worldMatrix *= Matrix.CreateRotationY( theta );
worldMatrix *=  Matrix.CreateRotationX( phi );
effect.World = worldMatrix;

没有更多的崩溃,但我的测试三角形不会按预期旋转。我在update函数中递增θ,调试器显示CreateRotation是用有效递增θ调用的。@nAuthier:您在不同方向上使用CreateRotationX三次。您可以使用CreateFromYawPitchRolly,x,z保存一些矩阵乘法。