CP2060 - Lecture 14 - OpenGL 3D - The Camera

Timely Task Tidbits:

  • Set the fps to 15 in the Scene Editor
  • Make a backup in a different physical location

The Camera

In OpenGL, "vertices and normals are transformed by the modelview and projection matrices before they're used to produce an image in the framebuffer" (taken from your OpenGL help file - use it).

  • The modelview matrix transforms points, lines, polygons, and raster positions from object coordinates to eye coordinates.
  • The view volume shape is set up by the projection matrix.

To make this a perspective projection (unlike the orthographic we have been using so far), we use the command gluPerspective(), applied to the projection matrix:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(viewAngle, aspect, nearplane, farplane);

Hill - Figure 7.1, page 360

The viewangle is theta in diagram b) above, and the nearplane and farplane parameters are obvious from these pictures.
Part c) of the diagram shows a point P in the world being mapped to P' in the world window (on the near plane), to be shown in the viewport on the screen.

In order to get different views of a scene, it is possible to move the camera around, rotating and translating it, which form part of the modelview matrix. This is done with gluLookAt();

Hill - Figure 7.3 - camera coordinate system

It is helpful to give the camera its own coordinate system, as described by the figure above. u, v and n are simply clones of x, y and z. To orient our camera, we move and rotate these axes.

Relative movements can be done by setting the camera's pitch, roll and heading (a change in heading is referred to as yaw) - see figure below.

Hill - Figure 7.4 - camera/plane analogy

 

The following diagram should be helpful in visualising the relationship between gluLookAt(eye,look,up) and the camera coordinates u,v and n.

Hill - Figure 7.7 - u,v and n / eye, look, up

Given the points eye and look and the vector up:

n - parallel to the vector created by eye - look.

u - perpendicular to up and n. i.e. u = up X n.
  (up x n (rather than n x up) points right (*tip)).

v - perpendicular to n and u. i.e. v = n X u.

Check with eye = (-2,2,0), look = (0,0,0) and up = (0,1,0) that:
n = (-2,2,0), u = (0,0,2) and v = (4,4,0).

Normalizing these three vectors will give us our camera's coordinate system.

gluLookAt() then transforms the camera's coordinate system into the generic position for the camera (in x, y and z).
Hill, page 365 contains the matrix that does this transformation, and this is coded in the function setModelViewMatrix() of the camera class.

We create a class 'camera', which is given to you in the code at the bottom as camera.cpp & camera.h.

To move our camera in a scene, we can add functions to the camera class for slide (moving the camera forwards or backwards), roll, pitch and yaw. After each of these adjustments, setModelViewMatrix() is called, which loads the new (camera) matrix into the modelview matrix.
Inspect the code to see how they work.

 


Tip:

To remember which order to do the cross product in, you can use your right hand coordinate system and know:

Fore finger X Middle finger = Thumb.

i.e. y X z = x.


 

The source code example below was compiled from various parts of the text to implement a movable camera in conjunction with the mesh loader of chapter 6. It uses several files to hold the various classes and functions required.
Download all of the files and then open the workspace (.dsw):

 

 

 Announcements

Lecture notes are provided as a guide of the content of lectures for pre-reading, revision and for students who cannot attend lectures. While almost every effort is made to make the online notes as complete as possible, there is always material discussed in lectures that is not in the notes.
You should not rely on these notes only!
If you miss a lecture and you do not understand something in the notes, you should consult (in this order) other students, the tutors, or the lecturer to try to work it out.

You are expected to read these lecture notes as well as attending lectures, as vital information is contained in both.

 

 Subject Coordinator: Lindsay {w} | {e}.

Last Update: September 14, 2001