2

I'm trying to get my point sprites to display with the correct opacity.

Originally, I was getting my sprite texture on a black square.

So, I added the following to my fragment shader:

"if(color.a < 0.5) "+
               "discard;"+

Now, this does seem to work, in that my sprite displays without the black background, however, my texture itself is 'partially transparent' - and it isn't showing this partial transparency, it is appearing solid. It's a bit difficult to explain, but I hope you understand what I mean. If I draw the same texture using canvas/surfaceview, it displays correctly.

Basically I'm trying to get my textures to display in their original format (ie as they do in the software in which they were created - ie, the Gimp / photoshop etc).

Would appreciate any help - thanks

Zippy
  • 3,826
  • 5
  • 43
  • 96

1 Answers1

6

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.

Jave
  • 31,598
  • 14
  • 77
  • 90
  • Thanks @Jave, I used your suggestions, I've removed the discard part of the Fragment Shader and although there is now no black box, it doesn't look right still - the texture is now 'transparent' but there is a dark outline around it and the image is duller than it should be - (Too transparent??) Would be grateful if you have any other suggestions - thanks – Zippy Mar 22 '13 at 12:55
  • 1
    I figured it out - I needed to use GLES20.glBlendFunc(GLES20.GL_ONE, GLES20.GL_ONE_MINUS_SRC_ALPHA); along with your suggestion - thanks for pointing me in the right direction! – Zippy Mar 22 '13 at 13:44
  • 1
    wouldn't transparency based on alpha channel be glBlendFunc(GL_APLHA, GL_ONE_MINUS_SRC_ALPHA), from example section of the documentation, https://www.khronos.org/opengles/sdk/docs/man3/html/glBlendFunc.xhtml – HPP Jul 16 '15 at 04:14