Hands on: Gouraud Shading

Lighting is used in computer graphics to bring out 3D appearance. In class we studied the Phong lighting model (where "lighting" may alternatively be "reflection" or "illumination"), which is the standard lighting model in computer graphics. The Phong lighting model can be implemented in the vertex shader (in which case it is called Gouraud shading) or in the fragment shader (in which case it is called Phong shading).

The opengl-series implements Phong shading. To deepen our understanding of the Phong lighting model, in this exercise we will implement Gouraud shading.
                
Gouraud                 Phong
 

Pipeline for Gouraud Shading

Gouraud shading calculates the lighting per vertex (which is then interpolated across the mesh surface to determine the lighting per fragment) as follows:
  1. The vertex shader receives the following information per vertex: (a) vertex coordinates, (b) vertex normal, and (c) vertex color, or vertex texture coordinates, or both. It also receives light information (light source intensity and position) in a uniform variable.

    The vertex shader uses the vertex coordinates, the vertex normal and the light information to compute the vertex illumination according to the Phong lighting model. If vertex color is available, the vertex illumination is combined with the vertex color to determine the vertex shaded color. This information (vertex illumination/color and vertex texture coordinates, if available) is passed on to the rasterizer.

  2. The rasterizer interpolates the vertex coordinates, the vertex texture coordinates (if available) and the vertex illumination/color across each triangle, to determine the position, texture coordinates and illumination/color for each fragment, then passes these values to the fragment shader.

  3. If texture information is available, the fragment shader uses the texture coordinates to determine the texture color, then combines it with the fragment illumination/color to determine the final fragment color.

Pipeline for Phong Shading

Phong shading calculates the lighting per fragment (not per vertex) as follows:
  1. The vertex shader receives the following information per vertex: (a) vertex coordinates, (b) vertex normal, and (c) vertex color, or vertex texture coordinates, or both. This information is passed on to the rasterizer.

  2. The rasterizer interpolates the vertex coordinates, the vertex normals, the vertex texture coordinates (if available) and the vertex color (if available) across each triangle, to determine the position, normal, texture coordinates and color for each fragment (pixel), then passes these values to the fragment shader.

  3. The fragment shader receives light information (light source intensity and position) in a uniform variable. It uses the fragment coordinates, the fragment normal and the light information to compute the fragment illumination according to the Phong lighting model.

    If texture information is available, the fragment shader uses the texture coordinates to determine the texture color. The fragment illumination is combined with the fragment color (if available) and the texture color (if available) to determine the final fragment color.

Gouraud vs. Phong Shading

         Gouraud          Phong
he opengl-series implementation calculates lighting for each pixel in the fragment shader. To deepen your understanding on lighting, we will calculate lighting for each vertex in the vertex shader, and let the rasterizer interpolate the colors for all interior pixels.

To Do:

  1. The opengl-series implements Phong shading. Open the project 08_even_more_lighting. Study the lighting calculations in the fragment shader, and the way data is passed from the application -- to the vertex shader -- thorough the rasterizer (behind the scene) --- to the fragment shader.

  2. Implement Gouraud shading instead of Phong shading in 08_even_more_lighting. You would only need to modify the vertex and fragment shaders (not the application code). Make a local copy of the vertex shader and the fragment shader from 08_even_more_lighting, before modifying them.

    Gouraud shading requires all lighting calculations to be done in the vertex shader instead of the fragment shader. The pipeline steps listed above, along with the Phong shading code from 08_even_more_lighting, should guide you through your Gouraud shading implementation. If you implement Gouraud shading correctly, the lighting should look a bit off (especially the specular highlights).

  3. Position the spotlight so that it does not hit any vertex. You will notice that, unlike Phong shading, Gourad shading misses the spotlight entirely. Demo your code to your instructor when finished.