Given an object:

  • With the bounds [x, y, z, width, height, depth]
  • And an orthographic projection [left, right, bottom, top, near, far]

I want to determine the radius of a sphere which allows me to randomly place my camera on so that:

  1. The object is fully visible from all positions on this sphere
  2. The sphere radius is the smallest possible value while still satisfying 1.

Assume the object is centered around the origin.
How can I find this radius?

I'm currently using sqrt(width^2 + height^2 + depth^2) but I'm not sure that's the correct value, as it doesn't take the camera into account.

Thanks for any advice.

I'm sorry for confusing a few things here. My comments below should clarify what I'm trying to do actually.

  • \$\begingroup\$ have you tried searching in math.stackexchange.com I think this question would be better suited there. \$\endgroup\$
    – gardian06
    May 21 '12 at 14:38
  • 2
    \$\begingroup\$ Are you sure you mean an orthographic projection? Not a perspective one? Given the properties of orthographic projections, you'll get the same object proportions with whatever sphere radius you set, so there's no answer to this question as-is. \$\endgroup\$ May 21 '12 at 19:13
  • \$\begingroup\$ @lorancou: that is not true since the camera location will affect the viewing angle \$\endgroup\$ May 21 '12 at 22:47
  • \$\begingroup\$ @SamHocevar True, but the correct answer for an orthographic essentially doesn't take the camera into account - all points along the camera's view axis are equivalent since an orthographic projection has no 'FOV' associated with it; for instance, if your camera is looking parallel to the Z axis, then it makes no difference if it's at (0, 0, -10) or (0, 0, -10000). The 'placement sphere' doesn't make sense for an orthographic projection. \$\endgroup\$ May 21 '12 at 23:27
  • \$\begingroup\$ Indeed the viewing angle still matters, but as Steven said, changing the sphere radius / viewing distance is pointless. \$\endgroup\$ May 22 '12 at 13:09

The answer depends on the FOV of your camera (which is why I'm also wondering about lorancou's comment about whether you want orthographic or perspective projection). If your FOV (the smaller of your two FOVs, technically, if your viewport is rectangular) is θ (from top to bottom), then the angle from the center of your viewport to the top of your viewport is θ/2. Now, the 'maximal radius' of your object itself is r=sqrt(w2+l2+h2) (w idth, l ength, h eight), so what you want is the distance away from your object's center such that that radius is less than the (half-height) of your projected viewport. Since at a distance d from the center of your object the 'height' of your projection will be d*sin(θ/2), you can find your distance d by just setting d=r/sin(θ/2).

  • \$\begingroup\$ I'm using an orthographic camera and I confused a few things in my initial question. Sorry about that, but thanks for that answer as it will come in handy later on :) \$\endgroup\$
    – BengtR
    May 22 '12 at 21:14

You specify the projection transformation but not the view transformation. I will assume that the camera simply looks in the z direction. I will also assume that you want the sphere radius to be the largest possible, not the smallest (otherwise, just choose radius = 0).

Note that since you have an orthographic projection, the z coordinates do not really matter apart from ensuring that the object fits inside the near and far planes. Your scene will look exactly the same whatever the z coordinates of your objects are. So your problem becomes a matter of fitting your object's rectangle boundaries inside the camera's viewport rectangle.

Because the problem is symmetric, it is obvious that if such a sphere exists, its x and y coordinates are precisely your object's x and y coordinates. Then, the radius is:

radius = min((right - left - width) / 2,
             (top - bottom - height) / 2,
             (far - near - depth) / 2);

Note that the third term (far - near - depth) is probably superfluous. You really only care about the x and y directions.

And the camera coordinates:

cx = x
cy = y
cz = z - depth/2 - near - radius;

Again, the z value is probably not that important and you could just move your camera on a disc rather than a sphere.

  • \$\begingroup\$ I'm not sure I understood correctly, or maybe I just wasn't clear what I mean. I need the radius of the sphere which makes sure that regardless of the point I pick on the sphere, I can see the object without clipping. This is for gluLookAt(sphere_x, sphere_y, sphere_z, 0, 0, 0, 0, 1, 0). \$\endgroup\$
    – BengtR
    May 21 '12 at 18:14
  • \$\begingroup\$ @BengtR: okay, then my assumption that the view vector is (0,0,1) no longer stands and you can indeed ignore most of what I wrote. I'll try to rework on an answer. \$\endgroup\$ May 21 '12 at 18:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .