DX9 z-buffer issue maybe

Started by
5 comments, last by Hamfiend 16 years, 2 months ago
Hi All, I have objects being drawn in my 3D engine. the objects are LPD3DXMESH loaded from X Files. My issue is that when I move the camera around, sometimes parts of object A show in front of object B. But object A is behind object B. Has anyone had issues like this? Thanks Hamfiend m_pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true); m_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true); m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, true ); m_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE,true); m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA); m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA); m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_TFACTOR); m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_TRUE ); m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW);
Advertisement
What are your near and far clip planes set to (They're parameters to D3DXMatrixPerspectiveFovLH if you use it)? Setting the near clip plane to a very small value (or worse, zero) is A Bad Thing, and setting the far clip plane to a very big number is also A Bad Thing.

And are you creating a Z-buffer (Is the EnableAutoDepthStencil member of your present parameters set to true)? And what format is it (the AutoDepthStencilFormat parameter of your present parameters)?
I had this problem yesterday. It's a two part problem.

First, where you create the device you need to adjust the d3dParams to enable stencil buffers:

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


Second, in your clear call you need to make sure that the second to last parameter is 1.0f:

g_pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_RGBA(50,100,200,255), 1.0f, 0);


Try that out and hopefully that will solve your problem.
-games4thefuture-
And just to round out everything that could go wrong... ;)

Ensure these render states haven't been disabled elsewhere.

D3DRS_ZWRITEENABLE, true
D3DRS_ZFUNC, D3DCMP_LESSEQUAL

And if you use SetViewport, ensure the min and max Z values make sense (usually 0.0f, and 1.0f)
Hi,

I do not create a viewport.

I create the device using this:
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = FALSE;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = m_DepthStencilFormat; // D3DFMT_D16
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;
d3dpp.BackBufferFormat = m_ColorFormat; // D3DFMT_A8R8G8B8
d3dpp.BackBufferWidth = m_iWidth; // 800
d3dpp.BackBufferHeight = m_iHeight; // 600
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, m_dwVertexProcessing, &d3dpp, &m_pd3dDevice );


My projection matrix is as follows:
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH( &matProj, (D3DX_PI/ 8), ((float)m_iWidth/(float)m_iHeight), 0.1f, 2000.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

I use this to clear my world:
m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), 1.0f, 0 );

Maybe I am using not so good values for the projection matrix.
I have it set to PI/8 because I need the objects to look like they are straight on. When I set it to PI/2, the objects appear like they are moving away from the camera as I move it closer to the object.

Do you think that could be the reason?



D3DXMatrixPerspectiveFovLH( &matProj, (D3DX_PI/ 8), ((float)m_iWidth/(float)m_iHeight), 0.1f, 2000.0f );

changed to:

D3DXMatrixPerspectiveFovLH( &matProj, (D3DX_PI/ 8), ((float)m_iWidth/(float)m_iHeight), 50.0f, 2000.0f );

Z-value of the near view-plane is too small¡£
Hi hiber,

I made the change you suggested and it seems to be working good now.

Thanks
Hamfiend

This topic is closed to new replies.

Advertisement