为什么 XMMatrixInverse() 有 pDeterminant 参数?

har*_*777 0 c++ directx-12

XMMATRIX XM_CALLCONV XMMatrixInverse(XMVECTOR *pDeterminant, FXMMATRIX M);
Run Code Online (Sandbox Code Playgroud)

我刚刚开始学习directx,我不明白为什么这个函数除了原始矩阵本身之外还要求行列式参数来计算逆。

根据我在数学部分的记忆,仅输入矩阵就足够了:

orig matrix -> cofactor matrix -> adjoint matrix
orig matrix -> det[orig matrix]
inverse = adjoint/det
Run Code Online (Sandbox Code Playgroud)

那么为什么它需要额外的参数呢?加快计算,消除上面的第二步?

Chu*_*urn 7

DirectXMath 中XMMatrixInverse函数使用Cramer 规则来计算逆。大部分代码是计算伴随项,最后一步是乘以行列式的倒数。因此,该函数还必须计算行列式

因此,如果您有其他用途,该函数可以选择返回该行列式。正如对您的问题的评论所述,这pDeterminant是 2.04 之前 xboxmath/XNAMath 中的必需输出参数,

XMMATRIX XMMatrixInverse(_Out_ XMVECTOR* pDeterminant, CXMMATRIX M);
Run Code Online (Sandbox Code Playgroud)

但我在 DirectXMath 中将其设为可选,如SAL 注释所示

XMMATRIX XM_CALLCONV XMMatrixInverse(_Out_opt_ XMVECTOR* pDeterminant, _In_ FXMMATRIX M);
Run Code Online (Sandbox Code Playgroud)

所以你可以这样做:

XMMATRIX mat = …

// Don't care about the determinant
XMMATRIX imat = XMMatrixInverse(nullptr, mat);

// Want the determinant
XMVECTOR d;
XMMATRIX imat = XMMatrixInverse(&d, mat);
Run Code Online (Sandbox Code Playgroud)

XNAMath 2.04 是一个纯网络版本,旨在更紧密地匹配我为 DirectXMath 所做的一些更改,以帮助从 Xbox 360 和旧版 DirectX SDK过渡。请参阅此博客文章

由于您是 DirectX 新手,您应该查看DirectX Tool Kit for DX11 / DX12DirectXMath 的 SimpleMath包装器。