1
votes

My rendering is bleeding pixels (the dots in red) along the edge of these two perpendicular quads.

I'm using:

  • GL_NEAREST min/max texture filtering.
  • texture function on fragment shader.
  • A sprite atlas of 1024x1024, with the desired sprite @ (x=320,y=732,width=8,height=8)

s1 = 320.0f/1024.0f; s2 = (320.0f+8.0f)/1024.0f; t1 = 732.0f/1024.0f; t2 = (732.0f+8.0f)/1024.0f;

Render #1 (with a dark texture to make it easier to see bleeding): enter image description here

Render #2: (with bright texture for counting texels, but the red pixels are still present): enter image description here

I understand that OpenGL's texture function will sample at the pixel center, but when I try to offset the "S,T" coordinates by half-a-pixel, it (fixes it but) has adverse effects.

For example, the texture coordinates below shrinks the width of the texels along the border of the sprite.

s1 = (0.5f+320.0f)/1024.0f; s2 = (0.5f+320.0f+8.0f)/1024.0f; t1 = (-0.5f+732.0f)/1024.0f; t2 = (-0.5f+732.0f+8.0f)/1024.0f;

enter image description here

I tried a bunch of possible S&T offset combinations (adding 0.5 to both S&T, subtracting from both, adding to T but subtracting from S) and different values, none of it worked. I don't know what I'm missing; I don't think this is a T-junction issue because the bleeding pixel color is coming from the texture (not the background).

1

1 Answers

3
votes

Only subtracting or adding a half texel offset will never solve the problem since you will always keep the problem on one side.

What you can do is to clamp your texel coordinates in the range x+0.5 and x+width-0.5 which will prevent bleeding on both sides. The code could looks like this:

s = clamp(texCoord.x, 320 + 0.5, 320 + 8 - 0.5) / 1024;
t = clamp(texCoord.y, 732 + 0.5, 732 + 8 - 0.5) / 1024;