Keeping transparency

Hi, I’ve recently converted a small game from Java 2D to LWJGL. My sprites were drawn in photoshop… ask for a transparent background… start drawing etc… which works fine for java 2D. I’m struggling to load the images correctly using DevIL. They display correctly but the parts that should be transparent are pure white. The code I use to load a texture from a file is:


public int loadTexture(String path) 
      {
       IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
       IL.ilGenImages(1, image);
       IL.ilBindImage(image.get(0));
       IL.ilLoadImage(path);
       IL.ilConvertImage(IL.IL_RGBA, IL.IL_BYTE);
       ByteBuffer scratch = ByteBuffer.allocateDirect(IL.ilGetInteger(IL.IL_IMAGE_WIDTH) * IL.ilGetInteger(IL.IL_IMAGE_HEIGHT) * 4);
       IL.ilCopyPixels(0, 0, 0, IL.ilGetInteger(IL.IL_IMAGE_WIDTH), IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 1, IL.IL_RGBA, IL.IL_BYTE, scratch);
       
       // Create A IntBuffer? For Image Address In Memory
       IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
       GL.glGenTextures(buf); // Create Texture In OpenGL

       GL.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
       // Typical Texture Generation Using Data From The Image

       // Linear Filtering
       GL.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
       // Linear Filtering
       GL.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
       // Generate The Texture
       GL.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, IL.ilGetInteger(IL.IL_IMAGE_WIDTH), 
               IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, scratch);

       return buf.get(0); // Return Image Address In Memory
   }

Then in the initGL method I have:


GL.glEnable(GL11.GL_BLEND);
            GL.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); 

What am I forgetting/not doing right? Oh… and the images are saved in .gif format. :slight_smile:

have you enabled blending? GL11.glEnable(GL11.GL_BLENDING)

You may also need to change the blending mode, if the textures are to be used for sprites then this should work well:
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

Heh, ye, towards the bottom of my post, the 2nd code segment I pasted. :wink: So does anyone know what I’m missing?

Probably an alpha layer in your image…

GIF doesn’t support alpha layers, RGB only. Well actually it uses an indexed palette which i presume DevIL converts into an RGBA image by setting the appropriate color for each pixel and setting the alpha value to 255, ie, white.

Use TGA textures instead, and export from PS as 32 bit image and it should work. I’ve never used DevIL but i’d imagine it can handle 32 bit TGAs, they’re a fairly common format.

D.

doh, sorry about that… shouldn’t post so late at night :wink:

Well Weston, turns out you were closest :wink: I was disabling blending in my particle rendering code :-/

Ok so they do have transparency now, but what I’m seeing now is that when two sprites overlap, the transparent area of the sprite on top is drawn over the sprite beneath it, so basically, the terrain under both sprites gets drawn over part of the lower sprite.

I’ve tried setting blending mode to both of

GL.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);

but I see the same effect in both. Any ideas?

ah well !

looks like devIL is cleverer than I am :slight_smile: Must selectively set the alpha channel in the resultant image depending on which color you’ve set to be transparent in the GIF.

As regards your other problem, If you’re just using straight blending you have to sort your sprites so the ones closest to the camera are drawn last. Otherwise the ones underneath will be depth culled if they’re drawn after another one thats in front of it from the cameras point of view.

Alternatively you can use alpha culling instead of alpha blending with a suitable cutoff value. This will ensure no fragments at all are written to the color or depth buffers.

D.