CPSC 324, Fall 2002
Lab 8b: OpenGL 3D, Part 2

THIS IS THE SECOND PART of a two part lab on 3D graphics programming in OpenGL. In this part of the lab you will add lighting and materials to the wireframe model that you constructed last week.

Exercise: The exercise for Part 2 of Lab 8 is the OpenGL program described on the remainder of this page. You should save one or two frames from the completed program and add them to your web page for lab 8. You should already have wireframe images from the first part of the lab on that page. You should turn in a printout of the file gl_canvas.cc from your completed program.

A Lit, Solid Model

solid car You will be adding light and materials to the program that you wrote for last week's lab. You might want to save a working copy of your gl_canvas.cc before you modify it. When light and materials are added to my sample program, it can make images of an illuminated solid object like the one shown here. In this image, the light is shining from the right. In the modified program, both the object and the light source can be rotated. The arrow keys are used to rotate the object as before. However, if you hold the shift key down as you press the arrow keys, the direction of the light will be changed instead. The HOME key will return both the object and the light to their starting positions. You can find a copy of my program in /home/cs324/lab8b_sample_program.

You should modify your program in the same way. Change each "glutWire" object into a "glutSolid" object. If you turn on glEnable(GL_COLOR_MATERIAL), then your objects will still get their color from the glColor commands that are already in the program. Remember that you have to turn on lighting and enable GL_LIGHT0 to get any lighting effect. The default position of GL_LIGHT0 is good enough to get you started. Any program that uses scaling and lighting should also enable GL_NORMALIZE so that normal vectors will be correctly recalculated after a scaling transformation. Finally, you should use glLightModelfv to turn on a small amount of ambient illumination.

Once you have the lighting working, you can worry about rotating the light. Remember that lights are subject to the same transformations as other objects. The transformation that affects light0 is the one that is in effect when glLightfv(GL_LIGHT0,GL_POSITION,pos) is called. This means that you should call this function after setting up the viewing transformation. Furthermore, it should be called before you draw any objects that are to be illuminated by the light. In order to apply a rotation to the light, you have to set up the rotation transformations before you set the position of the light. You'll need two new variables, such as light_rotate_x and light_rotate_y to keep track of the light's rotation.

In the keyPressEvent routine, you will need some way to know whether the user was holding down the SHIFT key when the user pressed the arrow key. To get this information, you can say:

            bool shifted = (evt->state() & ShiftButton) != 0;

The variable shifted will be true if the user was holding down the SHIFT key.

David Eck, October 2002