How to calculate the rectangle area of a projected sphere

To calculate the rectangle area of a projected sphere, here we could try with two different solutions:

1) project the center of sphere into screen first, then approximate the radius  in the screen space. (If you want to work with this way, please make sure that the center of the sphere should be located in the projection frustum. Because when the angle between the view direction and object to view point direction become very large, you will find that the projected screen point offset the normalized screen space too much. )

vector2D center = camera.worldToScreen(sphere.center);
float radius = sphere.radius / tan(camera.halfFOV) / distance(camera, sphere.center);
radius *= max(view_width, view_height);
vector2D min2D = center – vector2D(radius, radius);
vector2D max2D = center + vector2D(radius, radius);

 

2) With the view matrix, you could find out how your camera go up and right. That means you need to retrieve the camera up and right direction, then you need to combine them to find out how the min-max direction goes along in the camera view plane. Then you try to min – max your sphere center along this direction with the sphere radius, now you could get two point in world space. Then project those two points into screen space. Now you get it. The code as following:

vector3D dir = normalize( camera.upDirection() – camera.rightDirection() );
vector3D min = sphere.center – dir * sphere.radius;
vector3D max = sphere.center + dir * sphere.radius;
vector2D min2D = camera.worldToScreen(min);
vector2D max2D = camera.worldToScreen(max);

 

Some times, you may notice that some center point of a sphere was behind the camera. Now you need to mirror the center point against the camera view plane to the front of the camera first.

vector3D objToCamera = normalize( sphere.center – camera.location);
vector3D dir = normalize( objToCamera + 2.0 * camera.viewDirection() );
sphere.center = camera.location + dir * distance(camera.location, sphere.center);
posted @ 2012-10-28 08:17  opencoder  阅读(286)  评论(0编辑  收藏  举报