Skip to content

26. Mathematics of Lighting and Shading part 2

Dated: 01-07-2025

Light Types

Parallel or Directional Lights

These are used to simulate sun (93 million miles) and makes math easy. The attenuation factor is always \(1\) (for point or spotlights, it generally involves divisions if not square roots). Because they are simpler, the frame rates remain reasonably high enough.

Point Lights

These are one step better than parallel lights and we have intensity falloff as

\[\text{Attenuation Factor} = \frac k {d^2}\]
\[\text{where }d = |\text{surface location} - \text{light location}|\]

cs602_e_26_1.svg

\[\vec l + \vec L = \vec s\]
\[\vec L = \vec s - \vec l\]
\[|L| \hat L = \vec s - \vec l\]
\[\hat L = \frac {\vec s - \vec l} {|L|}\]
\[\hat L = \frac {\vec s - \vec l} {|\vec s - \vec l|}\]
\[\text{light direction} = \frac{\text{surface location} - \text{light location}}{|\text{surface location} - \text{light location}|}\]

cs602_i_26_1.png

Point Light

Spotlights

These should be avoided and are not for real time environment.

cs602_i_26_2.png

The inside cone defined by \(\theta\) angle and outside cone defined by \(\phi\) .

The light from inner cone to the outer cone has a linear fade out effect (i.e. the intensity depends upon how close a point is to the inner cone).

cs602_i_26_3.png

Linear fade out effect

Shading Models

Lambert

Triangles that use Lambertian shading are painted with one solid color instead of using a gradient. To light a triangle, you compute the lighting equations using the triangle's normal and any of the three vertices of the triangle.

cs602_i_26_4.png

Flat Shaded

Gouraud

Gouraud (pronounced garrow) shading is the current de facto shading standard in accelerated 3D hardware. Instead of specifying one color to use for the entire triangle, each vertex has its own separate color. The color values are linearly interpolated across the triangle, creating a smooth transition between the vertex color values. To calculate the lighting for a vertex, we use the position of the vertex and a vertex normal.1

cs602_i_26_5.png

Flat Shading vs Gouraud

One problem with Gouraud shading is that the triangles' intensities can never be greater than the intensities at the edges. So if there is a spotlight shining directly into the center of a large triangle, Gouraud shading will interpolate the intensities at the three dark corners, resulting in an incorrectly dark triangle.

Phong

Phong is relatively more realistic as it makes highlights on the surface (i.e. shininess). The way Phong does this is by interpolating the normal1 across the triangle face, not the color value, and the lighting equation is solved individually for each pixel.2

cs602_i_26_6.png

Phong
Source: metaltutorial.com

References


  1. Read more about surface normals

  2. Read more about pixels