我有一个使用 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 个函数调用,所以它很可能是视图矩阵。有人可以告诉我问题出在哪里吗?我尝试了乘法顺序的不同组合/转置两者/仅转置一个/任何东西。它永远不会正常工作。
如果有人再次看到这个问题:
OP似乎没有转置到他们生成的ViewProjection matrix。请注意,DirectXMath 按行主顺序工作,而 HLSL 默认为列主序。根据文档 - https://msdn.microsoft.com/en-us/library/windows/desktop/bb509634(v=vs.85).aspx