简体   繁体   English

DirectX10上的深度缓冲区不起作用

[英]Depth Buffer on DirectX10 not working

So I'm trying to create a scene in DirectX10 and for some reason my zbuffer(depth buffer) doesn't seem to be working. 所以我试图在DirectX10中创建一个场景,由于某种原因,我的zbuffer(深度缓冲区)似乎无法正常工作。 Here is the code where I set up the Zbuffer: 这是我设置Zbuffer的代码:

// Initialize the description of the depth buffer.
ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));

// Set up the description of the depth buffer.
depthBufferDesc.Width = _iScreenWidth;
depthBufferDesc.Height = _iScreenHeight;
depthBufferDesc.MipLevels = 1;
depthBufferDesc.ArraySize = 1;
depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthBufferDesc.SampleDesc.Count = 1;
depthBufferDesc.SampleDesc.Quality = 0;
depthBufferDesc.Usage = D3D10_USAGE_DEFAULT;
depthBufferDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL;
depthBufferDesc.CPUAccessFlags = 0;
depthBufferDesc.MiscFlags = 0;

// Create the texture for the depth buffer using the filled out description.
if(FAILED(m_pDevice->CreateTexture2D(&depthBufferDesc, NULL, &m_pDepthStencilBuffer)))
{
    return false;
}

// Initialize the description of the stencil state.
ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));

// Set up the description of the stencil state.
depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D10_COMPARISON_LESS;

depthStencilDesc.StencilEnable = true;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;

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

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

// Create the depth stencil state.
if(FAILED(m_pDevice->CreateDepthStencilState(&depthStencilDesc, &m_pDepthStencilState)))
{
    return false;
}

// Set the depth stencil state on the D3D device.
m_pDevice->OMSetDepthStencilState(m_pDepthStencilState, 1);

// Initailze the depth stencil view.
ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));

// Set up the depth stencil view description.
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDesc.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;

// Create the depth stencil view.
if(FAILED(m_pDevice->CreateDepthStencilView(m_pDepthStencilBuffer, &depthStencilViewDesc, &m_pDepthStencilView)))
{
    return false;
}

// Bind the render target view and depth stencil buffer to the output render pipeline.
m_pDevice->OMSetRenderTargets(1, &m_pRenderTargetView, m_pDepthStencilView);

// Setup the raster description which will determine how and what polygons will be drawn.
rasterDesc.AntialiasedLineEnable = true;
rasterDesc.CullMode = D3D10_CULL_BACK;
rasterDesc.DepthBias = 0;
rasterDesc.DepthBiasClamp = 0.0f;
rasterDesc.DepthClipEnable = true;
rasterDesc.FillMode = D3D10_FILL_SOLID;
rasterDesc.FrontCounterClockwise = false;
rasterDesc.MultisampleEnable = false;
rasterDesc.ScissorEnable = false;
rasterDesc.SlopeScaledDepthBias = 0.0f;

// Uncomment to turn off back-face culling
//rasterDesc.CullMode = D3D10_CULL_NONE;
//rasterDesc.FillMode = D3D10_FILL_WIREFRAME;

// Create the rasterizer state from the description we just filled out.
if(FAILED(m_pDevice->CreateRasterizerState(&rasterDesc, &m_pRasterState)))
{
    return false;
}

And here is what it looks like in my scene: http://imgur.com/UsaURcR 这就是我的场景: http : //imgur.com/UsaURcR

I used the resource ( http://msdn.microsoft.com/en-us/library/windows/desktop/bb205074(v=vs.85).aspx ) to step by step me through but am now quite confused as to why it's not working. 我使用该资源( http://msdn.microsoft.com/en-us/library/windows/desktop/bb205074(v=vs.85).aspx )逐步介绍了我的知识,但现在对为什么这样做很困惑它不起作用。

Any help would be VERY much appreciated, thanks! 非常感谢任何帮助,谢谢!

1> Your depthBufferDesc and depthStencilViewDesc format should be like: 1>您的depthBufferDesc和depthStencilViewDesc格式应类似于:

DXGI_FORMAT_D32_FLOAT

2> And depending on your model, you can try setup rasterDesc.FrontCounterClockwise to true. 2>并根据您的模型,可以尝试将rasterDesc.FrontCounterClockwise设置为true。

rasterDesc.FrontCounterClockwise = true;

3> Please check that you viewport has set: 3>请检查您的视口是否已设置:

ViewPort.MinDepth = 0.0f;
ViewPort.MaxDepth = 1.0f;

4> And you have to check you near clipping plane in your camera. 4>并且您必须在相机的裁剪平面附近检查您。 The value have to be greater then zero. 该值必须大于零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM