5

I'm trying to draw sphere with alpha, but I have a problem with my Z-buffer. Some pixels are transparent but write in the Zbuffer so a pixel opaque just behind is hidden.

Here are my settings:

gl Enable: gl DEPTH_TEST.
gl DepthFunc: gl LEQUAL.
gl Disable: gl CULL_FACE.
gl Enable: gl BLEND. 
gl BlendFunc: gl SRC_ALPHA with: gl ONE_MINUS_SRC_ALPHA.

I know the function glAlphaFunc(GREATER, aFloat) and enable(ALPHA_TEST) can do that but I read that it's a deprecated function since 3.1 version of OpenGL. How can I do a correct render without using ALPHAFunc? Does someone know a trick, or a way to do this via shaders?

JJJ
  • 32,902
  • 20
  • 89
  • 102
user3755394
  • 51
  • 1
  • 2
  • 3
    In modern GL you're supposed to do that in fragment shader, e.g. `if(result.a > threshold) discard;`. – keltar Jun 19 '14 at 10:37
  • Your question is very similar to this: http://stackoverflow.com/questions/23280692/opengl-es2-alpha-test-problems/23283256. I'm not going to suggest it as a duplicate for now since it's a slightly different use case. But my answer in that question should still apply to your case. – Reto Koradi Jun 19 '14 at 15:01

1 Answers1

16

This is very easy to implement in a fragment shader, using the discard statement to prevent a fragment from being rendered:

uniform float alpha_threshold;

void main() {
    vec4 color = ...;
    
    if(color.a <= alpha_threshold) // Or whichever comparison here
        discard;
    
    gl_FragColor = color;
}
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85