3

I wish to know the best algorithm to be able to zoom to a screen space selection box/window in 3D.

I have it currently sort of working but it doesn't seem to zoom in correctly when the distance from the target is significant.

Currently it works by figuring out the scale of the selection box relative to the viewport width/height and applies that scale to the distance/range of the camera to it's target.

2
  • Can you explain in more detail what you mean by "zoom to a screen space selection box?" May 11, 2012 at 7:04
  • What I mean by screen space selection box is a rectangle, or window, drawn by the user which is at screen space and not world space. I wish to then have the camera zoom into the region that is selected.
    – zman
    May 14, 2012 at 9:42

2 Answers 2

15

Actually it's not that simple. Look at this picture (it's a 2D projection for simplicity):

frustum1

The blue area is the current camera's view frustum. The yellow area is a part of the blue frustum covered by the rectangular selection of the screen. The aim is to make a new frustum which best represents the yellow area. The problem is what way should the new frustum fit the yellow area.

One possibility is presented in the picture below:

frustum2

The new camera's view frustum is coloured purple and the camera's eye lies on the green line. Assuming the new camera has the same properties (fovy, znear, zfar, aspect) as the old one, we can compute its new position and direction.

Now some calculations:

Height and width of the near plane:

h = 2 * tan(fovy/2) * znear
w = aspect * h

Screen-space coordinates of the rectangle:

rectangle = ( x0, y0, x1, y1 )

Screen-space center of the rectangle:

rcenter = ( (x0+x1)/2, (y0+y1)/2 )

Another image to clarify next calculations:

frustum3

View-space vector lying on the near plane, pointing from the near plane's center to the rectangle's center:

center = ( (rcenter.x / screen_width  - 0.5) * w,
           (0.5 - rcenter.y / screen_height) * h, 0 )

Then we have to transform this vector to world-space:

centerWS = center.x * camera_right_dir + center.y * camera_up_dir

New direction of the camera (dir2n):

dir1 = camera_dir * znear
dir2 = dir1 + centerWS
dir2n = normalize(dir2)

New position of the camera (pos2):

I've made some assumptions, in order to simplify calculations. Approximately new and old near planes are parallel, so:

(w, h) / dist = (w * (x1-x0)/screen_width, h * (y1-y0)/screen_height) / znear
(1, 1) / dist = ( (x1-x0)/screen_width, (y1-y0)/screen_height) / znear
(1/dist, 1/dist) = ( (x1-x0)/screen_width, (y1-y0)/screen_height) / znear

Hence:

dist = znear * screen_width / (x1-x0)

Which should be equal to:

dist = znear * screen_height / (y1-y0)

That is true only if the rectangle has the same proportions as the screen, which you can guarantee by locking the rectangle's proportions while the user is drawing it, or you can simply use only the rectangle's width (x1-x0) and ignore its height (y1-y0) or vice versa.

Finally:

pos2 = pos1 + dir2n * (dist-znear)
11
  • Awesome explanation, I have yet to try it but I will accept this answer until proven incorrect ;) I was going to post a picture of the problem I was facing but I didn't have enough stackoverflow points so I was hoping that someone understood what I meant.
    – zman
    May 14, 2012 at 9:37
  • I have already forced the rectangle to be the same proportions as the screen so that will make things easy. The only other issue I will have is that we do support orthographic as well as perspective rendering so the maths for orthographic will be slightly different.
    – zman
    May 14, 2012 at 9:46
  • For the ortographic rendering that will be much easier actually ;)
    – miloszmaki
    May 14, 2012 at 11:02
  • I implemented this today, but it appears that the distance calculated does not work for me (or I'm doing something wrong). For example if my near plane is 1, and my screen height is 668, and y1-y0 = 1 (i.e. a tiny rectangle), then my max distance is only 668 metres. This for me should be hundreds of kilometres. Not sure what I need to do to fix this :(
    – zman
    May 16, 2012 at 9:46
  • It really depends on how close the camera is to it's target. That must be what's missing. If the target is far then a selection will move the camera a greater distance than if the target were really close.
    – zman
    May 16, 2012 at 10:50
1

What I understand is that you a rectangle drawn on screen to align with the window boundaries and have everything inside that rectangle zoom in.

If I am right, what we are trying to achieve here is to align the near-plane with the an imaginary plane on which the rectangle was drawn.

To do this, we have to play around with some trigonometry. Assuming we align top and bottom of the rectangle:

We should compute the height of the near plane as:
H = tan(fovy/2) * nearPlane; --------(1)

height of the drawn rectangle on near plane:

fraction = WindowHeight / rectangle height;  
h = H * fraction; ---------(2)

To align near plane with the imaginary plane, camera must move forward. Assume D is the distance to move forward, then by geometry,
h / nearPlane = H / (nearPlane+D) --------(3)

Using equation(2), equation (3) can be reduce as:

fraction / nearPlane = 1 / (nearPlane+D)

which gives D as:

D = (nearPlane / fraction)  - nearPlane; 

or

D = nearPlane * (1-fraction)/fraction;

Now move camera forward by D. and that should do it.

If the rectangle was not center aligned, computation is slightly more complex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.