Notes on Oct 31 class

In class we went over the Phong shading model. Your homework for this coming Tuesday (Nov 7) is to incorporate Phong shading into your Z-buffered scene, and make something that looks cool and pretty, and ideally animates in a fun way. A Halloween theme is still a very nice idea (I think it's still a bit early for Thanksgiving...).

If you do vertex shading, you should be able to render your scene fast enough to be able to animate it. Fortunately the Phong model is extremely simple, so you shouldn't have much trouble adding it to your program. Notes on the phong shading model follow below.

Phong shading algorithm

The simplest widely used model for approximating surface highlights is the Phong model, originally developed by Bui-Tong Phong. In this model, we think of the interaction between light and a surface as having three distinct components: The ambient component is usually given a dim constant value, such as 0.2. It approximates light coming from a surface due to all the non-directional ambient light that is in the environment. In general, you'll want this to be some tinted color, rather than just gray. [ra,ga,ba]. For example, a slightly greenish object might have an ambient color of [0.1,0.3,0.1].

The diffuse component is that dot product nL that we discussed in class. It approximates light, originally from light source L, reflecting from a surface which is diffuse, or non-glossy. One example of a non-glossy surface is paper. In general, you'll also want this to have a non-gray color value, so this term would in general be a color defined as: [rd,gd,bd](nL).

Finally, the Phong model has a provision for a highlight, or specular, component, which reflects light in a shiny way. This is defined by [rs,gs,bs](RE)p, where R is the unit length mirror reflection direction vector we discussed in class, E is the unit length direction vector toward the camera ("E" is for "eye"), and where p is a specular power. The higher the value of p, the shinier the surface.

In class we derived that the reflection direction R that corresponds to direction toward a light source L can be derived from L and the surface normal n as:

R = 2 (nL) n - L

The complete Phong shading model for a single light source is:

[ra,ga,ba] + [rd,gd,bd]max0(nL) + [rs,gs,bs]max0(RE)p

If you have multiple light sources, the effect of each light source Li will geometrically depend on the normal, and therefore on the diffuse and specular components, but not on the ambient component. Also, each light might have its own [r,g,b] color. So the complete Phong model for multiple light sources is:

[ra,ga,ba] + Σi( [Lr,Lg,Lb] ( [rd,gd,bd]max0(nLi) + [rs,gs,bs]max0(REi)p ) )

Below you can see three different shaders for a sphere. In all cases, the sphere is lit by two light sources: a dim source from the rear left (-1,0,-.5) and a bright source from the front upper right (1,1,.5). The spheres have, respectively, no highlight, a highlight with an exponent of p = 4, and a highlight with an exponent of p = 16.