开发者

Radius of projected Sphere

i want to refine a previous question:

How do i project a sphere onto the screen?

(2) gives a simple solution:

approximate radius on screen[CLIP SPACE] = world radius * cot(fov / 2) / Z

with:
fov = field of view angle
Z   = z distance from camera to sphere

resu开发者_如何学运维lt is in clipspace, multiply by viewport size to get size in pixels

Now my problem is that i don't have the FOV. Only the view and projection matrices are known. (And the viewport size if that does help)

Anyone knows how to extract the FOV from the projection matrix?

Update:

This approximation works better in my case:

float radius = glm::atan(radius/distance);
radius *= glm::max(viewPort.width, viewPort.height) / glm::radians(fov);


I'm a bit late to this party. But I came across this thread when I was looking into the same problem. I spent a day looking into this and worked though some excellent articles I found here: http://www.antongerdelan.net/opengl/virtualcamera.html

I ended up starting with the projection matrix and working backwards. I got the same formula you mention in your post above. ( where cot(x) = 1/tan(x) )

radius_pixels = (radius_worldspace / {tan(fovy/2) * D}) * (screen_height_pixels / 2)

(where D is the distance from camera to the target's bounding sphere)

I'm using this approach to determine the radius of an imaginary trackball that I use to rotate my object.

Btw Florian, you can extract the fovy from the Projection matrix as follows:

If you take the Sy component from the Projection matrix as shown here:

Sx  0   0   0
0   Sy  0   0
0   0   Sz  Pz
0   0  -1   0

where Sy = near / range

and where range = tan(fovy/2) x near

(you can find these definitions at the page I linked above)

if you substitute range in the Sy eqn above you get:

Sy = 1 / tan(fovy/2) = cot(fovy/2)

rearranging:

tan(fovy/2) = 1 / Sy

taking arctan (the inverse of tan) of both sides we get:

fovy/2 = arctan(1/Sy)

so,

fovy = 2 x arctan(1/Sy)

Not sure if you still care - its been a while! - but maybe this will help someone else.


Update: see below.

Since you have the view and projection matrices, here's one way to do it, though it's probably not the shortest:

  • transform the sphere's center into view space using the view matrix: call the result point C
  • transform a point on the surface of the sphere, e.g. C+(r, 0, 0) in world coordinates where r is the sphere's world radius, into view space; call the result point S
  • compute rv = distance from C to S (in view space)
  • let point S1 in view coordinates be C + (rv, 0, 0) - i.e. another point on the surface of the sphere in view space, for which the line C -> S1 is perpendicular to the "look" vector
  • project C and S1 into screen coords using the projection matrix as Cs and S1s
  • compute screen radius = distance between Cs and S1s

But yeah, like Brandorf said, if you can preserve the camera variables, like FOVy, it would be a lot easier. :-)

Update: Here's a more efficient variant on the above: make an inverse of the projection matrix. Use it to transform the viewport edges back into view space. Then you won't have to project every box into screen coordinates.

Even better, do the same with the view matrix and transform the camera frustum back into world space. That would be more efficient for comparing many boxes against; but harder to figure out the math.


The answer posted at your link radiusClipSpace = radius * cot(fov / 2) / Z, where fov is the angle of the field of view, and Z is the z-distance to the sphere, definitely works. However, keep in mind that radiusClipSpace must be multiplied by the viewport's width to get a pixel measure. The value measured in radiusClipSpace will be a value between 0 and 1 if the object fits on the screen.

An alternative solution may be to use the solid angle of the sphere. The solid angle subtended by a sphere in a sky is basically the area it covers when projected to the unit sphere.

Radius of projected Sphere

The formulae are given at this link but roughly what I'm doing is:

if( (!radius && !distance) || fabsf(radius) > fabsf(distance) )
  ; // NAN conditions. do something special.

theta=arcsin( radius/distance )
sphereSolidAngle = ( 1 - cosf( theta ) ) ; // not multiplying by 2PI since below ratio used only
frustumSolidAngle = ( 1 - cosf( fovy / 2 ) ) / M_PI ; // I cheated here. I assumed
// the solid angle of a frustum is (conical), then divided by PI
// to turn it into a square (area unit square=area unit circle/PI)

numPxCovered = 768.f*768.f * sphereSolidAngle / frustumSolidAngle ; // 768x768 screen
radiusEstimate = sqrtf( numPxCovered/M_PI ) ; // area=pi*r*r

This works out to roughly the same numbers as radius * cot(fov / 2) / Z. If you only want an estimate of the area covered by the sphere's projection in px, this may be an easy way to go.

I'm not sure if a better estimate of the solid angle of the frustum could be found easily. This method involves more comps than radius * cot(fov / 2) / Z.


The FOV is not directly stored in the projection matrix, but rather used when you call gluPerspective to build the resulting matrix.

The best approach would be to simply keep all of your camera variables in their own class, such as a frustum class, whose member variables are used when you call gluPerspective or similar.

It may be possible to get the FOVy back out of the matrix, but the math required eludes me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜