INT32GP Graphics Programming

Start with the program, template3d.c, you developed in the last tutorial.

Add Cameras

Add one camera.

  1. Modify your data structures to specify two different cameras.
    • Replace
      GLdouble eye[]={
         2.0, 2.0, 2.0
      };
      
      with
      GLdouble eye[][3]={
         { 2.0, 2.0, 2.0 },
         { 0.0, 0.0, 5.0 }
      };
      
    • Replace
      GLdouble centre[]={
         0.0, 0.0, 0.0
      };
      
      with
      GLdouble centre[][3]={
         { 0.0, 0.0, 0.0 },
         { 0.0, 0.0, 3.0 }
      };
      
    • Replace
      GLdouble up[]={
         0.0, 1.0, 0.0
      };
      
      with
      GLdouble up[][3]={
         {0.0, 1.0, 0.0},
         {0.0, 1.0, 0.0}
      };
      
  2. To keep track of the current camera, define an enumeration and sepcify the current camera. Place the following code in the manage cameras part of the global section of the program.
    enum {cameraOne, cameraTwo, END_CAMERAS};
    GLint currentCamera = cameraOne;
    
  3. In the function setCamera replace
       gluLookAt(   eye[x],    eye[y],    eye[z],
                 centre[x], centre[y], centre[z],
                     up[x],     up[y],     up[z]);
    
    with
       gluLookAt(
          eye[currentCamera][x],    eye[currentCamera][y],    eye[currentCamera][z],
          centre[currentCamera][x], centre[currentCamera][y], centre[currentCamera][z],
          up[currentCamera][x],     up[currentCamera][y],     up[currentCamera][z]);
    
  4. To cycle through the cameras, add the following case to the switch in keyboard/
          case 'c':
             currentCamera++;
             if(currentCamera == END_CAMERAS)
                currentCamera = cameraOne;
             break;
    

Move Cameras

Move the cameras using the special function keys. Below is a suggestion for responding to the up arrow key (move into the scene down along the negative z-axis).

  1. Enhance the case GLUT_KEY_UP: in the switch statement in the function special.
          case GLUT_KEY_UP       :
             move(z, -0.1);
             break;
    
  2. Write the function to move the camera. CameraOne should remain focused (centred) on the origin but the centre of cameraTwo moves with the camera.
    /*
    ** TODO: Add comments
    */
    void move(int axis, GLdouble amount ){
       eye[currentCamera][axis] += amount;
       if(currentCamera == cameraTwo)
    	   centre[currentCamera][axis] += amount;
    }
    
  3. Write your own code for the other arrow keys and for PgUp and PgDn. See Assignment 2 for their specification.
  4. If the image flickers, it needs double buffering. However, be careful when changing glutInitDisplayMode. You need to be able to do both double buffering and depth buffering.
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    

Restore Defaults

You need to restore the position of the camera after it has moved.

One way to tackle this is to keep a copy of the default positions.

  1. Add the following data structures to the manage cameras section of your program.
    GLdouble eyeDefault[][3]={
       { 2.0, 2.0, 2.0 },
       { 0.0, 0.0, 5.0 }
    };
    GLdouble centreDefault[][3]={
       { 0.0, 0.0, 0.0 },
       { 0.0, 0.0, 3.0 }
    };
    GLdouble upDefault[][3]={
       {0.0, 1.0, 0.0},
       {0.0, 1.0, 0.0}
    };
    
  2. Write code so that, when the user presses 'C', the default positions for the current camera are restored. It is a good idea to restore the original fovy as well..
Copyright © 2006 Fran Soddell
last updated 15 August 2006 F.Soddell@latrobe.edu.au