All Zoom

I´m making a program to show different objects in OpenGL. The user can move by the scene. I want to know how make an “All zoom” , I mean, the user can view all the elements in the scene.
I know the camera properties and the width and heigth of the elements, how can I calculate the z of the camera to view all the objects???

If you know the viewing rectangle that you want (I assume you are using perspective - with ortho this is a irrelevant), you can compute it based on your viewing angle with just a little trig. Something like:
tan(angle/2) = maxof(width/2, height/2)/zCoordinate;

[I think the angle/2 is right, but it might just be angle…I can’t remember which is used in GL for the frustum]

[This message has been edited by chennes (edited 05-10-2001).]

The width and height of the viewport or the elements?

Doesn´t work…

The elements… or actually, a bounding box around all of the elements… perhaps I’ll try a picture:

  / | \ <- the angle from CL to here is
 /  |  \   the one I'm talking about...
/   |   \

/ |
/ |
/-------------<- half this length is the
/ <-frustum-> \ one you want to use…

[that’s almost what I wanted… ]

[This message has been edited by chennes (edited 05-10-2001).]

chennes answer will work, did you rearrange the equation?

the zCoordinate is the magnitude of the vector that is defined by the eye point and the camera target, the unnormalised view vector.

rearrange the equation by multiplying out “maxof(width/2, height/2)”. It’s max of element width or height, angle is the FOV of the camera.

maxof(width/2, height/2)/tan(FOV/2) = zCoordinate; // the length of the adjacent

Create a normalised vector and give it the same rotations as the view vector. Scale it by -(zCoordinate + 1). Put the eye coordinate here.

[This message has been edited by Leo (edited 05-10-2001).]

It works only to any sizes of the window(is resizeable)

How influence this windows size in the calculation ???

Window size has no bearing here - use glViewport for that.


I use glViewport, but it doesn´t work.

Define “doesn’t work” - what does it do?

It seems that you want to make sure every object you draw gets within the camera view.

Well this is not a task for glViewport because it sets just the window coords and extents. It does not extend the viewing frustum of your scene. (Small viewport just squeezes your scene into it. Using the same 3d space.)

What you have to do is to calculate the maximum bounding volume of all space that all your objects create in the rendered scene, and then set the camera position and projection to cover it all from some arbitrary angle.

I dont know if OGL can do this for you in anyway.

Maybe gluProject? Anyone?

Try using gluLookAt().

gluLookAt(eyeposX, eyeposY, eyeposZ,
0, 1, 0); /* positive Y up vector */

The eyepos is function is given above. COP = Center Of Projection, this, presumably, is the center of the bounded box that defines the extents of the scene. Don’t worry too much about the up vector until you’re more familiar with the concepts.

It works!!! Thanks to everybody.