I'm having a problem with images that I load in to use as textures, it seems like no alpha information is being used. Its not just that my textures have opaque backgrounds, but it seems like they are using indexed color mode or something, if I load in a texture that uses alpha blending for smooth edges or something I get weird artifacts, but an 8-bit png works perfect (still has an opaque background though...). Here are a couple screens of a texture created from a 24-bit png with anti-aliased (translucent) edges:
I'm using glTexEnvi(blah, blah, GL_DECAL) on this one:

and GL_MODULATE on this one:

here is the code for loading the image
1
| image = ImageIO.read(new BufferedInputStream(ImageUtils.class.getClassLoader().getResourceAsStream(resource))); |
this converts the image to use opengl's color model (from kevglass/matzon's 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
| private static ByteBuffer convertImageData(BufferedImage bufferedImage,Texture texture) { ByteBuffer imageBuffer = null; WritableRaster raster; BufferedImage texImage; int texWidth = 2; int texHeight = 2;
while (texWidth < bufferedImage.getWidth()) { texWidth *= 2; } while (texHeight < bufferedImage.getHeight()) { texHeight *= 2; } texture.setTextureHeight(texHeight); texture.setTextureWidth(texWidth);
if (bufferedImage.getColorModel().hasAlpha()) { raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,4,null); texImage = new BufferedImage(glAlphaColorModel,raster,false,new Hashtable()); } else { raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,texWidth,texHeight,3,null); texImage = new BufferedImage(glColorModel,raster,false,new Hashtable()); }
Graphics g = texImage.getGraphics(); g.setColor(new Color(0f,0f,0f,0f)); g.fillRect(0,0,texWidth,texHeight); g.drawImage(bufferedImage,0,0,null);
byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData();
imageBuffer = ByteBuffer.allocateDirect(data.length); imageBuffer.order(ByteOrder.nativeOrder()); imageBuffer.put(data, 0, data.length); imageBuffer.flip(); return imageBuffer; } |
I've tried forcing it to use glAlphaColorModel, but the texture is still opaque.
not sure whate else there is to show, I've just enabled GL_TEXTURE_2D and blending and tried messing with glTexEnvi, but I've run out of ideas. Could I have changed some ogl state to make it act this way? or do you think it is in the image loading? I'll mess around with DevIL some more tommorow, that should eliminate the possibility of the problem being in the image loading.
Any ideas?