Depth Buffer Question

Moderator
Posts: 3,572
Joined: 2003.06
Post: #1
I'm sure this is a very simple question, but for some reason I've never encountered it before. What I want to do is draw a depth tested scene in the background (so to speak), and then I want to layer another depth tested scene on top of that (which I've never done before). [edit] You may be wondering why I would do this, so I should add that it's for a menu which uses 3D elements for the buttons and controls, which is displayed over the game. [/edit] At first, I thought I could just layer objects by setting their depth values so that the two scenes "appear" sorted correctly, but that didn't work out so well. Then I thought maybe I'd have to render one of the scenes to a separate buffer like a texture or something and then layer it on top, but that seems overkill.

Then I thought, well, what would happen if I simply cleared the depth buffer after I finished drawing the first scene and then drew the next scene? It worked! But... That wasn't what I expected. Is this right? Can I multi-pass the depth buffer and get expected results?

It seems to work perfectly, but I thought that depth testing happened late in the pipeline and all the results of the depth testing would be combined. I also assumed that if I cleared the depth buffer after drawing some depth tested objects, their depth component would be essentially discarded (because the depth values would be cleared) and they wouldn't render with depth testing, but right now they appear to be correctly depth tested even after the clear at mid-render between the two scenes. I'm asking because it seems that my mental model of how/when depth testing occurs was incorrect all this time.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
What you described should work as you intended. As I understand, fragments are depth tested as soon as they're drawn, so clearing the depth buffer will have no effect on things you've already drawn; it simply discards accumulated depth buffer state for subsequent drawing.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #3
That is certainly the way it is behaving. For some reason, I thought fragments weren't depth tested until later. Transparent fragments need to stick around but I guess even those could be combined on the fly now that I think of it.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
AnotherJake Wrote:Transparent fragments need to stick around but I guess even those could be combined on the fly now that I think of it.

They are, which is why translucency is such a pain in the rear. Sad

Furthest-to-nearest translucent object depth sorting is necessary because the same value gets written to the depth buffer regardless of opacity level. So, if you draw behind an already-drawn translucent fragment, the new fragment is discarded since the depth test fails, so it doesn't get blended into the color buffer at all.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #5
I've always called it transparency, but I guess it's more properly termed translucency. Yeah, it's always a pain, but especially with meshes. The best I've been able to do is reorder the triangles so they're grouped a little more by location, but it's never perfect. But now that I'm looking at depth testing like this, it makes more sense to me why it is so tricky.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #6
For rendering GUI atop a 3d scene, I just disable depth testing all together. My gui is heirarchical, so it draws recursively from the root container up to the leaf widgets. Transparency is automatic, and it works quite well.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #7
Yeah, my 2D GUI overlay is with depth testing disabled -- no problems there, including translucency. But the 3D GUI that I'm trying to do right now uses 3D objects (meshes and cubes, etc.), so it needs depth testing, which is what led me to this issue. In the past, I've always just had a 3D scene in the background and a 2D GUI on top, so this is a little different for me with 3D on 3D.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #8
What you're doing is perfectly valid:

painters_algorithm(depth_tested_scene(), depth_tested_scene())
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #9
Cool. So then theoretically, a person could use multiple depth tested scenes to effectively multiply the precision of the depth buffer. Not easy to implement in all situations I suppose, but I didn't know that was possible. I guess a person could depth test extremely large scenes like that, although I don't know what that would be useful for...
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #10
AnotherJake Wrote:Yeah, my 2D GUI overlay is with depth testing disabled -- no problems there, including translucency. But the 3D GUI that I'm trying to do right now uses 3D objects (meshes and cubes, etc.), so it needs depth testing, which is what led me to this issue. In the past, I've always just had a 3D scene in the background and a 2D GUI on top, so this is a little different for me with 3D on 3D.

Ah... I didn't realize you were rendering 3d in your actual GUI. As arekkusu said, it should be fine. Personally, I'd probably do my 3D into an FBO, updating it only when needed, and then blit that into my gui.
Quote this message in a reply
Moderator
Posts: 3,572
Joined: 2003.06
Post: #11
TomorrowPlusX Wrote:Personally, I'd probably do my 3D into an FBO

That was my initial thought, but for some reason it struck me as being overkill, and I kept thinking, "It sure would be nice if I could just get a clean depth buffer to work with, and draw over what's already there. Aw, what the heck, let's try it... Holy crap, it works! ... but ... I'd better go check with iDG if this is expected because I thought it wasn't... Sneaky" So here we are!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  problems getting the depth buffer akb825 3 4,177 Jun 2, 2006 12:51 PM
Last Post: akb825
  Drawing to the Depth Buffer unknown 12 4,274 Dec 9, 2005 02:49 PM
Last Post: OneSadCookie
  Depth Buffer / Testing basic question... WhatMeWorry 5 6,361 Nov 18, 2005 12:50 AM
Last Post: arekkusu
  Reading from the Depth Buffer Joseph Duchesne 2 3,034 Jun 16, 2005 04:51 PM
Last Post: OneSadCookie
  Depth Buffer issues. NitroPye 7 4,148 May 30, 2005 01:11 PM
Last Post: NitroPye