What's wrong with this .PNG loader? (32 bit with alpha)

So, I am using this code from a JOGL piece from NeHe’s site to load a 32 bit .PNG file with alpha:


public static void makeRGBTexture(GL gl, GLU glu, [i]BufferedImage img[/i], int target, boolean mipmapped)
       {
                      ByteBuffer dest = null;
                      switch (img.getType())
                      {
                                case BufferedImage.TYPE_3BYTE_BGR:
                                case BufferedImage.TYPE_4BYTE_ABGR:
                                case BufferedImage.TYPE_CUSTOM:
                                {
                                  byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
                                  dest = ByteBuffer.allocateDirect(data.length);
                                  dest.order(ByteOrder.nativeOrder());
                                  dest.put(data, 0, data.length);
                                  break;
                                }
                                case BufferedImage.TYPE_INT_RGB:
                                {
                                  int[] data = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
                                  dest = ByteBuffer.allocateDirect(data.length * BufferUtils.SIZEOF_INT);
                                  dest.order(ByteOrder.nativeOrder());
                                  dest.asIntBuffer().put(data, 0, data.length);
                                  break;
                                }
                                default:
                                  throw new RuntimeException("Unsupported image type " + img.getType());
                      }
                                                                  
                      if(img.getType() == BufferedImage.TYPE_4BYTE_ABGR)                                                                              
                      {
                              if (mipmapped)
                              {
                                glu.gluBuild2DMipmaps(target, GL.GL_RGBA, img.getWidth(), img.getHeight(), GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, dest);
                              }
                              else
                              {
                                gl.glTexImage2D(target, 0, GL.GL_RGBA, img.getWidth(), img.getHeight(), 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, dest);
                              }
                      }
                      else // texture map has no alpha
                      {
                              if (mipmapped)
                              {
                                glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dest);
                              }
                              else
                              {
                                gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dest);
                              }
                      }
       }

The problem is that a texture mapped surface (with blending enabled) [glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE)] is blended okay, but the colors are completely wrong.

If I set this part

if(img.getType() == BufferedImage.TYPE_4BYTE_ABGR)

to

if( true )

forcing the img.getType() == BufferedImage.TYPE_4BYTE_ABGR code to run, the texture mapped surface looks correct, but other polygons with alpha blending and no texture mapping are invisible. It’s weird. ???

So do you see something wrong with the code, which is not specifically a .PNG loader, but a texture maker with the BufferedImage img full of a .PNG file’s contents?

hmm, you can try out the TextureIO helper class in the com.sun.opengl.util.texture package just to rule out that it’s your texture loader. I recently tested the TextureIO class with various file formats (some having alpha channels) and it worked pretty well for me.

Have you tried disabling TEXTURE_2D before rendering polygons without textures?

Johannes

Thanks, Schabby! :slight_smile:

Yes, disabling GL_TEXTURE_2D before rendering polygons without textures made such polygons remain visible. :slight_smile:

Also, I used the texture maker code which forces the img.getType() == BufferedImage.TYPE_4BYTE_ABGR branch.
Why img.getType() isn’t detecting the right image type is a different issue.

Thanks for the heads up on com.sun.opengl.util.texture package. I assume this requires JSR-231 JOGL? I will experiment with it later.

cool! glad I could help.

Yep, you are right. It is in the more recent nightly builds. You can download my TextureIO test from here as a little example.

Johannes

PS: Happy Chinese New Year!