Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 50 ยท Lighting and Shading

Flat, Gouraud and Phong Shading

Compare flat, Gouraud and Phong shading: per-face, per-vertex and per-pixel lighting, normal interpolation and Mach bands, with a live renderer.

The previous lesson gave you a formula that turns a normal, a light direction and a viewer direction into a color. But a 3D model is made of flat triangles, and a triangle covers hundreds of pixels. Where exactly should the formula be evaluated? Once for the whole triangle? At its three corners? At every pixel? That choice is called the shading method, and it decides whether a round object looks like a faceted gem or a smooth ball.

There are three classic answers: flat, Gouraud and Phong shading. They trade quality against cost, and the difference between the last two explains why modern GPUs run a small program for every pixel.

flowchart TD
  M["Triangle with vertex normals"] --> F["Flat:<br/>light once per face"]
  M --> G["Gouraud:<br/>light at 3 vertices,<br/>interpolate colors"]
  M --> P["Phong:<br/>interpolate normals,<br/>light at every pixel"]
  F --> R["Faceted look"]
  G --> R2["Smooth, but highlights<br/>can be lost"]
  P --> R3["Smooth and accurate<br/>highlights"]

The same mesh, three places to evaluate the lighting formula.

Flat shading

With flat shading you compute the lighting once per triangle, using the triangle's own face normal, and give every pixel of the triangle that one color. The face normal comes from the cross product of two edges (see polygon meshes):

TEXT
n = normalize( (v1 - v0) x (v2 - v0) )

Flat shading is the cheapest method, and it is exact for a surface that really is flat, such as a cube. For a curved object approximated by triangles it makes every facet visible, because neighboring triangles have different normals and therefore different colors. Sometimes that is the look you want (low-poly art), but usually you want the mesh to disappear.

Vertex normals

To hide the facets we pretend the surface is smooth. Instead of the normal of each face, we store a normal at each vertex that approximates the normal of the true smooth surface there. For shapes you build from formulas, such as a sphere, the exact normal is available: on a unit sphere centered at the origin it is the vertex position. For an arbitrary mesh, a common recipe is to average the face normals of all the triangles that share the vertex, then normalize the result:

JavaScript
// accumulate each face's normal into its three vertices, then normalize
const normals = vertices.map(() => [0, 0, 0]);
for (const [a, b, c] of faces) {
  const n = cross(sub(vertices[b], vertices[a]), sub(vertices[c], vertices[a]));  // length = 2 * area
  for (const i of [a, b, c]) for (let k = 0; k < 3; k++) normals[i][k] += n[k];
}
normals.forEach(n => { const len = Math.hypot(...n); for (let k = 0; k < 3; k++) n[k] /= len; });

Because the cross product's length is twice the triangle's area, larger faces automatically count more (area weighting). Where you want a hard edge, such as the rim of a cube, you duplicate the vertex so each side keeps its own normal.

Gouraud shading

Gouraud shading (Henri Gouraud, 1971) evaluates the lighting formula only at the three vertices, using the vertex normals. That gives three colors. The pixels in between get their color by interpolating the three vertex colors with the barycentric weights from the triangle rasterization lesson:

TEXT
color(pixel) = w0 * color0 + w1 * color1 + w2 * color2

It is cheap, because the expensive formula runs a few times per triangle instead of once per pixel, and the interpolation is just the gradient code you already have. Colors look smooth across triangle borders because neighboring triangles share vertices and therefore agree along the shared edge.

The weakness is that the lighting formula is not linear, so interpolating its output differs from evaluating it at the interpolated normal. The specular highlight suffers most. A small sharp highlight that falls inside a triangle, away from its corners, is never seen, because all three corners are dark. When the object turns, the highlight pops on as it reaches a vertex and fades off again. A large triangle can even lose the highlight altogether.

Phong shading

Phong shading (not the same thing as the Phong reflection model, though Phong invented both) interpolates the normals instead of the colors. For each pixel:

  1. Interpolate the vertex normals with the barycentric weights.
  2. Renormalize the result (interpolating unit vectors shortens them).
  3. Evaluate the full lighting formula with that normal.
TEXT
n(pixel) = normalize( w0 * n0 + w1 * n1 + w2 * n2 )
color(pixel) = light(n(pixel), L, V)

The highlight is computed where it belongs, so it is sharp and stable no matter how coarse the mesh is. The price is running the lighting formula at every pixel. Early hardware could not afford that. Today's GPUs run a fragment (pixel) shader for every pixel anyway, so per-pixel lighting is the normal way to light things, and the term "Gouraud shading" now usually means "lighting calculated per vertex".

Even Phong shading leaves one clue that the object is polygonal: the silhouette. The outline of the sphere is still made of straight segments, because only the shading is smooth, not the geometry.

A live comparison

The demo renders the same sphere mesh three times, with a software rasterizer you could write yourself. It uses an orthographic view, a fixed light, Blinn-Phong lighting and gamma-corrected output. Lower the mesh detail to see the differences exaggerated: flat shows facets, Gouraud smears or loses the highlight, Phong keeps a clean round spot. Switch on spin and watch the Gouraud highlight flicker as the sphere turns.

Flat, Gouraud and Phong side by side

Notice that with spin on and a coarse mesh, the Gouraud sphere's highlight pulses, while the Phong sphere's highlight stays put as it should: the light and viewer do not move, only the surface turns beneath them.

Mach bands

Gouraud shading has a second, perceptual problem. Within a triangle the color changes linearly, but across a triangle border the rate of change jumps. Your visual system exaggerates such kinks in brightness: you see a faint lighter or darker line along the edge, even though no such line exists in the data. These illusory stripes are called Mach bands, after the physicist Ernst Mach.

Mach bands appear in any piecewise-linear gradient. The second demo draws one on purpose: a flat dark region, a linear ramp, then a flat bright region. Look near the two bends. A slightly darker stripe seems to hug the start of the ramp, and a slightly brighter stripe the end. The red graph shows that the true values do nothing of the sort. Switch to the smooth ramp, whose slope changes gradually, and the effect largely disappears. Phong shading avoids the problem because normals interpolate linearly, but the lighting function turns them into smooth color curves, so the slope of the brightness no longer jumps at triangle borders.

Mach bands at the kinks of a linear ramp

Which one should you use?

Method Lighting evaluated Cost Looks like
Flat once per face lowest faceted, exact for truly flat surfaces
Gouraud once per vertex low smooth, but weak or flickering highlights, Mach bands
Phong once per pixel highest smooth and accurate highlights

Use flat shading for deliberately angular styles and for debugging, per-vertex lighting where vertices are dense or the surface is purely diffuse, and per-pixel lighting for everything else. Interpolating attributes across a triangle in a perspective view needs one more correction, which you will see in the texture mapping lesson.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Where does flat shading evaluate the lighting formula?

    Flat shading uses one normal per triangle and gives the whole triangle one color, which is why curved objects look faceted.
  2. 2. What does Gouraud shading interpolate across a triangle?

    Gouraud lights the three vertices and blends the resulting colors with barycentric weights.
  3. 3. Why can Gouraud shading miss a specular highlight?

    The highlight is only sampled at the vertices. If it falls between them, the interpolated colors never contain it.
  4. 4. What extra step is needed after interpolating normals in Phong shading?

    A weighted average of unit vectors is shorter than 1, and the lighting formula assumes unit length, so the interpolated normal is normalized again.
  5. 5. Which of these is still polygonal even with Phong shading?

    Shading only changes the colors inside the triangles. The outline is made of the real, straight triangle edges.

Summary

  • Shading methods differ in where the lighting formula runs: per face (flat), per vertex (Gouraud) or per pixel (Phong).
  • Smooth surfaces use vertex normals, either exact or the area-weighted average of the surrounding face normals.
  • Gouraud interpolates colors: cheap, but highlights between vertices are lost and Mach bands can appear.
  • Phong shading interpolates and renormalizes normals, then lights each pixel; this is what GPU fragment shaders do.
  • Shading cannot smooth the silhouette: only more triangles can.

Next up is giving surfaces detail without adding triangles at all: Texture Mapping.