gluLookAt eyeZ not working as excpected

Tag: opengl , glu Author: crmhost Date: 2012-04-07

I'm having some trouble with the eyeZ value of gluLookAt.

The way I'd imagine it to work is like moving a camera further away, thus shrinking the object in your field of view.

I have a simple setup with a simple shape in 3d space draw via glDrawElements with an 100x100x100 ortho where 0, 0, 0 is the center of the universe. The object is at 0, 0, 0.

I'm trying to make it so when you scroll the mouse wheel you get further away/closer to the object. Here's how glulookat is called.

float eyeX = 0;
float eyeY = 0;
float eyeZ = differenceInMouseWheel();

float centerX = 0;
float centerY = 0;
float centerZ = 0;

float upX = 0;
float upY = 1;
float upZ = 0;

gluLookAt(eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);

The only thing changing here is eyeZ.

The effect is strange, I scroll for about 10 seconds and then suddenly half of the object disappears. From there more and more of it disappears. This is probably because the camera is going out off into the 50 z distance limit, but I can't understand why the object doesn't scale like it would in 3D space.

Maybe I'm misunderstanding how the center values work?

I've also tried applying differenceInMouseWheel() to centerZ but that changed nothing, I'm going to assume the center values are just so glu can get a direction and nothing more.

Maybe the up vector should change? I don't know at this point.

Best Answer

You are using an orthographic projection. This means that no matter how great the distance, your objects will always appear to have the same size. Your object will disappear once it reaches the far clipping plane however, which is what you are seeing when you scroll for a long time.

You have two options: Either you use a perspective projection or you implement a zoom by modifying the orthographic projection matrix like so:

Let zoom be in (0, 1], and let viewport be a rectangle that is set to your current viewport. Let near be your near clipping plane distance and far be your far clipping plane distance.

glOrtho(zoom * viewport.width / 2, zoom * viewport.width / 2, zoom * viewport.height / 2, zoom * viewport.height / 2, near, far);

Other Answer1

Are you using a perspective projection matrix, or an orthographic one? If you don't use a perspective matrix the object's wont appear to change in size as you move the camera around.