Zooming in and out using gluLookAt|gluPerspective.

Here I’m drawing a model and I’m using gluLookAt to look at it. I have already implemented a panning function so that I can move the object through Cartesian coordinates. What I don’t know how to do is zooming in and out. I thought I would move the eye and the center forward for zooming in for example but it doesn’t work and I sort of understand why. How do I implement the zooming functionality? Can I use gluLookAt to implement it? If not, I have seen tutorials that use gluPerspective, but does that interfere with gluLookAt? I tried using both and some weird things kept happening.


glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glPushMatrix();
	gluLookAt(eye[0],eye[1],eye[2],center[0],center[1],center[2],up[0],up[1],up[2]);
	glPushMatrix();
	drawModel();
	glPopMatrix();
	glPopMatrix();

Zooming is usually realized by changing the field of view over time. For instance, to zoom in, you narrow your field of view and pass it to gluPerspective.

Can I use both gluLookAt and gluPerspective at the same time? Where exactly do I call them?

You typically would! gluLookAt establishes the VIEWING transform, while gluPerspective (glFrustum) establishes the PROJECTION transform. The former goes on the MODELVIEW matrix stack; the latter on the PROJECTION matrix stack. Frequently these are both set up at the top of you frame draw look just after you clear the screen.

VIEWING transform just establishes: where you are, where you are looking, and which direction is up. PROJECTION transform determines how big the viewing frustum is, distance to the screen, etc.

So as mentioned, you can “zoom” either by reducing the width/height of the frustum (in the PROJECTION transform), or you can move the eyepoint closer (the VIEWING transform) which, due to perspective foreshortening, will make the objects you move toward appear bigger.

Thank you very much. Very clear and helpful.