围绕创建的顶点旋转

时间:2013-07-19 14:47:51

标签: xna xna-4.0

我最近学习xna并试图使用顶点创建一个房间。我去尝试Riemeres XNA教程并学习了很多,但我似乎无法使我的相机工作得很好,每次我向左或向右移动我的一些图像或纹理似乎消失并重新出现。请帮助。

这是我的代码。

public struct MyOwnVertexFormat
{
    public Vector3 position;
    private Vector2 texCoord;

    public MyOwnVertexFormat(Vector3 position, Vector2 texCoord)
    {
        this.position = position;
        this.texCoord = texCoord;
    }

    public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
         (
             new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
             new VertexElement(sizeof(float) * 3, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
         );
}

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    GraphicsDevice device;

    Effect effect;
    Matrix viewMatrix;
    Matrix projectionMatrix;
    VertexBuffer vertexBuffer;
    Vector3 cameraPos;
    Texture2D streetTexture;
    private Vector3 Position = Vector3.One;
    private float Zoom = 2500;
    private float RotationY = 0.0f;
    private float RotationX = 0.0f;
    private Matrix gameWorldRotation;
    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        graphics.PreferredBackBufferWidth =1024;
        graphics.PreferredBackBufferHeight = 768;
        graphics.IsFullScreen = false;
        graphics.ApplyChanges();

        base.Initialize();
    }

    protected override void LoadContent()
    {
        device = GraphicsDevice;


        effect = Content.Load<Effect>("OurHLSLfile"); SetUpVertices();
        SetUpCamera();


        streetTexture = Content.Load<Texture2D>("streettexture");
    }
    private void UpdateKeyboard()
    {
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();
        if (Keyboard.GetState().IsKeyDown(Keys.Up))
            RotationX += 1.0f;
        if (Keyboard.GetState().IsKeyDown(Keys.Down))
            RotationX -= 1.0f;
        if (Keyboard.GetState().IsKeyDown(Keys.Left))
            RotationY += 1.0f;
        if (Keyboard.GetState().IsKeyDown(Keys.Right))
            RotationY -= 1.0f;
        gameWorldRotation =
            Matrix.CreateRotationX(MathHelper.ToRadians(RotationX)) *
            Matrix.CreateRotationY(MathHelper.ToRadians(RotationY));
    }
    private void SetUpVertices()
    {

        MyOwnVertexFormat[] vertices = new MyOwnVertexFormat[12];

        vertices[0] = new MyOwnVertexFormat(new Vector3(-20, 0, 10), new Vector2(-0.25f, 25.0f));
        vertices[1] = new MyOwnVertexFormat(new Vector3(-20, 0, -100), new Vector2(-0.25f, 0.0f));
        vertices[2] = new MyOwnVertexFormat(new Vector3(2, 0, 10), new Vector2(0.25f, 25.0f));
        vertices[3] = new MyOwnVertexFormat(new Vector3(2, 0, -100), new Vector2(0.25f, 0.0f));

        vertices[4] = new MyOwnVertexFormat(new Vector3(2, 1, 10), new Vector2(0.375f, 25.0f));
        vertices[5] = new MyOwnVertexFormat(new Vector3(2, 1, -100), new Vector2(0.375f, 0.0f));
        vertices[6] = new MyOwnVertexFormat(new Vector3(3, 1, 10), new Vector2(0.5f, 25.0f));
        vertices[7] = new MyOwnVertexFormat(new Vector3(3, 1, -100), new Vector2(0.5f, 0.0f));

        vertices[8] = new MyOwnVertexFormat(new Vector3(-13, 1, 10), new Vector2(0.75f, 25.0f));
        vertices[9] = new MyOwnVertexFormat(new Vector3(-13, 1, -100), new Vector2(0.75f, 0.0f));
        vertices[10] = new MyOwnVertexFormat(new Vector3(-13, 21, 10), new Vector2(1.25f, 25.0f));
        vertices[11] = new MyOwnVertexFormat(new Vector3(-13, 21, -100), new Vector2(1.25f, 0.0f));

        vertexBuffer = new VertexBuffer(device, MyOwnVertexFormat.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
        vertexBuffer.SetData(vertices);
    }

    private void SetUpCamera()
    {
        cameraPos = new Vector3(-25, 13, 75);
        viewMatrix = Matrix.CreateLookAt(cameraPos, new Vector3(0, 2, -12), new Vector3(0, 1, 0));
        projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 5000.0f);
    }

    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();
        UpdateKeyboard();
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DarkSlateBlue, 1.0f, 0);

        effect.CurrentTechnique = effect.Techniques["Simplest"];
        effect.Parameters["xViewProjection"].SetValue(viewMatrix * projectionMatrix * gameWorldRotation);
        effect.Parameters["xTexture"].SetValue(streetTexture);

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();

            device.SetVertexBuffer(vertexBuffer);
            device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 10);
        }

        base.Draw(gameTime);
    }
}

1 个答案:

答案 0 :(得分:1)

我认为这是预测问题。这段代码取自你的代码,强化了我的假设:

viewMatrix * projectionMatrix * gameWorldRotation

tl; dr ,正确的顺序是:

gameWorldRotation * viewMatrix * projectionMatrix

请记住,在乘以矩阵时,顺序很重要。用数学术语来说:

矩阵乘法不可交换!

这三个矩阵将矢量映射到三个不同的坐标系,即世界,视图和投影空间。通常,顶点在对象空间中定义。将矢量与世界(视图,投影)矩阵相乘将您的矢量带到世界(视图,投影)空间:

object space => world space => view space => projection space

XNA使用行向量布局(与列向量相对)。这意味着向量被水平声明(x, y, z)。跳过dry interesting details,意味着在转换向量(将向量与矩阵相乘)时,向量是左操作数,而矩阵是右操作数

A := [3x3 Matrix]

(x, y, z) * A = (x', y', z')  // The result is another 3D vector

现在为了应用所有三个矩阵,我们只需使用前一个变换的结果作为下一个变换的输入:

W .... world matrix
V .... view matrix
W .... projection matrix
x .... vector
x' ... transformed vector

x' = ((x * W) * V) * P

最后,矩阵乘法是关联的(括号无所谓)。这就是为什么我们可以在将世界,视图和投影矩阵发送到设备之前将它们组合成单个矩阵:

x' = ((x *  W) * V) * P  =  x * W * V * P  =  x * (W * V * P)

世界*查看*投影。这就是你所需要的。 (也许是你未来工作的一些基本矩阵数学。)