zooming - with glulookat()?

hi guys,

i’ve read several posts and tutorials regarding zooming in opengl now and yet i am still not able to get it to work.
what i have: left bottom corner is at (-1, -1), right top corner is at (1, 1), and my code draws some simple GL_POINTS (all of them at z = 0); my canvas dimensions are (1024, 768).
what i want to do: a simple zoom by a defined factor.

currently i’m trying this approach, i have trouble understand which argument values to use though. if i try it like this, i see only the background color after a repaint.

my code right now:


	zoom -= 0.1;
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective (zoom, 1024 / 768, 1, 3); 
 
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0, 0, 0, 0, 0, 0, 0, 1, -1); // eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz

what is the easiest way achieve this?

thanks :slight_smile:

What was the value of “zoom” before you subtracted 0.1 from it?

This places both the eye and the “look at” point at the origin, resulting in the forward vector being undefined, resulting in the matrix being undefined. These two points need to be different.

If you’re drawing points in the z=0 plane, eyeZ needs to be non-zero and centerZ-eyeZ needs to have the opposite sign to eyeZ so that you’re looking toward the z=0 plane.

thanks for the reply. the original value of zoom was/is 1.

anyway, i tried another approach, and heureka - it works, at least kinda. the remaining problem is that the painted objects are only visible at every second zoom, which seems pretty strange to me. the code:

        // Set gluPerspective (once)
 	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective (90, w / h, 1, 3); 
	glOrtho(-1, 1, -1.0, 1, 1, 3); 

        // Do this every time the user wants to zoom
        zoom = 1 + event->delta() / MOUSEWHEEL_DELTA_NORMALIZATION_FACTOR;	
	glOrtho(-zoom, zoom, -zoom, zoom, 1, 3);
	glMatrixMode( GL_MODELVIEW );

as i said, the zooming per se works. it’s just that the painted content seems to disappear with every second zooming operation (visible at first - without zooming -, invisible after first zooming, visible after second zooming, …). any ideas there?

[EDIT] maybe the problem is related to glMatrixPush() / glMatrixPull()? that’s just a suspicion though.

Using both gluPerspective() and glOrtho() doesn’t make much sense. Normally you’d use one or the other, but not both.

The glOrtho() call generates the matrix


1  0  0  0
0  1  0  0
0  0 -1  2
0  0  0  1

which is equivalent to calling


     glTranslatef(0, 0, 2);
     glScalef(1,1,-1);

Note that this flips the Z axis, which effectively turns the scene inside-out (e.g. front and back faces will be swapped for lighting and culling.

This flips the Z axis with each call.

Normalised device coordinates have +Z into the screen and -Z out of the screen. All of the standard projection matrices (glFrustum, glOrtho, gluPerspective, gluOrtho2D) place a negative value at matrix[2][2] to negate the Z coordinate so that the default coordinate system is right-handed (i.e. +Z is out of the screen).

I suggest reverting to your original example and just setting centerZ correctly.

Also, you need to decide what you mean by “zoom”. The zoom on a camera works by changing the field-of-view angle. In computer graphics, it can be used to mean that, but it can also mean scaling, or it can mean moving toward or away from a target object (with a perspective projection, this will change the size of the object on screen).

Each of those are different things, and the means to achieve them is different. Changing the field-of-view angle involves changing the first parameter (fovy) in a gluPerspective() call. Scaling is achieved via glScale(). Moving the viewpoint is achieved by changing the first three parameters (eyeX/Y/Z) in a gluLookAt() call or by calling glTranslate() (either appended to the projection matrix or prepended to the model-view matrix).

ah, ok…the flipped Z axis explains that “flicker”.

Also, you need to decide what you mean by “zoom”.

honestly, i didn’t put much thought into that. any working method suits me just fine.

I suggest reverting to your original example and just setting centerZ correctly.

thanks for your help! centerZ and eyeZ were the problem, it works flawlessly now. :slight_smile: