Directx 11深度测试不起作用 [英] Directx 11 depth test not working

查看:340
本文介绍了Directx 11深度测试不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让我的程序正确选择要放置在前面的模型。我完全遵循了MSDN代码。我的代码似乎可以在DrawIndexed的特定调用中正确绘制所有多边形,但是随后的每个调用似乎都导致模型以绘制顺序绘制,而不是基于它们是否更靠近屏幕。

I cannot get my program to correctly choose which models to place in front. I have followed the MSDN code exactly. My code appears to correctly draw all polygons in a particular call of DrawIndexed, but each subsequent call seems to cause models to be drawn in the order they are drawn, not based on whether they are closer to the screen.

这是我初始化Direct3d的代码:

Here is my code for initializing Direct3d:

DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory( &sd, sizeof( sd ) );
sd.BufferCount = 1;
sd.BufferDesc.Width = width;
sd.BufferDesc.Height = height;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hWnd;
sd.SampleDesc.Count = 4;
sd.SampleDesc.Quality = 0;
sd.Windowed = !fullScreen;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
D3D_FEATURE_LEVEL  FeatureLevelsRequested = D3D_FEATURE_LEVEL_11_0;
UINT               numFeatureLevelsRequested = 1;
D3D_FEATURE_LEVEL  FeatureLevelsSupported;
HRESULT hr;
if( FAILED (hr = D3D11CreateDeviceAndSwapChain( adapters[0], 
    D3D_DRIVER_TYPE_UNKNOWN, 
    NULL, 
    NULL,
    NULL, 
    NULL, 
    D3D11_SDK_VERSION, 
    &sd, 
    &swapchain, 
    &dev, 
    &FeatureLevelsSupported,
    &devcon )))
{
    //return;
}
ID3D11Texture2D *pBack = NULL;
swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBack);

// use the back buffer address to create the render target
dev->CreateRenderTargetView(pBack, NULL, &backbuffer);
pBack->Release();

// set the render target as the back buffer
// Create depth stencil texture
D3D11_TEXTURE2D_DESC descDepth;
ZeroMemory(&descDepth, sizeof(descDepth));
descDepth.Width = width;
descDepth.Height = height;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format =DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = 4;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;

hr = dev->CreateTexture2D( &descDepth, NULL, &g_pDepthStencil);
if(FAILED(hr))
    exit(hr);

// Create the depth stencil view
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory(&descDSV, sizeof(descDSV));

descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DMS;
descDSV.Texture2D.MipSlice = 0;;  
//descDSV.Texture2DMS.UnusedField_NothingToDefine = 0;  

hr = dev->CreateDepthStencilView( g_pDepthStencil, &descDSV, &g_pDepthStencilView);
if(FAILED(hr))
    exit(hr);
devcon->OMSetRenderTargets(1, &backbuffer, g_pDepthStencilView);
D3D11_VIEWPORT viewport;
ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));

viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
viewport.Width = width;
viewport.Height = height;

devcon->RSSetViewports(1, &viewport);

这是我的渲染代码:

void Direct3DRenderer::Render()
{

    devcon->ClearRenderTargetView(backbuffer, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
    devcon->ClearDepthStencilView(g_pDepthStencilView,  D3D11_CLEAR_DEPTH|D3D11_CLEAR_STENCIL, 1.0f, 0 );
    camera.location = simulation->GetWorld()->GetCameraCoordinates();
    camera.direction = simulation->GetWorld()->GetCameraLookAt();
    //camera.up = simulation->GetWorld()->GetCameraOrientation();
    Vec3d lookAt = camera.location + camera.direction;
    XMVECTOR eye = XMVectorSet((float)camera.location[0], (float)camera.location[1], (float)camera.location[2], 0.f);
    XMVECTOR look = XMVectorSet(lookAt[0], lookAt[1], lookAt[2], 0);
    XMVECTOR up = XMVectorSet(camera.up[0], camera.up[1], camera.up[2], 0);
    g_View = XMMatrixLookAtLH(eye, look, up);

    ConstantBuffer oncePerFrame;
    oncePerFrame.matrix = XMMatrixTranspose(g_View);
    devcon->UpdateSubresource(oncePerFrameBuffer, 0, NULL, &oncePerFrame, 0, 0);

    UINT stride = sizeof(VERTEX);
    UINT offset = 0;

    const std::vector<Graphical*> graphicalList = simulation->GetWorld()->GetGraphicalList();

    for(int ind = 0; ind < graphicalList.size(); ++ind)
    {
        switch(graphicalList[ind]->GetModelType())
        {
            case 1:  //Sphere
                {
                    ConstantBuffer oncePerModel2;
                    oncePerModel2.matrix = XMMatrixTranspose(XMMatrixScalingFromVector(graphicalList[ind]->GetScaleX()) * XMMatrixTranslationFromVector(graphicalList[ind]->GetTranslationX()));
                    devcon->UpdateSubresource(oncePerModelBuffer, 0, NULL, &oncePerModel2, 0, 0);
                    devcon->IASetVertexBuffers(0, 1, &(sphereModel.vertexBuffer), &stride, &offset);
                    devcon->IASetIndexBuffer(sphereModel.indexBuffer, DXGI_FORMAT_R32_UINT, 0);
                    devcon->DrawIndexed(sphereModel.indexCount, 0, 0);
                }
                break;
        }
    }
    ConstantBuffer oncePerModel;
    oncePerModel.matrix = XMMatrixTranspose(g_World);
    devcon->UpdateSubresource(oncePerModelBuffer, 0, NULL, &oncePerModel, 0, 0);

    devcon->IASetVertexBuffers(0, 1, &terrainModel.vertexBuffer, &stride, &offset);
    devcon->IASetIndexBuffer(terrainModel.indexBuffer, DXGI_FORMAT_R32_UINT, 0);
    devcon->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    devcon->DrawIndexed(terrainModel.indexCount, 0, 0);

    swapchain->Present(0, 0);
}

我尝试了广泛的搜索,并遵循了我能找到的所有教程。没什么能解决的。

I have tried searching extensively, and have followed every tutorial I could find. Nothing fixes it.

对于球体,如果从一侧而不是另一侧观察,深度似乎是正确的。

In the case of the spheres, depth appears to be correct if viewing from one side, but not the other.

任何帮助将不胜感激。谢谢。

Any help would be appreciated. Thanks.

推荐答案

来自OP评论:


问题最终变得更加简单。我没有为视口对象设置最小深度和
最大深度。

The problem ended up being much simpler. I did not set the minimum and max depth for the viewport object. It worked once I did so.

我遇到了同样的问题,现在可以正常工作:

I had the same problem, it works now:

D3D11_VIEWPORT screenViewport;
/* ... */
screenViewport.MinDepth = 0;
screenViewport.MaxDepth = 1;
/* ... */
context->RSSetViewports(1, &screenViewport);

这篇关于Directx 11深度测试不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆