SimpleLightModern contains sample C++ code illustrating an implementation of Phong lighting in vertex and fragment shaders which allow either Gouraud shading or Phong shading. This is part of a set of programs introducing the use of Modern OpenGL, which are intended to accompany a possible second edition of the book 3D Computer Graphics: A mathematical approach with OpenGL, Cambridge University Press, 2003.

SimpleLightModern includes vertex and fragment shaders for Phong lighting, with either Phong shading or Gouraud shading. It uses the general purpose EduPhong software to implementation Phong lighting. This includes C++ data structures for recording information about material properties, global lighting properties, and properties of individual lights.

It core source files include the main program routines in SimpleLightModern.cpp,, and routines for Phong lighting routines EduPhong[.cpp,.h]. The EduPhong routines provide a full-featured implementation of Phong lighting. Overall, the program is intended to illustrate the core capabilities of Phong light. The program also uses the GlShaderMgr C++ package for for compiling and linking shaders and the GlGeomShapes software for rendering spheres. In addition, it uses the GlLinearMath software that provides functions to handle 3-dimensional vectors and 4-dimensional vectors.

Available for download:

Things to try out:

  1. Compile and run the program. Or run the executable. You should see six colored spheres with a white light revolving around them. Try out the following keystroke commands in the graphics window:
    • 'R' or 'r' (Run) to start/stop the animation of the light.
    • 'W' or 'w' (Wireframe) toggles wireframe mode off and on.
    • 'M' or 'm' (Mesh) cycles through mesh resolutions of 8x8, 16x16 and 32x32. Wireframe mode makes this visible, but the effect of the mesh resolution on Phong lighting is also dramatic.
    • 'P' or 'p' (Phong) toggles between Phong shading and Gouraud shading. You should examine the difference between the shading methods while the light is animated, and at all three mesh resolutions.
    • 'L' or 'l' (Light) toggles whether the light is positional or directional.
    • 'V' or 'V' (Viewer) toggles whether the viewer is local or non-local. In other words, whether the viewer is positional or directional.
    • 'E' or 'e' (Emissive) turns rendering of emissive colors off and on.
    • 'A' or 'a' (Ambient) turns rendering of ambient colors off and on.
    • 'D' or 'd' (Diffuse) turns rendering of diffuse colors off and on.
    • 'S' or 's' (Specular) turns rendering of specular highlights off and on.
  2. Examine the source code for SimpleLightModern.cpp to see how materials and lights are specified in the software. See the EduPhong documentation for an overview of how to use the EduPhong code.
  3. Try modifying the code to make the light a spotlight, say with fixed spot direction (-1,0,0).
  4. Material properties are implemented as generic vertex attributes. You can see how this is done by examining the code in EduPhong[.cpp,.h].
  5. Lighting properties are implemented as uniform values for the vertex and fragment shaders. The code for this is in EduPhong[.cpp,.h] but it is fairly complex. See the web page referenced above for more details.

Version 1.0 of SimpleLightModern, February 19, 2018, revised September 13, 2020. Copyright 2018, 2020. All rights reserved.