next up previous contents
Next: 6.5 Mipmap Generation Up: 6 Texture Mapping Previous: 6.3 Merging Textures with   Contents

6.4 Texture Borders and Tiling

A useful (and sometimes misunderstood) feature of OpenGL is the texture border. OpenGL supports either a constant texture border color or a border that is a portion of the edge of the texture image. The key to understanding texture borders is understanding how textures are sampled when the texture coordinate values are near the edges of the [0,1] range and the texture wrap mode is set to GL_ CLAMP. For point sampled filters, the computation is quite simple: the border is never sampled. However, when the texture filter is linear and the texture coordinate reaches the extremes (0.0 or 1.0), the resulting texel value is a 50% mix of the border color and the outer texel of the texture image at that edge (25% and 75% at the corners).

The texture border is most useful when attempting to use a single high resolution texture image which is too large for the OpenGL implementation to support as a single texture map. For this case, the texture can be broken up into multiple tiles, each with a 1 pixel wide border from the neighboring tiles. The texture tiles can then be loaded and used for rendering in several passes. For example, if a 1K by 1K texture is broken up into four 512 by 512 images, the four images would correspond to the texture coordinate ranges (0-0.5,0-0.5), (0.5,1.0,0-0.5), (0-0.5,0.5,1.0) and (.5-1.0,.5-1.0). As each tile is loaded, only the portions of the geometry that correspond to the appropriate texture coordinate ranges for a given tile should be drawn. If you had a single triangle whose texture coordinates were (.1,.1), (.1,.7), and (.8,.8), you would clip the triangle against the four tile regions and draw only the portion of the triangle that intersects with that region as shown in Figure 30. At the same time, the original texture coordinates need to be adjusted to correspond to the scaled and translated texture space represented by the tile. This transformation can be easily performed by loading the appropriate scale and translation onto the texture matrix stack.

% latex2html id marker 5357
\vrule width 0pt height 0.1...
...column{1}{p{5.7in}}{\small Figure \thefigure . Texture Tiling}\\

Unfortunately, OpenGL does not provide much assistance for performing the clipping operation. If the input primitives are quads and they are appropriately aligned in object space with the texture, then the clipping operation is trivial; otherwise, it may involve substantially more work. One method to assist with the clipping uses stenciling to control which textured fragments are kept. Stencil testing is described in Section 8.6. Then you are left with the problem of setting the stencil bits appropriately. The easiest way to do this is to produce alpha values that are proportional to the texture coordinate values and use glAlphaFunc() to reject alpha values that you do not wish to keep. Unfortunately, you can not easily map a multidimensional texture coordinate value (e.g., $s$,$t$) to an alpha value by simply interpolating the original vertex alpha values, so it is best to use a multidimensional texture itself which has some portion of the texture with zero alpha and some portion with it equal to one. The texture coordinates are then scaled so that the textured polygon map to texels with an alpha of 1.0 for pixels to be retained and 0.0 for pixels to be rejected.

OpenGL 1.2 adds an alternative clamping behavior when the texture wrap mode is set to GL_ CLAMP_TO_EDGE.7This mode clamps the texture coordinates such that the texture border is never sampled. The filtered color is derived only from texels at the edge of the texture image. Unlike OpenGL's standard texture clamping, the clamp to edge behavior is unable to guarantee a consistent border appearance when used with mipmapping because the clamping range changes with each mipmap level because the clamping range depends on the selected mipmap level's dimensions. The clamp to edge behavior is easier to implement in hardware than OpenGL's standard clamping with texture borders because the texture dimensions are not augmented by additional border texels the dimensions are efficient powers of two. The clamp to edge behavior matches the texture clamping behavior of Direct3D.

next up previous contents
Next: 6.5 Mipmap Generation Up: 6 Texture Mapping Previous: 6.3 Merging Textures with   Contents