Working with Texture Sampling Parameters
In the computer graphics world, as in the real world, nothing is perfect. Textures don't always align perfectly to a geometry. Sometimes, a texture needs to be stretched or shrunk to fit a geometry. Other times, there isn't a 1 to 1 correspondence between texels and pixels. And other times, texture coordinates may fall out of range.
OpenGL gives you a lot of flexibility when these issues arise. It provides two set of modes which you can control. They are Filtering and Wrapping Mode. The Filtering Mode lets you decide what to do when pixels don't have a 1 to 1 ratio with texels. The Wrapping Mode lets you decide what to do with texture coordinates that fall out of range.
Texture Filtering
As I mentioned earlier, there is never a 1 to 1 ratio between texels in a texture map and pixels on a screen. For example, there are times when you need to stretch or shrink a texture as you apply it to a geometry. Sometimes, you stretch the texture in one coordinate but not the other. This will break up any initial correspondence between a texel and a pixel. Because of this, the color of a pixel needs to be approximated to the closest texel. This process is called Texture Filtering.
Stretching a texture is called Magnification. Shrinking a texture is known as Minification.
In OpenGL, Magnification and Minification, are known as Filters. In the OpenGL API, these filters are referred as GL_TEXTURE_MAG_FILTER and GL_TEXTURE_MIN_FILTER.
Each of these filters can have the following values:
- GL_NEAREST: Nearest Neighbor Filtering
- GL_LINEAR: Linear Filtering
GL_NEAREST
Nearest Neighbor Filtering is the fastest and simplest filtering method. Texture coordinates are plotted against a texture. Whichever texel the coordinate falls in, that color is used for the pixel color.
GL_LINEAR
Linear Filtering requires more work. It works by applying the weighted average of the texels surrounding the texture coordinates. In other words, it does a linear interpolation of the surrounding texels.
Texture Wrap
Texture coordinates are defined in a coordinate system known as the UV Coordinate System. This is a 2-dimensional coordinate system which ranges from 0 to 1 and consists of two axes known as U and V axes. The U and V components are also referred as S and T components.
Most texture coordinates fall between 0 and 1. But there are instances when coordinates may fall outside this range. If this occurs, OpenGL will handle them according to the Texture Wrapping mode specified.
You can set the wrapping mode for each component by using:
- GL_TEXTURE_WRAP_S
- GL_TEXTURE_WRAP_T
- GL_TEXTURE_WRAP_R
GL_TEXTURE_WRAP_S wraps the s component and only affects 1D, 2D and 3D textures. GL_TEXTURE_WRAP_T wraps the t component and only has effect on 2D and 3D textures. GL_TEXTURE_WRAP_R affects only 3D textures.
Each of these wrapping modes can have the following values:
- GL_REPEAT: Causes the texture to repeat in the direction in which the texture coordinate exceeded 1.0.
- GL_MIRRORED_REPEAT: Similar to GL_REPEAT but it repeats as a mirrored copied of the texture.
- GL_CLAMP_TO_EDGE: Forces texture coordinates out of range to be sampled along the last row or column to valid texels.
- GL_CLAMP_TO_BORDER: Forces texture coordinates out of range to be sampled along the texture border color.
PS. Sign up to my newsletter and get OpenGL development tips.