Abusing the depth buffer
(this might be an advanced topic, but because I'm an intermediate programmer I'll put it here for now.)
What I want to do is draw a shape, and write to gl_FragDepth so that it "erases" the existing depth values in that shape's region, setting them to what they would be if I had called glClear(GL_DEPTH_BUFFER_BIT). HOWEVER: I only want to erase the depth values that are *behind* the shape that I'm drawing. I want the depth values in front of the shape to remain as they are.
In theory, this is simple:
1) Ensure that the depth test is enabled.
2) Draw the shape, testing fragments against the depth buffer
3) If the fragments pass the depth test, proceed to write a uniform value to gl_FragDepth
The problem is that depth testing occurs after fragment processing, which means there's no way to tell whether a fragment has passed the depth test from within the fragment shader. Even on hardware that allows early depth testing, as soon as I try to write gl_FragDepth, the early depth test is disabled. Even if I explicitly request an early depth test, I can't write to gl_FragDepth!
Has anybody here had any luck trying something like this? Is it possible? What might be some workarounds?
Is it possible to read the value that's currently inside the depth buffer from within the fragment shader? Then I could do the depth test myself to see whether or not it should be overwritten... (Framebuffer Objects, maybe?)
Thanks in advance!
Last edited by WIld Sage; 02-03-2014 at 03:04 AM.
Reason: Had an additional thought
Tags for this Thread