INT32GP Graphics Programming: Tutorial #14
Animation and Lighting

B1.11 3:00 pm Tue 03 Sep 02
B1.11 12 midday Wed 04 Sep 02
Note: I have placed some student work on redgum. These have been compiled to run on the Silicon Graphics machines in the Indy room. Copy them with the following command.
cp ~fran/student/* .

Start with your program from Tutorial #13.

Step 3 - automatically rotate the teapot
When the left mouse button is pressed, automatically rotate the teapot.
  1. Register the following callbacks in the appropriate place in the function void setUpGLUT(int argc,char ** argv)
    glutMouseFunc(mouse);
    glutIdleFunc(idle);
  2. Add the mouse function. When the left mouse button is pressed (GLUT_DOWN) toggle idling on and off.
    void mouse(int button,int state,int xMouse,int yMouse){
       static int idling=TRUE;
       if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){
          idling=!idling;
          if(idling)glutIdleFunc(idle);
          else glutIdleFunc(NULL);
       }
       glutPostRedisplay();
    }
  3. Add the idle function. Increment the rotation angle by an appropriate amount. (The larger the increment the faster the rotation). The idle function is called continuously when nothing else is happening.Where is the rotation transform being applied in the program?
    void idle(){
       ra+=0.2;
       if(ra>=360) ra=0;
       glutPostRedisplay();
    }
  4. Note: If NULL does not work, you need another include file at the top of the program.
    #include <stdio.h>
Step 4 - add a green spotlight
  1. First add another light (GL_LIGHT4), with green diffuse and specular light.
  2. Position it at (0,0,5.0). It is a point, not a vector.
  3. Provide a keyboard facility to turn it on and off.
  4. To turn the light into a spotlight, specify its direction. Here the spotlight is pointing down the negative z-axis slightly to the left.
    GLfloat spotDirection[]={-0.1,0.0,-1.0};
  5. Specify the following (or try your own) spotlight characteristics.
    glLightfv(GL_LIGHT4,GL_SPOT_DIRECTION,spotDirection);
    glLightf(GL_LIGHT4,GL_SPOT_CUTOFF,25.0);
    glLightf(GL_LIGHT4,GL_SPOT_EXPONENT,100.0);
    glLightf(GL_LIGHT4,GL_CONSTANT_ATTENUATION,1);
    glLightf(GL_LIGHT4,GL_LINEAR_ATTENUATION,0);
    glLightf(GL_LIGHT4,GL_QUADRATIC_ATTENUATION,0.5);
  6. Try different values for the characteristics. What happens if you make GL_SPOT_EXPONENT much smaller?
  7. Use the glutTimerFunc to turn the spotlight on and off automatically every second (1000 milliseconds).
  8. Enhance the function so that the spotlight is turned on after one second. Then the white light (GL_LIGHT0) is turned off after half a second. After another half a second the white light is turned back on. After another one second the spotlight is turned off. Then the sequence starts again.
Fran Soddell email:F.Soddell@bendigo.latrobe.edu.au
last updated 09 September 2002