Depth Buffer Problems

Started by
5 comments, last by CodeReaver 18 years, 2 months ago
So basicaly I've done everything I know to do as far as depth buffering goes. This is my current depth buffer related snippets that I've included in my code in the appropriate places: pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_USEW); pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(7, 21, 63), 1.0f, 0); D3DPP.AutoDepthStencilFormat = D3DFMT_D16; D3DPP.EnableAutoDepthStencil = TRUE; D3DPP.Flags = D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL; pD3DDevice is clearly the device, and D3DPP is obviously the Direct3D Present Parameters. Also, I'm not using XYZRHW, just the XYZ coordinates. The problem is that when I render anything (at any depth) it always gets rendered on top of whats already been rendered. All help is GREATLY appreciated. Thanks in advance!
Advertisement
Almost no current drivers support W buffers, so that may be part of your problem.

Set ZEANBLE to TRUE instead.
Changed D3DRS_ZENABLE to D3DZB_TRUE (or 'TRUE' or '1'), but theres still no change.

edit: Got it! It didn't have anything to do with the zbuffer. I was using textures with alpha transparency, but what I didn't know is that you have to set these (?):

pD3DDevice->SetRenderState(D3DRS_ALPHAREF, 0x000099);
pD3DDevice->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);

It at least did what I was hoping for :)

P.S: It seemed like it was the depth buffer because when I put one quad behind the other I couldn't see through the transparent parts. Somehow I got it to alphablend with the backbuffer, but not with anything else, so as the transparent pictures DID match the backbuffer's color they were still occluding the quads behind them.

[Edited by - Christopher Harris on January 19, 2006 9:22:54 PM]
Firstly, alpha blending is draw order dependent - unlike regular opaque geometry, if you render it in an arbitrary order you'll get, well, odd results [smile]

With basic alpha blending it'll effectively control the contribution to the colour buffer. Depth buffer writing is seperate (unless you specifically code operations into your pixel shader) from this (I was reading yesterday that ATI's H-Z seems to test before the rasterizer executes). So, as far as the depth buffer is concerned the pixel still exists and has a valid depth that is written to the buffer - thus you appear to get transparent pixel occluding other geometry.

If you enable alpha testing (as you have) then it'll cull pixels below that threshold. This means that they not only don't write (nothing) to the colour buffer, but they're exluded from the depth/stencil writing. Problem solved.

Graphics engines will typically render ALL opaque geometry first (ideally front-to-back, but it's not required) and then render all semi-transparent geometry. The semi-transparent geometry must be rendered from back-to-front to get the correct results. Various tricks involving rendering models twice (once with CW culling and once with CCW culling), alpha testing and disabling Z writing are common as well.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Yeah, I'm writing a 2D engine, so I tried getting rid of the depth buffer, which was the only differance in my code and some code that worked, and voila! It worked. Seems to me like it shouldn't matter if you render alpha with depth buffers, considering your backbuffer format contains alpha values... anyways, thanks for the help!
uhh... that was me ^ :)
Is this at all similar to my problem?

http://www.gamedev.net/community/forums/topic.asp?topic_id=370776

This topic is closed to new replies.

Advertisement