I am creating an image in Java 6 using a Mac with the code below. Then I try to display this image on an Android device using OpenGL ES. The image is not displayed, and only a white image is displayed. However, if there are no translucent pixels, the image will display perfectly.
I have no problem displaying translucent images created in Photoshop. Only with translucent images created in Java I have a problem. I also tried various methods of creating a translucent image with Java, and I always get the same result, a white image displayed in OpenGL. At the moment, I believe this is a bug with Java, (on a Mac). Has anyone ever come across something like this? Any idea why this might happen?
// Code to create the image
int cmap[] = {
0x00000000,
0xFF000000,
0xFFFF0000,
0xFFFFFF00,
0xFF00FF00,
};
IndexColorModel colorModel = new IndexColorModel(8,
cmap.length, cmap, 0, true, -1, DataBuffer.TYPE_BYTE);
BufferedImage image = new BufferedImage(256, 256,
BufferedImage.TYPE_BYTE_INDEXED, colorModel);
Graphics2D g = image.createGraphics();
g.setBackground(new Color(0,0,0,0));
g.clearRect(0 , 0, image.getWidth(), image.getHeight());
g.setColor(Color.red);
g.draw(new Rectangle(10, 10, 40, 40));
ImageIO.write(image, "PNG", new File("rectangle.png"));
Below is the texture loading code. Please note that this applies to the Android environment.
public static void loadAndBindTexture(GL10 gl) throws Exception
{
int numberTextures=1;
int[] textures = new int[numberTextures];
gl.glGenTextures(numberTextures, textures, 0);
int textureID=textures[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureID);
checkGLError(gl, 998);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_LINEAR);
checkGLError(gl, 997);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
checkGLError(gl, 996);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,GL10.GL_CLAMP_TO_EDGE);
checkGLError(gl, 1000);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,GL10.GL_DECAL);
checkGLError(gl, 1001);
gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
checkGLError(gl, 1002);
if(mContext==null)
throw new Exception("Context has not been set.");
InputStream is = mContext.getResources().openRawResource(R.drawable.metal128x128);
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(is);
} finally {
try {
is.close();
} catch(IOException e) {
Log.printCaughtExceptionMsg(e);
}
}
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
checkGLError(gl, 1003);
}