8

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?

Dolda2000
  • 25,216
  • 4
  • 51
  • 92

1 Answers1

10

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)

Grizzly
  • 19,595
  • 4
  • 60
  • 78
  • Thanks! It was the GL_ALPHA_TEST I was looking for. It's amazing how I didn't find it. :) – Dolda2000 Nov 04 '10 at 01:13
  • @user134252: Do you hace any compelling reason to use the fixed pipeline? In the long run shaders are typically less of a headache. Furthermore the fixed pipeline is deprecated in newer opengl versions so it's generally a good idea to avoid it (some fixed pipeline functions might actually be unimplemented in hardware and force a fallback to software). Assuming you are still learning opengl (pardon me if I'm wrong) wouldn't it make more sense to learn the futureproof way of doing thinks instead of the deprecated one? Just as a suggestion. – Grizzly Nov 04 '10 at 01:23
  • 3
    For one thing, I still want to maintain backwards compatibility with some devices. Even with that, however, the fixed function pipeline does have the advantage that it's easy to combine various features (like choosing to enable lighting, alpha testing, texturing &c. independently of each other), without having to implement a whole meta-programming system for shader code combinatorics. That's quite significant, if you ask me. – Dolda2000 Mar 31 '11 at 07:15