Introduction

Look closely at a brick wall in a game and you’ll usually find it’s a flat rectangle made of two triangles, with a picture of bricks on it. That picture is a texture, and it’s one of the oldest and most important tricks in real-time graphics: it adds detail without adding triangles. This post, the first in the Textures & Sampling series, covers:

  • What a texture is, and what a texel is
  • UV coordinates: how each vertex says which part of the texture it shows
  • Wrap modes: what happens when UV coordinates go beyond the edge of the texture

It builds on the Graphics Fundamentals series, especially interpolation.

Textures and Texels

A texture is an image stored in the graphics card’s memory, ready to be read by shaders. It’s a grid of coloured cells, just like the screen’s grid of pixels from part 1 of the fundamentals. To avoid confusion, the cells of a texture are called texels (texture elements), and “pixels” is kept for the screen.

Textures aren’t only pictures. The same mechanism stores surface bumps (normal maps), shininess, transparency, pre-calculated lighting and much more: any data that varies across a surface. And reading a texture, called sampling, has dedicated hardware on the graphics card, as the rest of this series shows.

UV Coordinates

To put a texture on a triangle, each vertex needs to say which point of the texture it should show. That’s its texture coordinate, usually called its UV: two numbers, u across the texture and v up it (the letters x and y were already taken by positions).

Crucially, UVs are measured as fractions of the texture, from 0 to 1, not in texels. (0, 0) is one corner, (1, 1) is the opposite corner, and (0.5, 0.5) is the middle, whatever the texture’s resolution. That means you can swap a 256 Ă— 256 texture for a 4096 Ă— 4096 one without changing the mesh at all.

The UVs are interpolated across the triangle like any other varying, so each pixel gets its own UV, and the fragment shader reads the texture there. In GLSL, sampler2D is the type for a texture, vec2 is a pair of numbers (the UV), and the built-in function texture reads a colour from the texture at a UV:

in vec2 vUV;                // interpolated from the three vertices
uniform sampler2D uTexture; // the texture
out vec4 outColor;
void main() {
    outColor = texture(uTexture, vUV);   // "sample" the texture at this UV
}

Try It: Moving the UVs

On the left is the texture in UV space, with the 0 to 1 square outlined. Its four letters mark the corners of a square on the right, which is drawn by your graphics card with those UVs. Drag the corner points A to D on the left (or select it, press 1 to 4 to pick a corner, and use the arrow keys), or try a preset.

A square's UV coordinates on the texture (left), and the result (right)

UV space: the texture
On screen: a square using those UVs

Some things to notice:

  • Zoom in uses UVs from 0.25 to 0.75: the square shows only the middle of the texture, magnified.
  • Mirror swaps the left and right UVs, and Rotate turns the UV square, so the picture flips or turns even though the square on screen hasn’t moved.
  • Slant moves only the top two UVs. The texture bends along the square’s diagonal, because the square is two triangles, and each triangle interpolates its own three UVs.

UV unwrapping

For a real 3D model, an artist decides each vertex’s UV by unwrapping the model: cutting its surface along chosen seams and flattening it out onto the texture, like flattening a cardboard box. Where the surface is cut, a vertex needs two different UVs, one for each side of the seam, so it has to be stored twice. That’s the same reason a cube needs 24 vertices rather than 8, as Vertices, Triangles and Meshes explained.

Wrap Modes: Going Past the Edge

UVs aren’t limited to 0 to 1. What should the texture look like at u = 1.3? That’s controlled by the texture’s wrap mode (also called the address mode):

  • Repeat ignores the whole number and keeps the fraction, so 1.3 reads the same texel as 0.3. The texture tiles endlessly. It’s how a small brick texture covers a whole wall: give the wall UVs from 0 to 20, and the bricks repeat 20 times.
  • Clamp to edge limits the UV to the range 0 to 1, so anything past the edge reads the texel at the edge, smeared outwards. It’s used for things that mustn’t tile, like a character’s face or a user interface element.
  • Mirrored repeat tiles the texture but flips every other copy, so the edges always match up and there are no visible seams.

Choose Repeat 3 Ă— 3 in the demo and try each mode: the left side shows what the texture “looks like” beyond the edges in each mode.

Which way is up?

In Direct3D, Vulkan and Metal, UV (0, 0) is the top-left of the image. OpenGL and WebGL treat (0, 0) as the first row of texels in memory, which they consider the bottom. Image files store their top row first, so OpenGL programs often flip images when loading them (the demos on this site do, so v points up). If a texture appears upside down after moving between APIs, this is usually why.

Under the hood

Graphics cards have dedicated texture units next to their shader cores. The shader just asks for “the colour at this UV”; the texture unit works out which texels to fetch, applies the wrap mode, fetches them through a dedicated texture cache (a small, fast memory of recently read texels) and blends them (the next post covers that blending). To make the cache effective, textures aren’t stored row by row in memory: texels are rearranged into small square tiles, so texels that are close together on screen are close together in memory too. Most game textures are also compressed in special formats that the hardware decodes as it reads them, typically to between a quarter and an eighth of the uncompressed size.

Summary

  • A texture is an image in GPU memory, made of texels; reading it is called sampling.
  • Each vertex has a UV coordinate, measured from 0 to 1 across the texture, independent of its resolution.
  • UVs are interpolated across each triangle, and the fragment shader samples the texture there.
  • Wrap modes (repeat, clamp, mirror) decide what happens outside 0 to 1.

But a pixel’s UV almost never lands exactly on a texel. What colour should it get then? That’s texture filtering, the next part.

Further Reading