我想利用DirectXMath提供的SSE本质(主要是为了速度优势),我将如何做到这一点?这是当前im使用的代码
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;发布于 2021-05-19 15:58:00
如果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和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;这些是你的屏幕坐标。
https://stackoverflow.com/questions/67605019
复制相似问题