﻿ Game Programming

JavaScript EditorFree JavaScript Editor     Ajax Editor ﻿

Main Page
  Previous Section Next Section


Texture coordinates are usually described as floating-point values. We have just seen how they can extend beyond the 0..1 range, but for the sake of this section, let's assume they are restricted to 0..1 values. We can multiply the texture coordinate by the size of the texture map to obtain the texel we must apply to a given pixel. For example, a texturing coordinate of (0.75,0.35) applied to a 256x256 map will end up mapped at texel (192, 89.6). The U coordinate is an integer value, but notice how the V value falls in the middle of two values, or texels.

We could choose to truncate or round that value to the nearest integer, but that would result in an awkward behavior. Textures would flicker with subtle camera motions as pixels "jumped" from one texel to another. That's why most graphics cards support texture filtering, which averages texel values using different filters to obtain the proper appearance.

A card with no filtering, such as the early models and most software rasterizers, is said to use a nearest neighbor policy. It rounds the texel values to the nearest integer. But the most popular texture filter is bilinear filtering, which filters both in U and V directions using the two contiguous values. In the previous case, the U value would remain unfiltered because it's already an integer number, whereas the V value would be the composite of 60 percent of texel (192,90) and 40 percent of texel (192,89).

A more sophisticated type of filtering is trilinear filtering, which is used in mipmapping strategies. Mipmapping is a very important technique that we will discuss in the next section.

Whatever the case, texture filtering is usually available under most programming APIs. In OpenGL, filtering can be set per texture, so each one can have its own filtering options. The required function call is

glTexParameteri(GLenum target, GLenum pname ,GLenum param);

where target must either be GL_TEXTURE_1D or GL_TEXTURE_2D depending on the texture type. The second parameter, pname, specifies which filter we want to modify. OpenGL has a magnification filter, which is used whenever a texel is assigned to an area larger than a pixel, and a minification filter, which is applied to texels of subpixel size. The two symbolic constants are GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER. The last parameter (Glenum param) is used to specify the value of the selected filter. Available values are GL_NEAREST and GL_LINEAR, which sets bilinear filtering. Here is the code to set bilinear texture under OpenGL:


Under DirectX, the same results can be achieved with texture samplers, which are objects that determine how texture maps are accessed and filtered. Here is the equivalent source code:

IDirect3DDevice9::SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
IDirect3DDevice9::SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
      Previous Section Next Section

    JavaScript EditorAjax Editor     JavaScript Editor