Maik Klein
Maik Klein

Reputation: 16158

Avoiding a depth buffer write with an alpha value of 0

I want to render several png files with transparency on top of each other. I only need "absolute transparency" with the alpha value being 1 or 0.

Example

  glEnable(GL_DEPTH_TEST);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

This is what I am currently having, the problem is that OpenGL still writes to the depth buffer even if the alpha value is 0, how can I avoid this?

Do I have to sort my sprites with the depth value? I am currently grouping my sprites by texture id to reduce texture state changes. Sorting by depth value would definitely increase my texture state changes.

Upvotes: 0

Views: 1160

Answers (1)

Reigertje
Reigertje

Reputation: 725

You can use alpha testing.

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, threshold); 

Which only draws the pixel if the alpha value of the incoming pixel is greater than threshold.

This is deprecated in newer OpenGL versions, but can easily be mimiced by adding something like this to your fragment shader:

if (col.a <= threshold)
    discard;

Upvotes: 2

Related Questions