I have a big, 2048x2048 image with 4096 32x32 sprites. That is, a 64x64 table of 32x32 images. I'm using triangles to draw them. Each trig must cut the right piece of the texture. It works alright, except that on the border of the faces, the engine is accessing pixels from neighbor sprites on the sheet, resulting in noise. Notice the pixels below the orc:
They are from the sprite below it on the sheet. I'm using the following calculation to cut the area:
Vertex shader:
// position of each pixel of this face on texture
varying vec2 pix_pos;
// pos_in_sheet holds the position of the sprite on the sheet
// considering it is a 64x64 sheet, it is on the range vec2(0..63, 0..63)
vec2 pos_in_sheet = vec2(floor(mod(sprid,texture_cols)), floor(sprid/texture_cols));
// `vec2 vertex` indicates which of the 4 vertex in the quad this is:
// vec2(-1.0,-1.0)|vec2(-1.0,1.0)|vec2(1.0,-1.0)|vec2(1.0,1.0)
// inside main() I calculate the bounding quad that contains the sprite
pix_pos = vec3(
((pos_in_sheet.x+(vertex.x+1.0)/2.0)/64.0), //64 = rows in texture
((pos_in_sheet.y+(-vertex.y+1.0)/2.0)/64.0));
And this is the fragment shader:
varying vec3 pix_pos;
uniform sampler2D sampler;
void main(void){
vec4 col = texture2D(sampler,vec2(pix_pos.x,pix_pos.y));
gl_FragColor = vec4(col.x,col.y,col.z,1.0);
}
I've checked my calculation and it seems fine. So what could be causing the fragment shader to access sprites outside its own cutting region?