Index: trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingOctreeSceneManager.cpp
===================================================================
--- trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingOctreeSceneManager.cpp	(revision 51)
+++ trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingOctreeSceneManager.cpp	(revision 52)
@@ -16,6 +16,6 @@
 		//mDisplayNodes = true;
 		//mShowBoundingBoxes = true;
-		mShowBoxes = true;
-		mMaxDepth = 20;
+		//mShowBoxes = true;
+		//mMaxDepth = 20;
 	}
 	//-----------------------------------------------------------------------
@@ -43,12 +43,11 @@
 		mOcclusionCullingOctreeSceneTraverser->setSceneRoot(mOctree);
 		mOcclusionCullingOctreeSceneTraverser->renderScene(mCameraInProgress);
-		
+
 		_deleteRenderedQueueGroups();
 
-		//-- render overlay
+		//-- render rest, e.g., overlay
 		clearSpecialCaseRenderQueues();
-		SceneManager::_renderVisibleObjects( );
-	
-		clearSpecialCaseRenderQueues();
+		SceneManager::_renderVisibleObjects();
+		//_deleteRenderedQueueGroups();
 	}
 	//-----------------------------------------------------------------------
@@ -61,8 +60,5 @@
 	void OcclusionCullingOctreeSceneManager::_updateSceneGraph(Camera* cam)
 	{
-		//char *msg = "now updating scenegraph\n"; OutputDebugString(msg);
-		//mOcclusionCullingOctreeSceneTraverser->preprocess();
-
-     	mOcclusionCullingOctreeSceneTraverser->setNumSceneNodes((int)mSceneNodes.size());
+		mOcclusionCullingOctreeSceneTraverser->setNumSceneNodes((int)mSceneNodes.size());
 		mOcclusionCullingOctreeSceneTraverser->setRenderSystem(mDestRenderSystem);
         
Index: trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingSceneTraverser.cpp
===================================================================
--- trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingSceneTraverser.cpp	(revision 51)
+++ trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingSceneTraverser.cpp	(revision 52)
@@ -49,4 +49,5 @@
         //for(unsigned int i=0; i < 1000; i++)
 		//mOcclusionQueries.push_back(mRenderSystem->createHardwareOcclusionQuery());
+
 	    //mCurrentAlgorithm = RENDER_CULL_FRUSTUM;
 
@@ -66,5 +67,5 @@
 				break;
 		}	
-		
+
 		mFrameId ++;
 	}
@@ -91,5 +92,5 @@
 				if(visiblePixels > mVisibilityThreshold)
 				{
-					pullUpVisibility(cam, node);
+					pullUpVisibility(node);
 					traverseNode(cam, node);
 				}
@@ -119,5 +120,5 @@
 					// update node's visited flag
 					node->setLastVisited(mFrameId);
-					pullUpVisibility(cam, node);			
+					pullUpVisibility(node);			
 					traverseNode(cam, node);
 
@@ -129,5 +130,5 @@
 					
 				// identify nodes that we cannot skip queries for
-				bool leafOrWasInvisible = !wasVisible || isLeaf(node);
+				bool mustQuery= !wasVisible || (node->numAttachedObjects() > 0) || isLeaf(node);
 
 				// reset node's visibility classification 
@@ -137,6 +138,6 @@
 				node->setLastVisited(mFrameId);
 				
-				// skip testing previously visible interior nodes
-				if(leafOrWasInvisible)
+				// skip testing previously visible interior nodes without geometry
+				if(mustQuery)
 				{
 					HardwareOcclusionQuery *query = issueOcclusionQuery(&node->_getWorldAABB(), wasVisible);
@@ -235,5 +236,5 @@
 		
 		setRenderingMode(MODE_QUERY);
-		//setRenderingMode(MODE_RENDER);
+		//mRenderSystem->_setRasterisationMode(SDL_SOLID);setRenderingMode(MODE_RENDER);
 
 		// get next available test id
@@ -247,4 +248,10 @@
 		query->endOcclusionQuery();
 
+		
+		//mRenderSystem->_setRasterisationMode(SDL_WIREFRAME);
+		setRenderingMode(MODE_RENDER);
+		renderBoundingBox(box);
+		//mRenderSystem->_setRasterisationMode(SDL_SOLID);
+		
 		return query;
 	}
@@ -253,15 +260,24 @@
 	{	
 		// avoid unnecessary state changes
-		//if(mode != mQueryMode)
-		//{
+		if(mode != mQueryMode)
+		{
+			mQueryMode = mode;
 			bool enabled = (mode == MODE_RENDER);
-//if(mode == MODE_QUERY){
-			mRenderSystem->_setColourBufferWriteEnabled(enabled, 
-				enabled, enabled, enabled);
+			
+			//if(mode == MODE_RENDER) return;
+
+			/*
+			mRenderSystem->_setRasterisationMode(SDL_SOLID);
+			mRenderSystem->_setSurfaceParams(ColourValue(0.9, 0.2, 0.2),
+			ColourValue(0.2, 0.2, 0.2), ColourValue(0.2, 0.2, 0.2),
+			ColourValue(0, 0, 0), Real(100));
+			mRenderSystem->_setDepthBufferCheckEnabled(false);
+			mRenderSystem->_setAlphaRejectSettings(CMPF_ALWAYS_PASS, 255);
+			*/
+
+          	mRenderSystem->_setColourBufferWriteEnabled(enabled, enabled, enabled, enabled);
 			mRenderSystem->_setDepthBufferWriteEnabled(enabled);
 			mRenderSystem->setLightingEnabled(enabled);
-//}
-			mQueryMode = mode;
-		//}
+		}
 	}
 	//-----------------------------------------------------------------------
@@ -290,5 +306,11 @@
 		setRenderingMode(MODE_RENDER);
 		
-		mSceneManager->_renderSceneNode(cam, node);
+		if(node->lastRendered() != node->lastVisited())
+		{
+			node->setLastRendered(node->lastVisited());
+			mNumRenderedNodes ++;
+
+			mSceneManager->_renderSceneNode(cam, node);
+		}
 	}
 	//-----------------------------------------------------------------------
@@ -334,14 +356,8 @@
 	}
 	//-----------------------------------------------------------------------
-	void OcclusionCullingSceneTraverser::pullUpVisibility( Camera *cam, SceneNode *node )
+	void OcclusionCullingSceneTraverser::pullUpVisibility(SceneNode *node )
 	{
 		while(node && !node->isNodeVisible())
 		{
-			// if we come across some renderable geometry => render it
-			if(node->numAttachedObjects() > 0)
-			{
-				renderSceneNode(cam, node);
-			}
-
 			node->setNodeVisible(true);
 			node = node->getParentSceneNode();
@@ -367,5 +383,6 @@
 			halfbox->setupBoundingBox(*box);
 			
-			//mRenderSystem->_setWorldMatrix(Matrix4::IDENTITY);
+			mRenderSystem->_setWorldMatrix(Matrix4::IDENTITY);
+
 			// Set world transformation
 			/*halfbox->getWorldTransforms(xform);
@@ -380,5 +397,5 @@
 				mRenderSystem->_setWorldMatrix(*xform);
 			}
-			mRenderSystem->setClipPlanes(halfbox->getClipPlanes());
+			mRenderSystem->setClipPlanes(halfbox->getClipPlanes());*/
 
 			static RenderOperation ro;
@@ -387,8 +404,7 @@
 			halfbox->getRenderOperation(ro);
 			ro.srcRenderable = halfbox; 
-			mRenderSystem->_render(ro);*/
-
-			mSceneManager->myrenderSingleObject(getSolidHalfBoundingBox(half), 
-				getSolidHalfBoundingBox(half)->getTechnique()->getPass(0), false);
+			mRenderSystem->_render(ro);
+
+			//mSceneManager->myrenderSingleObject(getSolidHalfBoundingBox(half), getSolidHalfBoundingBox(half)->getTechnique()->getPass(0), true);
 		}
 	}
@@ -480,4 +496,9 @@
 		{
 			* static_cast < unsigned int * > ( val ) = mNumFrustumCulledNodes;
+			return true;
+		}
+		if ( key == "NumRenderedNodes" )
+		{
+			* static_cast < unsigned int * > ( val ) = mNumRenderedNodes;
 			return true;
 		}
@@ -493,5 +514,5 @@
 		refKeys.push_back( "NumQueryCulledNodes" );
 		refKeys.push_back( "NumFrustumCulledNodes" );
-		//refKeys.push_back( "mNumRenderedGeometry" );
+		refKeys.push_back( "mNumRenderedGeometry" );
 
 		return true;
Index: trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingOctreeSceneTraverser.cpp
===================================================================
--- trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingOctreeSceneTraverser.cpp	(revision 51)
+++ trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingOctreeSceneTraverser.cpp	(revision 52)
@@ -18,5 +18,5 @@
 	}
 	//-----------------------------------------------------------------------
-	void OcclusionCullingOctreeSceneTraverser::traverseOctant(Camera *cam, Octree *octant )
+	void OcclusionCullingOctreeSceneTraverser::traverseOctant(Camera *cam, Octree *octant)
 	{
 		mNumTraversedNodes ++;
@@ -34,5 +34,7 @@
 
 			if(nextChild)
+			{
 				mOctreeDistanceQueue->push(nextChild);
+			}
 		}
 	}
@@ -51,5 +53,4 @@
 			octant->_getCullBounds(&box);
 			
-			//if(static_cast<OctreeCamera *>(cam)->getVisibility(box) == OctreeCamera::NONE)
 			if(!cam->isVisible(box))
 			{
@@ -85,4 +86,11 @@
 			bool intersects = false;
 
+			/*char msg2[100];
+			Vector3 min = box.getMinimum();
+			Vector3 max = box.getMaximum();
+
+			sprintf(msg2, "culling box: %3.3f %3.3f %3.3f %3.3f %3.3f %3.3f\n", min.x, min.y, min.z, max.x, max.y, max.z);
+			OutputDebugString(msg2);*/
+
 			if(!cam->isVisible(box, intersects))
 			{
@@ -94,4 +102,6 @@
 			{
 				octant->setOctreeVisible(true);
+			//	char msg[100]; sprintf(msg, "intersecting\n");
+			//	OutputDebugString(msg);
 				traverseOctant(cam, octant);
 				continue;
@@ -103,5 +113,8 @@
 			// wait if result not available
 			query->pullOcclusionQuery(&visiblePixels);
-//visiblePixels = 1;
+
+			char msg[100]; sprintf(msg, "visible pixels: %d\n", visiblePixels, intersects);
+			OutputDebugString(msg);
+
 			// node visible
 			if(visiblePixels > mVisibilityThreshold)
@@ -111,11 +124,4 @@
 			else
 			{
-				char msg[100];
-				Vector3 min = box.getMinimum();
-				Vector3 max = box.getMaximum();
-
-				sprintf(msg, "culling box: %3.3f %3.3f %3.3f %3.3f %3.3f %3.3f\n", min.x, min.y, min.z, max.x, max.y, max.z);
-				OutputDebugString(msg);
-
 				mNumQueryCulledNodes ++;
 			}
@@ -143,8 +149,8 @@
 				unsigned int visiblePixels;
 				query->pullOcclusionQuery(&visiblePixels);
-//visiblePixels = 1;
+
 				if(visiblePixels > mVisibilityThreshold)
 				{
-					pullUpVisibility(cam, octant);
+					pullUpVisibility(octant);
 					traverseOctant(cam, octant);
 				}
@@ -176,5 +182,6 @@
 					// update octant's visited flag
 					octant->setLastVisited(mFrameId);
-					pullUpVisibility(cam, octant);			
+
+					pullUpVisibility(octant);			
 					traverseOctant(cam, octant);
 
@@ -185,11 +192,13 @@
 				bool wasVisible = octant->isOctreeVisible() && (octant->lastVisited() == mFrameId - 1);
 				
-				/* char msg[100];
+				/* 
+				char msg[100];
 				if(wasVisible) sprintf(msg, "yes, was visible, %d\n", mFrameId);
 				else sprintf(msg, "no was invisible, %d\n", mFrameId);
-				OutputDebugString(msg);*/
+				OutputDebugString(msg);
+				*/
 
 				// identify nodes that we cannot skip queries for
-				bool leafOrWasInvisible = !wasVisible || isLeaf(octant);
+				bool mustQuery = !wasVisible || (octant->numNodes() > 0) || isLeaf(octant);
 
 				// reset node's visibility classification 
@@ -200,5 +209,5 @@
 			
 				// skip testing previously visible interior nodes
-				if(leafOrWasInvisible)
+				if(mustQuery)
 				{
 					HardwareOcclusionQuery *query = issueOcclusionQuery(&box, wasVisible);
@@ -215,11 +224,8 @@
 	}
 	//-----------------------------------------------------------------------
-	void OcclusionCullingOctreeSceneTraverser::pullUpVisibility( Camera *cam, Octree *octant )
+	void OcclusionCullingOctreeSceneTraverser::pullUpVisibility( Octree *octant )
 	{
 		while(octant && !octant->isOctreeVisible())
 		{
-			// if we come across some renderable geometry => render it
-			renderOctant(cam, octant);
-			
 			octant->setOctreeVisible(true);
 			octant = octant->getParent();
@@ -229,23 +235,25 @@
 	void OcclusionCullingOctreeSceneTraverser::renderOctant( Camera *cam, Octree *octant )
 	{
-		if(octant->numNodes() == 0) return;
-		if(octant->lastVisited() != octant->lastRendered())
-		{
+		if(octant->lastRendered() != mFrameId)
+		{
+			octant->setLastRendered(mFrameId);
+
+			if(octant->numNodes() == 0) return;
+
 			//TODO: does nothing useful
 			setRenderingMode(MODE_RENDER);
+
+			((OctreeSceneManager *)mSceneManager)->_renderOctant(cam, octant);
+
 			mNumRenderedNodes ++;
-
-			octant->setLastRendered(octant->lastVisited());
-			((OctreeSceneManager *)mSceneManager)->_renderOctant(cam, octant);
-		}
-		//else OutputDebugString("already rendered");
-	}
-	//-----------------------------------------------------------------------
-	void OcclusionCullingOctreeSceneTraverser::setSceneRoot(Octree *root)
+		}
+	}
+	//-----------------------------------------------------------------------
+	void OcclusionCullingOctreeSceneTraverser::setSceneRoot( Octree *root )
 	{
 		mOctreeSceneRoot = root;
 	}
 	//-----------------------------------------------------------------------
-	void OcclusionCullingOctreeSceneTraverser::initDistanceQueue(Camera *cam)
+	void OcclusionCullingOctreeSceneTraverser::initDistanceQueue( Camera *cam )
 	{
 		if(mOctreeDistanceQueue)
@@ -274,5 +282,5 @@
 	bool OcclusionCullingOctreeSceneTraverser::getOption( const String & key, void *val )
 	{
-		if ( key == "NumOctreeNodes" )
+		if (key == "NumOctreeNodes")
 		{
 			* static_cast < unsigned int * > ( val ) = mNumOctreeNodes;
@@ -285,5 +293,5 @@
 	bool OcclusionCullingOctreeSceneTraverser::getOptionKeys( StringVector & refKeys )
 	{
-		refKeys.push_back( "NumOctreeNodes" );
+		refKeys.push_back("NumOctreeNodes");
 		
 		return OcclusionCullingSceneTraverser::getOptionKeys(refKeys);		
Index: trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingTerrainSceneManager.cpp
===================================================================
--- trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingTerrainSceneManager.cpp	(revision 51)
+++ trunk/VUT/OcclusionCullingSceneManager/src/OgreOcclusionCullingTerrainSceneManager.cpp	(revision 52)
@@ -16,8 +16,8 @@
 			new OcclusionCullingOctreeSceneTraverser(this, mDestRenderSystem);
 
+		mShowBoxes = true;
 		//mDisplayNodes = true;
 		//mShowBoundingBoxes = true;
-		mShowBoxes = true;
-		mMaxDepth = 20;
+		//mMaxDepth = 20;
 	}
 	//-----------------------------------------------------------------------
@@ -35,5 +35,5 @@
 	
 		setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
-		SceneManager::_renderVisibleObjects( );
+		SceneManager::_renderVisibleObjects();
 		_deleteRenderedQueueGroups();
 
@@ -51,6 +51,4 @@
 		clearSpecialCaseRenderQueues();
 		SceneManager::_renderVisibleObjects( );
-	
-		clearSpecialCaseRenderQueues();
 	}
 	//-----------------------------------------------------------------------
@@ -59,13 +57,9 @@
 		// must be empty because objects are found and rendered in an interleaved fashion
 		// in _renderVisibibleObjects 
-		//char *msg = "now finding visible objects\n"; OutputDebugString(msg);
 	}
 	//-----------------------------------------------------------------------
 	void OcclusionCullingTerrainSceneManager::_updateSceneGraph(Camera* cam)
 	{
-		//char *msg = "now updating scenegraph\n"; OutputDebugString(msg);
-		//mOcclusionCullingTerrainSceneTraverser->preprocess();
-
-     	mOcclusionCullingOctreeSceneTraverser->setNumSceneNodes((int)mSceneNodes.size());
+		mOcclusionCullingOctreeSceneTraverser->setNumSceneNodes((int)mSceneNodes.size());
 		mOcclusionCullingOctreeSceneTraverser->setRenderSystem(mDestRenderSystem);
         
