Filtering (Texture Mapping) (OpenGL Programming)

Texture maps are square or rectangular, but after being mapped to a polygon or surface and transformed into screen coordinates, the individual texels of a texture rarely correspond to individual pixels of the final screen image. Depending on the transformations used and the texture mapping applied, a single pixel on the screen can correspond to anything from a tiny portion of a texel (magnification) to a large collection of texels (minification), as shown in Figure 9-8. In either case, it’s unclear exactly which texel values should be used and how they should be averaged or interpolated. Consequently, OpenGL allows you to specify any of several filtering options to determine these calculations. The options provide different trade-offs between speed and image quality. Also, you can specify independently the filtering methods for magnification and minification.

In some cases, it isn’t obvious whether magnification or minification is called for. If the texture map needs to be stretched (or shrunk) in both the x- and y- directions, then magnification (or minification) is needed. If the texture map needs to be stretched in one direction and shrunk in the other, OpenGL makes a choice between magnification and minification that in most cases gives the best result possible. It’s best to try to avoid these situations by using texture coordinates that map without such distortion. (See "Computing Appropriate Texture Coordinates" on page 422.)


Texture Magnification and Minification

Figure 9-8 Texture Magnification and Minification

The following lines are examples of how to use glTexParameter*() to specify the magnification and minification filtering methods:

tmp1cfe-30_thumb

The first argument to glTexParameter*() is GL_TEXTURE_1D, GL_ TEXTURE_2D, GL_TEXTURE_3D, or GL_TEXTURE_CUBE_MAP, whichever is appropriate. For the purposes of this discussion, the second argument is either GL_TEXTURE_MAG_FILTER, or GL_TEXTURE_MIN_FII TER, to indicate whether you’re specifying the filtering method for magnification or minification. The third argument specifies the filtering method; Table 9-3 lists the possible values.

Parameter

Values

GL_TEXTURE_.MAG_FILTER

GL_NEAREST or GL_LINEAR

GL_TEXTURE_MIN_FILTER

GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST, or GL_LINEAR_MIPMAP_LINEAR

Table 9-3 Filtering Methods for Magnification and Minification

If you choose GL_NEAREST, the texel with coordinates nearest the center of the pixel is used for both magnification and minification. This can result in aliasing artifacts (sometimes severe). If you choose GL_LINEAR, a weighted linear average of the 2×2 array of texels that lie nearest to the center of the pixel is used, again for both magnification and minification. (For threedimensional textures, it’s a 2 χ 2 χ 2 array; for one-dimensional, it’s an average of 2 texels.) When the texture coordinates are near the edge of the texture map, the nearest 2×2 array of texels might include some that are outside the texture map. In these cases, the texel values used depend on which wrapping mode is in effect and whether you’ve assigned a border for the texture. (See "Repeating and Clamping Textures" on page 423.) GL_NEAREST requires less computation than GL_LINEAR and therefore might execute more quickly, but GL_LINEAR provides smoother results.

With magnification, even if you’ve supplied mipmaps, only the base level texture map is used. With minification, you can choose a filtering method that uses the most appropriate one or two mipmaps, as described in the next paragraph. (If GLJSJEAREST or GL_LINEAR is specified with minification, only the base level texture map is used.)

As shown in Table 9-3, four additional filtering options are available when minifying with mipmaps. Within an individual mipmap, you can choose the nearest texel value with GL_NEAREST_MIPMAP_NEAREST, or you can interpolate linearly by specifying GL_LINEAR_MIPMAP_NEAREST. Using the nearest texels is faster but yields less desirable results. The particular mipmap chosen is a function of the amount of minification required, and there’s a cutoff point from the use of one particular mipmap to the next. To avoid a sudden transition, use GL_NEAREST_MIPMAP_LINEAR or GL_LINEAR_MIPMAP_LINEAR for linear interpolation of texel values from the two nearest best choices of mipmaps. GL_NEAREST_MIPMAP_LINEAR selects the nearest texel in each of the two maps and then interpolates linearly between these two values. GL_LINEAR_MIPMAP_LINEAR uses linear interpolation to compute the value in each of two maps and then interpolates linearly between these two values. As you might expect, GL_ LINEAR_MIPMAP_LINEAR generally produces the highest-quality results, but it requires the most computation and therefore might be the slowest.

Caution: If you request a mipmapped texture filter, but you have not supplied a full and consistent set of mipmaps (all correct-sized texture images between GL_TEXTURE_BASE_LEVEL and GL_TEXTURE_MAX_LEVEL), OpenGL will, without any error, implicitly disable texturing. If you are trying to use mipmaps and no texturing appears at all, check the texture images at all your mipmap levels.

Some of these texture filters are known by more popular names. GL_NEAREST is often called point sampling. GL_LINEAR is known as bilinear sampling, because for two-dimensional textures, a 2 χ 2 array of texels is sampled.

GL_LINEAR_MIPMAP_LINEAR is sometimes known as trilinear sampling, because it is a linear average between two bilinearly sampled mipmaps.

Note: The minification-to-magnification switchover point is usually at λ = 0.0, but is affected by the type of minification filter you choose. If the current magnification filter is GL_LINEAR and the minification filter is GL_NEAREST_MIPMAP_NEAREST or GL_NEAREST_MIPMAP_ LINEAR, then the switch between filters occurs at λ = 0.5. This prevents the minified texture from looking sharper than its magnified counterpart.

Nate Robins’ Texture Tutorial

If you have downloaded Nate Robins’ suite of tutorial programs, now run the texture tutorial. (For information on how and where to download these programs, see "Nate Robins’ OpenGL Tutors" on page xxiv.) With this tutorial, you can experiment with the texture-mapping filtering method, switching between GL_NEAREST and GL_LINEAR.

Next post:

Previous post: