OpenGl alpha test - How to replace AlphaFunc with obsolete?

I am trying to draw a sphere with alpha, but I have a problem with my Z-buffer. Some pixels are transparent, but they are written in Zbuffer, so the hidden pixel is immediately 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 that the function glAlphaFunc(GREATER, aFloat)and enable(ALPHA_TEST)can do it, but I have read that it is deprecated feature from version 3.1 OpenGL. How can I make the correct render without using ALPHAFunc? Does anyone know a trick or a way to do this through shaders?

+4
source share
1 answer

This is very easy to implement in the fragment shader:

uniform float alpha_threshold;

void main() {
    vec4 color = ...;

    if(color.a <= alpha_threshold) // Or whichever comparison here
        discard;

    gl_FragColor = color;
}
+10
source

Source: https://habr.com/ru/post/1545195/


All Articles