LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Daslee on December 09, 2013, 19:39:37

Title: Problem with texture edges
Post by: Daslee on December 09, 2013, 19:39:37
Hello. I am working now with VAO and I needed to use multiple textures, so I started using sprite sheet. The problem is, when I moved to sprites texture edges has changed to something weird it seems to be transparent or other sprites overlapping that texture. Here is image how it looks: http://s27.postimg.org/u6b2v6k0j/problem.png (http://s27.postimg.org/u6b2v6k0j/problem.png)

In texture file there isn't problem.

This how I generate my tex coords:
float xsize = (float)32 / Textures.spriteWidth; //Sprite width / Image (texture) width
float ysize = (float)32 / Textures.spriteWidth; //Sprite height / Image (texture) height

return new float[]{
xsize*xindex, ysize*yindex,
xsize*(xindex+1), ysize*yindex,
xsize*(xindex+1), ysize*(yindex+1),
xsize*xindex, ysize*(yindex+1)
};


I also tried to set my texture wrap_s and wrap_t to GL_CLAMP_TO_EDGE and tried GL_CLAMP, but no one fixes my problem. So where could be the problem?
Title: Re: Problem with texture edges
Post by: Cornix on December 09, 2013, 19:49:10
If you use linear filtering you should usually leave 1 pixel in between different images in a texture atlas.
Otherwise the filtering could pick colors from adjacent sprites.
Title: Re: Problem with texture edges
Post by: Daslee on December 10, 2013, 13:00:57
But I'm not using linear filtering, I'm using GL_NEAREST. What could be another solution?
Title: Re: Problem with texture edges
Post by: Fool Running on December 10, 2013, 13:53:44
One thing you could try is subtracting a fudge-factor from the ends of your texture coords:

final float fudge = 0.01f;
float xsize = (float)32 / Textures.spriteWidth; //Sprite width / Image (texture) width
float ysize = (float)32 / Textures.spriteWidth; //Sprite height / Image (texture) height

return new float[]{
xsize*xindex+fudge, ysize*yindex+fudge,
xsize*(xindex+1)-fudge, ysize*yindex+fudge,
xsize*(xindex+1)-fudge, ysize*(yindex+1)-fudge,
xsize*xindex+fudge, ysize*(yindex+1)-fudge
};

If that fixes the problem, then you are definitely getting texture bleed.

Another, I think unlikely, possibility is that your polygons are not actually touching. Make sure that the vertex position of one polygon ends at the exact location as the next one starts.
Title: Re: Problem with texture edges
Post by: Daslee on December 10, 2013, 15:38:01
fudge fixes the problem except for those quads which are far away from camera. http://s10.postimg.org/ph9wxg189/problem.png (http://s10.postimg.org/ph9wxg189/problem.png) As you can see, far quads still getting that problem.

And trust me, there is no spaces between cubes, because if I do not use sprite sheet and those tex coords, then everything is fine, but then I can use only one texture per all cubes. Here is how it looks with one texture: http://s16.postimg.org/qcwh4jhcl/problem1.png (http://s16.postimg.org/qcwh4jhcl/problem1.png)
Title: Re: Problem with texture edges
Post by: Cornix on December 10, 2013, 17:02:16
Are you using mipmaps? Because when generating mipmaps different tiles might also bleed together and their colors mix up.
This would explain why the tiles which are far away show this problem.
Title: Re: Problem with texture edges
Post by: Daslee on December 10, 2013, 19:53:29
Yes, I'm using mipmaps, but it doesn't changes nothing even if I do not use it.  :-\
Title: Re: Problem with texture edges
Post by: Fool Running on December 11, 2013, 13:30:42
Could we see a screenshot of the problem without mipmaps?
Title: Re: Problem with texture edges
Post by: Daslee on December 11, 2013, 14:35:58
Without mipmaps and without fudge: http://s12.postimg.org/gxm2ka2cd/withoutmipmapfudge.png (http://s12.postimg.org/gxm2ka2cd/withoutmipmapfudge.png)
Without mipmaps and with fudge: http://s12.postimg.org/l80qfv7fh/withoutmipmapwithfudge.png (http://s12.postimg.org/l80qfv7fh/withoutmipmapwithfudge.png)

EDIT: I found the problem. It occurs when I set samples greater than 0 in PixelFormat, but why?
Title: Re: Problem with texture edges
Post by: Cornix on December 11, 2013, 16:17:49
Thats anti-aliasing. It also filters the texture and can lead to bleeding.
Title: Re: Problem with texture edges
Post by: Daslee on December 11, 2013, 17:18:40
And there isn't any way to fix it without disabling anti-aliasing?
Title: Re: Problem with texture edges
Post by: Cornix on December 11, 2013, 17:19:34
Did you try leaving 1 pixel between each sprite in your texture atlas?

Edit: An alternative might be to use texture arrays instead of texture atlases.
Texture arrays require openGL 3.0 however.
Title: Re: Problem with texture edges
Post by: Daslee on December 11, 2013, 17:41:50
Yes, I tired, but it didn't helped. Only thing what can help in here so increasing fudge, but then texture is like cropped from center and greater fudge will makes texture smaller. Or disabling anti-aliasing, but I do not want to disable it.
Title: Re: Problem with texture edges
Post by: Cornix on December 11, 2013, 19:17:43
If you dont want to use texture arrays you could also try to implement your own multisampling with shaders. Maybe you could tweak it in a way that satisfies your needs.
Title: Re: Problem with texture edges
Post by: Daslee on December 11, 2013, 19:35:20
I'm newbie at shaders, so I couldn't do that. ;D I think I'll just use fudge, because that way fixed my problem mostly.