# Use different values for Depth Test and Depth Write in OpenGL

Is there a way to use something similar to PolygonOffset to make a depth test more permissive (move fragment depth towards eye) but still write the original depth value to the depth buffer? The only thing I can come up with is a tricky two pass solution using a stencil buffer, but I'd like to avoid that if possible.

Context of my question: We have an entity with a particle based smoke plume that is often in front of a semi-transparent building. Do to some constraints with batching, the building gets drawn after the smoke plume. Originally the smoke plume particles were rendered with a depth test but no depth write. This caused the building to be drawn on top of the smoke.

Turning the depth write on for the smoke particles fixes the building, but now the smoke particles interfere with each other. Ideally, I'd like to find a way for them to stop interfering while still writing to the depth buffer.

• Did you take a look at glDepthFunc? opengl.org/sdk/docs/man/docbook4/xhtml/glDepthFunc.xml – glampert May 15 '14 at 18:22
• If you "lie" with the depth testing, you'll eventually get some other undesirable artifact, where the smoke appears in front of something it should be behind. I think you should revisit how to draw the smoke last... – david van brink Feb 10 '15 at 5:45

I think you may be interested in glDepthRange (...). That affects the mapping from NDC [-1.0,1.0] to window-space Z (the depth buffer stores window-space Z) during the viewport transformation. Effectively the nearplane becomes nearVal and the farplane becomes farVal. You can fool with this to bias and/or rescale your assigned depth values, but GL clamps these values to [0.0,1.0].
• Alpha blending will not prevent it from working, alpha testing on the other hand will. That is to say, if you use discard in your fragment shader. You may need to add layout(early_fragment_tests) in; to the beginning of your fragment shader to force this behavior. That said, if you are using alpha blending then you already have a pretty strict required draw order, I am not sure why you want to write to the depth buffer at all in this situation? – Andon M. Coleman May 19 '14 at 18:51