我正在尝试使用 OPENXR 将 VR 耳机合并到我的 DIRECTX11 应用程序中。 我需要使用 DirectX::XMMatrixPerspectiveOffCenterLH 创建投影矩阵。 我有 openXR XrCompositionLayerProjectionView 视图。
在 OpenXR 示例中,他们使用
计算了视图和投影矩阵const DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixTranspose(DirectX::XMMatrixInverse(nullptr, poseToXmMatrix(view.pose)));
//XMStoreFloat4x4(&m_constants.data.view, viewMatrix);
Matrix4x4f projectionMatrix{};
createProjectionFov(&projectionMatrix, view.fov, nearZ, farZ);
const DirectX::XMMATRIX projection = DirectX::XMMatrixTranspose(LoadXrMatrix(projectionMatrix));
inline DirectX::XMMATRIX XM_CALLCONV ToXmMatrix(const XrPosef& pose, float scale)
{
return XMMatrixAffineTransformation(DirectX::XMVectorReplicate(scale), DirectX::g_XMZero,
XMLoadFloat4(reinterpret_cast<const DirectX::XMFLOAT4*>(&pose.orientation)), XMLoadFloat3(reinterpret_cast<const DirectX::XMFLOAT3*>(&pose.position)));
}
DirectX::XMMATRIX XM_CALLCONV poseToXmMatrix(const XrPosef& pose)
{
return XMMatrixAffineTransformation(DirectX::g_XMOne, DirectX::g_XMZero, XMLoadFloat4(reinterpret_cast<const DirectX::XMFLOAT4*>(&pose.orientation)),
XMLoadFloat3(reinterpret_cast<const DirectX::XMFLOAT3*>(&pose.position)));
}
我需要左上右下的值
void FI3PerspectiveOffCenterProjection::Calculate()
{
//float w = fabs(r-l);
//float h = fabs(t-b);
FI3MATRIX matProjection = FI3MatrixPerspectiveOCFOVLH(m_fNearClip * l, m_fNearClip * r, m_fNearClip * b, m_fNearClip * t, m_fNearClip, m_fFarClip);
FI3Matrix4x4AFromMatrix(&m_matProjection, matProjection);
}
void FI3PerspectiveOffCenterProjection::setParams(float _l, float _t, float _r, float _b, float _fFarClip, float _fNearClip, bool _bAngular /*= false*/)
{
if (_bAngular)
{
l = tan(_l);
t = tan(_t);
r = tan(_r);
b = tan(_b);
// float w = r - l;
// float h = b - t;
}
else
{
l = _l;
r = _r;
t = _t;
b = _b;
}
Calculate();
}
对于矩阵的单个元素访问,请将其存储到
XMFLOAT4X4
或 XMFLOAT4X4A
并直接访问。这通常比对 XMVectorGet*
的各个行使用 XMMATRIX
方法更有效。
请参阅 Microsoft Learn。