PDA

View Full Version : All Zoom

Vero
05-10-2001, 03:52 AM
Hello,
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???
Thanks,
Vero

chennes
05-10-2001, 07:44 AM
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).]

Vero
05-10-2001, 08:38 AM
The width and height of the viewport or the elements?

Doesn´t work... http://www.opengl.org/discussion_boards/ubb/frown.gif

chennes
05-10-2001, 10:15 AM
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... http://www.opengl.org/discussion_boards/ubb/smile.gif]

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

Leo
05-10-2001, 10:33 AM
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).]

Vero
05-11-2001, 01:37 AM
It works only to any sizes of the window(is resizeable)

How influence this windows size in the calculation ????

chennes
05-11-2001, 05:04 AM
Window size has no bearing here - use glViewport for that.

Chris

Vero
05-11-2001, 06:06 AM
I use glViewport, but it doesn´t work.

chennes
05-11-2001, 10:03 AM
Define "doesn't work" - what does it do?

Hull
05-11-2001, 10:53 AM
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?

Leo
05-12-2001, 06:40 AM
Try using gluLookAt().

gluLookAt(eyeposX, eyeposY, eyeposZ,
COPX, COPY, COPZ,
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.

Vero
05-14-2001, 08:51 AM
It works!!!! Thanks to everybody.