First make sure your textures are loaded from transparent pngs through a Bitmap
with either RGBA_8888
or RGBA_4444
configuration, so you don't lose the alpha channel.
Second you need to enable GL_BLEND
with the glEnable()
command. On Android you will write it like this: GLES20.glEnable(GLES20.GL_BLEND);
. This allows you to blend the already drawn color with the new color, achieving a transparent look.
The blending function should be set to GL_ONE, GL_ONE_MINUS_ALPHA
for regular transparency: glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
GL_SRC_ALPHA, GL_ONE_MINUS_ALPHA
for regular transparency: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Finally, you do not need to use discard, just set the gl_FragColor
to a 4-component vector with the alpha in the fourth channel (which is what you get when reading a texture from a sampler), e.g. you could just do gl_FragColor = texture2D(sampler, texCoord);
if you wanted to.
You will most likely have to turn off depth-testing with glDisable(GL_DEPTH_TEST)
to avoid problems with unsorted triangles.
You can read a little bit more about transparency here.