# z-buffering(I think)

## Recommended Posts

cutovoi    122
Hi guys, thanks for the support in my questions.... I have a new one. I have 3 cubes one in the middle of space, one for the left, one for the right. The middle is rotating in their Y axis and the other around of the middle cube. I've noticed that when the left side and the back side of the most front cube are shown the cubes that are behind shows the left side and the back side too. It seems that the cubes are transparent. I used the SetRenderState like this:
g_DirectDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
g_DirectDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);


When "g_DirectDevice" is my LPDIRECT3DDEVICE8. I don't have ANY idea what's going on... Some one can shows a little bit of light or indicates a tutorial? Thanks a lot for help. P.S: If someone needs that I post more lines of code I put....

##### Share on other sites
Demirug    884

I have the felling that you don’t create a Z-Buffer at all.

##### Share on other sites
JoeyBlow2    100
There could be 3 issues. I'd check for all 3 and make sure you are doing it correctly.

1) With Cullmode CCW, it means thats any indexes (or vertex) which are aligned counter clockwise will be culled.

For example.

Lets say you have a square (side of a cube) made up of 2 triangles.

1-----2|    /||   / ||  /  || /   ||/    |3-----4

If you set up the index as 1, 3, 2 that sets up the triangle to be drawn CCW, and therefore, that triangle will be culled with a CCW Cullmode. You need to render it in the order 1,2,3, and the other triangle as 2,4,3. The starting number of the points do not matter, as long as they are clockwise, for the first triangle you could draw it 2,3,1 or even 3,1,2... Doesn't matter. As long as the points go clockwise.

2) Check the ZBuffer write renderstate. For ZBuffer, there are 2 different states which control the reading and writing of a zbuffer. Having ZBuffer on will check the zbuffer to see which objects are in front. However, if you have zbuffer write off, nothing will write to the zbuffer. So the location of the objects never get written to the zbuffer. You should set both of them on when drawing cubes. (There are instances where you want to read the zbuffer but not write to it, and vise versa)

3) The camera projection matrix needs to include zbuffer range. (or scene min/max) If you have 0,0 for those ranges, its basically saying read/write to the zbuffer with those ranges and as you can tell, everything will be 0 and it will act like there is no zbuffer. Also, you can't use a 0 for a znear, or unlimited number for the zfar or you will get problems. Try setting numbers like 1 and 1000. Which means everything between those 2 distances will read/write to the zbuffer. The 3d view is like a pyramid going into the scene with the top of the pyramid cut off (to create a square which gets mapped to the square monitor)...

Look over that, one or more of those are usually the cause of the problem you are seeing.

##### Share on other sites
cutovoi    122
the problem is the left side and the back side of the cube that is shown is the most first drawed rectangle when the app starts.
When the left and center and right rectangle are aligned in the z-axis the back side that is shown is the most behind rectangle, and after comes the middle rectangle and finally the first rectangle.
I'm set this in my app now:
g_pDirect3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);g_pDirect3DDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_GREATER);

But the probles persists. Below is how I can put the vertices:

bool Cube::CreateVerticesBuffer(){   void * theVertices;   VertexNonFVF CubeVertices[] =    {       //Top      {-m_fXVertex, m_fYVertex, -m_fZVertex, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f},  //branco      {-m_fXVertex, m_fYVertex,  m_fZVertex, D3DCOLOR_XRGB(255,255,255), 1.0f, 1.0f},      { m_fXVertex, m_fYVertex, -m_fZVertex, D3DCOLOR_XRGB(255,255,255), 0.0f, 0.0f},      { m_fXVertex, m_fYVertex,  m_fZVertex, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f},      //Back      {-m_fXVertex, -m_fYVertex, -m_fZVertex, D3DCOLOR_XRGB(255,0,0), 0.0f, 1.0f},     //vermelho      {-m_fXVertex,  m_fYVertex, -m_fZVertex, D3DCOLOR_XRGB(255,0,0), 0.0f, 0.0f},      { m_fXVertex, -m_fYVertex, -m_fZVertex, D3DCOLOR_XRGB(255,0,0), 1.0f, 1.0f},      { m_fXVertex,  m_fYVertex, -m_fZVertex, D3DCOLOR_XRGB(255,0,0), 1.0f, 0.0f},      //Right      {m_fXVertex, -m_fYVertex, -m_fZVertex, D3DCOLOR_XRGB(255,255,0), 0.0f, 1.0f},    //amarelo      {m_fXVertex,  m_fYVertex, -m_fZVertex, D3DCOLOR_XRGB(255,255,0), 0.0f, 0.0f},      {m_fXVertex, -m_fYVertex,  m_fZVertex, D3DCOLOR_XRGB(255,255,0), 1.0f, 1.0f},      {m_fXVertex,  m_fYVertex,  m_fZVertex, D3DCOLOR_XRGB(255,255,0), 1.0, 0.0f},      //Front      { m_fXVertex, -m_fYVertex, m_fZVertex, D3DCOLOR_XRGB(255,0,255), 0.0f, 1.0f},  //rosa      { m_fXVertex,  m_fYVertex, m_fZVertex, D3DCOLOR_XRGB(255,0,255), 0.0f, 0.0f},      {-m_fXVertex, -m_fYVertex, m_fZVertex, D3DCOLOR_XRGB(255,0,255), 1.0f, 1.0f},      {-m_fXVertex,  m_fYVertex, m_fZVertex, D3DCOLOR_XRGB(255,0,255), 1.0f, 0.0f},      //Left      {-m_fXVertex, -m_fYVertex,  m_fZVertex, D3DCOLOR_XRGB(0,255,0), 0.0f, 1.0f},     //verde      {-m_fXVertex,  m_fYVertex,  m_fZVertex, D3DCOLOR_XRGB(0,255,0), 0.0f, 0.0f},      {-m_fXVertex, -m_fYVertex, -m_fZVertex, D3DCOLOR_XRGB(0,255,0), 1.0f, 1.0f},      {-m_fXVertex,  m_fYVertex, -m_fZVertex, D3DCOLOR_XRGB(0,255,0), 1.0f, 0.0f},      //Bottom      { m_fXVertex, -m_fYVertex, -m_fZVertex, D3DCOLOR_XRGB(0,255,255), 1.0f, 1.0f},  //azul claro      { m_fXVertex, -m_fYVertex,  m_fZVertex, D3DCOLOR_XRGB(0,255,255), 1.0f, 0.0f},      {-m_fXVertex, -m_fYVertex, -m_fZVertex, D3DCOLOR_XRGB(0,255,255), 0.0f, 1.0f},      {-m_fXVertex, -m_fYVertex,  m_fZVertex, D3DCOLOR_XRGB(0,255,255), 0.0f, 0.0f},   };

JoeyBlow2, when you say that I need to include the zbuffer range in the camera projection, you are saying that I need to define the z-value of the near view-plane and the z-value of the z-far view-plane??? If you are saying this, I set these values to my D3DXMatrixPerspectiveFovLH:

D3DXMatrixPerspectiveFovLH(&PerspectiveMatrix, D3DX_PI/4, 1.0f, 1.0f, 500.0f)

Fellows thanks for the support

##### Share on other sites
What demirug was suggesting was that your presentation parameters on device creation may not be requesting a depth buffer, for example:

d3dpp.AutoDepthStencilEnable = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8;

Also, ensure you clear the depth buffer to 1.0f each frame, like so:
g_pDev->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER , 0x123456, 1.0f, 0);

##### Share on other sites
cutovoi    122
And Demirug is right. And you too. I added in my code that you say:
d3dpp.AutoDepthStencilEnable = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8;
And I've included the D3DCLEAR_ZBUFFER in my g_pDirect3DDevice->Clear function.

Thanks for you, Demirug and the JoeyBlow2.