13
votes

I'm trying to set the transparency of a texture on a quad in opengl, playing with the blend functions didn't help neither did anything on google. Any suggestions?

3

3 Answers

32
votes

I had a similar problem, with one piece of code that worked correctly and another that didn't. After a lot of logging and debugging, I found that the difference was one line of code.

In the code that had working alpha, I was calling the following before setting my renderer.

    setEGLConfigChooser(false)

If that doesn't help, here are a couple of other pointers...

Make sure that you've enabled blending before you load the texture.

For example:

    // Enable blending using premultiplied alpha.
    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);

...and make sure that the bitmap that you're using to generate the texture actually has an alpha component.

1
votes

Following Ron's answer I got closer to solving my problem of the texture's transparency not displaying. The next issue I had was, I had a non-GL SurfaceView behind my GLSurfaceView and by using:

setEGLConfigChooser(false);

I saw my texture okay but my entire SurfaceView was blacked out. The following fixed my issue:

setEGLConfigChooser(8, 8, 8, 8, 0, 0);

Setting false as the only parameter is one way to disable depth testing but you can effectively do the same by setting the 2nd to last parameter to 0 as well. The former will create a default RGB_565 config whereas I required a RGBA_8888 config hence the change.

1
votes

I was able to fix my problem listed using a different method. I first set

gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, /*GL10.GL_REPLACE*/ GL10.GL_MODULATE);

Then was able to set the alpha of a texture using

gl.glColor4f