Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 50 ยท Lighting and Shading

Texture Mapping

Learn texture mapping: UV coordinates, nearest and bilinear filtering, mipmaps, perspective-correct interpolation and procedural textures, with live demos.

Shading gives a surface smooth color, but real objects have detail: wood grain, brick joints, printed labels. Modeling every brick as geometry would need millions of triangles. Texture mapping takes a cheaper road: store the detail in an ordinary image, the texture, and look up a color from it for every pixel you draw.

The idea sounds simple, and the core of it is. The tricky parts are the questions around it. How do you say which spot of the image belongs to which spot of the triangle? What do you do when the image is smaller or larger than the area it covers? And why do textures swim and bend on a tilted surface unless you interpolate them with care? This lesson answers all three, with a software renderer you can poke at.

UV coordinates

Every vertex of a mesh carries, besides its position, a pair of texture coordinates called (u, v) (sometimes written (s, t)). They say which point of the texture image sits on that vertex. Both run from 0 to 1 across the image, independent of its pixel size, so the same coordinates work for a 64 x 64 and a 4096 x 4096 texture.

flowchart LR
  A["Vertex:<br/>position (x, y, z)<br/>uv (u, v)"] --> B["Rasterizer interpolates<br/>uv across the triangle"]
  B --> C["Per pixel:<br/>(u, v)"]
  C --> D["Texel lookup<br/>and filtering"]
  D --> E["Color used in<br/>the lighting formula"]

Texture coordinates travel with the vertices, are interpolated per pixel, and select the texel color.

Conventions differ between systems: OpenGL puts (0, 0) at the bottom-left of the image, while many image formats and Direct3D start at the top-left. Pick one and stay consistent.

A pixel of the texture is called a texel. To turn a coordinate into a texel index, scale by the texture size:

TEXT
x = u * width      y = v * height

Values outside 0 to 1 are legal, and the wrap mode decides what happens: repeat tiles the image (u - floor(u)), clamp sticks to the edge texels, mirror flips every other tile. Tiling is how a small brick image covers a whole wall.

For a rectangle the mapping is obvious: corners get (0,0), (1,0), (1,1), (0,1). For a sphere, longitude and latitude become u and v. For arbitrary models, artists "unwrap" the surface into a flat 2D layout, much like cutting a globe into a map, and paint on that layout.

Looking up a texel: nearest and bilinear

The first filter is the plainest one. Nearest-neighbor sampling rounds to a single texel and returns its color:

JavaScript
function sampleNearest(tex, u, v) {
  const x = Math.min(tex.w - 1, Math.floor(u * tex.w));
  const y = Math.min(tex.h - 1, Math.floor(v * tex.h));
  return tex.get(x, y);
}

When the texture is magnified, meaning one texel covers several screen pixels, nearest sampling shows the texels as hard blocks. That is fine for pixel art and ugly for a photo.

Bilinear filtering blends the four texels around the sample point. Treat texel colors as living at the texel centers, which sit at half-integer positions, so shift by one half before splitting into whole and fractional parts:

JavaScript
function sampleBilinear(tex, u, v) {
  const x = u * tex.w - 0.5, y = v * tex.h - 0.5;
  const x0 = Math.floor(x), y0 = Math.floor(y);
  const fx = x - x0, fy = y - y0;                  // fractions in [0, 1)
  const top = mix(tex.get(x0, y0),     tex.get(x0 + 1, y0),     fx);
  const bottom = mix(tex.get(x0, y0 + 1), tex.get(x0 + 1, y0 + 1), fx);
  return mix(top, bottom, fy);
}

Here mix(a, b, t) is plain linear interpolation, done once per axis, hence "bi-linear". The result is smooth, at the cost of four reads and some blur. The tex.get helper applies the wrap mode to the integer coordinates.

The demo below stores a tiny 8 x 8 texture and magnifies it with both filters. Drag the slider to change the magnification and watch the blocks stay crisp on the left and melt together on the right.

Nearest versus bilinear magnification

Bilinear filtering also needs a policy at the borders: with clamp the outermost texels repeat, with repeat the opposite edge bleeds in. The demo clamps.

Minification and mipmaps

Now flip the situation. A texture of 1024 x 1024 texels painted onto a wall that is only 50 pixels wide on screen is minified: each pixel covers about 20 x 20 texels. Sampling one texel per pixel, with either filter, throws almost all of them away. Which texel you hit changes with the slightest movement, so fine patterns crawl and sparkle. This is aliasing again, in texture space.

The true answer is the average of all texels under the pixel, but computing that per pixel would be far too slow. Mipmapping precomputes it. You store the texture and a pyramid of smaller copies, each half the size of the last (each texel is the average of a 2 x 2 block above it), down to 1 x 1. This costs only one third more memory. At draw time, pick the level whose texels are about one pixel big, then sample it.

flowchart LR
  L0["Level 0<br/>64 x 64"] --> L1["Level 1<br/>32 x 32"] --> L2["Level 2<br/>16 x 16"] --> L3["Level 3<br/>8 x 8"] --> L4["... 1 x 1"]

A mipmap chain: each level averages 2 x 2 blocks of the one before.

The level follows from how fast the texture coordinates change per pixel. If one screen pixel step moves d texels, then level = log2(d). GPUs measure this from neighboring pixels automatically. Trilinear filtering blends two adjacent levels, so the switch between levels is invisible; anisotropic filtering takes several samples along the stretched direction for surfaces seen at a glancing angle.

The demo draws a finely striped texture into a square of adjustable size. Left: level 0 only, nearest. Right: mipmap level chosen from the size. Shrink the square and compare.

Minification with and without mipmaps

With stripes this fine, the left square becomes a coarse random-looking pattern that changes erratically as you drag, while the right one settles to a steady gray that matches the average brightness of the stripes. That steady gray is what your eye expects from a distant striped surface.

Perspective-correct interpolation

Now the subtle part. The rasterizer interpolates attributes with barycentric weights computed in screen space. That is exactly right for colors in a flat 2D triangle, but a 3D triangle that was projected is not a linear image of the original. Parallel lines on a floor converge toward the horizon, so equal steps on the screen do not match equal steps along the surface. If you interpolate u linearly in screen space (affine mapping), the texture visibly bends along the triangle's diagonal and stretches in the wrong places.

The cure uses the fact that 1/z (more precisely 1/w, the clip-space divisor from the projection matrix) is linear in screen space. So interpolate u/w, v/w and 1/w with the ordinary barycentric weights, then divide at each pixel:

TEXT
u = (b0*u0/w0 + b1*u1/w1 + b2*u2/w2) / (b0/w0 + b1/w1 + b2/w2)

The same recipe applies to every attribute you want to vary correctly across a triangle: texture coordinates, vertex colors, normals. GPUs do this automatically for every varying.

The demo projects a rectangular floor in perspective and fills it with a checkerboard texture. Use the radio buttons to switch between affine and perspective-correct interpolation, and drag the far edge farther away to make the difference more extreme.

Affine versus perspective-correct texturing

In affine mode the checker squares are the same height from front to back and the pattern kinks along the diagonal where the two triangles meet. In perspective-correct mode the squares shrink toward the horizon, as a real tiled floor does, and the diagonal vanishes.

Procedural textures

A texture does not have to be an image. A procedural texture computes the color from (u, v) with a function, so it needs no memory, has unlimited resolution and can be tuned with parameters. Checkerboards, stripes, gradients and bricks are a few lines of arithmetic. For natural-looking randomness (marble, clouds, wood), shaders use noise functions built from a hash of integer grid points and smooth interpolation, the same bilinear idea you just saw, applied to random values.

The final demo evaluates four procedural patterns on a grid. Change the pattern and the scale and note that the detail never gets blurry however high the scale.

Procedural textures

Textures beyond color

Once you can look things up in an image, you can store any per-surface quantity in it. A normal map stores perturbed normals so flat polygons light as if they were bumpy. A specular map varies shininess, an alpha map punches holes for leaves and fences, and a light map bakes shadows computed ahead of time. In a fragment shader all of these are just extra lookups feeding the lighting formula.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What do the (u, v) coordinates of a vertex describe?

    UV coordinates are stored per vertex, interpolated across the triangle, and used to look up a texel for each pixel. They run from 0 to 1 regardless of the image size.
  2. 2. Why does bilinear filtering subtract 0.5 from u * width before taking the floor?

    A texel's value belongs to its center, at x + 0.5. Shifting by one half makes the four surrounding centers the ones that get blended.
  3. 3. What problem do mipmaps solve?

    When a texture is shrunk, sampling a single texel per pixel produces flicker. A mip level whose texels are about a pixel wide already holds the average.
  4. 4. In perspective-correct interpolation, which quantities are interpolated linearly in screen space?

    Dividing by w makes the attributes linear in screen space. The per-pixel division of u/w by 1/w then recovers the correct u.
  5. 5. What is the main advantage of a procedural texture?

    A function of (u, v) can be evaluated at any scale without blur and with adjustable parameters, though complex ones can cost more than a lookup.

Summary

  • Textures store detail in an image; each vertex carries (u, v) coordinates that the rasterizer interpolates.
  • Wrap modes (repeat, clamp, mirror) define what happens outside 0 to 1.
  • Nearest sampling is blocky when magnified; bilinear blends the four nearest texel centers.
  • Mipmaps are a pyramid of averaged copies that fix aliasing when a texture is minified; trilinear blends two levels.
  • Interpolate u/w, v/w and 1/w, then divide per pixel, for perspective-correct results.
  • Procedural textures compute colors from functions, and textures can store normals, shininess, transparency and more.

Next, move from surfaces to the lines that shape them: Parametric Curves and Interpolation.