CS405 Lab 4: Illumination

Highlights of this lab:

    This lab is an introduction to illumination


    After the lab lecture, you have approximately two weeks of time to:

Online OpenGL Manual

A. Colors

B. Materials and Lights

Please refer to glLight*( ) Description.

  • Creating a scene with multiple light sources

    We'll use multiple lights to create a scene. The source code for this can be found in the "LIGHTING 3" subdirectory. Figure 6 shows a rendering of the scene.

    Figure 6: 3D text demonstrating chrome text with colored lights

    The following is the RenderScene( ) member function:

    BOOL CLightingView::RenderScene( void )
            // Need to add a check for the font here before assuming it's there...
    //      static GLuint myFont = GenerateDisplayListForFont( "Avalon Quest", 0.2f
            static GLuint myFont = GenerateDisplayListForFont( "Impact", 0.2f );
            GLfloat materialSpecular[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
            GLfloat materialShininess[1] = { 128.0f };
            GLfloat materialAmbient[4] = { 0.25f, 0.25f, 0.25f, 1.0f };
            GLfloat materialDiffuse[4] = { 0.4f, 0.4f, 0.4f, 1.0f };
        GLfloat local_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
        ::glLightModelfv(GL_LIGHT_MODEL_AMBIENT, local_ambient);
        GLfloat ambient0[] =  { 0.0f, 0.0f, 0.0f, 1.0f };
        GLfloat diffuse0[] =  { 1.0f, 0.0f, 0.0f, 1.0f };
        GLfloat specular0[] = { 1.0f, 0.0f, 0.0f, 1.0f };
        GLfloat position0[] = { 2.0f, -1.5f, -1.5f, 1.0f };
        GLfloat ambient1[] =  { 0.0f, 0.0f, 0.0f, 1.0f };
        GLfloat diffuse1[] =  { 1.0f, 1.0f, 1.0f, 1.0f };
        GLfloat specular1[] = { 0.5f, 0.5f, 0.5f, 1.0f };
        GLfloat position1[] = { 2.0f, 1.0f, -1.0f, 0.0f };
        GLfloat ambient2[] =  { 0.0f, 0.0f, 0.0f, 1.0f };
        GLfloat diffuse2[] =  { 0.0f, 0.0f, 1.0f, 1.0f };
        GLfloat specular2[] = { 0.0f, 0.0f, 1.0f, 1.0f };
        GLfloat position2[] = { -0.5f, -0.5f, -1.0f, 1.0f };
            GLfloat ambient3[] =  { 0.0f, 0.0f, 0.0f, 1.0f };
        GLfloat diffuse3[] =  { 1.0f, 1.0f, 1.0f, 1.0f };
        GLfloat specular3[] = { 0.0f, 0.0f, 0.0f, 1.0f };
        GLfloat position3[] = { 2.0f, 0.5f, 0.5f, 0.0f };
            // Disable lighting to draw some simple spheres that
            // represent the lights
            ::glDisable( GL_LIGHTING );
            ::glColor3fv( diffuse0 );
            ::glTranslatef( position0[0], position0[1], position0[2] );
            ::auxWireSphere( 0.15f );
            ::glColor3fv( diffuse1 );
            ::glTranslatef( position1[0], position1[1], position1[2] );
    //      ::auxWireSphere( 0.15f );
            ::glColor3fv( diffuse2 );
            ::glTranslatef( position2[0], position2[1], position2[2] );
            ::auxWireSphere( 0.15f );
            ::glColor3fv( diffuse3 );
            ::glTranslatef( position3[0], position3[1], position3[2] );
    //      ::auxWireSphere( 0.15f );
            ::glEnable( GL_LIGHTING );
        ::glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
        ::glLightfv(GL_LIGHT0, GL_POSITION, position0);
        ::glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
        ::glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
        ::glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
        ::glLightfv(GL_LIGHT1, GL_POSITION, position1);
        ::glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
        ::glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
        ::glLightfv(GL_LIGHT2, GL_AMBIENT, ambient2);
        ::glLightfv(GL_LIGHT2, GL_POSITION, position2);
        ::glLightfv(GL_LIGHT2, GL_DIFFUSE, diffuse2);
        ::glLightfv(GL_LIGHT2, GL_SPECULAR, specular2);
        ::glLightfv(GL_LIGHT3, GL_AMBIENT, ambient3);
        ::glLightfv(GL_LIGHT3, GL_POSITION, position3);
        ::glLightfv(GL_LIGHT3, GL_DIFFUSE, diffuse3);
        ::glLightfv(GL_LIGHT3, GL_SPECULAR, specular3);
            ::glMaterialfv( GL_FRONT, GL_SPECULAR, materialSpecular );
            ::glMaterialfv( GL_FRONT, GL_SHININESS, materialShininess );
            ::glMaterialfv( GL_FRONT, GL_DIFFUSE, materialDiffuse );
            ::glMaterialfv( GL_FRONT, GL_AMBIENT, materialAmbient );
            ::glTranslatef( -3.0f, 0.0f, 0.0f );
            GLTextOut( myFont, "OpenGL & Windows" );
            return TRUE;

    For examples mentioned in the lab notes click here

    C. Lab Exercise

    This exercise is broken into two parts:

    1. Color
    2. Light and Material
    Part 1-Color


    Instructions (Record answers for 4, 5, and 6):
    1. Create an empty "Win32 Console Project"
    2. Add the following code to the project:
    3. Build and Run this code
    4. Change the glShadeModel from GL_SMOOTH to GL_FLAT.
      What color is the triangle? Why?
    5. Change the GL_TRIANGLES to GL_LINE_LOOP.
      What color are the lines? Why?
    6. Change the GL_LINELOOP to GL_POLYGON.
      What color is the triangle now? Why?
    7. Undo steps 4, 5, and 6.
    8. Use three other versions of glColor (besides glColor3f) to create the full intensity (red, green, blue) of the original triangle. The following table outlines the suffixes you can use and provides a hint of the maximum values.
      Suffix Data Type
      b 1-byte integer
      s 2-byte integer
      i 4-byte integer
      ub unsigned 1-byte integer
      us unsigned 2-byte integer
      ui unsigned 4-byte integer

    Show Instructor the results and answers to 4, 5, 6, and 8


    Part 2-Light and Material


    Instructions (Record answers for 3, 5, 7, 11, and 13):
    1. Create an empty "Win32 Console Project"
    2. Add the following code to the project:
    3. This code has a positional light which rotates around a Torus.
      How do I know that it is a positional light?
    4. Left mouse click to see how the light rotates around the object.
    5. Modify the "position" array so that the light is directional.
      What happens?
    6. Change the light back so that it is a positional light.
    7. You will notice that there is a "glShadeModel (GL_SMOOTH);"
      Why do you want smooth shading instead of flat?
    8. Modify the code so that a right click will reverse the rotation.
    9. Replace the rotation with translation so that clicking on the right mouse will move the light infinitely along the positive x-axis (toward the right), and the left mouse click will reverse the direction moving the light toward the left.
      Hint: use glTranslated() rather than glRotated() in display(), and choose an appropriate value to use instead of spin.
    10. Change the attenuation so that the light decreases in intensity as it's moved away from the object.
      Hint: Add calls to glLight*() to set the desired attenuation parameters. By default, GL_CONSTANT_ATTENUATION is 1.0, GL_LINEAR_ATTENUATION is 0, and GL_QUADRATIC_ATTENUATION is 0. Modify them to be higher values. Suggestion: try linear and quadratic values which are fractions less than 1.
    11. Try to change the color of the Torus to red using glColor. Does it work?
    12. Use "glMaterialfv" with DIFFUSE and AMBIENT values set to pure red.
    13. Use "glLightfv" with DIFFUSE and AMBIENT values set to pure green. What would you expect to happen? What happens?
    14. Even though you may have predicted otherwise, the object is viewable. This is because there is a global ambient light on the scene, which is set to (0.2,0.2,0.2,1.0).
    15. To set the global ambient light use the following format:
      glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient)
      where lmodel_ambient has been set to an array with RGBA values.
    Show the results and answers to your instructor. Be prepared to comment out the glLightModelfv function and ambient and diffuse light settings.