views:

69

answers:

2

Hi again,

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 =  world radius * cot(fov / 2) / Z

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

result is in clipspace, apply viewport 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);
A: 

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.

Brandorf
I've been told that it is possible to extract it easily as long as there is 'no rotation involved' ... but i haven't had time to investigate there.
Florian
@Florian - whoever told you that, ask them how. :-)
LarsH
A: 

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.

LarsH
Actually i do this at the moment ... but i don't think that's the best way :(
Florian
@Florian, see updated answer...
LarsH
ah ... that's a good idea ... should have had that one myself :)
Florian