使用 D3DXMath

D3DXMath 是 Direct3D 應用程式的數學協助程式連結庫。 D3DXMath 是長期存在的,包含在 D3DX 9 和 D3DX 10 中,而且可以追溯到舊版 DirectX。

注意

D3DX 公用程序連結庫(D3DX 9、D3DX 10 和 D3DX 11)已針對 Windows 8 淘汰,因此強烈建議您移轉至 DirectXMath,而不是使用 D3DXMath。

 

DirectXMath 在 D3DXMath 中共用許多相同的功能,而內部 D3DXMath 包含許多處理器特定的優化。 主要差異在於 D3DXMath 裝載於 D3DX9*。DLL 和 D3DX10*。DLL 和極少數的函式會內嵌。 DirectXMath 連結庫呼叫慣例明確為 SIMD 易記,而 D3DXMath 必須執行負載和儲存轉換,以實作 SIMD 優化。

混合 DirectXMath 和 D3DXMath

D3DX11 不包含 D3DXMath,一般而言,我們建議改用 DirectXMath。 不過,您可以繼續連結至應用程式中的 D3DX9 和/或 D3DX10,因此您可以繼續使用 D3DXMath,或在應用程式中同時使用 D3DXMath 和 DirectXMath。

一般而言,將 XMVECTOR* 轉換成採用 D3DXVECTOR4* 的函式,或將 XMMATRIX* 轉換成接受D3DXMATRIX*的函式是安全的。 不過,反函數通常並不安全,因為 XMVECTOR 和 XMMATRIX 必須對齊 16 位元組,而D3DXVECTOR4和D3DXMATRIX沒有這類需求。 若未遵守這項需求,可能會導致運行時間的對齊例外狀況無效。

安全地將 XMVECTOR* 轉換成接受 D3DXVECTOR2* 或 D3DXVECTOR3* 的函式,但反之亦然。 對齊考慮和D3DXVECTOR2和D3DXVECTOR3都是較小的結構,使這項作業變得不安全。

注意

D3DX (因此 D3DXMath) 被視為舊版,不適用於在 Windows 8 上執行的 Windows 市集應用程式,且不包含在 Windows 8 SDK 中用於傳統型應用程式的 Windows 8 SDK。

 

搭配 Direct3D 使用 DirectXMath

使用 Direct3D 時,DirectXMath 和 D3DXMath 都是選擇性的。 Direct3D 9 定義D3DMATRIX和D3DCOLOR作為 Direct3D API 的一部分,以支援 (現在舊版) 固定函式管線。 D3DX9 中的 D3DXMath 會擴充這些 Direct3D 9 類型與常見的圖形數學運算。 針對 Direct3D 10.x 和 Direct3D 11,API 只會使用可程式化的管線,因此矩陣或色彩值沒有 API 特定的結構。 當較新的 API 需要色彩值時,它們會採用明確的浮點數陣列,或由 HLSL 著色器解譯之常數數據的泛型緩衝區。 HLSL 本身可以支援數據列主要或數據行主要矩陣格式,因此配置完全由您決定(如需詳細資訊,請參閱 HLSL、 矩陣排序;如果您在著色器中使用數據行主要矩陣格式,則需要將 DirectXMath 矩陣數據轉置成常數緩衝區結構)。 雖然是選擇性的,但 DirectXMath 和 D3DXMath 連結庫都提供常見的圖形相關功能,因此在執行 Direct3D 程式設計時非常方便。

由於 Direct3D 9 不會對傳入數據結構做出對齊假設,所以將 XMVECTOR* 轉換成 D3DMATRIX D3DVECTOR* 或 XMMATRIX* 是安全的。 將 XMCOLOR 轉換成D3DCOLOR也是安全的。 您可以透過 XMStoreColor 將色彩的 4 浮點數表示轉換成 XMCOLOR,以取得相當於 D3DCOLOR 的 8:8:8:8 32 位 DWORD。

使用 Direct3D 10.x 或 Direct3D 11 時,您通常會使用 DirectXMath 類型來建置每個常數緩衝區的結構,在這些情況下,這在很大程度上取決於您控制對齊方式,使這些有效,或使用 XMStore*() 作業,將 XMVECTOR 和 XMMATRIX 數據轉換成正確的數據類型。 呼叫需要色彩值的 float[4] 陣列的 Direct3D 10.x 或 Direct3D 11 API 時,您可以轉換 XMVECTOR* 或包含色彩數據的XMFLOAT4* 。

從 D3DXMath 移植

D3DXMath 類型 DirectXMath 對等專案
D3DXFLOAT16 一半
D3DXMATRIX XMFLOAT4X4
D3DXMATRIXA16 XMMATRIXXMFLOAT4X4A
D3DXQUATERNION
D3DXPLANE
D3DXCOLOR
使用 XMVECTOR 而不是具有唯一類型,因此您可能需要使用XMFLOAT4Note:**D3DXQUATERNION::operator *** 呼叫 D3DXQuaternionMultiply 函式,其乘以兩個四元數。 但是,除非您明確使用 XMQuaternionMultiply 函式,否則當您在四元數上使用 **XMVECTOR::operator ** 時,會收到不正確的答案。
D3DXVECTOR2 XMFLOAT2
D3DXVECTOR2_16F XMHALF2
D3DXVECTOR3 XMFLOAT3
D3DXVECTOR4 XMFLOAT4(或者,如果可以保證數據是16位元組對齊、XMVECTOR XMFLOAT4A )
D3DXVECTOR4_16F XMHALF4

 

注意

XNAMath 中沒有直接對等D3DXVECTOR3_16F。

 

D3DXMath 宏 DirectXMath 對等專案
D3DX_PI XM_PI
D3DX_1BYPI XM_1DIVPI
D3DXToRadian XMConvertToRadian
D3DXToDegree XMConvertToDegrees

 

D3DXMath 函式 DirectXMath 對等專案
D3DXBoxBoundProbe BoundingBox::Intersects(XMVECTOR、XMVECTOR、float&)
D3DXComputeBoundingBox BoundingBox::CreateFromPoints
D3DXComputeBoundingSphere BoundingSphere::CreateFromPoints
D3DXSphereBoundProbe BoundingSphere::Intersects(XMVECTOR、XMVECTOR、float&)
D3DXIntersectTriFunction TriangleTests::Intersects
D3DXFloat32To16Array XMConvertFloatToHalfStream
D3DXFloat16To32Array XMConvertHalfToFloatStream
D3DXVec2Length XMVector2LengthXMVector2LengthEst
D3DXVec2LengthSq XMVector2LengthSq
D3DXVec2Dot XMVector2Dot
D3DXVec2CCW XMVector2Cross
D3DXVec2Add XMVectorAdd
D3DXVec2Subtract XMVectorSubtract
D3DXVec2Minimize XMVectorMin
D3DXVec2Maximize XMVectorMax
D3DXVec2Scale XMVectorScale
D3DXVec2Lerp XMVectorLerpXMVectorLerpV
D3DXVec2Normalize XMVector2NormalizeXMVector2NormalizeEst
D3DXVec2Hermite XMVectorHermiteXMVectorHermiteV
D3DXVec2CatmullRom XMVectorCatmullRomXMVectorCatmullRomV
D3DXVec2BaryCentric XMVectorBaryCentricXMVectorBaryCentricV
D3DXVec2Transform XMVector2Transform
D3DXVec2TransformCoord XMVector2TransformCoord
D3DXVec2TransformNormal XMVector2TransformNormal
D3DXVec2TransformArray XMVector2TransformStream
D3DXVec2TransformCoordArray XMVector2TransformCoordStream
D3DXVec2TransformNormalArray XMVector2TransformNormalStream
D3DXVec3Length XMVector3LengthXMVector3LengthEst
D3DXVec3LengthSq XMVector3LengthSq
D3DXVec3Dot XMVector3Dot
D3DXVec3Cross XMVector3Cross
D3DXVec3Add XMVectorAdd
D3DXVec3Subtract XMVectorSubtract
D3DXVec3Minimize XMVectorMin
D3DXVec3Maximize XMVectorMax
D3DXVec3Scale XMVectorScale
D3DXVec3Lerp XMVectorLerpXMVectorLerpV
D3DXVec3Normalize XMVector3NormalizeXMVector3NormalizeEst
D3DXVec3Hermite XMVectorHermiteXMVectorHermiteV
D3DXVec3CatmullRom XMVectorCatmullRomXMVectorCatmullRomV
D3DXVec3BaryCentric XMVectorBaryCentricXMVectorBaryCentricV
D3DXVec3Transform XMVector3Transform
D3DXVec3TransformCoord XMVector3TransformCoord
D3DXVec3TransformNormal XMVector3TransformNormal
D3DXVec3TransformArray XMVector3TransformStream
D3DXVec3TransformCoordArray XMVector3TransformCoordStream
D3DXVec3TransformNormalArray XMVector3TransformNormalStream
D3DXVec3Project XMVector3Project
D3DXVec3Unproject XMVector3Unproject
D3DXVec3ProjectArray XMVector3ProjectStream
D3DXVec3UnprojectArray XMVector3UnprojectStream
D3DXVec4Length XMVector4LengthXMVector4LengthEst
D3DXVec4LengthSq XMVector4LengthSq
D3DXVec4Dot XMVector4Dot
D3DXVec4Add XMVectorAdd
D3DXVec4Subtract XMVectorSubtract
D3DXVec4Minimize XMVectorMin
D3DXVec4Maximize XMVectorMax
D3DXVec4Scale XMVectorScale
D3DXVec4Lerp XMVectorLerpXMVectorLerpV
D3DXVec4Cross XMVector4Cross
D3DXVec4Normalize XMVector4NormalizeXMVector4NormalizeEst
D3DXVec4Hermite XMVectorHermiteXMVectorHermiteV
D3DXVec4CatmullRom XMVectorCatmullRomXMVectorCatmullRomV
D3DXVec4BaryCentric XMVectorBaryCentricXMVectorBaryCentricV
D3DXVec4Transform XMVector4Transform
D3DXVec4TransformArray XMVector4TransformStream
D3DXMatrixIdentity XMMatrixIdentity
D3DXMatrixDeterminant XMMatrixDeterminant
D3DXMatrixDecompose XMMatrixDecompose
D3DXMatrixTranspose XMMatrixTranspose
D3DXMatrixMultiply XMMatrixMultiply
D3DXMatrixMultiplyTranspose XMMatrixMultiplyTranspose
D3DXMatrixInverse XMMatrixInverse
D3DXMatrixScaling XMMatrixScaling
D3DXMatrixTranslation XMMatrixTranslation
D3DXMatrixRotationX XMMatrixRotationX
D3DXMatrixRotationY XMMatrixRotationY
D3DXMatrixRotationZ XMMatrixRotationZ
D3DXMatrixRotationAxis XMMatrixRotationAxis
D3DXMatrixRotationQuaternion XMMatrixRotationQuaternion
D3DXMatrixRotationYawPitchRoll XMMatrixRotationRollPitchYaw (請注意參數的順序不同:D3DXMatrixRotationYawPitchRoll 採用 yaw、pitch、roll、 XMMatrixRotationRollPitchYaw 採用 pitch、yaw、roll)
D3DXMatrixTransformation XMMatrixTransformation
D3DXMatrixTransformation2D XMMatrixTransformation2D
D3DXMatrixAffineTransformation XMMatrixAffineTransformation
D3DXMatrixAffineTransformation2D XMMatrixAffineTransformation2D
D3DXMatrixLookAtRH XMMatrixLookAtRH
D3DXMatrixLookAtLH XMMatrixLookAtLH
D3DXMatrixPerspectiveRH XMMatrixPerspectiveRH
D3DXMatrixPerspectiveLH XMMatrixPerspectiveLH
D3DXMatrixPerspectiveFovRH XMMatrixPerspectiveFovRH
D3DXMatrixPerspectiveFovLH XMMatrixPerspectiveFovLH
D3DXMatrixPerspectiveOffCenterRH XMMatrixPerspectiveOffCenterRH
D3DXMatrixPerspectiveOffCenterLH XMMatrixPerspectiveOffCenterLH
D3DXMatrixOrthoRH XMMatrixOrthographicRH
D3DXMatrixOrthoLH XMMatrixOrthographicLH
D3DXMatrixOrthoOffCenterRH XMMatrixOrthographicOffCenterRH
D3DXMatrixOrthoOffCenterLH XMMatrixOrthographicOffCenterLH
D3DXMatrixShadow XMMatrixShadow
D3DXMatrix 反思 XMMatrix 反思
D3DXQuaternionLength XMQuaternionLength
D3DXQuaternionLengthSq XMQuaternionLengthSq
D3DXQuaternionDot XMQuaternionDot
D3DXQuaternionIdentity XMQuaternionIdentity
D3DXQuaternionIsIdentity XMQuaternionIsIdentity
D3DXQuaternionConjugate XMQuaternionConjugate
D3DXQuaternionToAxisAngle XMQuaternionToAxisAngle
D3DXQuaternionRotationMatrix XMQuaternionRotationMatrix
D3DXQuaternionRotationAxis XMQuaternionRotationAxis
D3DXQuaternionRotationYawPitchRoll XMQuaternionRotationRollPitchYaw (請注意參數的順序不同:D3DXQuaternionRotationYawPitchRoll 採用 yaw、pitch、roll、 XMQuaternionRotationRollPitchYaw 採用投球、yaw、roll)
D3DXQuaternionMultiply XMQuaternionMultiply
D3DXQuaternionNormalize XMQuaternionNormalizeXMQuaternionNormalizeEst
D3DXQuaternionInverse XMQuaternionInverse
D3DXQuaternionLn XMQuaternionLn
D3DXQuaternionExp XMQuaternionExp
D3DXQuaternionSlerp XMQuaternionSlerpXMQuaternionSlerpV
D3DXQuaternionSquad XMQuaternionSquadXMQuaternionSquadV
D3DXQuaternionSquadSetup XMQuaternionSquadSetup
D3DXQuaternionBaryCentric XMQuaternionBaryCentricXMQuaternionBaryCentricV
D3DXPlaneDot XMPlaneDot
D3DXPlaneDotCoord XMPlaneDotCoord
D3DXPlaneDotNormal XMPlaneDotNormal
D3DXPlaneScale XMVectorScale
D3DXPlaneNormalize XMPlaneNormalizeXMPlaneNormalizeEst
D3DXPlaneIntersectLine XMPlaneIntersectLine
D3DXPlaneFromPointNormal XMPlaneFromPointNormal
D3DXPlaneFromPoints XMPlaneFromPoints
D3DXPlaneTransform XMPlaneTransform
D3DXPlaneTransformArray XMPlaneTransformStream
D3DXColorNegative XMColorNegative
D3DXColorAdd XMVectorAdd
D3DXColorSubtract XMVectorSubtract
D3DXColorScale XMVectorScale
D3DXColorModulate XMColorModulate
D3DXColorLerp XMVectorLerpXMVectorLerpV
D3DXColorAdjustSaturation XMColorAdjustSaturation
D3DXColorAdjustContrast XMColorAdjustContrast
D3DXFresnelTerm XMFresnelTerm

 

注意

DirectXMath 的球面調和 函式可分開使用。 也提供相當於 ID3DXMatrixStack 的 DirectXMath。

 

DirectXMath 程式設計指南