据我所知,我已经正确设置了深度测试,但是对象只是从头到尾绘制的。我查看了图形调试器,似乎在绘制网格时,无论其距离如何,它都会将深度缓冲区设置为 0。
D3D11_TEXTURE2D_DESC depthBufferDesc;
d3d11FrameBuffer->GetDesc(&depthBufferDesc);
d3d11FrameBuffer->Release();
depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthBufferDesc.Width = 1920;
depthBufferDesc.Height = 1080;
depthBufferDesc.MipLevels = 1;
depthBufferDesc.ArraySize = 1;
depthBufferDesc.SampleDesc.Count = 1;
depthBufferDesc.SampleDesc.Quality = 0;
depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
depthBufferDesc.CPUAccessFlags = 0;
depthBufferDesc.MiscFlags = 0;
ID3D11Texture2D* depthBuffer;
HRESULT res = d3d11Device->CreateTexture2D(&depthBufferDesc, nullptr, &depthBuffer);
D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;
res = d3d11Device->CreateDepthStencilView(depthBuffer, nullptr, depthBufferView);
ID3D11DepthStencilState* depthStencilState;
{
D3D11_DEPTH_STENCIL_DESC depthStencilDesc = {};
depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthStencilDesc.StencilEnable = false;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
d3d11Device->CreateDepthStencilState(&depthStencilDesc, &depthStencilState);
}
d3d11DeviceContext->RSSetState(rasterizerState);
d3d11DeviceContext->OMSetDepthStencilState(depthStencilState, 1);
d3d11DeviceContext->OMSetRenderTargets(1, &d3d11FrameBufferView, depthBufferView);
我尝试更改各种参数并移动某些代码的位置,但这没有用。我找不到任何其他似乎适用于此的问题。 我在光栅器上禁用了深度剪辑,因为当我启用它时,不会渲染任何内容,但据我所知,它不应该对此产生任何影响。
你为什么有:
depthStencilDesc.StencilEnable = false;
必须将其设置为
true
。