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:
http://www.cyntaks.com/projects/wjge/decal.png
and GL_MODULATE on this one:
http://www.cyntaks.com/projects/wjge/modulate.png
here is the code for loading the image
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)
private static ByteBuffer convertImageData(BufferedImage bufferedImage,Texture texture) {
ByteBuffer imageBuffer = null;
WritableRaster raster;
BufferedImage texImage;
int texWidth = 2;
int texHeight = 2;
// find the closest power of 2 for the width and height
// of the produced texture
while (texWidth < bufferedImage.getWidth()) {
texWidth *= 2;
}
while (texHeight < bufferedImage.getHeight()) {
texHeight *= 2;
}
texture.setTextureHeight(texHeight);
texture.setTextureWidth(texWidth);
// create a raster that can be used by OpenGL as a source
// for a texture
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());
}
// copy the source image into the produced image
Graphics g = texImage.getGraphics();
g.setColor(new Color(0f,0f,0f,0f));
g.fillRect(0,0,texWidth,texHeight);
g.drawImage(bufferedImage,0,0,null);
// build a byte buffer from the temporary image
// that be used by OpenGL to produce a texture.
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?