Last updated: 2026-11-26

21. Bump and normal maps

For new readers

A smooth surface can be shaded as though it were rough by tilting its normal: a normal map gives the tilt directly, a height map gives it by its slope. The tilt is made in the surface's own frame (from the mapping that places the picture) so that it survives any stretching of the shape. Light at a point then shades with the tilted normal.

Normal Maps and Bump Maps

A texture map changes the colour of a surface. A bump changes how the surface faces the light: shading depends on the normal, so a picture that tilts the normal makes a smooth sphere look dimpled or a flat wall look like brick, with no extra geometry and no extra rays. The silhouette stays smooth, which is the price.

Two kinds of picture do it:

  • a normal map stores a tilted normal in each pixel, as a vector in tangent space: x along the direction in which u grows, y along the direction in which v grows, z along the surface normal, each component stored as (x + 1) / 2 so that (1/2, 1/2, 1) (the blue-purple of the usual pictures) is "no change";
  • a height map stores a height in each pixel (brightness, bright is high), and the normal tilts against its slope.

Both use the mappings of Pictures on surfaces: texture and cube maps, so a bump is normal_map(picture, mapping) or height_map(picture, mapping), goes into a material with with_bump, and has its own transformation (set_bump_transform) and strength (set_bump_strength).cf. texture maps: same lookup, different use

Step 1 — a frame at every point

Tangent space needs two directions on the surface, T where u increases and B where v increases. For each mapping they are a short formula in the pattern-space point. On a sphere, T is east, (−z, 0, x) / ρ with ρ = √(x² + z²), and B is north. At a pole ρ = 0: u has no direction there, and the surface keeps its geometric normal.

Then the frame has to reach the world. T and B are vectors lying in the surface, so unlike the normal they travel by the shape's own matrix (and its groups'), not by the inverse transpose. The new vector_to_world does that. In the world the two are made perpendicular to the geometric normal and to each other, and the bumped normal is x T + y B + z N. Because this last step is done in the world, the tilt is the same angle however the shape is stretched.

# a direction (a vector, not a normal) from object space to world space: this shape's
# transformation, then each enclosing group's. Tangents move this way, normals do not.
make a function called vector_to_world takes s, v returns w
  let w = v
  if not s[8] then
    let w = mat_tuple(s[1], v)
  end
  if s[7] != false then
    let w = vector_to_world(s[7], w)
  end
  return w
end

Step 2 — the bump itself

For a height map the slope comes from the picture itself: the brightness one pixel either side, halved, gives the change of height per pixel in u and in v, and the tangent-space normal is (−s g_u, −s g_v, 1) for a strength s. A strength of 4 turns a ramp that rises a quarter of the range per pixel into a slope of 1, a 45 degree tilt. For a normal map the pixel is the vector, and the strength scales its sideways part before it is normalised. Both sample with the bilinear smoothing of Pictures on surfaces: texture and cube maps, so the normal does not jump from pixel to pixel.

make a function called normal_map takes img, mapping returns b
  return ["normal_map", mapping, img, 1, identity_matrix(), identity_matrix()]
end

make a function called height_map takes img, mapping returns b
  return ["height_map", mapping, img, 1, identity_matrix(), identity_matrix()]
end

make a function called set_bump_strength takes b, s returns b2
  return [b[0], b[1], b[2], s, b[4], b[5]]
end

make a function called set_bump_transform takes b, m returns b2
  return [b[0], b[1], b[2], b[3], m, inverse(m)]
end

# [T, B] at the pattern-space point q: unit vectors tangent to the mapped surface in the
# directions of increasing u and increasing v; false where there is none (a pole)
make a function called uv_frame takes mapping, q returns frame
  if mapping == "planar" then
    return [vector(1, 0, 0), vector(0, 0, 1)]
  end
  if mapping == "cube" then
    # u runs along the viewer's right and v along its up, face by face (texture.patlang)
    let f = cube_face_index(q)
    let ts = [vector(0, 0, -1), vector(1, 0, 0), vector(0, 0, 1), vector(-1, 0, 0), vector(1, 0, 0), vector(1, 0, 0)]
    let bs = [vector(0, 1, 0), vector(0, 1, 0), vector(0, 1, 0), vector(0, 1, 0), vector(0, 0, 1), vector(0, 0, -1)]
    return [ts[f], bs[f]]
  end
  let x = get_x(q)
  let y = get_y(q)
  let z = get_z(q)
  let rho = sqrt(x * x + z * z)
  if rho < 0.000000000001 then
    return false
  end
  let t = vector(-z / rho, 0, x / rho)
  if mapping == "cylindrical" then
    return [t, vector(0, 1, 0)]
  end
  let r = sqrt(rho * rho + y * y)
  return [t, vector(-x * y / (rho * r), rho / r, -z * y / (rho * r))]
end

# the height of the picture at (u, v), smoothed
make a function called bump_height takes smooth, u, v returns h
  let c = uv_pattern_at(smooth, u, v)
  return (red(c) + green(c) + blue(c)) / 3
end

# the tangent-space vector [x, y, z] of the bump at (u, v), strength applied
make a function called bump_vector takes b, u, v returns tv
  let smooth = uv_image_smooth(b[2])
  let s = b[3]
  if b[0] == "normal_map" then
    let c = uv_pattern_at(smooth, u, v)
    return [s * (2 * red(c) - 1), s * (2 * green(c) - 1), 2 * blue(c) - 1]
  end
  # a height map: central differences one pixel either side, per pixel
  let du = 1 / (b[2][0] - 1)
  let dv = 1 / (b[2][1] - 1)
  let gx = (bump_height(smooth, u + du, v) - bump_height(smooth, u - du, v)) / 2
  let gy = (bump_height(smooth, u, v + dv) - bump_height(smooth, u, v - dv)) / 2
  return [-s * gx, -s * gy, 1]
end

# the geometric normal n (a unit vector in world space, already turned to face the eye) of
# the shape at the world point wp, tilted by the shape's bump; n itself if it has none
make a function called perturb_normal takes s, wp, n returns n2
  let b = m_bump(shape_material(s))
  if b == false or b[3] == 0 then
    return n
  end
  let q = mat_tuple(b[5], world_to_object(s, wp))
  let f = uv_frame(b[1], q)
  if f == false then
    return n
  end
  let uv = uv_map(b[1], q)
  let tv = bump_vector(b, uv[0], uv[1])
  let t = vector_to_world(s, mat_tuple(b[4], f[0]))
  let t = tuple_sub(t, tuple_mul(n, dot(t, n)))
  # a mapping whose tangent lies along the normal (a planar map seen edge-on) has no frame there
  if dot(t, t) < 0.000000000001 then
    return n
  end
  let t = normalize(t)
  let bt = vector_to_world(s, mat_tuple(b[4], f[1]))
  let bt = tuple_sub(tuple_sub(bt, tuple_mul(n, dot(bt, n))), tuple_mul(t, dot(bt, t)))
  if dot(bt, bt) < 0.000000000001 then
    return n
  end
  let bt = normalize(bt)
  return normalize(tuple_add(tuple_add(tuple_mul(t, tv[0]), tuple_mul(bt, tv[1])), tuple_mul(n, tv[2])))
end

prepare_computations applies it after it has decided whether the hit is from inside: that is a fact about the geometric normal, and a bumped normal near a grazing angle may point away from the eye without the ray being inside anything. A material has a tenth field for the bump, false by default, and a shape without one costs a single test.

Step 3 — scenarios and oracles

  • The material: no bump by default, with_bump changes only that field.
  • Frames against numerical derivatives of the mapping. For 40 points on a sphere and each of the spherical and cylindrical mappings, T and B are unit and perpendicular, and a step of 10⁻⁶ along T raises u at a real rate and leaves v where it was, along B the reverse. The formulas for T and B and the mappings share nothing but the geometry, so one cannot hide a mistake in the other. The planar frame is +x, +z; at (1, 0, 0) on a sphere T is +z and B is +y; a pole has no frame.
  • A uniform normal map tilting every pixel by an angle a turns the normal by exactly a, toward T or toward B as the colour says, at points on the axes where the frame is known by hand; strength 2 doubles the sideways part; strength 0 and a flat map change nothing.
  • The tilt angle survives any transformation: on 30 points of a sphere stretched 2 × ½ × 3, turned and moved, the dot product of the bumped and the geometric normal is cos a to 10⁻⁸, and the same for a sphere inside a rotated group.
  • Height maps: a constant picture changes nothing; a ramp rising along u at slope 1 tilts the normal 45 degrees toward −T, the same ramp along v toward −B; strength 2 gives atan(1/2); and the ramp agrees with the normal map of the same tilt.
  • In a world: a flat normal map gives the same colour as no map on 20 rays; a tilted one changes all 20; prepare_computations carries the turned normal and still reports inside correctly from inside the sphere.
  • Negative controls: a planar frame on a slanting point of a sphere tilts elsewhere than the spherical one; slope 1 is not slope 1/2.

Everything before still passes (material gained a field) and no benchmark picture changed:

Step 4 — Putting it together

Three of the pictures are made by formula (tools/make_bump_textures.patlang): a brick wall whose darkest part is the mortar, so that one picture is the wall's colour and its height map, and a grey height map of 8 × 8 dimples. The fourth is the normal map that comes with the grass of Pictures on surfaces: texture and cube maps.

> (cd tools && v2_interp.exe make_bump_textures.patlang)
wrote textures/bricks.ppm and textures/dimples.ppm, 128x128

the brick picture the dimple height map

The scene: the lawn with its colour and normal map, a brick wall from the one picture, a golf ball (dimples, strength 3), a smooth ball of the same material beside it for comparison, and a mossy ball with the grass colour and normals.

# chapter 22: normal and height maps. `tex` is [grass colour, grass normal map, bricks,
# dimples]. A lawn with the grass picture's own normal map, a brick wall whose picture is
# both its colour and its height map, a golf ball (a height map of dimples) beside a smooth
# ball of the same material, and a mossy ball wearing the grass colour and normals.
make a function called build_bump_scene takes tex returns w
  let tile = scaling((3/2), 1, (3/2))
  let lawn_c = set_pattern_transform(texture_map(uv_image_smooth(tex[0]), "planar"), tile)
  let lawn_b = set_bump_transform(normal_map(tex[1], "planar"), tile)
  let lawn_m = with_bump(with_pattern(material_with(color(1, 1, 1), (1/5), (8/10), 0, 200), lawn_c), lawn_b)
  let lawn = set_material(plane(), lawn_m)
  let brick_t = scaling(4, 1, 4)
  let wall_c = set_pattern_transform(texture_map(uv_image_smooth(tex[2]), "planar"), brick_t)
  let wall_b = set_bump_strength(set_bump_transform(height_map(tex[2], "planar"), brick_t), 6)
  let wall_m = with_bump(with_pattern(material_with(color(1, 1, 1), (3/10), (7/10), (1/10), 30), wall_c), wall_b)
  let wall = set_material(set_transform(plane(), mat_mul(translation(0, 0, 5), rotation_x(pi() / 2))), wall_m)
  let ball_m = material_with(color((95/100), (95/100), (9/10)), (1/10), (8/10), (6/10), 80)
  let golf_b = set_bump_strength(height_map(tex[3], "spherical"), 3)
  let golf = set_material(set_transform(sphere(), translation(-(5/2), 1, 0)), with_bump(ball_m, golf_b))
  let smooth_ball = set_material(set_transform(sphere(), mat_mul(translation((1/2), (7/10), -(3/2)), scaling((7/10), (7/10), (7/10)))), ball_m)
  let moss_c = texture_map(uv_image_smooth(tex[0]), "spherical")
  let moss_b = set_bump_strength(normal_map(tex[1], "spherical"), (3/2))
  let moss_m = with_bump(with_pattern(material_with(color(1, 1, 1), (1/5), (8/10), (1/10), 30), moss_c), moss_b)
  let moss = set_material(set_transform(sphere(), translation((5/2), 1, 0)), moss_m)
  return [[lawn, wall, golf, smooth_ball, moss], point_light(point(-6, 8, -6), color(1, 1, 1)), true]
end
> tools/prender.sh examples/ch22_bump.patlang
assembled 400x200 from 40 bands
rendered with 9 parallel processes (40 bands) in 426 s wall clock
a brick wall, a golf ball, a smooth ball, a mossy ball and a lawn

Checking it independently

The exporter writes a B line for each bump: the mapping, the kind, the strength, the PPM file, and the matrix. The independent tracer reads the picture with its own reader and takes the tangent frame from the parametrisation of the surface (a point at longitude L and latitude φ is (cos φ sin L, sin φ, −cos φ cos L), so T is along its derivative in u, (cos L, 0, sin L)) instead of the closed forms above; the slope of a height map is sampled with its own bilinear function.

> v2_interp.exe tools/export_scene.patlang --scene bump --out out/bump_scene.txt
wrote out/bump_scene.txt (5 shapes)
> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/ch22_bump.png -SceneFile out/bump_scene.txt -Stride 4
decoded by .NET: 400x200; pixels differing from the independent recursive tracer by more than 1 level: 0 (max difference 0); stride 4 in each direction, 5000 pixels checked

Negative controls: the same scene without its bumps, and with the golf ball's strength changed from 3 to 2.

grep -v "^B " out/bump_scene.txt > out/bump_scene_nobump.txt
sed 's/^B 2 spherical height_map 3/B 2 spherical height_map 2/' out/bump_scene.txt > out/bump_scene_strength.txt
> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/ch22_bump.png -SceneFile out/bump_scene_nobump.txt -Stride 4
decoded by .NET: 400x200; pixels differing from the independent recursive tracer by more than 1 level: 2936 (max difference 121); stride 4 in each direction, 5000 pixels checked
> powershell.exe -NoProfile -File tools/verify_recursive.ps1 out/ch22_bump.png -SceneFile out/bump_scene_strength.txt -Stride 4
decoded by .NET: 400x200; pixels differing from the independent recursive tracer by more than 1 level: 223 (max difference 69); stride 4 in each direction, 5000 pixels checked

How it is checked

> v2_interp.exe tests/run.patlang --node surfaces/bump --depth 2
[ ok ] Bump and normal maps
[ ok ]   A flat normal map, or strength 0, leaves the normal alone
[ ok ]   A normal map's pixel is the normal in the surface's frame (u along x on a floor)
[ ok ]   A height map rising along x tilts the normal back towards -x, more with more strength
[ ok ]   The tilt is the same however the surface is stretched or turned
[ ok ]   The tilted normal is the one prepared for shading
[ ok ]   At a sphere's pole, where u has no direction, the normal is kept
[ ok ]   The tangent frame of a mapping points where u and v grow, at right angles to the surface
[ ok ]   Further checks, independent of the code they check
[ ok ]     Material
[ ok ]     Tangent frames
[ ok ]     Normal maps
[ ok ]     Height maps
[ ok ]     In a world
checks: 13 passed, 0 failed, 0 blocked, 0 skipped, 0 not run
ALL PASSED

Graduate Attributes

Modelled on this page

  • Subject Specialist: Systematic knowledge of computer graphics is applied to solve the problem of simulating surface roughness without adding geometry.
  • Critical Thinker: Reliable evidence is sought through negative controls that compare the rendered scene with and without bump mapping to verify the effect.
  • Digitally Fluent: Discipline-specific digital tools are used to implement tangent space calculations and bilinear sampling for texture mapping.

University of Reading Graduate Attributes framework.