Texture atlas bleeding w/mipmap

Hi guys,

I am working a 2D tile based side scroller and attempting to use a texture atlas as suggested by many here to avoid multiple texture bind calls.

I was able to fix bleeding using half pixel correction using GL_NEAREST filtering, but I need a fix when using mipmaps. My issue is that my atlas is tightly packed 32x32 but I want my main character to be scaled to 48x48. When scaled (either up or down) it looks horrible when using GL_NEAREST so I decided to use mipmaps (GL_LINEAR_MIPMAP_LINEAR) which looks great, but breaks my fix for bleeding. All of my textures are POT btw.

I have tried google but can’t seem to find a fix. My relevent code is below and I would appreciate any suggestions.

Texture Loader:


	public static Texture loadTexture(String pathToFile)
	{
		int width = 0;
		int height = 0;
		BufferedImage img;
		int[] pixels = null;
		int id = 0;
		ByteBuffer bb = null;
		InputStream is;
		
		try 
		{
			is = ResourceManager.class.getClassLoader().getResourceAsStream(pathToFile);
			img = ImageIO.read(is);
			width = img.getWidth();
			height = img.getHeight();
			pixels = img.getRGB(0, 0, width, height, null, 0, width);
			is.close();
			bb = BufferUtils.createByteBuffer((width * height) * 4);
			id = glGenTextures();
			
		} catch (IOException e) 
		{
			System.out.println(e.getMessage());
			System.exit(1);
		}
		
		for(int i=0; i<pixels.length; i++)
		{
			byte r = (byte) ((pixels[i] >> 16) & 0xFF);
			byte g = (byte) ((pixels[i] >> 8) & 0xFF);
			byte b = (byte) ((pixels[i]) & 0xFF);
			byte a = (byte) ((pixels[i] >> 24) & 0xFF);
			
			bb.put(r);
			bb.put(g);
			bb.put(b);
			bb.put(a);
		}
		
		bb.flip();
		
		glBindTexture(GL_TEXTURE_2D, id);
		
		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bb);
		
		glBindTexture(GL_TEXTURE_2D, 0);
		
		return new Texture(id,width,height);
	}

SpriteSheet code:


public SpriteSheet(Texture texture, int tileSize)
	{
		this.texture = texture;
		this.tileSize = tileSize;
		
		// Figure out how many texture coordinates we will have
		
		rows = texture.getHeight() / tileSize;
		cols = texture.getWidth() / tileSize;
	
		// Initialize the float buffers
		
		textureCoordinates = new FloatBuffer[rows * cols];
		vertexData = BufferUtils.createFloatBuffer(8);
		vertexData.put(new float[] {0,0,tileSize,0,tileSize,tileSize,0,tileSize});
		vertexData.flip();
		
		// Calculate all the texture coordinates ahead of time
		
		for(int i=0; i<rows; i++)
		{
			for(int j=0; j<cols; j++)
			{
				float srcX = j * tileSize;
				float srcY = i * tileSize;
				float u = (srcX + 0.5f) / texture.getWidth();
				float v = (srcY + 0.5f) / texture.getHeight();
				float u2 = (srcX + tileSize) / texture.getWidth();
				float v2 = (srcY + tileSize) / texture.getHeight();
				
 				//displayPosX -= displayPosX%0.1f;
            			//displayPosY -= displayPosY%0.1f;
				textureCoordinates[i * cols + j] = BufferUtils.createFloatBuffer(8);
				textureCoordinates[i * cols + j].put(new float[] {u,v,u2,v,u2,v2,u,v2});
				textureCoordinates[i * cols + j].flip();
			}
		}
		
	}

Mipmapping is used for down-scaling, not up-scaling. The only valid parameters for GL_TEXTURE_MAG_FILTER is GL_NEAREST or GL_LINEAR.
http://www.opengl.org/sdk/docs/man/xhtml/glTexParameter.xml

When you have too tightly packed an atlas, you will end up with bleeding where linear filtering samples neighbouring pixels. See here:

The solution is to use greater spacing between tiles. Also, you can use “duplicate edge padding” as I explained in the post; there are tools like this one which do it for you:
https://code.google.com/p/libgdx-texturepacker-gui/

If you are targeting newer desktops, you could instead use GL_TEXTURE_2D_ARRAY.

If you are targetting older desktops and don’t want to use mipmapping, 3d textures with +0,5 depth shift and each sprite on another depth level works good, too. :wink:

EDIT: I tried the texture packer you suggested with duplicate and edge padding to regenerate the sheet and it made no difference. Still seeing the bleeding.

This post seems to suggest not to use GL_LINEAR but if I don’t my scaled textures look really bad hence my frustration. Also, I am seeing the bleeding even though my tiles are not scaled.

My original sprite sheet design divided the sheet into individual textures but I was quickly informed that this a no-no! I must tell you though, my original design works flawlessly when it comes to rendering with no bleeding, strange artifacts or noticeable change in performace.

Dave I should have mentioned that I am scaling down from 256x256 and really didn’t want to use mipmapping but it made everything nice and smooth. What about setting filtering to GL_NEAREST just for my static tiles? Is this possible? Hmmm.

Would this require a major change in my code? I realize that most of my code is considered legacy opengl but I am still learning. At least I have vertex arrays working nicely though.

But I digress, I really want to perfect a technique using a texture atlas so I’m not giving up yet! :point:

I think you’ll save yourself a lot of trouble if you switch to texture arrays.

After researching ways to correct atlas bleeding, I was left with the impression that doing so leads down the path of extra processing and trying to handle special cases. Before texture arrays, this was the only viable way to handle tiled atlases, but now I wouldn’t recommend it. Not the same case for UV maps though of course.

Aside from not having to worry about bleeding, texture arrays have the added benefit of being able to wrap. In my opinion, it’s this which makes them exponentially better than a regular tiled atlas.

Working with texture arrays and adding them into your project should be pretty easy. I find them to work very similar to regular textures. Most the changes would be in your initial loading of textures. After that it’s just making sure you bind a texture array when you draw and adding another coordinate for accessing the texture in your shader.

Here’s an explanation about how to use them.
http://www.opengl.org/wiki/Array_Texture