Math 155A - Introduction to Computer Graphics – Winter 2018
Instructor: 
Sam Buss,  Univ. of California, San Diego

Project #5 – Add Phong lighting to the ground plane and the surface of rotation.
Due date: Friday, February 23, 9:00pm.

Goals: Continue working with your program from Project 4 to add Phong lighting to the scene. Use illumination and shading to make your scene from Project # look more three-dimensional.  Learn how to shade objects with the Phong lighting model in OpenGL.  Create four lights, including a spotlight, and create three materials.
If your project 4 did not get completed successfully: You may instead use the floor and circular surface rendering code that is provided in MySurfaces.cpp that is downloaded with the zip file below. 

Grading:  

·       Get an individual grading session with Professor Buss or a TA. Place your C++ files, executable, and Visual Studio solution together in a separate folder, named “Project5” as a suggestion, in your PC computer account in the APM basement labs. (This should be under your networked storage so it is not subject to erasure.) The program must compile and run on these computers.  Grading will be personalized and one-on-one with a TA or with Sam Buss in the PC lab.  As usual, you will have to show your source code, run the program, make changes on the spot to your program and recompile as requested by the grader, and be able to explain how your program works and why it renders what it does.  The grading should be completed by Friday, March 2 if possible.  
Get graded for Project 4 at the same time.

·       You do not need to turn in anything to gradescope this time.

INSTRUCTIONS:

1.     Make sure to save copies of all your files from Project 4.  This is very important!

2.     Download the file PhongProject5.zip from the web.  This zip file contains twenty (20) C++ source and header files.  It also contains an executable PhongProfDemo.exe.  (Full URL for the zip file is:
http://www.math.ucsd.edu/~sbuss/CourseWeb/Math155A_2018Winter/Project5/PhongProject5.zip.)

3.     Run the executable on a PC.  You will see a scene with a surface of rotation and a letter "S".  (That's my initial.).  There are a lot of keystroke commands act on the scene. Some of them are familiar:

a.      Pressing the arrow keys changes the view position. 

b.     Pressing the “W” or “w” key toggles wire frame mode. 

c.      Pressing the “C” or “c” key toggles culling of back faces. 

d.     Pressing the “M” or “m” key increases or decreases the mesh resolution on the spheres (ellipsoids) and cylinders.

e.      The animation can be turned off and on by pressing “R” or “r”. The animation can be made to run faster or slower by pressing “F” (faster) or “f” (slower).

4.     There are also a number of new keystroke commands:

a.      “1” turns the first light off and on.  There is a small emissive sphere that appears and disappears when the light is turned off and on.  This emissive sphere is *not* the light: instead it just marks the position of the light.

b.     “2” turns the second light off and on.

c.      “3” turns the third light off and on.

d.     “4” turns a spotlight off and on.

e.      “P” toggles between Phong shading and Gouraud shading.

f.      “E” toggles emissive lighting off and on. (Note the spheres marking the lights appear and disappear, since they are the only emissive materials in the scene.)

g.     “A” toggles ambient lighting off and on.

h.     “D” toggles diffuse lighting off and on.

i.      “S” toggles specular lighting off and on.

5.     Take some time to run the program and look carefully how things render in the Demo code.

6.     There is another source program available, SimpleLightModern, which is available at http://www.math.ucsd.edu/~sbuss/MathCG2/OpenGLsoft/SimpleLightModern. This program shows a light rotating around three spheres: the source code is available, and there is a web page there describing how to use the Phong lighting code.

7.     Your job is to re-create this program -- sort of!!  However, you should continue to use your own initial from Project 3 instead using of the “S” in the Demo program. You will also use your own surfaces from Project 4 if they worked successfully.

8.     If your Project 4 failed to give surfaces for the surface of revolution, you may use routines “SamsRemeshFloor” and “SamsRenderCircularSurface” in the downloaded zip file. If neither the ground plane nor the surface of revolution worked for you, you may use the MySurfaces.cpp as is.

9.     Form a new project and solution; the next two steps describe how to integrate your project 4 code into the new system. Include the all supplied source files in the new project. (Again, save copies of your old source files!  Do not overwrite them.)  The supplied source code handles all the keystroke commands. It has routines in PhongData.cpp that you must finish writing..

a.      Replace the supplied MyInitial.cpp with your own version of this file from Project 4. (The one from project 4 is best, not from Project 3.)

b.     Integrate MyInitial.cpp into the new code..  For this to work in the new project, you will need to make at least three changes in your MyInitial.cpp:      

                                               i.     Replace the line  #include "ShaderMgrLTR.h" with the line       #include "PhongData.h"

                                             ii.     Replace the line
glVertexAttrib3f(aColor_loc, 0.4f, 0.9f, 0.4f);  
with the line
myMaterials[0].LoadIntoShaders();
Your colors may vary. If so, you may look in PhongData.cpp, and change the emissive color from (0.4, 0.9.0.4) to whatever color you used.

                    iii.   Remove the following three lines of code. (Single step mode has been removed from PhongProj’s functionality.)
   
if (singleStep) {
        spinMode = false; // If in single step mode, turn off future animation
     }

c.      If you are using your own ground plane and surface of revolution, there is still a little more work to do.  Replace the supplied MySurfaces.cpp with your version of this file from Project 4. For this work, there is a few changes to be made: 

                                               i.     Replace the line  #include "ShaderMgrLTR.h" with the line       #include "PhongData.h".

                                             ii.     Find the two lines for the floor colors:
glVertexAttrib3f(aColor_loc, 1.0f, 0.4f, 0.4f);
and replace them with
myMaterials[1].LoadIntoShaders();
Again, your colors may vary, and can be matched in PhongData.cpp as before.

                                            iii.     Then find the two lines for the floor colors:
glVertexAttrib3f(aColor_loc, 1.0f, 0.8f, 0.4f);
and replace them with
myMaterials[2].LoadIntoShaders();
Again, your
colors may vary, and can be matched in PhongData.cpp as before.

d.     These changes make the colors for the surfaces now be given as emissive Phong colors.

e.      If your Project 4 did not get successfully completed:  If you finished the ground plane, but not the surface of revolution, bring the supplied routines SamsRemeshCircularSurf and SamsRenderCircularSurf into your MySurfaces.cpp file, and use these instead of your “My….” versions of these routines. If you did not complete either the ground plane or the surface of revolution, just use MySurfaces.cpp without modification.

10.  There is a new source file: EduPhong.h and EduPhong.cpp. You do not need to modify these, but it is an important addition since it implements Phong lighting.

11.  There is a new source file, PhongData.cpp and associated header file. This is the routine you will modify for Project 5. Yours tasks are in steps 12-16:

12.  Create material properties. You will design three different materials: one for the initial (or more than one material if you wish). One for the ground plane. One for the surface of revolution. These each need to have ambient, diffuse and specular colors, and a specular exponent.  In the supplied code, the colors are purely emissive, with the other colors black (0.0, 0.0, 0.0).  You will change these to be different colors --- they should be completely non-emissive.
These materials are designed by changing the numbers in the routine MySetupMaterials.

13.  Create three lights, by updating the routine MySetupLights(). Values for the first light are already given here, so you can see how it is done.  You do not need to use the same values for the first light! It is suggested that you experiment with changing them! And, you need to design values for the second and third lights (that is, myLights[1] and myLights[2]). This is done by giving ambient, diffuse and specular values for each light. The suggested positions for the lights are given in the array myLightPositions[3]. However, you may modify these positions somewhat if it improves your scene (e.g. avoids hitting your initial).  The positions are set in the routine LoadAllLights(). You do not need to modify this part of that routine.

14.  Create a spotlight.  myLights[3] should be a spotlight. Set its emissive, diffuse and specular colors in  MySetupLights().  Also, set its “cosine cutoff” value, and its spot exponent, in MySetupLights().  Set the position of the spotlight, and the direction of the spotlight in the routine LoadAllLights().  LoadAllLights() already has the needed coded, but commented out. You will need to provide numbers for the position and the spot direction to make a nice looking spotlight.

15.  Suggestions:

a.     You do NOT need to match the colors in the Demo. In fact, you should choose your own colors and experiment with colors.

b.     Ambient and Diffuse colors are typically either equal or are scalar multiples of each other.  However, it is OK to modify the ambient color if you want mimic the ambient light picking up some its color from the environment. You may wish to scale down the Ambient colors be less bright.

c.      Specular colors on materials should be white or gray (with R, G and B values all equal.

d.     Specular colors of lights should be a scalar multiple of their diffuse color. This way the specular highlights will be the correct color.

e.      Add material colors and lights one at a time to get the look you want in stages.

f.      A common problem is to set too many colors too bright. If you scene looks overly saturated or is “bright and washed out”, try lowering your color values.

g.     Avoid using R,G,B color values for materials that are equal to zero. It makes the surface too monochromatic, and unresponsive to light in that color. However, it is OK for the lights to have a zero value for a color.

h.     Use the “e”, “a”, “d” and “s” commands to view ambient, diffuse and specular light separately. This will help you debug visual problems, and design your colors more effectively.

i.      The spotlight will look best when using Phong lighting. Point the spotlight at the floor, and circular mesh (or your initial) to make a nice image out of it.

16.  Goals for your scene:

a.     Choose your own colors for the scene; do not just copy the colors of the Demo executable.

b.     Specular highlights should be visible for all three surfaces, at least under the right viewing conditions.

c.      One of the three lights should be mostly white to render the surfaces in their native colors.

d.     The colors should give a really good impression of three dimensionality.

e.      There should be several ways to set the lights to give an attractive scene.  Choose your own colors, and try to make it a bit artistic colorwise.

f.      The scene will probably not look good in all combinations of lights, but you should try to avoid having it look terrible in any setting of the lights.

g.     Your code should still support all the keystrokes described above

17.  Grading session: Be able to show what is attractive about your scene. Which lights should be turned off or on to make the most attractive versions of the scene?  Which lights can show specular highlights on which surfaces? Be able to discuss on Phong shading improves over Gouraud shading.

18.  There is nothing to turn into gradescope for this project.

Program grading: Scale of 0 to 10.  Personal grading session with a TA or Professor Sam Buss.