Perspective Projection with OpenGL

John Cassidy picture John Cassidy · Feb 26, 2012 · Viewed 13.6k times · Source

I am confused about perspective projection.

Here is the scenario that is confusing me. My frustrum's front plane is basically positioned at at positive z-axis and the back plane at a negative axis and rotated about some deg along the posive z-axis.

Now, whenever I go through examples I see that the near and the far plane are all present in the negative z axis.

My approach in this case was basically something like:

glMatrixMode(GL_PROJECTION);    
glLoadIdentity();
glFrustrum(-xval,+xval,-yval,+yval,-10,+10);
gluLookAt(); // some point lying on this axis of symmetry about the point
glMatrixMode(GL_MODELVIEW);

So, in the above case what is the behavior that I should expect for the glFrustum with a negative as well as positive value for z. i.e. in this case -10 to +10.

Answer

Mandark picture Mandark · Feb 28, 2012

The view volume defines how much of the world you actually see, every object outside the view volume is not visible, and everything inside is potentially visible(if not obscured by an object in front of it).

The shape of the view volume is determined by the type of projection you use.

There are 2 types of projections ortographic and perspective projections.

Ortographic don't take distance from viewer into account (not natural but useful for CAD software etc.) and perspective projections which take the distance from the viewer into account and therefore objects that are further away appear smaller.

In an ortographic projection the view volume is a cube and in a perspective projection the view volume has the shape of a frustum (near clipping plane is smaller than the far clipping plane).

In either case the contents of the view volume are "projected" onto the near clipping plane.

Of course, the process is a little bit more involved than simple projection_matrix multiplication with every vertex in the view volume; but it's basically explained in almost every OpenGL related book.

The view volume shape models camera properties to some extent...how wide you see, how far you see etc.

The red book explains this well in the dedicated viewing chapter.

When talking about a frustum you imply you are using a perspective projection.

To construct an frustum you need 8 points and you'll generally want to construct a symmetrical frustum which simplifies the projection matrix.

Instead of specifying the 8 points directly by hand you'll use helper methods like gluPerspective(fovy,aspect_ratio,near,far) which uses "more intuitive" parameters for frustum construction; it's is basically a wrapper around glFrustum which calculates the 8 points for you and then calls glFrustum instead of you.

For OpenGL 3.0 and higher you'll provide your own implementation of gluPerspective, you can simply google the code.

Just remember projection makes 3D become 2D.

Regarding viewing, OpenGL uses a right-handed coordinate system (x axis points left, y axis point up and z axis point outward the screen.); DirectX uses a left-handed coordinate system.

Remember, in 3D space you only have 2 coordinate system orientations(left and right handed).

What makes the difference is the orientation of the z axis; if you make z axis point inwards it's a left-handed system or if you make z point outward it's a right-handed system.

Any other direction for the z axis wouldn't make the z axis perpendicular to the x and y axes and therefore you wouldn't have a coordinate system.

The handedness determines stuff like in which direction the positive rotation occurs and so on.

Your camera is located at the origin (0,0,0) and is facing down the -z axis by default; and it will be like that until you don't apply some kind of transformation.

Your view frustum is constructed around the viewing direction and the near clipping plane is located at z = -near from the camera which is located at the origin initially.

When you specify near and far as positive values to glFrustum you haven't specified their z location it just so happens that near clipping plane is located at z=-near because of the default orientation of the camera along the negative z axis and because the camera is at the origin (0,0,0)initially and your near clipping plane is always near away from the viewers eye position a.k.a camera position.

And whenever you change your view (change camera position or change camera orientation or both) your frustum will still be "wrapped around the new view direction" and the near clipping plane will still be "near" away from the new camera position.

Hopefully, you get the concept.

Frustum defines how much.

No matter where the camera stands and point to.