INT32GP Graphics Programming: Tutorial #13: Lights

In the Indy Lab
B1.11 3:00pm Mon 09 Sep 02
B1.11 12midday Tues 03 Sep 02

Start with step0.c. This program renders a red GLUT teapot on a black background using perspective projection and double buffering. When the window is resized by the uzer, the proportions of the teapot are preserved and the teapot remains centred. Step 1
  1. Initially, set up flat (not smooth) shading, enable lighting, and turn on GL_LIGHT0, which is associated with default RGB values for ambient light, Add the following code to the bottom of the function, initialisGL().
    glShadeModel(GL_FLAT);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    
  2. Run the program and rotate the teapot. The appearance of the teapot is not very realistic because we also need to enable depth testing.
  3. Add the facility to toggle between GL_FLAT and GL_SMOOTH shading by pressing the s key.
Step 2
  1. Add keyboard facilities to enable and disable lighting and to turn light0 off and on.
    static int lighting=TRUE;
    static int light0=TRUE;
    
    
    case '0': light0=!light0;
              if(light0)glEnable(GL_LIGHT0);
                 else glDisable(GL_LIGHT0);break;
    case 'l': lighting=!lighting;
              if(lighting)glEnable(GL_LIGHTING);
              else glDisable(GL_LIGHTING);break;
    
  2. We will set up 3 lights. Add the following global definitions.
    /* manage lighting
    */
    enum {whiteLight,redLight,redLaserLight};
    GLfloat ambient[][4]={
       {0.5,0.5,0.5,1.0},{1.0,0.0,0.0,1.0},{0.05,0.0,0.0,1.0} };
    GLfloat diffuse[][4]={
       {1.0,1.0,1.0,1.0},{1.0,0.0,0.0,1.0},{0.10,0.0,0.0,1.0} };
    GLfloat specular[][4]=
       { {1.0,1.0,1.0,1.0},{1.0,0.0,0.0,1.0},{0.99,0.0,0.0,1.0} };
    GLfloat position[][4]={
       {0.0,10.0,0.0,0.0},{2.0,2.0,0.0,1.0},{2.0,2.0,0.0,1.0} };
  3. Add the following function to set up the lights and call it from the top of display(), after the call to setCamera().
    void setLights(){
       int i, light=whiteLight;
       for(i=GL_LIGHT0;i<=GL_LIGHT2;i++){
          glLightfv(i,GL_POSITION,position[light]);
          glLightfv(i,GL_AMBIENT, ambient[light]);
          glLightfv(i,GL_DIFFUSE, diffuse[light]);
          glLightfv(i,GL_SPECULAR,specular[light++]);
       }
    }
  4. Add keyboard facilities so that users can turn all three lights on and off.
  5. Since we are using default material settings, we are not getting the full effect of the lights. We have to specify our own material.
  6. What is the effect of the redLaserLight on its own? What is its effect when it is on with the white light?
  7. Add a blue light, with the facility to turn it on and off.
  8. What colour does the teapot appear when both the red and blue lights are on?
  9. Add other materials (see Lecture #14 for some suggestions) with a keyboard facility to cycle through all the materials.
Fran Soddell email:F.Soddell@bendigo.latrobe.edu.au
last updated 09 September 2002