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?

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

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).]

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:
[b]
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.[/b]

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)