Projecting a World Point to the Display

From C4 Engine Wiki

Jump to: navigation, search

The following code snippet returns a point in display space for a given point in world space.

Point3D ProjectWorldPointToDisplay(const FrustumCamera *camera, const Point3D& inWorldPoint)
{
    // Transform the world point into camera space
    Point3D displayPoint = camera->GetInverseWorldTransform() * inWorldPoint;
 
    // Project the point onto the focal plane
    const FrustumCameraObject *object = camera->GetObject();
    float focalLength = object->GetFocalLength();
    displayPoint.x *= focalLength / displayPoint.z;
    displayPoint.y *= focalLength / displayPoint.z;
 
    // Rescale to the width and height of the display
    float w = (float) (TheDisplayMgr->GetDisplayWidth() / 2);
    float h = (float) (TheDisplayMgr->GetDisplayHeight() / 2);
    displayPoint.x = w * displayPoint.x + w;
    displayPoint.y = h * displayPoint.y / object->GetAspectRatio() + h;
 
    return (displayPoint);
}
Personal tools