Phong Shader

Total Intensity due to positional lights :         I = IL_0 + IL_1 + IL_2 + ...+ IL_numlilghts-1 + IG   

      where

              IL_i = Idiff + Ispec + Iamb   for Lighti

              and IG = intensity due to global ambient light

 

  1. Must have the actual hit point on the primitive by the original ray.

    P = ray(hit_time)
  1. Calculate vector l.

    l = position of light - P

  2. Must have the generic hit point on the primitive by the inverse transformed ray.

    P' = it_ray(hit_time)

  3. Calculate the vector, m', that is normal (perpendicular) to the generic hit point.
    1. The vector that is normal to the generic sphere is the same as the generic hit point.

       m' = P' - center of sphere
       m'  = P' - (0,0,0)
       m'  = P'
    2. The vector that is normal to the generic plane is:

    3. The vector that is normal to the generic square is:

    4. The vector that is normal to the generic tapered cylinder's:
      1. wall is

      2. base is

      3. cap is


  4. Calculate the vector that is normal to the actual hit point. It is the result of post-multiplying the object's inverse transformed matrix M by the vector normal to the generic hit point:

    m = M * m'

  5. Normalize both vectors, l and m.

  6. Calculate value for lambert.

    lambert = max{ l dot m, 0}

  7. Calculate the diffuse portion of the intensity of the color due to this light, (each for red, green, and blue):

    Idiff = lightdiff * materialdiff * lambert

  8. Calculate vector e, the vector from the actual point, P, to the eye (camera position).

    e = - ray's direction
             or
    e = ray.start - P
  9. Normalize vector e.

  10. Calculate the halfway vector h, the vector that is halfway between vector l and vector e.

    h = l + e

  11. Normalize vector h.

  12. Calculate value for phong.

    phong = max {0, h dot m}

  13. Calculate the specular portion of the intensity of the color due to this light (each for red, green, and blue):

    Ispec = lightspec * materialspec * phong f

        where  f
    is the specular exponent and 0<=f<=200.

  14. Calculate the ambient portion of the intensity of the color due to this light (each for red, green, and blue):

    Iamb = lightamb * materialamb

  15. Calculate the total intensity of the color due to this light:

    IL_i = Idiff + Ispec + Iamb

  16. Calculate the global ambient value for intensity of the color (each for red, green, and blue):

    IG = GlobalAmbient * materialamb

  17. After calculating the IL for each light, calculate the total value (each for red, green, and blue):

    I = IL_0 + IL_1 + IL_2 + ...+ IL_numlilghts-1 + IG

  18. Return this value.