   Solving Z buffer issues
  I'm working on an isometric tile renderer as a little pet project, but I'm having some issues with overdraw. I'm using D3D 9, so I was hoping that it could handle z-buffering correctly. Does this work when performing orthographic projection, or does the ortho matrix flatten everything before depth tests can be performed (I haven't actually done the math on this, sorry)?Of course, it could just be that my index buffer is all screwy and triangles are not ordered correctly. Here's my drawing and cube creation code:void ISO::Graphics::Renderer::Render(){    if( NULL == m_D3DDevice )   return;    // Clear the backbuffer to a blue color    m_D3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );    int CurrentVertBuffer = -1;// Begin the scene    if( SUCCEEDED( m_D3DDevice->BeginScene() ) )    {m_D3DDevice->SetTransform(D3DTS_PROJECTION, &m_MatProj);   // Rendering of scene objects can happen herefor(unsigned i = 0; i < m_Tiles.size(); ++i){if (m_Tiles.first != CurrentVertBuffer){CurrentVertBuffer = m_Tiles.first;m_D3DDevice->SetStreamSource(0, m_VertBuffers[CurrentVertBuffer]->GetVertexBufferPointer(), 0, sizeof(Vertex));}m_D3DDevice->SetIndices(m_Tiles.second.GetIndexBuffer()->GetIndexBufferPointer());HRESULT hr = m_D3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,m_Tiles.second.GetVertexBufferIndex(),m_Tiles.second.GetVertexBufferIndex(),8,0,12 );if (FAILED(hr)){assert(0);}}  // End the scene   m_D3DDevice->EndScene();    }    // Present the backbuffer contents to the display    m_D3DDevice->Present( NULL, NULL, NULL, NULL );}int ISO::Graphics::Renderer::AddTile(float bottomLeftX, float bottomLeftY, float bottomLeftZ, float topRightX, float topRightY, float topRightZ){Vertex verts[] = {{bottomLeftX, bottomLeftY, bottomLeftZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(0, 255, 0), 0.f, 0.f},{bottomLeftX, bottomLeftY, topRightZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(0, 255, 0), 0.f, 0.f},{bottomLeftX, topRightY, topRightZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(0, 255, 0), 0.f, 0.f},{bottomLeftX, topRightY, bottomLeftZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(0, 0, 0), 0.f, 0.f},{topRightX, topRightY, bottomLeftZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(0, 255, 0), 0.f, 0.f},{topRightX, bottomLeftY, bottomLeftZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(0, 255, 0), 0.f, 0.f},{topRightX, bottomLeftY, topRightZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(0, 255, 0), 0.f, 0.f},{topRightX, topRightY, topRightZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(0, 255, 0), 0.f, 0.f}};if (m_VertBuffers.empty() ||m_VertBuffers.back()->GetCurrentIndex() > 796){m_VertBuffers.push_back(new VertexBuffer(m_D3DDevice, 800));}assert(!m_VertBuffers.empty());assert(m_VertBuffers.back()->GetCurrentIndex() <= 796);short i = static_cast<short>(m_VertBuffers.back()->AddVertices(verts, 8));short indices[] = { i, i+1, i+2,i, i+2, i+3,i, i+3, i+4,i, i+4, i+5,i, i+5, i+6,i, i+6, i+1,i+7, i+2, i+1,i+7, i+1, i+6,i+7, i+4, i+3,i+7, i+3, i+2,i+7, i+6, i+5,i+7, i+5, i+4 };IndexBuffer *indexBuff = new IndexBuffer(m_D3DDevice, 36);indexBuff->AddIndices(indices, 36);m_Tiles.push_back(std::pair<int, Tile>((int)m_VertBuffers.size() - 1, Tile(i, indexBuff)));return -1;}Any tips would be greatly appreciated.Thanks![Edited by - Driv3MeFar on September 8, 2007 5:56:09 PM]
Quote:Original post by Driv3MeFarI'm using D3D 9, so I was hoping that it could handle z-buffering correctly. Does this work when performing orthographic projection, or does the ortho matrix flatten everything before depth tests can be performed (I haven't actually done the math on this, sorry)?Whether the ortho matrix "flattens the depth" depends on what your matrix is like. There are no problems with orthogaphic projection + z-buffering by itself. Your problems are either in how you construct m_matProj, how your shader code transforms the vertices, or what kind of render states you have applied (The relevant ones are depth test enable, depth function and depth write enable, also backface culling). Enable depth testing and writing, pick the depth function that's suitable for you, and construct a projection matrix + shader code that transforms the vertices so that the proper depth value is used for depth testing.I know, perhaps not the answer you're looking for, but you really should work through the math to see what values are supposed to be passed in each stage. Your sample screenshot looks simple enough, why not debug it with PIX to see what your vertex shader really outputs?;Quote:Original post by clbI know, perhaps not the answer you're looking for, but you really should work through the math to see what values are supposed to be passed in each stage. Your sample screenshot looks simple enough, why not debug it with PIX to see what your vertex shader really outputs?I'm using the fixed function pipeline for this project. I have taken a look at it in Pix, but couldn't find anything too useful, probably because I don't know how to use it well enough yet. Just that the correct pixel color is being drawn, but then overridden by a later primitive in my drawprimitive call.Thanks for the tips, though.   ;  Problem is solved. Z buffering doesn't work when it isn't turned on, who'd have thought that?Specifically, I was missing these two lines when creating my device:    params.EnableAutoDepthStencil = TRUE;    params.AutoDepthStencilFormat = D3DFMT_D16;