Game Development Reference
In-Depth Information
As with project, we want to compute the world/view/projection matrix, however, because
ourgoalistobringaworldspace coordinate intoscreen space, weneedtheinverse matrix.
We then transform our point p' by this matrix.
Finally we need to convert our vector into non-homogeneous coordinates, dividing by the
homogenizing factor r w .
r now holds the position in world space we were looking for.
vector3 Unproject(vector3 screenPos, matrix world, matrix view, matrix projection)
{
vector4 result;
result.x() = (2.f * (screenPos.x() - m_left) / Width()) - 1.f;
result.y() = 1.f - (2.f * (screenPos.y() - m_top) / Height());
result.z() = screenPos.z() - m_minDepth;
if ( IsEqual(m_maxDepth - m_minDepth, 0.f ) )
result.z() = 0.f;
else
result.z() = result.z() / (m_maxDepth - m_minDepth);
result.w() = 1.f;
matrix iwvp = matrix::Invert(world) * matrix::Invert(view) *
matrix::Invert(projection);
vector4 finalResult = vector4::Transform(result, iwvp);
finalResult /= finalResult.w();
return finalResult;
}
Search WWH ::




Custom Search