Alex Rivera | Logout

OpenGL ES 2.0 PNG alpha channel

Asked 2011-08-17T17:12:21.033
9

I am just learning to work with OpenGL ES 2.0 for Android. I have been trying to simply display a texture on the middle of the screen, which was easy enough, but I cannot seem to get the PNG alpha to work properly. The image will either show with a black background, or the entire image will be blended into the background color slightly, depending on the settings I use.

The actual tutorials I have followed to get to this point never worked with transparency, so I have tried to work in code I have found by searching around, and likely have just missed one important step. I have searched quite a lot to figure this problem out though, and I have not seen any answers that had something my setup does not. I have tried every combination of glBlendFunc and what not with no luck.

I figured if I tried to paste in all of the code that may be related to this, the question would seem very bloated, so I will be happy to post any bits of code you guys ask for. I'd greatly appreciate any ideas for what I should try next.

EDIT :: Here is my fragment shader, which is what I believe to be the cause. This is the only part that I have never really found a decent example for working with transparency, and everything else matches what I have seen elsewhere.

        final String fragmentShader =           
        "precision mediump float;       \n"  
        + "varying vec2 v_Color;          \n"     
        + "uniform sampler2D s_baseMap;   \n"
        + "void main()                    \n"     
        + "{                              \n"
        + "  vec4 baseColor;              \n"
        + "  baseColor = texture2D( s_baseMap, v_Color );   \n"
        + "   gl_FragColor = baseColor;     \n"    
        + "}                              \n"; 

It never does anything with the alpha explicitly, it is from an example that doesn't use it after all, but I still don't know much about fragment shaders and bec

Edit
Report

1 Answer

1

your initial shader is fine. alpha is inherent in color ops, it just may not be applied depending on blend/mode/etc.

given that your texture comes out black if you do fragcolor = base.aaa, that implies your texture data is 'bad'.

looking at your texture load, yeah, it's wrong. you never copy over the alpha, just the rgb. assuming java clears the byte array to 0s, all alpha will be zero, that would get you your black box, that would cause the image to 'vanish' when you enable alpha blending.

To simplify your life, instead of all the hand copying and stuff, you can simply load the bitmap normally and use the GLUtils helper to upload instead of directly using glTexImage2d:

  bitmap = BitmapFactory.decodeStream(is);  
  GLES20.glGenTextures ( 1, textureId, 0 );
  GLES20.glBindTexture ( GLES20.GL_TEXTURE_2D, textureId[0] );
  GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

Something like that. Then enable blending, use src+invsrc blend mode if not premultiplied, and render, you should get the result desired.

answered 2011-08-18T13:24:05.983

Your Answer