How come when I load png or gif files as textures, the transparency dissapears?
this is my texture loader method:
public static int loadTexture(GL gl, String filename) throws TextureException
{
if(TextureManager.loadedTextures.containsKey(filename))
return ((Integer)TextureManager.loadedTextures.get(filename)).intValue();
BufferedImage img = null;
// Load the image
try {
img = ImageIO.read(new File(filename));
} catch (Exception e) {
throw new TextureException("TextureException - " + filename + " - " + e.getMessage());
}
ByteBuffer dest = null;
int[] data = new int[img.getWidth()*img.getHeight()];
for(int y = 0; y<img.getHeight(); ++y)
{
int _y = y*img.getWidth();
for(int x = 0; x<img.getWidth(); ++x)
{
data[_y+x] = img.getRGB(x,y);
}
}
// Convert from bgra to rgba pixel format
bgra2rgba(data);
dest = ByteBuffer.allocateDirect(data.length * BufferUtils.SIZEOF_INT);
dest.order(ByteOrder.nativeOrder());
dest.asIntBuffer().put(data, 0, data.length);
int[] id = new int[1];
// Generate a texture and bind it
gl.glGenTextures(1, id);
gl.glBindTexture(gl.GL_TEXTURE_2D, id[0]);
// Set texture filters to linear interpolation
gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_MIN_FILTER,gl.GL_LINEAR);
gl.glTexParameteri(gl.GL_TEXTURE_2D,gl.GL_TEXTURE_MAG_FILTER,gl.GL_LINEAR);
// Upload the image to video memory
gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, img.getWidth(), img.getHeight(), 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, dest);
// Store the image id
TextureManager.loadedTextures.put(filename, new Integer(id[0]));
return id[0];
}
//converts picture buffers from bgra to rgba
protected static void bgra2rgba(int[] argb)
{
for(int v = 0; v<argb.length; ++v)
{
int c = (argb[v] & 0xFF00FF00);
int r = (argb[v] & 0x00FF0000) >> 16;
int b = (argb[v] & 0x000000FF) << 16;
int finalc = c|r|b;
int rgb = ((finalc & 0x00FFFFFF) == 0x00FF00FF)?(finalc & 0x00FFFFFF):finalc;
argb[v] = rgb;
}
}
thanx a lot for any help