NOTE: This is snipped from notes from Prof. Sequin's CS184 Fall04 class. For the full document, see http://www.cs.berkeley.edu/~ug/slide/pipeline/assignments/as8/SLIDEHOME/docs/slide/spec/spec_frame_nongeonode.shtml#lighting

Calculating Illumination

This section explains the way in which the various lighting parameters are used to characterize the illumination of the scene.

The lighting model is used to calculate the the color of every point on a surface under illumination. It is defined to be the sum of the illuminations from each individual light source.

The following symbols are used in all the lighting calculations below:

Note: L, V, N, and R are all unit vectors

The illumination at a point on a surface is dependent on both the properties of the surface and those of the illuminating light sources. The parameters of the surface statement define the various surface properties.

surfaceid
color   (Cr Cg Cb)
reflectivity   (Kamb Kdiff Kspec)
exponent   Nphong
metallic   m
endsurface

The (Cr Cg Cb) triple specifies the diffuse color of the surface - i.e. the color of the surface when viewed under diffuse illumination or the normal color of the surface. This is Cdiff.

Kamb, Kdiff, and Kspec are the ambient, diffuse, and specular reflection coefficients respectively. All should be between 0 and 1. Each is multiplied by the three color values of the surface to provide the reflectance properties of the surface for each of the three colors. The Kamb coeffiecient controls the fraction of ambient light that is reflected from the surface. This coefficient can be raised or lowered to match the general reflectivity of the surface (i.e. Kamb = Kdiff) or to represent the amount of ambient light that affects the object (i.e. the ambient light coefficient may be lowered if the object is believed to be in a dark corner of the scene.) The Kdiff coeffiecient controls the fraction of light that is reflected diffusely from the surface. This diffuse reflection is calculated with Lambert's law. The Kspec coeffiecient controls the fraction of light that is reflected specularly from the surface. This specular reflection is calculated according to the Phong illumination model.

Nphong is the exponent in Phong's specular term.

Lighting calculations should be performed in the light's coordinate system. The point being lit and its normal vector should be transformed to the light's coordinate system using Qlight<-object and Qobject<-lightT respectively. This will enable correct calculations for falloff in the case of a light that has been scaled and will allow for spotlights or point lights to be nonuniformly scaled in the world.


Metallic Surfaces

m is the metallic factor of the surface and is used to calculate Cspec, the specular color of the surface. The value of m should be between 0 and 1. The more metallic a surface is, the more of its natural color is reflected in specular reflections. If a surface is purely metallic (m=1) then specular reflections off the surface will have the same color as diffuse reflections; if a surface is purely plastic (m=0) then specular reflections will be exactly the color of the incoming light. If Clight is the color of the illuminating light, then

Cspec = mCdiffClight + (1-m)Clight


Ambient Light


light  id
  type  SLF_AMBIENT
  color (Cr Cg Cb)

endlight

An ambient light defines non-directional background illumination. The color of a surface illuminated by ambient light is

Iamb = KambCdiffClight

For example:

light bg
  type  SLF_AMBIENT
  color (0.86 0.2 0)
endlight
defines a reddish background illumination.

Directional Light


light  id

  type  SLF_DIRECTIONAL
  color (Cr Cg Cb)
endlight

A directional light is a light source at infinity with light being radiated in one principal direction, D = (xd yd zd). By default that direction is (0 0 -1), along the -z-axis, but it can be changed by the transformations that place the light in the scene. If Qworld<-light is the light's transformation then

D = (xd yd zd 0) = Qworld<-light [0 0 -1 0]T
L = -D / |D|

Similarly, the surface and its normal could be transformed to the light's coordinate system using Qlight<-world and Qworld<-lightT respectively. Then the default light vector, (0 0 -1), is used. This method will give correct results in the case of a scaled light source.

For a directional light, the normalized light vector, L, is constant for all points under consideration.

The color of a point on a surface illuminated by a directional light source is

Idir = Kdiff (N . L) CdiffClight + Kspec (R . V)Nphong Cspec


Point Light


light  id
  type         SLF_POINT
  color        (Cr Cg Cb)
  deaddistance (d0)
  falloff      (n1)

endlight

A point light source is located at a point P = (xp yp zp), and radiates light equally in all directions. By default that position is at the origin, (0 0 0), but it can be changed by the transformations that place the light in the scene. If Qworld<-light is the light's transformation then

P = (xp yp zp 1) = Qworld<-light [0 0 0 1]T

is the position of the light source in world coordinates.

Similarly, the surface and its normal could be transformed to the light's coordinate system using Qlight<-world and Qworld<-lightT respectively. Then the default light position, (0 0 0), is used. This method will give correct results in the case of a scaled light source.

The light vector, L, is different for each point on the surface and is the vector from the point under consideration to the light source. d0 is the dead distance and n1 is the exponent in the falloff factor.

The color of a point on a surface illuminated by a point light source is the same as for a directional light source except that it is attenuated with distance by a factor 1/(d0 + d)n1. If d is the distance from the light source to the point under consideration, the color of a point on a surface illuminated by a point light source is:

Ipoint = [Kdiff (N . L) CdiffClight + Kspec (R . V)Nphong Cspec] / (d0 + d)n1


Spot Light


light  id
  type           SLF_SPOT
  color          (Cr Cg Cb)
  deaddistance   (d0)
  falloff        (n1)
  angularfalloff (n2)

endlight
A spot light source is located at a point P = (xp yp zp), but like a directional light source, it radiates light in one principal direction. D = (xd yd zd) is the vector in the principal direction of the radiated light. By default the spot light source is positioned at the origin, (0 0 0), looking down the -z-axis, (0 0 -1). These can be changed by the transformations that place the light in the scene just as for the point light and the directional light respectively.

The light vector, L, is different for each point on the surface and is the unit vector from the point under consideration to P. d is the distance from the light source to the point. d0 and n1 are the same as for a point light and n2 is the exponent of the angular falloff between D and -L. The color of a point on a surface illuminated by a spot light source is the same as for a point light source except that it is attenuated with angle out of the beam by a factor [D . (-L)]n2. The color of a point on a surface illuminated by a spotlight is

Ispot = [Kdiff (N . L) CdiffClight + Kspec (R . V)Nphong Cspec] [D . (-L)]n2 / (d0 + d)n1