Skip to end of metadata
Go to start of metadata

CS 351 - Assignment 8: Shading

Elena Kirillova and Titobiloluwa Awe


In this assignment we completed our 3D z-buffer rendering system by adding to it implementations of Phong shading for polygons with ambient and point light sources.

Task Description

For implementing light sources we had to create a light source data type header file and a library file with light source supporting functions.

Phong shading required interpolation of the surface normals and surface locations across the polygons, so that any polygon would be shaded according to its angular orientation.

Solution Description

For all types of lighting the contribution of the light source to the color was calculated for every channel and then clipped to [0,1] range.

Ambient lighting

Resultant color was a simple combination of object color and the light source color:

Point lighting

Equation for a single light source was the sum of the ambient, body, and surface reflection terms with taken in into account angle between the light vector L? and the surface normal N?:

  • Cb - body color of the surface
  • Cs - surfacecolor (generally (0.2,0.2,0.2) )
  • CLd - direct illuminant color
  • L? - light vector
  • N? - surface normal
  • V? - view vector
  • H? - halfway vector
  • n - sharpness value

For all types of lights there was made a Lighting_shading() method in which a switch statement between types took care of all necessary calculations.

Phong shading

We had to make a number of changes to our drawing/filling function to let the calculations of normals, as well as color transformations and interpolation.

To be able to calculate the color at each pixel, we calculated coordinates and surface normals at each vertex and interpolated them across the surface.


Lighting_shading test

Required Image 1, with the light source from the view point:

Simple Cube

Required Image 2, with the light source on its right:

X-wing Scene

Required Image 3:


Required Image 4:

Required Creative Images

For the creative part we tool the 3D models we created for the last project and implemented new light sources and Phong shading in them.

Each of the two images below has two light sources: point and ambient, as well as Phong shading. 

Blue Crosses getting closer to the screen, with both surface and body color being blue:

Formation of one big cube rotating around y-axis, while small cubes orbit it rotating around various axis, with grey surface color:


This project was a part that made our rendering system all nice and complete. With a new system we now are able to shade the polygons accordingly to their position in the world, their angle; and the position, type and color of light sources that surround these polygons.

This project was a lot about details, remembering to ?pass all the right parameters into Lighting_shading function, assigning types for both shading and types. So quite a bit of debugging took place for very little errors that would be just missing the right shading name or so, which wan fun to search for and a big relieve to have found out. =)