7. Lighting & Shading

Phong, Blinn-Phong, and the math that makes flat triangles look three-dimensional

A sphere made of flat-colored triangles looks like a disco ball. Add lighting and suddenly it looks smooth, solid, and three-dimensional. Lighting is what bridges the gap between geometry and realism.

This article covers the Phong reflection model — the foundation that every real-time renderer builds on — and its faster cousin, Blinn-Phong.

The Three Components of Light

The Phong model breaks the light arriving at a surface into three independent terms:

  1. Ambient — constant light that hits everything equally (fakes global illumination)
  2. Diffuse — light that scatters equally in all directions from a rough surface
  3. Specular — the bright highlight you see on shiny surfaces

The final color is the sum of all three:

I = I_ambient + I_diffuse + I_specular

Surface Normals

Before we can compute lighting, we need to know which direction each point on the surface faces. That direction is the surface normal — a unit vector perpendicular to the surface.

For a flat triangle, the normal is the same everywhere — the cross product of two edges:

n = normalize((v1 - v0) × (v2 - v0))

For smooth surfaces, we store a normal at each vertex and interpolate across the face using barycentric coordinates (from the rasterization article). This is called Phong shading — not to be confused with the Phong reflection model.

Interactive

Flat vs. smooth normals

Same low-poly icosphere, same lighting. The only difference is which normal is used at each pixel.

Ambient Light

The simplest term — a constant amount of light applied everywhere:

I_ambient = k_a * I_a
  • k_a — the material's ambient reflectance (a color)
  • I_a — the ambient light intensity

This prevents unlit areas from being pure black. It's a cheap hack — real global illumination is far more complex — but it works well enough for most real-time applications.

Diffuse Reflection (Lambert's Law)

A matte surface scatters incoming light equally in all directions. The amount of light hitting the surface depends on the angle between the surface normal and the light direction:

I_diffuse = k_d * I_L * max(0, n · l)
  • k_d — the material's diffuse color
  • I_L — the light's intensity and color
  • n — the surface normal (unit vector)
  • l — the direction from the surface to the light (unit vector)

The dot product n · l is the cosine of the angle between them. When the surface faces the light directly, the dot product is 1 (full brightness). When perpendicular, it's 0 (no light). We clamp to zero because negative values would mean the light is behind the surface.

Lambert's Cosine Law

A surface receives light proportional to the cosine of the angle between its normal and the light direction. This single dot product is the foundation of all diffuse lighting.

Interactive

Diffuse shading (Lambert)

Drag to move the light source around the sphere.

Specular Reflection (Phong)

Shiny surfaces have a bright spot where the light reflects most directly toward the viewer. The Phong model computes this using the reflection vector:

r = 2(n · l)n - l

The specular intensity depends on how closely the reflection direction aligns with the view direction:

I_specular = k_s * I_L * max(0, r · v)^alpha
  • k_s — the material's specular color (often white or gray)
  • v — the direction from the surface to the viewer (unit vector)
  • alpha — the shininess exponent — higher values make the highlight smaller and sharper
Interactive

Specular highlight

Adjust the shininess exponent to see the highlight change.

Blinn-Phong: A Faster Alternative

Jim Blinn proposed a modification that's both cheaper to compute and more physically plausible. Instead of computing the reflection vector, use the halfway vector between the light and view directions:

h = normalize(l + v)

The specular term becomes:

I_specular = k_s * I_L * max(0, n · h)^alpha'

This avoids the reflection vector computation (which requires a dot product and a multiply-add), replacing it with a single normalization and dot product. The exponent alpha' needs to be roughly 2-4x larger than the Phong exponent for a similar highlight size.

Blinn-Phong is the default lighting model in OpenGL's fixed-function pipeline and is still widely used in games.

Flat vs. Gouraud vs. Phong Shading

These are three different strategies for where to evaluate the lighting equation:

Flat shading — compute lighting once per triangle using the face normal. Fast but faceted.

Gouraud shading — compute lighting at each vertex, then interpolate the resulting colors across the triangle. Smooth but can miss specular highlights that fall in the middle of a large triangle.

Phong shading — interpolate the normals across the triangle, then compute lighting per pixel. Most accurate, and the standard approach on modern GPUs (fragment shaders evaluate per pixel).

Interactive

Flat vs. Gouraud vs. Phong shading

Same geometry and lighting. Flat shades per face. Gouraud shades per vertex and interpolates color. Phong interpolates normals and shades per pixel.

Multiple Lights

The Phong model is additive — for multiple lights, simply sum the diffuse and specular contributions from each:

I = I_ambient + sum( I_diffuse_i + I_specular_i )

Each light has its own position/direction, color, and intensity. The ambient term is computed only once.

Light Types

  • Directional light — infinitely far away, all rays parallel (like the sun). The light direction l is constant everywhere.
  • Point light — emits in all directions from a position. The direction l varies per fragment, and intensity drops off with distance (usually 1/d²).
  • Spot light — a point light with a cone. Fragments outside the cone receive no light. A smoothstep on the cone angle creates a soft edge.

Attenuation

Real light dims with distance. The standard attenuation formula uses three constants:

atten = 1 / (k_c + k_l * d + k_q * d²)

Where d is the distance from the light to the surface. The quadratic term (k_q) dominates at distance and matches the inverse-square law of real light.

Normal Mapping (A Preview)

Interpolated vertex normals can only approximate smooth surfaces. To add fine surface detail — bumps, scratches, pores — without adding geometry, we can use a normal map: a texture that stores perturbed normals at each texel.

During shading, instead of using the interpolated vertex normal, we sample the normal map and use that as n in the lighting equation. The geometry stays simple, but the shading looks complex.

What's Next

Phong and Blinn-Phong are fast approximations that work well for real-time rendering. But a lit surface with a single flat color still doesn't look like much. To give it the appearance of brick, wood, or metal, we need to paint an image onto it — that's texture mapping.