无法正确生成ViewProjection矩阵

时间:2013-12-26 02:41:10

标签: c++ graphics matrix directxmath

我有一个使用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 个答案:

答案 0 :(得分:1)

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

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

的文档