我有以下功能代码:
D3DXVECTOR3 Mouse::GetClickDirection(D3DXMATRIX projMatrix, D3DXMATRIX viewMatrix, D3DXVECTOR3 cameraPosition)
{
POINT mousePoint;
GetCursorPos(&mousePoint);
ScreenToClient(hwnd, &mousePoint);
float x = ((mousePoint.x * 2.0f) / (float)backBufferWidth) - 1.0f;
float y = ((mousePoint.y * -2.0f) / (float)backBufferHeight) + 1.0f;
D3DXVECTOR3 mouseClip = D3DXVECTOR3(x, y, 1.0f);
D3DXMATRIX invViewMatrix;
D3DXMatrixInverse(&invViewMatrix, 0, &viewMatrix);
D3DXMATRIX invProjMatrix;
D3DXMatrixInverse(&invProjMatrix, 0, &projMatrix);
D3DXMATRIX inversedMatrix = invViewMatrix * invProjMatrix;
D3DXVECTOR3 mouseWorldSpace;
D3DXVec3TransformCoord(&mouseWorldSpace, &mouseClip, &inversedMatrix);
D3DXVECTOR3 direction = mouseWorldSpace - cameraPosition;
D3DXVec3Normalize(&direction, &direction);
system("CLS");
std::cout << "sX: " << x << std::endl;
std::cout << "sY: " << y << std::endl;
std::cout << "X: " << direction.x << std::endl;
std::cout << "Y: " << direction.y << std::endl;
std::cout << "Z: " << direction.z << std::endl;
return direction;
}
我正在尝试根据用户在我的 DirectX 3D 应用程序中单击的 x 和 y 屏幕点来计算和创建定向射线。到目前为止,打印结果似乎表明我的计算是错误的,因为 Z 值始终在 0.9 左右,我不知道为什么。我在这里到底做错了什么?请帮忙。谢谢