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 fewer 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 128x128 pixels. Our triangle maps to half that image, so uses (128x128) / 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 (320x480) / 2 pixels. That's a lot more pixels 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 minification. For each case we need to tell OpenGL ES how it should upscale or downscale the texture. The up- and downscaling are also referd 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 have to first 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 GLl0.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 for us. 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 the second if we want a smoothed look. Figure 7-12 shows the difference between the two types of filters.

GL NEAREST GL_LINEAR

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.

Our texture object is now fully defined: we created an ID, set the image data, and specified the filters to be used in case our rendering is not pixel perfect. It is a common practice to unbind the texture once we are done defining it. We should also recycle the Bitmap we loaded, as we no longer need it. Why waste memory? That can be achieved with the following snippet:

gl.glBindTexture(GL10.GL_TEXTURE_2D, 0); bitmap.recycle();

0 is a special ID that tells OpenGL ES that it should unbind the currently bound object. If we want to use the texture for drawing our triangles, we need to bind it again, of course.

0 0

Post a comment