OpenGL: Can I mask fully transparent fragments from the depth buffer?

Is there a way to tell OpenGL not to write the depth of fully transparent fragments to the depth buffer?

Just be sure the texture I want to display is never translucent; its alpha values ​​are only 1.0 or 0.0, and I use the GL_NEAREST filter, so it never interpolates any value between them.

I would have thought that it would be reasonable for OpenGL to simply not write to the depth buffer when the fragment had an alpha value of 0.0 (so I could display primitives with such textures in any order), but I cannot find a way to do this is opengl. Does anyone know if this is possible, and in this case, how is this done?

+4
source share
1 answer

Just to clarify: do you want the fragments with alpha = 0.0 not to be recorded either in colorbuffer or in depth? Also, I assume that you are using blending to mask transparent pixels, because otherwise it should not be a problem.

In this case, you can simply discard the fragment in your fragmentshader:

if( color.a<=0.0 ){ discard; } 

This ensures that all fragments for which the condition is true will not be displayed at all (instead of being mixed with a zero coefficient on the framebuffer).

If you (for some reason) program a fixed pipeline, you can use the alpha test to get the same behavior (personally, I would suggest switching to a direct compatible (implying a shader) path, but it’s not).

 glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER,0.0f); 

As an added bonus, since these methods kill the fragment rather than making it invisible, it can be faster than mixing (although I have not looked at it recently, so it’s possible, but in general it should not be slower, so it’s still a plus)

+6
source

All Articles