我想利用DirectXMath提供的SSE内部特性(主要是为了速度优势(我该怎么做?这是当前使用的代码
DirectX::XMFLOAT4 clipCoordinates;
DirectX::XMFLOAT4X4 WorldMatrix4x4;
DirectX::XMStoreFloat4x4(&WorldMatrix4x4, WorldMatrix);
clipCoordinates.x = pos.x * WorldMatrix4x4._11 + pos.y * WorldMatrix4x4._12 + pos.z * WorldMatrix4x4._13 + WorldMatrix4x4._14;
clipCoordinates.y = pos.x * WorldMatrix4x4._21 + pos.y * WorldMatrix4x4._22 + pos.z * WorldMatrix4x4._23 + WorldMatrix4x4._24;
clipCoordinates.z = pos.x * WorldMatrix4x4._31 + pos.y * WorldMatrix4x4._32 + pos.z * WorldMatrix4x4._33 + WorldMatrix4x4._34;
clipCoordinates.w = pos.x * WorldMatrix4x4._41 + pos.y * WorldMatrix4x4._42 + pos.z * WorldMatrix4x4._43 + WorldMatrix4x4._44;
如果您所说的2D位置是指屏幕空间坐标:
首次从局部空间到世界空间的转换:
XMVECTOR world_pos = XMVector4Transform(pos, world_matrix);
然后从世界空间转换到相机空间:
XMVECTOR view_pos = XMVector4Transform(world_pos, view_matrix);
然后从相机空间转换到剪辑空间:
XMVECTOR clip_pos = XMVector4Transform(view_pos , proj_matrix);
请记住,通过将
XMMATRIX matrix = world_matrix * view_matrix * proj_matrix;
相乘,然后将点变换为matrix
,可以将所有这些连接到一个变换中。
现在除以w坐标得到NDC:
XMVECTOR w_vector = XMVectorSplatW(clip_pos);
XMVECTOR ndc = XMVectorDivide(clip_pos, w_vector);
您可以使用
XMVECTOR ndc = XMVector3TransformCoord(clip_pos, matrix);
组合变换和除法
您的NDC x和y组件位于[-1,1]
间隔中。要在[0,ScreenWidth]
和[0,ScreenHeight]
中转换它们,请使用以下公式:
float ndc_x = XMVectorGetX(ndc);
float ndc_y = XMVectorGetY(ndc);
float pixel_x = (1.0f + ndc_x) * 0.5 * ScreenWidth;
float pixel_y = (1.0f - ndc_y) * 0.5 * ScreenHeight;
这些是你的屏幕坐标。