Game Development Reference
In-Depth Information
Texture Filtering
There's one last thing we need to define before we can use the texture object. It has to do with
the fact that our triangle might take up more or less pixels on the screen than there are pixels in the
mapped region of the texture. For example, the image of Bob in Figure
7-10
has a size of 128×128
pixels. Our triangle maps to half that image, so it uses (128×128) / 2 pixels from the texture (which
are also called
texels
). When we draw the triangle to the screen with the coordinates we defined in
the preceding snippet, it will take up (320×480) / 2 pixels. That's a lot more pixels that we use on
the screen than we fetch from the texture map. It can, of course, also be the other way around: we
use fewer pixels on the screen than from the mapped region of the texture. The first case is called
magnification
, and the second is called
minification
. For each case, we need to tell OpenGL ES
how it should upscale or downscale the texture. The up- and downscaling are also referred to as
minification and magnification filters in OpenGL ES lingo. These filters are attributes of our texture
object, much like the image data itself. To set them, we first have to make sure that the texture
object is bound via a call to
glBindTexture()
. If that's the case, we can set them like this:
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
Both times we use the method
GL10.glTexParameterf()
, which sets an attribute of the texture. In
the first call, we specify the minification filter; in the second, we call the magnification filter. The
first parameter to that method is the texture type, which defaults to
GL10.GL_TEXTURE_2D
. The
second argument tells the method which attributes we want to set—in our case, the
GL10.GL_
TEXTURE_MIN_FILTER
and the
GL10.GL_TEXTURE_MAG_FILTER
. The last parameter specifies the type
of filter that should be used. We have two options here:
GL10.GL_NEAREST
and
GL10.GL_LINEAR
.
The first filter type will always choose the nearest texel in the texture map to be mapped to a
pixel. The second filter type will sample the four nearest texels for a pixel of the triangle and
average them to arrive at the final color. We use the first type of filter if we want to have a
pixelated look and use the second if we want a smooth look. Figure
7-12
shows the difference
between the two types of filters.
Figure 7-12.
GL10.GL_NEAREST vs. GL10.GL_LINEAR. The first filter type makes for a pixelated look; the second one
smoothes things out a little