0

I'm running this example,

but changing:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

to:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);

And I got a slightly different image at the top and right.

enter image description here

But as can seen from the code:

glBegin(GL_QUADS);
//lower left
glTexCoord2f(0, 0); 
glVertex2f(-1.0, -1.0);
//upper left
glTexCoord2f(0, 1.0);
glVertex2f(-1.0, 1.0);
//upper right
glTexCoord2f(1.0, 1.0);
glVertex2f(1.0, 1.0);
//lower right_
glTexCoord2f(1.0, 0); 
glVertex2f(1.0, -1.0);
glEnd();

The texture will not go out of range, why GL_CLAMP matters?

Community
  • 1
  • 1
new_perl
  • 7,345
  • 11
  • 42
  • 72

1 Answers1

1

In the fragment shader the colors are mixed with colors top right from the current texel. There is a vec2(0.0625, 0.0625) added to the texture coordinate. So you always get the same value using GL_CLAMP when looking up texture coordinates higher or equal (1.0 - 0.0625) = 0.9375.

void main() {
    vec4 s1 = texture2D(tex0, gl_TexCoord[0].st);
    vec4 s2 = texture2D(tex1, gl_TexCoord[0].st + vec2(0.0625, 0.0625));
    gl_FragColor = mix(vec4(1, s1.g, s1.b, 0.5), vec4(s2.r, s2.g, 1, 0.5), 0.5);
}
Matthias
  • 7,432
  • 6
  • 55
  • 88
  • Can you explain where this magic value of `0.0625`=`1.0/16` comes from? – Steven Lu Jul 16 '12 at 21:36
  • @StevenLu I just found out that this vector was added to the texture coordinate which therefore can count beyond 1.0, where `GL_CLAMP` comes into play. To get to know what is actually done in the shader you could better ask the [author of this code](http://stackoverflow.com/a/3585515/344480). – Matthias Jul 17 '12 at 09:37