Vector4 Transforms::screenSpaceToNDCSpace(const Point& screenPoint) { CSize screenDimensions = GraphicsInterface::screenSize(); Vector4 screenSize = Vector4((float)screenDimensions.width, (float)screenDimensions.height, 0, 0); Vector4 screenSpace((float)screenPoint.x, (float)screenPoint.y, 0, 1); Vector4 ndcSpace = screenSpace / screenSize; ndcSpace.y = 1.0f - ndcSpace.y; ndcSpace = ndcSpace * 2.0f - 1.0f; ndcSpace.z = screenPoint.z; ndcSpace.w = 1; return ndcSpace; }
// Project the mouse cursor from screen space to object space void EditExt::ProjectScreenToWorld(D3DXVECTOR3* pOut, float screenX, float screenY, float worldZ) { D3DXVECTOR3 lineBegin, lineEnd; // Unproject the near and far points given by the screen X,Y coords D3DXVECTOR3 screenSpace(screenX, screenY, 0.0f); D3DXVec3Unproject(&lineBegin, &screenSpace, &viewport, &projection_matrix, &view_matrix, &worldMatrix); screenSpace.z = 1.0f; D3DXVec3Unproject(&lineEnd, &screenSpace, &viewport, &projection_matrix, &view_matrix, &worldMatrix); // Using a plane intersection, we can determine the object space coordinates of the screen space coords // at a certain Z depth, intersecting the line given above. orig.z = worldZ; D3DXPlaneFromPointNormal(&plane, &orig, &normal); D3DXPlaneIntersectLine(pOut, &plane, &lineBegin, &lineEnd); }
Vector4 Transforms::screenSpaceToViewSpace(const Matrix4x4& projInv, const Point& screenPoint) { CSize screenDimensions = GraphicsInterface::screenSize(); Vector4 screenSize = Vector4((float)screenDimensions.width, (float)screenDimensions.height, 0, 0); Vector4 screenSpace((float)screenPoint.x, (float)screenPoint.y, 0, 1); Vector4 ndcSpace = screenSpace / screenSize; ndcSpace.y = 1.0f - ndcSpace.y; ndcSpace = ndcSpace * 2.0f - 1.0f; ndcSpace.z = screenPoint.z; ndcSpace.w = 1; Vector4 viewSpace = projInv * ndcSpace; viewSpace /= viewSpace.w; return viewSpace; }