关于OpenGl alpha测试:OpenGl alpha测试-如何替换已弃用的AlphaFunc?

OpenGl alpha test - How to replace AlphaFunc deprecated?

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

这是我的设置:

1
2
3
4
5
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.

我知道函数glAlphaFunc(GREATER, aFloat)enable(ALPHA_TEST)可以做到这一点,但我读到自3.1版本的OpenGL起,它已被弃用。在不使用ALPHAFunc的情况下如何进行正确的渲染?有人知道技巧或通过着色器做到这一点的方法吗?


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

1
2
3
4
5
6
7
8
9
10
uniform float alpha_threshold;

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

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

    gl_FragColor = color;
}