【问题标题】:Can't properly produce a ViewProjection matrix无法正确生成 ViewProjection 矩阵
【发布时间】:2013-12-26 02:41:09
【问题描述】:

我有一个使用 DirectXMath API 的相机类:

__declspec(align(16)) class Camera
{
    public:
        XMVECTOR Translation;
        XMMATRIX Rotation;
        XMVECTOR Scale;
        XMMATRIX Transform;

        XMFLOAT3 RotAngles;

        XMMATRIX ProjectionMatrix;

        float Width;
        float Height;

        float NearZ;
        float FarZ;
        float AspectRatio;
        float FieldOfView;

        Camera()
        {
            Translation = XMVectorZero();
            Rotation = XMMatrixIdentity();
            Scale = XMVectorSplatOne();

            Transform = XMMatrixIdentity();

            Width = 800;
            Height = 600;

            NearZ = 0.1f;
            FarZ = 100.0f;
            AspectRatio = 800 / 600;
            FieldOfView = (XM_PIDIV4);

            ProjectionMatrix = XMMatrixPerspectiveFovLH(FieldOfView, AspectRatio, NearZ, FarZ);
        }

        void Update()
        {
            Rotation = XMMatrixRotationRollPitchYaw(RotAngles.x, RotAngles.y, RotAngles.z);
            XMMATRIX scaleM = XMMatrixScalingFromVector(Scale);
            XMMATRIX translationM = XMMatrixTranslationFromVector(Translation);

            Transform = scaleM * Rotation * translationM;
        }

        XMMATRIX GetViewMatrix()
        {
            XMVECTOR Eye;
            XMVECTOR At;
            XMVECTOR Up;

            Eye = Translation;
            At = Translation + Transform.r[2];
            Up = Transform.r[1];

            return(XMMatrixLookAtLH(Eye, At, Up));
        }

        XMMATRIX GetViewProjectionMatrix()
        {
            return(XMMatrixTranspose(GetViewMatrix() * ProjectionMatrix));
        }
};

当我将 GetViewProjectionMatrix() 的结果存储在 XMFLOAT4X4 中并将其更新到常量缓冲区时,当我使用键盘移动/旋转相机时,几何图形会被撕裂或根本不显示。我已经隔离相机出现变形/消失几何的问题,但我不知道问题出在哪里。我的意思是投影矩阵不会错,它只是 1 个函数调用,所以很可能是视图矩阵。有人能告诉我吗我问题出在哪里?我尝试了乘法顺序的不同组合/转置两者/仅转置一个/任何东西。它永远无法正常工作。

【问题讨论】:

    标签: c++ graphics matrix directxmath


    【解决方案1】:

    如果有人再次看到这个问题:

    似乎 OP 没有转置到他们生成的 ViewProjection 矩阵。请注意,DirectXMath 以行优先顺序工作,而 HLSL 默认为列优先。根据文档 - https://msdn.microsoft.com/en-us/library/windows/desktop/bb509634(v=vs.85).aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 2012-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多