Depth Problem

Started by
12 comments, last by jollyjeffers 18 years, 8 months ago
I'm not too sure how to describe this problem. Perhaps it has something to do with the Z-buffer? But I have no idea. Just look at this screenshot: Image Hosted by ImageShack.us The top image is looking at the 'trees' from behind, whereas the bottom image is looking at them from the front. The problem is quite clear. Any ideas what could be causing this, and how I could go about fixing it? (I'm using DirectX 8)
luke88
Advertisement
Are these 3D trees? or are they billboarded? If it's 3D trees that indeed the problem could be with the zbuffer, things to check would includ:

- Did you enable the AutoDepthStencil when creating the device
- Are you clearing the zbuffer before rendering?
- Are you sure you havent turned the D3DRS_ZWRITEENABLE render state off somewhere? Try turning it on
- Also make sure the D3DRS_ZENABLE renderstate is set to D3DZB_TRUE
[size=2]aliak.net
Quote:Original post by IFooBar
Are these 3D trees? or are they billboarded?

The trees are 3D.
Quote:- Did you enable the AutoDepthStencil when creating the device

If I do that, I get errors when trying the change the ambient light, moving the wolrd matrix etc, so I can't enable it.
Quote:- Are you clearing the zbuffer before rendering?

I think so, but I'm not too sure how to check such a thing. (Sad, I know)
Quote:- Are you sure you havent turned the D3DRS_ZWRITEENABLE render state off somewhere? Try turning it on

I checked, and this is definately enabled.
Quote:- Also make sure the D3DRS_ZENABLE renderstate is set to D3DZB_TRUE

This is also enabled.

Thanks IFooBar, but the problem still occurs :(
Any other suggestions?
luke88
To enable Z-buffering, you need to set these values:
(d3dpp is D3DPRESENT_PARAMETERS)
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;

You can use a different depth format if you want.

If you don't set these, then you don't have a z-buffer, so z-buffering is off, no matter what values you set with SetRenderState.

If you get errors with the z-buffer enabled, this is a different issue, which needs to be solved if you want z-buffering. What kind of errors do you get?
Sirob Yes.» - status: Work-O-Rama.
Apon doing that, the program runs, but there are huge graphical problems. Most of the time, the window is blank, but occasionally, weird lines and dots can be seen.
Image Hosted by ImageShack.us
Maybe that has something to do with my poor graphics card (a GeForce 2).
And perhaps the main issue isn't z-buffer related? Although, I have no idea.
luke88
Well, perhaps it doesn't have a stencil buffer (though I doubt it).

Does a format of D3DFMT_D16 give different results?

More likely, you're simply not clearing the z-buffer every frame, which is causing problems in drawing. To clear the z-buffer, you need to add the D3DCLEAR_ZBUFFER flag to your clear operation. Also, you'd need to supply a correct value to clear to, which is 1.0f.

All in all, your clear function should look something like this (this is c++):

m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0xFF777777, 1.0f, 0 );
(note the parts in bold).
Sirob Yes.» - status: Work-O-Rama.
That last screenshot looks a bit odd for depth buffering - are the trees the only thing you're rendering, or is there a background/whatever in there as well?

Being a GeForce2, I would recommend setting your format to D3DFMT_D16 as sirob suggested - you can use the DXCaps viewer (or programmatic enumeration) to check for D3DFMT_D24S8 (and others), but a D16 should be a safe choice.

Two others things that haven't been mentioned yet that you might want to look into:

1. Your projection matrix - how is it configured? specifically the near and far clipping planes? Some combinations (particularly when zNear is less than 1.0f) can cause odd depth buffering behaviour.

2. Run your program against the debug runtimes, you might have made a configuration mistake and/or a parameter mistake that the debug spew will scream at you for [smile]. See the link in my signiture below for details on debugging Direct3D (if you're not already familiar).

hth
Jack

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

If I use the D3DFMT_D16 for the stencil buffer, I still get those odd (even worse) graphical problems. So, the stencil buffer is not enabled.
Also, if I try to clear the z-buffer, I get an error when the program tries to run: "Run-time error '-2005530516 (8876086c)': Automation error".

My projection matrix is configured: FoV: pi / 4, Aspect: 1, zNear: 0.1, zFar: 300. Changing any of these does not affect the problem in any way.
As for running my program the debug runtimes, well, I'll look into it :)

Here's another screen:
Image Hosted by ImageShack.us

[Edited by - luke88 on August 13, 2005 8:55:27 AM]
luke88
Ahg, I'm so stupid. Ok, I just found out the cause of the problem is down to my bad coding.
The code I have to render the meshes is as follows:

D3DDevice.BeginScene
For a = 0 To ObjCount - 1
D3DDevice.SetTransform D3DTS_WORLD, MeshMatrix(a)
D3DDevice.SetTexture 0, ObjTexture(a)
ObjMesh(a).DrawSubset 0
Next a
D3DDevice.EndScene

MeshMatrix(1) and ObjMesh(1) is the first blue tree
MeshMatrix(2) and ObjMesh(2) is the second blue tree
MeshMatrix(3) and ObjMesh(3) is the first green tree
Etc.

Of course I have to render what is closest to the camera first. Any ideas of how I could do this?
luke88
I'm thinking you mean drawing what is farthest from that camera first, but you don't need to do that.

Z-buffering allows you to draw in any order you want. Your problem, almost surely, is that your z-buffer is not enabled. Why this is happening could be several things.

First, if you go to Start->Programs->DX SDK->Utilities->DX Error lookup and enter the error number you got (8876086c in hex form) you get this:

HRESULT: 0x8876086c (2289436780)
Name: D3DERR_INVALIDCALL
Description: Invalid call
Severity code: Failed
Facility Code: FACILITY_D3D (2166)
Error Code: 0x086c (2156)


Did you enable AutoDepthStencil when you tried to clear the z-buffer? You need to BOTH enable AutoDepthStencil AND clear the z-buffer every frame. You also need to enable the D3DRS_ZENABLE and D3DRS_ZWRITEENABLE using SetRenderState (this needs to be done once).

If doing all these things doesn't help, I don't know what to say :).
Sirob Yes.» - status: Work-O-Rama.

This topic is closed to new replies.

Advertisement