Right now I can transform a 3D world coordinate to a 2D screen coordinate. I can achieve that with this:
D3DXMATRIX viewMatrix = renderer->viewMatrix;
D3DXMATRIX projectionMatrix = renderer->projectionMatrix;
D3DXMATRIX viewProjectionMatrix = D3DXMATRIX();
D3DXMatrixMultiply(&viewProjectionMatrix, &viewMatrix, &projectionMatrix);
D3DXVECTOR3 pScreen = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 pWorld(world->getX(), world->getY(), world->getZ());
D3DXVec3TransformCoordImpl(&pScreen, &pWorld, &viewProjectionMatrix);
Now pScreen
contains the 2D screen coordinate
How could I reverse this to get a 3D world coordinate out of a 2D screen coordinate? I'm using this implementation of the D3DXVec3TransformCoord
function:
D3DXVECTOR3* WINAPI D3DXVec3TransformCoordImpl(D3DXVECTOR3* pout, CONST D3DXVECTOR3* pv, CONST D3DXMATRIX* pm)
{
FLOAT norm = pm->m[0][3] * pv->x + pm->m[1][3] * pv->y + pm->m[2][3] * pv->z + pm->m[3][3];
if (norm)
{
pout->x = (pm->m[0][0] * pv->x + pm->m[1][0] * pv->y + pm->m[2][0] * pv->z + pm->m[3][0]) / norm;
pout->y = (pm->m[0][1] * pv->x + pm->m[1][1] * pv->y + pm->m[2][1] * pv->z + pm->m[3][1]) / norm;
pout->z = (pm->m[0][2] * pv->x + pm->m[1][2] * pv->y + pm->m[2][2] * pv->z + pm->m[3][2]) / norm;
}
else
{
pout->x = 0.0f;
pout->y = 0.0f;
pout->z = 0.0f;
}
return pout;
}