OpenGL is very flexible in the way it renders 3D scenes. There are a number of texture parameters that can be set to create visual effects and adjust how textures are used to render polygons. This section covers some commonly used texture parameters—the full list is beyond the scope of this book, but you can read the OpenGL documentation online (www.opengl.org/sdk/docs/man/) for all the details.
Min and Max Filters
In Listing 11-2 we set two texture parameters, GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAX_FILTER, which define the minimizing and maximizing filters for texture scaling. Both were set to GL_LINEAR, which gives good results in most cases, but there are other values that you can use to fine-tune the scaling.
When OpenGL renders a textured polygon to the screen, it samples the texture at regular intervals to calculate the color of pixels in the polygon. If the texture is mip mapped (see our earlier discussion), OpenGL also has to decide which mip level(s) it should sample. The method it uses to sample the textures and select a mip level is defined by the minimizing or maximizing filter parameter.
The only values you can set for the maximizing filter (GL_TEXTURE_MAX_FILTER) are GL_NEAREST and GL_LINEAR. Generally it is best to stick with GL_LINEAR, which makes OpenGL use bilinear filtering to smooth the texture when scaled, but textures can appear blurry at high scales. The alternative is GL_NEAREST, which looks sharper but blocky. These values are also supported by the minimizing filter (GL_TEXTURE_MIN_FILTER), in addition to four other constants that tell OpenGL how to include the mip level in the color calculation (see Table 11-2). The highest-quality setting for the minimizing filter is GL_LINEAR_MIPMAP_LINEAR, which softens the texture like GL_LINEAR but also blends between the two nearest mip levels (known as trilin-ear filtering). The following lines set the minimizing and maximizing filter methods to the highest-quality settings:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
Table 11-2. Potential Values for Min and Max Filter Parameters
GL NEAREST MIPMAP NEAREST
GL LINEAR MIPMAP NEAREST
GL NEAREST MIPMAP LINEAR
GL LINEAR MIPMAP LINEAR
Selects the texel nearest to the sample point. This keeps the texture looking sharp, but can appear low quality.
Selects the four texels nearest to the sample point and blends between them. This smoothes the rendered texture and makes it appear less jagged when scaled.
Selects the texel nearest to the sample point, and the mip level that is closest to the size of the pixel being rendered. Minimizing filter only.
Blends between the four texels nearest to the sample point and selects the mip level that is closest to the size of the pixel being rendered. Minimizing filter only.
Selects the texel nearest to the sample point and blends between the two mip levels that most closely match the size of the pixel being rendered. Minimizing filter only.
Blends between the four texels nearest to the sample point and blends between the two mip levels that most closely match the size of the pixel being rendered. Minimizing filter only.
A texture coordinate with components in the zero to one range will refer to a point inside the texture, but it is not an error to have a texture coordinate with components outside that range—in fact, it can be very useful. How OpenGL treats coordinates that are not in the zero to one range is defined by the GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T texture parameters. The default for these parameters is G L_R EPEAT, which causes samples that go off one edge of the texture to appear on the opposite edge. This creates the effect of tiling the texture, so that multiple copies of it are placed edge to edge. You can see this in action by editing the calls to glTexCoord2f in Listing 11-2. Try replacing the lines between the calls to glBegin and glEnd with the following:
glVertex3f(-300, 300, 0)
glVertex3f(300, 300, 0)
glVertex3f(300, -300, 0)
If you run the edited Listing 11-2, you should see something like Figure 11-4. Only a single quad is drawn, but the texture is repeated nine times because the texture components range from zero to three. Tiling textures like this is useful in games because you can texture very large polygons without having to break them up in to smaller pieces. For example, a long piece of fence could be created with a single elongated quad and a tiled texture, which is more efficient than small quads for each piece of fence.
Repeating textures has the same effect as transforming the components of every texture coordinate with the following Python function (although the wrapping is done by your graphics card and not Python). The % symbol is the modulus operator, which returns the remainder in a division, so % 1.0 would return the fractional part of a number.
def wrap_repeat(component): return component % 1.0
Was this article helpful?