Problem with texture look up

I have this 800x800 texture that covers the entire GL window

Now I’m trying to access to different textels with a fragment shader using this code:

uniform sampler2D Tex0;

void main()
{
	vec2 coord = gl_TexCoord[0].st;

	float pi = 1.0 / 800.0;

	if(gl_FragCoord.x < 200.0)
		cord.s =  X_START * pi;

	vec4 color = texture2D(Tex0, coord);
	gl_FragColor = color;
}

Everything works fine, but I get a strange result when I set X_START = 400.00 (the half of the width):

As you can see the values I get are an interpolated value between the color of the two squares.

I get the same result when I set GL_TEXTURE_WRAP_S to GL_REPEAT and I try to access to 0.0 or 800.0 texels, I know this is normal and that I can sort this out using GL_CLAMP, but I really don’t understand why I get the same result trying to access to the texels in the middle of the image (I have this problem with every setting of GL_TEXTURE_WRAP_S).

Any ideas?

What is the filtering mode of the texture unit?
GL_LINEAR ???

yep, that was the problem… GL_NEAREST fixes everything.

The funny thing is that I use GL_NEAREST in all my programs, this is the only one that used GL_LINEAR… damn copy&past :smiley:

Cheers Ido Ilan.

FYI don’t use GL_CLAMP unless you want a border filtered on the edge - use GL_CLAMP_TO_EDGE instead.

FY :confused:

Thank you for the suggestion, but I get the same result using GL_CLAMP and GL_CLAMP_TO_EDGE.

Because those wrap modes don’t differ with nearest filtering.
But with linear filter you probably want GL_CLAMP_TO_EDGE.
Otherwise sample locations on the texture where the filter fetches from the border will access the texture border color which defaults to black and then your textured objects get a dimmed edge around the textures.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.