OpenGL - don't write depth if alpha

2019-04-14 05:44发布

问题:

What I'm want to do in OpenGL using C++ and GLSL: When texture has alpha (texture.a! = 1.0;) then this pixel is not written to the depth buffer. (for color buffer it is written) Write depth occurs only when a pixel texture.a == 1.0;

Discarding in shader is not a solution - then this pixel is not written to color buffer.

Any ideas?

@UPDATE: Example: I've got some UI images rendered by OpenGL. Some of them have alpha in corners. In scene rendering I have "depth prepass" to save some pixels by not calculating light on them. I want to also get UI images to that prepass - but only completely opaque pixels (alpha = 1.0).

回答1:

As mentioned in the comments by Andon: "You always have to write a depth value when you write a fragment, there is no way to selectively enable or disable this at the shader level."