Why interpolation gives different results than a gradient?

Viewed 54

I have differents grayscale textures of a height of one pixel, with GL_LINEAR interpolation. Texture T(k) is made of a gradient from left to right of k pixels from 0 (black) to 255 (white).

Examples :

T(2) = 0 255
T(3) = 0 128 255
T(4) = 0 85 170 255
T(256) = 0 1 2 ... 255

Some examples rendered are there :

T(002)= enter image description here

T(003)=enter image description here

T(004)=enter image description here

T(255)=enter image description here

Why textures do have different results ? The gradient appears more nicely arranged when more pixels are used.

In practice, I can't use less pixel to render a nice gradient to save memory if I want the same result. I suppose the "right" gradient is the one with the most pixels, T(256).

1 Answers

Why textures do have different results ? The gradient appears more nicely arranged when more pixels are used.

Because your assumptions on the texture coordinates are wrong, s=0.0 is the left edge of the left texel, and 1.0 the right edge of the rightmost one.

For a texture with width 2, the texel centers are at 0.25 and 0.75, and if you sample outside of that range, your wrap mode (which in your case seems to be GL_CLAMP_TO_EDGE) will apply.

If you want to see a linear gradient, you'll have to go from 1/(2*width) to 1-1/(2*width). If you increase the width, these values will of course move closer to 0 and 1, so it converges to the result you want when width approaches infinity.

Related