PDA

View Full Version : How to calculate optimal distance for camera?



Robbo
05-14-2002, 02:18 AM
Hi,


How can I calculate the optimal distance from a object for the camera eye-point such that all of the object is visible on screen (assume that the camera is axis aligned and that I only care about the x width of the object to look at).

As a follow up, given a cylinder of radius R, how can I calculate the optimal camera distance such that the end section (circular) of the cylinder is completely visible on screen?

Thanks for any help you may be able to give.

MickeyMouse
05-14-2002, 04:35 AM
hi!

if i understand you well you're trying to do following:
your camera has certain position and certain viewing angles...and there're some objects somewhere - you want to move camera forward or back so that all objects are visible and moving "a little bit" forward would cause that some of the objects are invisible

if that's what you mean, then for each vertex of your object you need to compute how much your present frustum requires to be moved (forward or backward) so that this vertex is in front of every frustum plane

say at the beginning you set move_length to -INFINITY, and then for each vertex you do:
move_length = max(move_length, length_required_for_this_vertex)

the only problem is now computation of length_required_for_this_point, but that's easy - just dot product + Pitagoras (more or less http://www.opengl.org/discussion_boards/ubb/smile.gif

sorry for not being user_not_familiar_with_frustum friendly in this post, if you're not take a look at some frustum culling tuts

[This message has been edited by MickeyMouse (edited 05-14-2002).]

gdelarosa
05-14-2002, 04:47 AM
First, find the ratio between the width of the object and the pixel width of the screen.

Then use this ratio to scale the view distance. This new distance is where you want to position the object in front of the eye point.

By the way,
View distance = (SCR_W/2) / tan(VIEWANGLE/2)

Jambolo
05-14-2002, 06:29 PM
Originally posted by Robbo:

How can I calculate the optimal distance from a object for the camera eye-point such that all of the object is visible on screen (assume that the camera is axis aligned and that I only care about the x width of the object to look at).


Assuming that you know the aspect ratio and the field-of-view (you must supply these to gluPerspective), and assuming that Y is up and you are looking down the -Z axis and the object centered:

d = w / 2. * aspect / tan( fov )

d is the distance to the widest part.



Originally posted by Robbo:

As a follow up, given a cylinder of radius R, how can I calculate the optimal camera distance such that the end section (circular) of the cylinder is completely visible on screen?

Thanks for any help you may be able to give.


Assuming you know the aspect ratio and field-of-view and the cylinder is centered on the camera's -Z axis:

if (aspect > 1)
d = r / tan( fov )
else
d = r * aspect / tan(fov)

Bounding sphere (in case you are wondering):

If the object is within a bounding sphere with a radius of r and the camera is pointing at the center of the sphere,

if (aspect > 1)
d = r / sin( fov )
else
d = r * aspect / sin(fov)

Note the bounding sphere uses sin rather than tan