DirectX深度缓冲无法正常工作

时间:2010-08-09 00:02:35

标签: c++ directx depth direct3d9 zbuffer

由于一些奇怪的原因,我的深度缓冲区不起作用,即后面绘制的三角形总是重叠,无论它们的位置如何。

我有这些演示者参数

D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = mWindow;

d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = mScreenWidth;
d3dpp.BackBufferHeight = mScreenHeight;

d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

和这些呈现状态:

d3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);    // turn off the 3D lighting
d3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);    // turn on the z-buffer
d3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
d3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));    // ambient light

编辑: 感谢回复。这是渲染代码:

d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3dDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

d3dDevice->BeginScene();
// View transform
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView,
                   &PlayerPos,    // the camera position
                   &(LookAtRelative + PlayerPos),    // the look-at position
                   &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
d3dDevice->SetTransform(D3DTS_VIEW, &matView);

// Projection transform
D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&matProjection,
                           D3DXToRadian(45),    // the horizontal field of view
                           (FLOAT)mScreenWidth / (FLOAT)mScreenHeight, // aspect ratio
                           0.0f,    // the near view-plane
                           1000.0f);    // the far view-plane
d3dDevice->SetTransform(D3DTS_PROJECTION, &matProjection);



for (unsigned int i=0; i < mModels.size(); i++) {
    mModels[i]->Draw();
}   

d3dDevice->EndScene();
d3dDevice->Present(NULL, NULL, NULL, NULL);

Model::Draw()代码是这样的:

void Model :: Draw () {
// Setup the world transform matrix
D3DXMATRIX matScale;
D3DXMATRIX matRotate;
D3DXMATRIX matTranslate;
D3DXMATRIX matWorldTransform;

D3DXMatrixScaling(&matScale, mScale->x, mScale->y, mScale->z);
D3DXMatrixRotationY(&matRotate, 0);
D3DXMatrixTranslation(&matTranslate, mPosition->x, mPosition->y, mPosition->z);

matWorldTransform = matScale * matRotate * matTranslate;
d3dDevice->SetTransform(D3DTS_WORLD, &matWorldTransform);

d3dDevice->SetFVF(CUSTOMFVF);
d3dDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(CUSTOMVERTEX));
d3dDevice->SetIndices(indexBuffer);

d3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, vertexCount, 0, indexCount/3); 
}

其中vertexBufferindexBuffer及其计数属于该类的属性。

以下是一些截图(FU,垃圾邮件防护):

1)http://img822.imageshack.us/img822/1705/dx2010080913182262.jpg这就是情况

2)http://img691.imageshack.us/img691/7358/dx2010080913183790.jpg这是多维数据集在前面时的(正确)视图(稍后绘制多维数据集)

3)http://img340.imageshack.us/img340/4720/dx2010080913184509.jpg但是当我在前面有截断的金字塔时,立方体仍然重叠

当你自己移动相机时更容易看到......

1 个答案:

答案 0 :(得分:2)

现在这是一个问题。问题是我将近视平面设置为0.0f - 当我将其更改为0.001f时,z缓冲区突然开始工作。