I would like to produce an effect where certain objects are only rendered if they fall within a certain space. Think of a “magic decoder volume” that reveals secret objects when they are inside it.
Let’s assume that the secret objects and revealing volume are all spheres. I first render the non-secret objects. I then render the back face of the revealing volume into the depth and stencil buffers. If I render only the secret objects that pass both depth and stencil tests, they’ll also be drawn in front of the revealing volume. I’d like the ability to create a second depth buffer that holds front of the revealing volume. Is there a standard way to do this?
I think I could probably set the near clipping plane to pass through the center of the revealing sphere. Then I could pull the near-clipping plane back closer to the eye, render the front face of the revealing volume and set the far clipping plane to its center and only render secret object vertices that are greater than the depth test and pass the stencil test.
This two-pass approach feels a bit ugly and if there’s a non-secret object in the volume that should be in front of a secret object, it might get squished because of the reversed depth test. I don’t have much experience with shaders, but I’m guessing that’s the way to go?
Vincent’s deleted answer regarding a shader isn’t such a bad idea — per fragment work out the distance from the centre of your revealing sphere; if the distance is too great then
discard.Otherwise, shadow volumes work because they never reveal any additional geometry. Though thinking as I type, I guess what you could do is:
GL_LESSdepth testing, to establish depth values;GL_GREATERenabled, so portions of those faces will be drawn only if things are in front of them;GL_EQUAL;The net effect should be that after the first four steps your depth buffer has values equal to the true values only where it intersected the clipping geometry. Hence when you switch to
GL_EQUALcolours will get through for only that geometry.Specifically, step 3 wipes out anything behind the clipping geometry, step four wipes out anything in front. The implicit assumption is that your clipping geometry fits entirely within the view frustum.