   Questions about Occlusion Query
  I just tested D3D's Occlusion Query. My code is showed as below: if( SUCCEEDED( pd3dDevice->BeginScene() ) )    {//Occlusion QuerygQuery->Issue(D3DISSUE_BEGIN);pd3dDevice->SetTransform(D3DTS_WORLD, &gMatWorld2);gMesh2->DrawSubset(0);gQuery->Issue(D3DISSUE_END);while(S_FALSE == gQuery->GetData(&numPix, sizeof(DWORD), D3DGETDATA_FLUSH));DXUTOutputDebugString(L"%d\n", numPix);pd3dDevice->SetMaterial(&mtrl1);pd3dDevice->SetTransform(D3DTS_WORLD, &gMatWorld1);gMesh1->DrawSubset(0);pd3dDevice->SetMaterial(&mtrl2);pd3dDevice->SetTransform(D3DTS_WORLD, &gMatWorld2);gMesh2->DrawSubset(0);   V( pd3dDevice->EndScene() );    }And when teapot2 is half behind teapot1The numPix is 6606But when teapot2 is totally behind teapot1, why the numPix is not 0, it's 1433?Does D3D add a boundingbox for you to query?Thanks in advance.   
  Try drawing mesh1 before doing the occlusion query for mesh2. That way it'll have written itself into the zbuffer to occlude it.I have a feeling you don't need to render mesh2 twice either - the draw done for the query will also draw it on screen, but I'm not certain.   ;  I'm assuming that 'teapot2' is gMesh2 in the code. I'm also assuming that you are clearing the depth buffer before you draw this every frame. The first thing you draw is 'teapot2' with the occlusion query issued around it. Meaning that nothing else is in the depth buffer, so of course fragments (pixels) are going to pass! If you were to draw 'teapot1' (gMesh1 I'm assuming) first, then no fragments would pass for 'teapot2' if it is behind it.An occlusion query counts how many fragments pass the depth and stencil test. What you need to do is sort objects front to back based on their distance from the camera, then draw them in that order and retrieve the results you want as necessary.   ;Quote:Original post by Adam_42Try drawing mesh1 before doing the occlusion query for mesh2. That way it'll have written itself into the zbuffer to occlude it.I have a feeling you don't need to render mesh2 twice either - the draw done for the query will also draw it on screen, but I'm not certain.It works. And I don't need to render mesh2 twice.Thanks very much.   ;Quote:Original post by Halifax2I'm assuming that 'teapot2' is gMesh2 in the code. I'm also assuming that you are clearing the depth buffer before you draw this every frame. The first thing you draw is 'teapot2' with the occlusion query issued around it. Meaning that nothing else is in the depth buffer, so of course fragments (pixels) are going to pass! If you were to draw 'teapot1' (gMesh1 I'm assuming) first, then no fragments would pass for 'teapot2' if it is behind it.An occlusion query counts how many fragments pass the depth and stencil test. What you need to do is sort objects front to back based on their distance from the camera, then draw them in that order and retrieve the results you want as necessary.Thanks for your detailed explanation. It works now.   