OpenGl alpha测试 - 如何替换AlphaFunc已被弃用?

use*_*394 4 opengl alpha depth-testing

我正在尝试使用alpha绘制球体,但我的Z缓冲区存在问题.有些像素是透明的,但在Zbuffer中写入,因此隐藏在后面的不透明像素.

这是我的设置:

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.
Run Code Online (Sandbox Code Playgroud)

我知道这个函数glAlphaFunc(GREATER, aFloat)并且enable(ALPHA_TEST)可以做到这一点,但我读到它是自3.1版OpenGL以来的一个不推荐使用的函数.如何在不使用ALPHAFunc的情况下进行正确的渲染?有人知道一个技巧,或通过着色器做到这一点的方法?

Col*_*Two 12

这在片段着色器中很容易实现:

uniform float alpha_threshold;

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

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

    gl_FragColor = color;
}
Run Code Online (Sandbox Code Playgroud)