Skip to main content
gamedev.net
🔒 Locked

Problems with Depth

Started by SuBG Apr 7, 2011 at 8:54 PM 1 replies 1.7k views
Original Post
SuBG
SuBG
I am experiencing a strange problem with depth. Here's the background:
1. I successfuly create the depth buffer
2. I successfuly render objects with perspective

---
My depth is incorrect. I tried to output the depth from the shader using:
VS:
psInput.Pos = mul( Pos, mul(mul(World,View),Projection) );
PS:
float depth = psInput.Pos.z;
return float4(depth,depth,depth,1);

The result is black screen. In contrast, when I show psInput.Pos.x or psInput.Pos.y i get correct gradient.

---
I decided it was transformation problem so I examined in debug mode the world, view, and projection matrices, which looked like this:
WORLD: Identity Matrix

VIEW: D3DXMatrixLookAtLH( &mViewMatrix, &eye, &view, &up );
1,0,0,0
0,1,0,0
0,0,1,0
5,5,5,1

PROJECTION: D3DXMatrixPerspectiveFovLH ( &mProjectionMatrix, mCamera->mFOV, mCamera->mAspectRatio, mCamera->mNearClipping, mCamera->mFarClipping);
1.8, 0, 0, 0
0, 2.4, 0, 0
0, 0, 1, 1
0, 0, -1, 0

---
Heres my rasterizer setup:
D3D10_RASTERIZER_DESC rasterizerState;
rasterizerState.CullMode = D3D10_CULL_BACK;
rasterizerState.FillMode = D3D10_FILL_SOLID;
rasterizerState.FrontCounterClockwise = true;
rasterizerState.DepthBias = false;
rasterizerState.DepthBiasClamp = 0;
rasterizerState.SlopeScaledDepthBias = 0;
rasterizerState.DepthClipEnable = true;
rasterizerState.ScissorEnable = false;
rasterizerState.MultisampleEnable = false;
rasterizerState.AntialiasedLineEnable = true;

Here's my depth setup:
// Create depth stencil texture
D3D10_TEXTURE2D_DESC descDepth;
ZeroMemory( &descDepth, sizeof(D3D10_TEXTURE2D_DESC));

descDepth.Width = mWidth;
descDepth.Height = mHeight;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D10_USAGE_DEFAULT;
descDepth.BindFlags = D3D10_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;

mDX10Device->CreateTexture2D( &descDepth, NULL, &pDepthStencil );

D3D10_DEPTH_STENCIL_DESC dsDesc;
ZeroMemory( &dsDesc, sizeof(D3D10_DEPTH_STENCIL_DESC));

// Depth test parameters
dsDesc.DepthEnable = true;
dsDesc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = D3D10_COMPARISON_LESS;

// Stencil test parameters
dsDesc.StencilEnable = true;
dsDesc.StencilReadMask = 0xFF;
dsDesc.StencilWriteMask = 0xFF;

// Stencil operations if pixel is front-facing
dsDesc.FrontFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilDepthFailOp = D3D10_STENCIL_OP_INCR;
dsDesc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;

// Stencil operations if pixel is back-facing
dsDesc.BackFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilDepthFailOp = D3D10_STENCIL_OP_DECR;
dsDesc.BackFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilFunc = D3D10_COMPARISON_ALWAYS;

// Create depth stencil state
mDX10DSState = NULL;
mDX10Device->CreateDepthStencilState(&dsDesc, &mDX10DSState);
mDX10Device->OMSetDepthStencilState(mDX10DSState, 1);

// Create the depth stencil view
D3D10_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory( &descDSV, sizeof(D3D10_DEPTH_STENCIL_VIEW_DESC));
descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;

mDX10Device->CreateDepthStencilView( pDepthStencil, &descDSV, &mDX10DSView );

// Set render targets
mDX10Device->OMSetRenderTargets( 1, &mDX10RTView, mDX10DSView );


Thanks in advance.

Cheers,
Dimo
The subhuman jaded terrorist
karwosts
karwosts
Try this:

float depth = psInput.Pos.z / psInput.Pos.w;

You need to perform perspective divide before you can get the 0 to 1 depth range that you typcially expect.

Note that depth doesn't tend to visualize very well, because it is so concentrated towards the near plane. If you took a typical scene and rendered depth as color it would probably look entirely white. If you have near/far planes like 1/1000, anything greater than 2 units from the camera probably occupies the range from 0.95 to 1.0, making it very hard to see as color.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
SuBG
SuBG
Thanks for the reply.
Yes i made the division but had no change.
I also experimented with dividing with the far-near range, but again - no change.
Anyways, i get objects rendered well as a whole, but when i disable backface culling there's z fighting occuring.

Thanks again,
Dimo
The subhuman jaded terrorist

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.