View Full Version : Camera magic?

06-22-2011, 08:18 PM
I understand that there isn't really a such thing as moving a camera around in OpenGL. Still, something odd is happening. I'll provide three chunks of code.

My viewing range.


This is in the display function. It changes how close the model is to the screen.


Stuff used to increase the value of the variable, zoom, which should bring the model closer.

case 'W':
case 'w':

However, when I press W, instead of the model getting closer (or further away), parts of it may disappear. I know why the parts disappear. It's because the coordinates inside it go out of range after the translation (I've also used glScalef).

My question is... how would I manipulate the model in a way that makes the camera appear to be getting closer without moving it out of the range? (Making the range larger just makes the model appear smaller, so things still balance out and I still get the problem.)


06-22-2011, 09:01 PM
If you want the typical perspective effect of closer objects appearing larger, you should use glFrustum to set your viewing volume, instead of glOrtho.

With orthographic projection, the object will be the same apparent size on the screen, regardless of the distance from the near plane. glFrustum provides a perspective projection that "makes the camera appear to be getting closer". You still have to be careful about keeping the object inside the view volume, though.

06-23-2011, 08:54 AM
Thank you for your time. However, when I use glFrustum with the aforementioned coordinates, I get a black screen. Might there be a way to just update the camera position and actually have it move in so the range isn't ever exceeded? Even trying gluLookAt(0, 0, zoom, 0, 0, 0, 0, 1, 0); makes the model get nowhere closer/further. It just disappears after so long.

06-23-2011, 01:23 PM
You have to use different coordinates for glOrtho and glFrustum, due to their different type of projection. In particular, for glFrustum, both the near and the far plane must be in front of the eye.

If you want things to get bigger/smaller with distance, glOrtho will never to that. glFrustum will, provided you supply the correct parameters.

I recommend taking the time to understand what orthographic projection and perspective projection are in general (i.e. look at some diagrams of how points are projected to the page/screen). Then the reasons why you need to set the boundaries in a particular way will make more sense.

The OpenGL "Red Book" has illustrations of how these different types of projections work, and how they relate to the OpenGL API calls. Or you can try to find a website that gives diagrams also. You might take a look at this (http://www.arcsynthesis.org/gltut/Positioning/Tut04%20Perspective%20Projection.html). (Edit: That link uses only the "core" profile from newer OpenGL releases, so it doesn't use the glOrtho and glFrustum commands, but the ideas are the same).

06-30-2011, 03:14 AM
With orthogonal projection:
- Simplest way to "zoom" is to making viewing range smaller. Settings for left, right, bottom and top directly choose what will be rendered (if you are not scaling).
- Translation along Z will only affect rendering results through near and far plane clipping. This is not zooming in any way, it is only selecting which Z slice will be visible (and what is not).