Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Just be be sure, the texture I want to render is never semi-transparent; its alpha values are only ever 1.0 or 0.0, and I use the GL_NEAREST filter, so it never interpolates into any value in between.

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

like image 866
Dolda2000 Avatar asked Nov 04 '10 00:11

Dolda2000


1 Answers

Just to clarify: You want fragments with alpha=0.0 to be written into neither the colorbuffer nor the depthbuffer? Furthermore I assume you are currently using blending to mask transparent pixels, because otherwise this shouldn't be a problem.

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

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

This will ensure that all fragments for which the condition is true won't be rendered at all (instead of being blended with a zero-factor onto the framebuffer).

If you are (for whatever reason) programming fixed pipeline you can use the alpha test to get the same behaviour (personally I would suggest switching to a forward compatible (meaning shader) path, but thats beside the point).

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

As an added bonus since these methods kill the fragment instead of rendering it invisible it might be faster then blending (although I haven't looked into that lately, so it's a might, but it should generally not be slower, so its still a plus)

like image 156
Grizzly Avatar answered Oct 11 '22 09:10

Grizzly