Transparent or transparent image created in Java is not displayed in Android OpenGL ES

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, /*transparent*/ 
0xFF000000, /*black*/ 
0xFFFF0000, /*red*/ 
0xFFFFFF00, /*yellow*/ 
0xFF00FF00, /*green*/ 
//...... 
}; 
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)); /*transparent*/ 
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);//polyBreaker3DObject_.mTextureID);

    checkGLError(gl, 998);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_LINEAR);
    checkGLError(gl, 997);
    //NOTE: FOR GL_TEXTURE_MAG_FILTER only GL_NEAREST or GL_LINEAR are valid.
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    checkGLError(gl, 996);

    /*Commented on Sept. 30, 2010 6:21AM
    gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_NEAREST);
    gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR);
    */
    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)//mContext is the Context of the current Android application (it is set before this method is called
    throw new Exception("Context has not been set.");
 InputStream is = mContext.getResources().openRawResource(R.drawable.metal128x128);//blueski);//robot);
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);

}
+3
source share
2 answers

glDrawElements() glDrawArrays()? , , OpenGL draw. , , .

- :

gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_BLEND);

, .

EDIT:

, . BufferedImage, . TYPE_BYTE_INDEXED, , - . TYPE_INT_ARGB ?

+1

, , Java, Android OpenGL ES. , , .

java, , ,

        g.drawImage(Image img, x, y,  null);

//g - Graphics,

// img - , 100% ( ) , Photoshop. 4x4, .

. Android OpenGL ES.

, , , PNG, Android OpenGL ES.

0

All Articles