使用 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 8 SDK for Desktop Apps 中的 Windows 应用商店应用。
将 DirectXMath 与 Direct3D 配合使用
使用 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 编程时非常方便。
将 XMVECTOR* 转换为 D3DVECTOR* 或 XMMATRIX* 到 D3DMATRIX* 是安全的,因为 Direct3D 9 不对传入数据结构进行对齐假设。 将 XMCOLOR 强制转换为 D3DCOLOR 也是安全的。 可以通过 XMStoreColor () 将颜色的 4 浮点表示形式转换为 XMCOLOR,以获取等效于 D3DCOLOR 的 8: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 | XMMATRIX 或 XMFLOAT4X4A |
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 | XMConvertToRadians |
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 | XMVector2Length 或 XMVector2LengthEst |
D3DXVec2LengthSq | XMVector2LengthSq |
D3DXVec2Dot | XMVector2Dot |
D3DXVec2CCW | XMVector2Cross |
D3DXVec2Add | XMVectorAdd |
D3DXVec2Subtract | XMVectorSubtract |
D3DXVec2Minimize | XMVectorMin |
D3DXVec2Maximize | XMVectorMax |
D3DXVec2Scale | XMVectorScale |
D3DXVec2Lerp | XMVectorLerp 或 XMVectorLerpV |
D3DXVec2Normalize | XMVector2Normalize 或 XMVector2NormalizeEst |
D3DXVec2Hermite | XMVectorHermite 或 XMVectorHermiteV |
D3DXVec2CatmullRom | XMVectorCatmullRom 或 XMVectorCatmullRomV |
D3DXVec2BaryCentric | XMVectorBaryCentric 或 XMVectorBaryCentricV |
D3DXVec2Transform | XMVector2Transform |
D3DXVec2TransformCoord | XMVector2TransformCoord |
D3DXVec2TransformNormal | XMVector2TransformNormal |
D3DXVec2TransformArray | XMVector2TransformStream |
D3DXVec2TransformCoordArray | XMVector2TransformCoordStream |
D3DXVec2TransformNormalArray | XMVector2TransformNormalStream |
D3DXVec3Length | XMVector3Length 或 XMVector3LengthEst |
D3DXVec3LengthSq | XMVector3LengthSq |
D3DXVec3Dot | XMVector3Dot |
D3DXVec3Cross | XMVector3Cross |
D3DXVec3Add | XMVectorAdd |
D3DXVec3Subtract | XMVectorSubtract |
D3DXVec3Minimize | XMVectorMin |
D3DXVec3Maximize | XMVectorMax |
D3DXVec3Scale | XMVectorScale |
D3DXVec3Lerp | XMVectorLerp 或 XMVectorLerpV |
D3DXVec3Normalize | XMVector3Normalize 或 XMVector3NormalizeEst |
D3DXVec3Hermite | XMVectorHermite 或 XMVectorHermiteV |
D3DXVec3CatmullRom | XMVectorCatmullRom 或 XMVectorCatmullRomV |
D3DXVec3BaryCentric | XMVectorBaryCentric 或 XMVectorBaryCentricV |
D3DXVec3Transform | XMVector3Transform |
D3DXVec3TransformCoord | XMVector3TransformCoord |
D3DXVec3TransformNormal | XMVector3TransformNormal |
D3DXVec3TransformArray | XMVector3TransformStream |
D3DXVec3TransformCoordArray | XMVector3TransformCoordStream |
D3DXVec3TransformNormalArray | XMVector3TransformNormalStream |
D3DXVec3Project | XMVector3Project |
D3DXVec3Unproject | XMVector3Unproject |
D3DXVec3ProjectArray | XMVector3ProjectStream |
D3DXVec3UnprojectArray | XMVector3UnprojectStream |
D3DXVec4Length | XMVector4Length 或 XMVector4LengthEst |
D3DXVec4LengthSq | XMVector4LengthSq |
D3DXVec4Dot | XMVector4Dot |
D3DXVec4Add | XMVectorAdd |
D3DXVec4Subtract | XMVectorSubtract |
D3DXVec4Minimize | XMVectorMin |
D3DXVec4Maximize | XMVectorMax |
D3DXVec4Scale | XMVectorScale |
D3DXVec4Lerp | XMVectorLerp 或 XMVectorLerpV |
D3DXVec4Cross | XMVector4Cross |
D3DXVec4Normalize | XMVector4Normalize 或 XMVector4NormalizeEst |
D3DXVec4Hermite | XMVectorHermite 或 XMVectorHermiteV |
D3DXVec4CatmullRom | XMVectorCatmullRom 或 XMVectorCatmullRomV |
D3DXVec4BaryCentric | XMVectorBaryCentric 或 XMVectorBaryCentricV |
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 采用偏航、间距、roll、 XMMatrixRotationRollPitchYaw 采用俯仰、偏转、滚动) |
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 |
D3DXMatrixReflect | XMMatrixReflect |
D3DXQuaternionLength | XMQuaternionLength |
D3DXQuaternionLengthSq | XMQuaternionLengthSq |
D3DXQuaternionDot | XMQuaternionDot |
D3DXQuaternionIdentity | XMQuaternionIdentity |
D3DXQuaternionIsIdentity | XMQuaternionIsIdentity |
D3DXQuaternionConjugate | XMQuaternionConjugate |
D3DXQuaternionToAxisAngle | XMQuaternionToAxisAngle |
D3DXQuaternionRotationMatrix | XMQuaternionRotationMatrix |
D3DXQuaternionRotationAxis | XMQuaternionRotationAxis |
D3DXQuaternionRotationYawPitchRoll | XMQuaternionRotationRollPitchYaw (注意参数的顺序不同:D3DXQuaternionRotationYawPitchRoll 采用偏航、俯仰、滚动、 XMQuaternionRotationRollPitchYaw 采用俯仰、偏航、滚动) |
D3DXQuaternionMultiply | XMQuaternionMultiply |
D3DXQuaternionNormalize | XMQuaternionNormalize 或 XMQuaternionNormalizeEst |
D3DXQuaternionInverse | XMQuaternionInverse |
D3DXQuaternionLn | XMQuaternionLn |
D3DXQuaternionExp | XMQuaternionExp |
D3DXQuaternionSlerp | XMQuaternionSlerp 或 XMQuaternionSlerpV |
D3DXQuaternionSquad | XMQuaternionSquad 或 XMQuaternionSquadV |
D3DXQuaternionSquadSetup | XMQuaternionSquadSetup |
D3DXQuaternionBaryCentric | XMQuaternionBaryCentric 或 XMQuaternionBaryCentricV |
D3DXPlaneDot | XMPlaneDot |
D3DXPlaneDotCoord | XMPlaneDotCoord |
D3DXPlaneDotNormal | XMPlaneDotNormal |
D3DXPlaneScale | XMVectorScale |
D3DXPlaneNormalize | XMPlaneNormalize 或 XMPlaneNormalizeEst |
D3DXPlaneIntersectLine | XMPlaneIntersectLine |
D3DXPlaneFromPointNormal | XMPlaneFromPointNormal |
D3DXPlaneFromPoints | XMPlaneFromPoints |
D3DXPlaneTransform | XMPlaneTransform |
D3DXPlaneTransformArray | XMPlaneTransformStream |
D3DXColorNegative | XMColorNegative |
D3DXColorAdd | XMVectorAdd |
D3DXColorSubtract | XMVectorSubtract |
D3DXColorScale | XMVectorScale |
D3DXColorModulate | XMColorModulate |
D3DXColorLerp | XMVectorLerp 或 XMVectorLerpV |
D3DXColorAdjustSaturation | XMColorAdjustSaturation |
D3DXColorAdjustContrast | XMColorAdjustContrast |
D3DXFresnelTerm | XMFresnelTerm |
注意
DirectXMath 的球面调音函数单独提供。 也提供与 ID3DXMatrixStack 等效的 DirectXMath 。
相关主题
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈