出于某种奇怪的原因,我的深度缓冲区不起作用,即后来绘制的三角形总是重叠,无论它们的位置如何。
我有这些演示者参数
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);
}
wherevertexBuffer
和indexBuffer
with 是类的属性。
以下是一些屏幕截图(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
但是当我前面有截断的金字塔时,立方体仍然重叠
当你自己移动相机时更容易看到......