When you use linear filtering (value GL_LINEAR
for the GL_TEXTURE_MIN_FILTER
and/or GL_TEXTURE_MAG_FILTER
texture parameters), the value of your samples will generally be determined by the two slices closest to your 3rd texture coordinate.
If the size of your 3D texture in the 3rd dimension is d
, the position of the first slice in texture coordinate space is 0.5 / d
. This is easiest to understand if you picture each slice having a certain thickness in texture coordinate space. Since you have d
slices, and the texture coordinate range is [0.0, 1.0]
, each slice has thickness 1.0 / d
. Therefore, the first slice extends from 0.0
to 1.0 / d
, and its center is at 0.5 / d
.
When you use 0.0
for the 3rd texture coordinate, this is not the center of the first slice, and linear sampling comes into play. Since 0.0
is at the edge of the texture, the wrap modes become critical. The default wrap mode is GL_REPEAT
, meaning that sampling at the edges acts as if the texture was repeated. With this setting, the neighbor of the first slice is the last slice, and texture coordinate 0.0
is exactly in the middle between the first slice and the last slice.
The consequence is that linear sampling with texture coordinate 0.0
will give you the average of the first slice and the last slice.
While GL_REPEAT
is the default for the wrap modes, it is rarely what you want, unless your texture really contains a repeating pattern. It's certainly not the right setting in this case. What you need here is:
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…