CS 424: Computer Graphics, Fall 2013
Lab 6: Light and Material
This lab is about using OpenGL lights and materials. You will add several colored lights to a scene, and you will apply a variety of materials to one of the objects in that scene. You should be familiar with Section 9 of the notes before starting the lab.
As usual, you have your choice of working either in C or in Java. The files that you will need for C can be found in the directory /classes/cs424/lab6-glut. The files for use with JOGL are in /classes/cs424/lab6-jogl. For Java, you will only need to work on the program LightAndMaterial.java, but you will also need Camera.java, since it is used by the main program. For C, you will only need to work on the program LightAndMaterial.c, but you will also need camera.c and the header file camera.h.
This lab is due at the beginning of lab next week. You should copy your work into your homework folder in /classes/cs424/homework. Your work should be in a folder named lab6 or something very similar.
Exercise 1: Light
The program LightAndMaterial.java or LightAndMaterial.c shows a teapot standing on a circular platform. The material is basic light gray for the ambient and diffuse colors. The base has no specular color, but for the teapot, a darker gray specular color with a shininess value of 64 has been added, so that the teapot shows specular highlights. The illumination comes from a viewpoint light, the only type of light that we have used up until now. The viewpoint light is GL_LIGHT0, which has default diffuse and specular colors equal to (1,1,1,1). This would be too bright once more lights have been added to the scene (since too much light can give over-saturated, washed out colors), so the diffuse and specular colors for light zero have been set to (0.6,0.6,0.6,1). You should run the program, since this is the first time you've seen specular highlights. You can use the mouse to rotate the scene.
Your assignment for this exercise is to add three more lights to the scene. For the lights, you should use GL_LIGHT1, GL_LIGHT2, and GL_LIGHT3. One light should be red, one should be green, and one should be blue. You should give each light some specular and some diffuse color, but don't make them too bright.
Now, the hard part is that the lights should be animated. They should rotate in circles above the teapot, moving at different speeds. Furthermore, I want to see where the lights are. A light is invisible; to see where it is, you have to draw some geometry at the same location as the light. In my program, I draw a red sphere at the same place as the red light, a green sphere for the green light, and a blue sphere for the blue light.
The user will start and stop the animation by typing a space, and the user can turn the lights on and off by hitting the r, g, b, and v keys. (The v key turns the viewpoint light, light zero, on and off.) Here is what my completed program looks like. In the picture on the left, all four lights are on. On the right, the three colored lights are on but the viewpoint light has been turned off:
To turn a light on and off, you only have to enable/disable it, using, for example, glEnable(GL_LIGHT1) and glDisable(GL_LIGHT1). If a light is disabled, it makes no difference how it's configured, since it has no effect on the scene. In my program, when one of the colored lights is disabled, I draw a gray sphere instead of a colored sphere for that light.
As for the animation, you should know how to make the rotating spheres, and I suggest that you get that working before you add the lights. Then, you just have to put the lights at the same points as the spheres. Remember that a light is affected by the same modelview transformation as other objects, but the transformation is applied when you set the position of the light using, for example,
glLightfv( GL_LIGHT1, GL_POSITION, position );
That is, you have to call this function when the appropriate modelview transformation is already set up. In fact, you can use (0,0,0,1) for the light position in all cases, and use the same modelview transformation that you use for the sphere to translate and rotate the light. (You don't have to do anything with light 0 except enable and disable it, since it's not animated.)
Finally, remember that you are not using color, you are using material. The glColor* commands will have no effect because GL_COLOR_MATERIAL is not turned on in this program. The program already includes some examples of setting light and material properties; you might want to look for them.
Exercise 2: Material
For the second part of this lab, you will implement a variety of materials and apply them to the teapot. The user will switch among the various materials by hitting the left and right arrow keys.
The default material, the one that is applied to the teapot when the program starts, should be the same as the original material in the program. That is, it should have ambient and diffuse color equal to (0,7,0.7,0.7,1), specular color equal to (0.3,0.3,0.3,1), and shininess equal to 64. For the other materials, you should use the table of material values from http://devernay.free.fr/cours/opengl/materials.html. This table shows settings for the ambient, diffuse, specular, and shininess properties that imitate a variety of physical materials. You should select at least four materials from this table and implement them in the program. Note that the ambient and diffuse properties for these materials are different. Note also that the shininess value from the table must be multiplied by 128 to give a number that you can use with OpenGL. Look below the table for a suggestion about how its data can be used in OpenGL.
In my program, hitting the HOME key resets everything to the default, initial values. That is, it applies the default material, enables all the lights, and resets the frameNumber to 0.