Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Texture atlas bleeding w/mipmap  (Read 553 times)
0 Members and 1 Guest are viewing this topic.
Offline songangel

Junior Member


Medals: 1



« Posted 2013-08-22 16:42:38 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
   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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
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();
         }
      }
     
   }


  
Offline davedes
« Reply #1 - Posted 2013-08-22 17:01:04 »

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:
http://www.java-gaming.org/topics/bleeding/26823/msg/237162/view.html#msg237162

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.

Offline Mac70
« Reply #2 - Posted 2013-08-22 17:04:43 »

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

Check out my Devblog! Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline songangel

Junior Member


Medals: 1



« Reply #3 - Posted 2013-08-22 17:18:40 »

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.

Quote
When you have too tightly packed an atlas, you will end up with bleeding where linear filtering samples neighbouring pixels. See here:
http://www.java-gaming.org/topics/bleeding/26823/msg/237162/view.html#msg237162

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.

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

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!   Pointing
Offline Redocdam

Senior Member


Medals: 17



« Reply #4 - Posted 2013-08-22 22:40:03 »

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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (13 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (29 views)
2014-07-23 21:07:15

Riven (30 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!