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 @   opencoder  阅读(289)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示