9. Surface Textures in the Fragment Shader
that take texture coordinates as their starting point. And later, in Chapter 15
on scientic visualization, we will show how textures can be used to pass data
Texture coordinates specify the coordinates in texture space for each vertex of a
graphics primitive. Texture coordinates are not part of the basic geometry of
a primitive, but rather are an aribute aached to each vertex. In the vertex
shader, the per-vertex texture coordinates are typically assigned to variables
that can be interpolated by the rasterizer across the entire polygon and then
given to the fragment shaders.
In the previous chapter on fragment shaders, you saw that you access the
interpolated texture coordinates with the texture coordinate variables we have
vST in the vertex shader, and that you can get the RGBA color of
a texel from one of the
texture( ) functions. You are not limited to using just
the single texels at those given texture coordinates, however. You can also use
any texture coordinates you need in developing the color of the pixel. As an
example, in the chapter on scientic visualization, we will describe the line
integral convolution (LIC) process that probes the texture map along specic
function streamlines to compute the color of each pixel. You can use a great
deal of creativity in how you use textures.
Traditional Texture Mapping
Traditional OpenGL texture mapping uses a number of functions that dene
the way a texture is read, stored, accessed, and processed. The apparent com-
plexity comes mostly from the exibility that a generalized graphics API must
have in order to be used so widely. If you are writing your own texture func-
tions in a fragment shader, you can implement just those operations you need,
which should make the task less intimidating than it might appear.
Your experience is that xed-function OpenGL supports four kinds of
textures: 1D, 2D, and 3D textures, and cube maps. It also supports multitextur-
ing. Our goal is to see how you can create each of these standard functional-
ities with fragment shaders.
When you rst encounter texturing in OpenGL, you nd that to use tex-
tures, you must rst set up a number of texture properties. You must associate