   Occlusion culling problem
  Hello everyone!I'm having problems implementing "Efficient Occlusion Culling Algorithm" (Sekulic, GPU Gems).Here is the code I'm using:// Create query for each objectOcclusionQuery* TempQuery;for( ULONG i = 0; i < ObjectCount; ++i ){TempQuery = new OcclusionQuery;TempQuery->Visible = true;D3D9Device->CreateQuery( D3DQUERYTYPE_OCCLUSION, &TempQuery->Query );OcclusionQueries.push_back( TempQuery );}// RENDER FUNCTION//-----------------// Frustum cull octree leaves (flag all objects in visible leaves as visible)FrustumCullOctree();// Loop through all objectsDWORD Pixels;for( ULONG i = 0; i < OcclusionQueries.size(); i++ ){Pixels = 0;  // Skip if this object is listed as not visibleif( !Object->Visible )continue;// Flag this object as not visible for the next call to  FrustumCullOctree   Object->Visible = false;CMesh* Mesh = Object->Mesh;   // Frustum cull object AABB (maybe leaf is visible but object is not)Mesh->GetBoundingBox( boundsMin, boundsMax );if( Camera->AABBInFrustum( boundsMin, boundsMax ) == BaseCamera::OUTSIDE ){Object->Visible = false;continue;}   // Check query results from last frameif( S_FALSE == OcclusionQueries->Query->GetData( (void*)&Pixels, sizeof(DWORD), D3DGETDATA_FLUSH ) ){OcclusionQueries->Visible = true;}else{if( Pixels > 0 )OcclusionQueries->Visible = true;elseOcclusionQueries->Visible = false;}   // Issue new query   OcclusionQueries->Query->Issue( D3DISSUE_BEGIN );   // If object was visible in last frame, render itif( OcclusionQueries->Visible ){Mesh->Render();}   // If object was not visible, render its AABBelse{ // Disable writing to color buffer and depth buffer D3D9Device->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );D3D9Device->SetRenderState( D3DRS_ZENABLE, TRUE );D3D9Device->SetRenderState( D3DRS_LIGHTING, FALSE );D3D9Device->SetRenderState( D3DRS_COLORWRITEENABLE, FALSE );D3D9Device->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ZERO );RenderAABB( boundsMin, boundsMax ); // Restore old render states // ...}   // End queryOcclusionQueries->Query->Issue( D3DISSUE_END );}This is simplified version of code, without sorting visible objects based on their distance to camera before occlusion testing. However, this doesnt work right. When I place camera behind some object in the scene, so that camera is viewing only that object, everything else is culled as it should be. But when I move away, so that other objects in the scene are no more occluded, they dont get rendered. Sometimes they popup later when I move camera around randomly.Frustum culling works fine and objects AABB calculation and rendering works fine. If anyone can see what Im doing wrong, please tell me.Thanks!   
  I solved the problem. Before I used shader for rendering my objects and FF for rendering objects AABBs. When I set the same shader to render AABBs also, it worked, but Im not sure why...Its like my shader and FF pipeline used different depth and color buffers so occlusion query results were incorrect. I dont know how this can be...   