Jump to content
  • Advertisement
Sign in to follow this  
staticVoid2

OpenGL More than one depth buffer

This topic is 2991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was just wondering if it is at all possible to have two or more z buffers in either OpenGL or DirectX and to somehow specify a logic condition between the two that determines whether the fragment should be rendered or not? (AND, OR etc.)

Or is their any similar way this can be done with multiple render passes?

Share this post


Link to post
Share on other sites
Advertisement
You can use the stencil buffer to mark off areas that passed the first ZBuffer test, and then using another pass, only pass fragments that pass both the second ZBuffer test and the stencil test. This would be an AND operation. OR wouldn't need the stencil buffer since you can just render both of the passes without checking for anything. Something like NOT AND or AND NOT would be done by marking off the opposite area of the stencil buffer.

Share this post


Link to post
Share on other sites
Ok, your solution does work if both z buffers have a comparison operator of '<' but I'm talking about having multiple z buffers with different comparison operators.

The reason I'm asking this is because I'm trying to clip geometry that is inside an arbitrary mesh and the only way I can think of doing this without using complex csg operations is to use two or three z buffers.

If I render the front faces of the mesh to buffer 1 and the back faces to buffer 2 and set the comparison operators to <less_than> and <greater_than> respectively for these buffers then I render the scene to the normal z buffer it means any fragment inside the mesh will be discarded.

You can't use the stencil buffer in the case where the comparison operator is <greater_than> because if you think about it - when you render the back faces and then have say a mesh that intersects with one of these back faces - any fragment that lies inside ( less than ) will be discarded which means the stencil value will be set to 0 (good so far), but then what if another mesh is directly behind this one and is also behind the clip mesh? it will set the stencil value to 1. So when you render the scene again all the fragments that lie inside the clip mesh will still be rendered.


I'm sure there's another way of doing this somehow but I don't know at this point whether to pursue this further or to just stick with csg?

Share this post


Link to post
Share on other sites
Well, there're two solutions coming to mind.

1. z-buffer emulation:
You can use a color buffer with two channels of atleast 16 bit (float) or better 32 bit (float) precision per channel. Then write the max and min z value to the according channels of the color buffer. In your final render pass you can read back the value from the buffer, compare the z value and discard the pixel if it is outside the bounding volumne. This approach could be slower, because early-z rejection etc. is turned off for the last pass.

2. stencil magic:
Never have done something like this, but it could work:
I. render all backfaces of the bounding volume, but only write them to the z-buffer
II. render your target mesh which will be inside the bounding volumne, but only render to the stencil buffer (value=1) and z-buffer, not to the color buffer.
III. render all frontfaces of the bounding volume, but only write to the stencil buffer(value = 0, clearing infact the stencils) with inverse z-test.
IV. clear z-buffer, keep stencil buffer
V. render target mesh with stencil test on (reference value=1)


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!