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:
- SimpleLightModern.zip: A zip file with
the primary source file SimpleLightModern.cpp
and a Windows executable. To run it, extract the executable file
and the EduPhong.glsl file to the same folder, then run the program.
- To recompile SimpleLightModern, you also need the GlShaderMgr software,
the EduPhong software and GlLinearMath routines:
- The EduPhong software implements Phong
lighting. You need the two C++ files EduPhong.[cpp,.h]
and the GLSL shader programs in EduPhong.glsl.
- The GlShaderMgr software compiles and links
shader programs.
You need the two C++ files GlShaderMgr.[cpp,.h].
- The GlLinearMath software supplies
classes for vectors, and miscellaneous mathematics routines.
You need the five files LinearR3.[cpp,h],
LinearR4.[cpp,h], and MathMisc.h.
- Documentation
on how SimpleLightModern
uses the EduPhong routines for
setting up materials and lights for
Phong lighting is available.
Things to try out:
- 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.
-
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.
-
Try modifying the code to make the light a spotlight, say with
fixed spot direction (-1,0,0).
-
Material properties are implemented as generic vertex attributes.
You can see how this is done by examining the code in EduPhong[.cpp,.h].
-
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.