0
101 Feb 09, 2003 at 06:48

Hello,

Whats the best way to zoom in and out.

Thanks for helping

#### 5 Replies

0
101 Feb 09, 2003 at 09:43

there is no way to really ‘zoom’ in OpenGL. The camera stays at 0, 0, 0 looking along the z axis. Everything else moves around it. The best way to zoom out object a is to translate it further away, eg.

glTranslatef(APosX, APosY, APosZ);
glTranslatef(0.0f, 0.0f, -Zoom);
DrawObjectA();

then as zoom increases, the object gets further away

watch out though, anything drawn after that will be ‘zoomed’ as well.

0
101 Feb 09, 2003 at 11:00

Actually u can zoom with OpenGL by altering ur viewing matrix using the gluPerspective(..) function.

The actuall function parameter is as follows:

void gluPerspective(
float angle, // the viewable anlge infront of the camera/eye
float aspect, // typically width/height of window or screen
float near, // min distance from camera that is renderable
float far, // max distance from camera that is renderable
)


To do zooming the first parameter is the one u wnat to alter. The smaller the angle u specify the greater the zoom. Try experimenting and u’ll see this in action.

The advantage of using this technique over the one above is that u won’t get into a situation where u zoom past an object because u aren’t actually repositioning any objects.

The disadvantage is that everytime u want to zoom or animate zooming and zooming out u have to recreate ur perspective viewing transformation. Well this isn’t too bad.

0
101 Feb 09, 2003 at 19:26

the non-glu way is to use glOrtho and glMatrixMode

here are references to both:

http://www.cevis.uni-bremen.de/\~uwe/opengl…MatrixMode.html

example:

void window_scene2D()
{
glViewport(left, top, width, height);
glOrtho(left, right, bottom, top, near, far);
glMatrixMode(GL_PROJECTION);
}
/////////////////////////////////////////
void window_scene3D()
{
glViewport(left, top, width, height);
glMatrixMode(GL_MODELVIEW);
glOrtho(left, right, bottom, top, near, far);
}


remember, in glMatrixMode, you can only get the effect of zooming in or out with GL_MODELVIEW. only use GL_PROJECTION to do non-zooming effects like a menu system or the like.

If I offended you with any of this, please accept my apologies. I’ve seen way too many questions as to why people can’t zoom in and out and when they show their code, they’re using GL_PROJECTION.

cheers.

0
101 Feb 10, 2003 at 13:16

@Yau

The disadvantage is that everytime u want to zoom or animate zooming and zooming out u have to recreate ur perspective viewing transformation. Well this isn’t too bad.

Correct me if i’m wrong, but performance-wise it’s usually much heavier to recalculate the view (projection e.t.c) matrices than perform translations in model-space. I’m not quite sure about the actual performance impact, but, well, fps’s don’t do it that way :-)

0
101 Feb 10, 2003 at 15:48

interesting point.

the fewer the number of objects in space, the easier it is to translate the objects instead of recalculating the perspective matrix. the greater the number of objects, the more likely that it will be eaiser to recalc the perspective matrix than it is to translate all the objects.

of course, you can come up with algorithms the decide which of the two OR maybe even make it so that if you just translate, to only translate in the view frustum.

cheers.