INT32GP Graphics Programming: Tutorial #12
3D Perspective Projection

B1.11 3:00 pm Tue 03 Sep 02
B1.11 12 midday Wed 04 Sep 02

Make sure you have made the following changes to your program from Tutorial #10.

Insert the following function above display().
void setCamera(){
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   gluLookAt(eye[x], eye[y],eye[z],
             centre[x],centre[y],centre[z],
             up[x],up[y],up[z]);
}
Call setCamera() from the top of display(). Insert the following function above reshapeWindow.
void setProjection(){
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(world[xLeft],  world[xRight],
           world[yBottom],world[yTop],
           world[zNear],  world[zFar]);

}
Call setProjection() from the bottom of reshapeWindow(). So that the translation is better behaved, change the code in specialKeys() to the following. Move the GLUT object around noting how it "disappears" as it moves outside the clipping volume.
void specialKeys(int key,int xMouse,int yMouse){
   int i;
   switch(key){
      case GLUT_KEY_PAGE_UP  : t[teapot][z]+= 0.1;break;
      case GLUT_KEY_PAGE_DOWN: t[teapot][z]+=-0.1;break;
      case GLUT_KEY_RIGHT    : t[teapot][x]+= 0.1;break;
      case GLUT_KEY_LEFT     : t[teapot][x]+=-0.1;break;
      case GLUT_KEY_UP       : t[teapot][y]+= 0.1;break;
      case GLUT_KEY_DOWN     : t[teapot][y]+=-0.1;break;
   }
   glutPostRedisplay();
}
Add Perspective
See Lecture #13 for a discussion of perspective projection. Add a keyboard facility to toggle between orthographic and perspective projection.
  1. Define two global variables.
    int perspective = FALSE;
    GLdouble fovy = 60;
  2. Alter setProjection().
    void setProjection(){
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(!perspective)
    glOrtho(world[xLeft], world[xRight],
    world[yBottom],world[yTop],
    world[zNear], world[zFar]);
    else
    gluPerspective(fovy,1,world[zNear],world[zFar]);
    }
  3. Toggle perspective projection from the keyboard() function. Note the difference between perspective and orthographic projection.
    case 'p': perspective=!perspective;
    setProjection(); break;
Transforms
  1. Provide a keyboard facility to rotate around the y-axis.
  2. Provide keyboard facilities to rotate around the x and z-axes.
  3. Provide keyboard facilities to scale the teapot to half size, in response to the keyboard.
Camera Control
Add keyboard facilities to control the eye of the camera.
case 'x': eye[x]+=0.1; break;
case 'X': eye[x]-=0.1; break;
case 'y': eye[y]+=0.1; break;
case 'Y': eye[y]-=0.1; break;
case 'z': eye[z]+=0.1; break;
case 'Z': eye[z]-=0.1; break;

Move the camera around. Move it inside the object and rotate the object around it.
Add a keyboard facility to change fovy the field of view of the camera. This is like changing the lens or zooming.
case 'f': if(fovy <180) fovy+=0.5; setProjection(); break;
case 'F': if(fovy > 0) fovy-=0.5; setProjection(); break;

Experiment with different positions for the "centre" of the camera so that it does not always point at the object. It will no longer swing round to point at the object.

Fran Soddell email:F.Soddell@bendigo.latrobe.edu.au
last updated 02 September 2002