gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:51:04 26 4
gpt4 key购买 nike

所以我试图在 DirectX10 中创建一个场景,但出于某种原因,我的 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;
}

这是它在我的场景中的样子: http://imgur.com/UsaURcR

我使用资源 ( http://msdn.microsoft.com/en-us/library/windows/desktop/bb205074(v=vs.85).aspx) 逐步完成,但现在很困惑为什么它不起作用。

非常感谢任何帮助,谢谢!

最佳答案

1> 你的 depthBufferDesc 和 depthStencilViewDesc 格式应该是这样的:

DXGI_FORMAT_D32_FLOAT

2> 根据您的模型,您可以尝试将 rasterDesc.FrontCounterClockwise 设置为 true。

rasterDesc.FrontCounterClockwise = true;

3> 请检查您的视口(viewport)是否已设置:

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

4> 而且你必须在你的相机中检查你靠近裁剪平面。该值必须大于零。

关于c++ - DirectX10 上的深度缓冲区不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23379353/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com