2

我最近学习 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);
    }
}
4

1 回答 1

1

我认为这是一个投影问题。取自您的代码的这一行加强了我的假设:

viewMatrix * projectionMatrix * gameWorldRotation

tl;博士,正确的顺序是:

gameWorldRotation * viewMatrix * projectionMatrix

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

矩阵乘法不可交换!

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

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

XNA 使用行向量布局(与列向量相反)。这意味着一个向量是水平声明的(x, y, z)。跳过枯燥的有趣细节,这意味着在变换向量时(将向量与矩阵相乘),向量是左操作数,而矩阵是右操作数

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)

世界*视图*投影。这就是你所需要的。(也许为您未来的工作提供一些基本的矩阵数学。)

于 2013-07-19T16:56:14.237 回答