Sign in to follow this  

DX9 z-buffer issue maybe

This topic is 3592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);

Share this post


Link to post
Share on other sites
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)?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?



Share this post


Link to post
Share on other sites
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¡£

Share this post


Link to post
Share on other sites

This topic is 3592 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this