To render a nontextured surface, its fragments are painted into the color buffer where the image forms, pixel by pixel. Diffuse color is supplied by the surface’s material. In contrast, to render textured surfaces, each fragment’s color is looked up in the image texture. This relies on a correspondence, or mapping, between points of the surface and points of the texture image. This in turn requires texture coordinates.
Texture coordinates define a 2D coordinate system on a rectangular texture image. The horizontal coordinate, labelled s by convention, ranges from 0 at the image’s left edge to 1 at its right edge, and the vertical coordinate, labelled t, ranges from 0 at the bottom to 1 at the top. For example, (0,0) identifies the lower-left corner of the image, (0.5, 0.5) its center point, and (1,1) its upper-right corner. Values of s or t outside the range of 0 to 1 are valid coordinates even though (s,t) lies outside the image. This is because the image repeats in all directions in texture space. For example, for all integers a and b, the texture coordinates (a+0.5, b+0.5) refer to the image’s center point.
To apply a texture to a surface, we map each of the surface’s facets to texture space. Each vertex is assigned texture coordinates (s,t), and interior points are assigned texture coordinates by interpolating from the vertices. The simplest mapping is to a rectangular facet such as the side of a box. Here, we usually assign texture coordinates (0,0), (0,1), (1,1), and (1,0) to each of the facet’s four corners, aligning the bottom of the texture to one of the rectangle’s four edges. For more complex surfaces, the mapping can be more complex. Many mappings are possible, reflecting the many ways an image can be applied to a surface.
Specifying a mapping — even just knowing how we want the texture to appear on the surface — can be tricky. In general, a texture can be applied to a surface in infinitely many different ways. Even reducing a surface to its simplest constituent — triangles — may not help since triangles fit together to form (say) a sphere very different from how they fit together to form a torus or a knot or some other type of surface. In the following program, we rely on the default mapping supplied by each type of geometry. You can see that it may not always be what we want.