I feel the need to explain OpenGL texture filtering and sampling every so often. The spec doesn’t explain it well – I rewrote this for 1.4, but it didn’t make it in.
One easy way to think about texture sampling is to assume that your texture map is a piece of graph paper. (0,0) is the lower left corner of the lower left texel. (1,1) is the upper right corner of the same texture. If your texture is m by n, (m,n) is the upper right corner of the last texel. The center of the texel is always (X.5, Y.5).
GL_NEAREST is basically “give me the color of the cell containing the sample point”. GL_LINEAR is basically “draw a 1-pixel square around the sample point and compute an average color”.
If you draw an aligned texture rectangle with window coordinates (0,0) through (m,n), your texture coordinates should cover the same range. Each pixel is sampled at the center with window coordinates (x.5, y.5). The corresponding texture coordinates are the same, so you hit the middle of the texel.
It shouldn’t matter if you use NEAREST or LINEAR (the 1-pixel square for LINEAR falls right on the texture map). In practice, you might get small rounding errors.
Clamp modes deal with what happens when your coordinates make you fall off.
First, we fix the sample point. For REPEAT and MIRRORED_REPEAT, the sample point bounces around in the obvious manner. For CLAMP, the sample point is clamped to the extent of the texture map. If it falls off, it goes straight to the edge. For now, CLAMP_TO_EDGE and CLAMP_TO_BORDER do nothing.
Next, we look at the sample filter, in particular what happens if we fall off the edge of the map. For 2D (or rectangle) textures, normal filters will sample 1-4 texels. For REPEAT, if the texel you sample is off one edge, you wrap around to the other edge. For CLAMP_TO_EDGE, your samples go back to the edge texture. MIRRORED_REPEAT works like CLAMP_TO_EDGE (seams between textures have the same texel repeated twice). For CLAMP and CLAMP_TO_BORDER, if you fall of the edge, you get either the border texel (if it’s in the image) or the constant border color.
CLAMP is the least intuitive mode. If you fall of the edge of a texture map, you start getting the border color blended in. But you will never more than 50% border (75% in corners) since your sample point is always clamped to the edge, producing a 50/50 filter.
This is pretty useless. If you see seams in a tiled texture, it’s probably because you used CLAMP and are seeing the border color. (To be sure, change the border color and your seams should change color.)
As I mentioned above, if you use a LINEAR filter with a 1:1 mapping of pixel to texels, you should get the same results as NEAREST. Except rounding errors may leave you off by a little bit, and you end up seeing a little bit of border. Annoying! Use CLAMP_TO_EDGE instead.
Anyone still awake? If so, hope this helps.
[This message has been edited by pbrown (edited 09-30-2002).]