Index: /trunk/VUT/GtpVisibility/include/CoherentHierarchicalCullingManager.h
===================================================================
--- /trunk/VUT/GtpVisibility/include/CoherentHierarchicalCullingManager.h	(revision 146)
+++ /trunk/VUT/GtpVisibility/include/CoherentHierarchicalCullingManager.h	(revision 147)
@@ -25,9 +25,9 @@
 
 protected:
-	/** Decides wether occlusion query should be issued depending on some 
+	/** Decides if node is considered to be visible depeding on the 
 		assumed visibility factor.
-		@returns if occlusion query should be issued
+		@returns if node is considered to be visible
 	*/
-	bool DecideQueryTest(HierarchyNode *node) const;
+	bool DecideVisible(HierarchyNode *node) const;
 
 	unsigned int mAssumedVisibility;
@@ -35,5 +35,5 @@
 		mAssumedVisibility.
 	*/
-	unsigned int mThreshold;
+	int mThreshold;
 };
 
Index: /trunk/VUT/GtpVisibility/src/FrustumCullingManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibility/src/FrustumCullingManager.cpp	(revision 139)
+++ /trunk/VUT/GtpVisibility/src/FrustumCullingManager.cpp	(revision 147)
@@ -17,6 +17,6 @@
 
 		if (!mHierarchyInterface->CheckFrustumVisible(node))
-		{//if (mHierarchyInterface->mIsShadowPass)
-			mNumFrustumCulledNodes ++;
+		{
+			++ mNumFrustumCulledNodes;
 
 			if (mVisualizeCulledNodes)
Index: /trunk/VUT/GtpVisibility/src/CoherentHierarchicalCullingManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibility/src/CoherentHierarchicalCullingManager.cpp	(revision 146)
+++ /trunk/VUT/GtpVisibility/src/CoherentHierarchicalCullingManager.cpp	(revision 147)
@@ -1,4 +1,4 @@
 #include "CoherentHierarchicalCullingManager.h"
-//#include <OgreLogManager.h>
+#include <OgreLogManager.h>
 
 namespace GtpVisibility {
@@ -55,7 +55,7 @@
 			if (!mHierarchyInterface->CheckFrustumVisible(node, intersects))
 			{
-				mNumFrustumCulledNodes ++;
+				++ mNumFrustumCulledNodes;
 				
-				if(mVisualizeCulledNodes)
+				if (mVisualizeCulledNodes)
 				{
 					mHierarchyInterface->VisualizeCulledNode(node, FRUSTUM_CULLED);
@@ -79,13 +79,14 @@
 				bool wasVisible = mHierarchyInterface->IsNodeVisible(node) && 
 					(mHierarchyInterface->LastVisited(node) == mHierarchyInterface->GetFrameId() - 1);
-				
+				// if visibility is assumed to be true in this frame
+				bool assumeVisible = wasVisible && (mAssumedVisibility > 0) && DecideVisible(node);
+
 				// identify nodes that we cannot skip queries for
 				// geometry not only in leaves => test for renderable geometry
 				bool issueQuery = !wasVisible || 
-					(mHierarchyInterface->HasGeometry(node) && 
-					(!mAssumedVisibility || DecideQueryTest(node)));
-				
-				// reset node's visibility classification 
-				mHierarchyInterface->SetNodeVisible(node, false);
+					(mHierarchyInterface->HasGeometry(node) && !assumeVisible);
+								
+				// reset node's visibility classification
+				mHierarchyInterface->SetNodeVisible(node, !issueQuery && assumeVisible);
 
 				// update node's visited flag
@@ -95,10 +96,10 @@
 				if (issueQuery)
 				{
-					mNumQueriesIssued ++;
+					++ mNumQueriesIssued;
 					
 					queryQueue.push(QueryPair(node, mHierarchyInterface->
 						IssueOcclusionQuery(node, wasVisible)));
 				}
-					
+				
 				// always traverse a node if it was visible
 				if (wasVisible)
@@ -117,12 +118,17 @@
 	mThreshold = 0;
 
-	if (mAssumedVisibility)
+	if (mAssumedVisibility > 0)
 	{
-		mThreshold =  RAND_MAX / mAssumedVisibility;
+		mThreshold =  RAND_MAX - RAND_MAX / mAssumedVisibility;
 	}
+	//std::stringstream d; d << "*** setting assumed vis: " << mAssumedVisibility; Ogre::LogManager::getSingleton().logMessage(d.str());
 }
 //-----------------------------------------------------------------------
-bool CoherentHierarchicalCullingManager::DecideQueryTest(HierarchyNode *node) const
+bool CoherentHierarchicalCullingManager::DecideVisible(HierarchyNode *node) const
 {
+	/*bool result = rand() < mThreshold;
+	std::stringstream d; d << "Assumed vis: " << mAssumedVisibility << ", result: " << result;
+	Ogre::LogManager::getSingleton().logMessage(d.str()); return result;*/
+	
 	return rand() < mThreshold;
 }
Index: /trunk/VUT/GtpVisibility/src/VisibilityManager.cpp
===================================================================
--- /trunk/VUT/GtpVisibility/src/VisibilityManager.cpp	(revision 146)
+++ /trunk/VUT/GtpVisibility/src/VisibilityManager.cpp	(revision 147)
@@ -13,5 +13,6 @@
 mQueryManager(new DummyQueryManager(NULL)),
 mPreprocessingManager(new DummyPreprocessingManager(NULL)),
-mVisibilityEnvironment(visEnvironment)
+mVisibilityEnvironment(visEnvironment),
+mAssumedVisibility(0)
 {
 	SetCullingManager(VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING);
Index: /trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp
===================================================================
--- /trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp	(revision 146)
+++ /trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp	(revision 147)
@@ -170,4 +170,7 @@
 	dynamic_cast<PlatformQueryManager *>(mVisibilityManager->GetQueryManager())->
 		SetViewport(mWindow->getViewport(0));
+
+	// reset statistics
+	resetStats();
 } 
 //-----------------------------------------------------------------------
@@ -528,8 +531,11 @@
 		writeFrames();
 		
+		float avgFps = (float)mAvgFps / (float)mFrameCount;
+
 		//-- write out stats for recorded walkthrough
 		std::stringstream d;
 		d << "Algorithm: " << currentAlgorithmCaptions[mCurrentAlgorithm] << "\n"
-		  << "avg. FPS: " << mWindow->getAverageFPS() << "\n"
+		  //<< "avg. FPS: " << mWindow->getAverageFPS() << "\n"
+		  << "avg. FPS: " << avgFps << "\n"
 		  << "best FPS: " << mWindow->getBestFPS() << "\n"
 		  << "worst FPS: " << mWindow->getWorstFPS() << "\n"
@@ -564,6 +570,7 @@
 			mWindow->setDebugText("Replay");
 			
-			// reset, because we measure fps stats during walkthrough
+			// reset, because we measure fps stats during walkthrough (warning: average fps broken)
 			mWindow->resetStatistics();
+			resetStats();
 
 			//-- initialise frame data
@@ -630,4 +637,10 @@
 }
 //-----------------------------------------------------------------------
+void TerrainFrameListener::resetStats()
+{
+	mAvgFps = 0;
+	mFrameCount = 0;
+}
+//-----------------------------------------------------------------------
 void TerrainFrameListener::updateStats()
 {
@@ -635,10 +648,15 @@
 	char str[100];
 	
-	static String currFps = "Current FPS: ";
-	static String avgFps = "Average FPS: ";
-	static String bestFps = "Best FPS: ";
-	static String worstFps = "Worst FPS: ";
-	static String tris = "Triangle Count: ";
-
+	static String currFpsString = "Current FPS: ";
+	static String avgFpsString = "Average FPS: ";
+	static String bestFpsString = "Best FPS: ";
+	static String worstFpsString = "Worst FPS: ";
+	static String trisString = "Triangle Count: ";
+
+	mAvgFps += mWindow->getStatistics().lastFPS;
+	++ mFrameCount;
+	
+	float avgFps = (float)mAvgFps / (float)mFrameCount;
+    
 	// update stats when necessary
     try 
@@ -651,13 +669,14 @@
 		const RenderTarget::FrameStats& stats = mWindow->getStatistics();
 
-		guiAvg->setCaption(avgFps + StringConverter::toString(stats.avgFPS));
-		guiCurr->setCaption(currFps + StringConverter::toString(stats.lastFPS));
-		guiBest->setCaption(bestFps + StringConverter::toString(stats.bestFPS)
+		//guiAvg->setCaption(avgFps + StringConverter::toString(stats.avgFPS));
+		guiAvg->setCaption(avgFpsString + StringConverter::toString(avgFps));
+		guiCurr->setCaption(currFpsString + StringConverter::toString(stats.lastFPS));
+		guiBest->setCaption(bestFpsString + StringConverter::toString(stats.bestFPS)
 			+" "+StringConverter::toString(stats.bestFrameTime)+" ms");
-		guiWorst->setCaption(worstFps + StringConverter::toString(stats.worstFPS)
+		guiWorst->setCaption(worstFpsString + StringConverter::toString(stats.worstFPS)
 			+" "+StringConverter::toString(stats.worstFrameTime)+" ms");
 
 		OverlayElement* guiTris = OverlayManager::getSingleton().getOverlayElement("Core/NumTris");
-        guiTris->setCaption(tris + StringConverter::toString(stats.triangleCount));
+        guiTris->setCaption(trisString + StringConverter::toString(stats.triangleCount));
 
 		OverlayElement* guiDbg = OverlayManager::getSingleton().getOverlayElement("Core/DebugText");
Index: /trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.h
===================================================================
--- /trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.h	(revision 146)
+++ /trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.h	(revision 147)
@@ -126,4 +126,6 @@
 protected:
 
+	void resetStats();
+
 	/** Clamp camera to terrain.
 	*/
@@ -228,4 +230,6 @@
 	bool mShowQueryStats;
 
+	unsigned long mAvgFps;
+	unsigned long mFrameCount;
 	//bool mUseBufferedInputKeys, mUseBufferedInputMouse, mInputTypeSwitchingOn;
 };
Index: /trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp
===================================================================
--- /trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 141)
+++ /trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 147)
@@ -223,10 +223,13 @@
 	else
 	{	
-		// we interleave identification and rendering of objects 
-		// in _renderVisibibleObjects
-
+		// if not the shadow pass we interleave identification 
+		// and rendering of objects in _renderVisibibleObjects
+		if (mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE && 
+			mIlluminationStage == IRS_RENDER_TO_TEXTURE)
+		{
+			TerrainSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
+		}
 		// only shadow casters will be rendered in shadow texture pass
-		mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
-		//InitVisibilityCulling(cam);
+		//mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
 	}
 	//TerrainSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
@@ -247,4 +250,12 @@
 		mIlluminationStage = IRS_NONE;
 	
+		TerrainSceneManager::_renderVisibleObjects();
+
+		mIlluminationStage = savedStage;
+	}
+	// normal rendering for shadow maps because of performance
+	else if (mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE && 
+			 mIlluminationStage == IRS_RENDER_TO_TEXTURE)
+	{
 		TerrainSceneManager::_renderVisibleObjects();
 	}
@@ -313,6 +324,6 @@
 		
 	// set the new render level index afterwards => new level in the next frame 
-	TerrainRenderable::setCurrentRenderLevelIndex(
-		(TerrainRenderable::getCurrentRenderLevelIndex() + 1) % 10);
+	int levelIdx = (TerrainRenderable::getCurrentRenderLevelIndex() + 1) % 10;
+	TerrainRenderable::setCurrentRenderLevelIndex(levelIdx);
 
 	getRenderQueue()->clear();
Index: /trunk/VUT/Ogre/resources/VisibilityDemo.overlay
===================================================================
--- /trunk/VUT/Ogre/resources/VisibilityDemo.overlay	(revision 146)
+++ /trunk/VUT/Ogre/resources/VisibilityDemo.overlay	(revision 147)
@@ -46,5 +46,5 @@
 		element TextArea(Example/Visibility/AlgorithmInfo): Example/Visibility/Templates/BasicText
 		{
-			left 155
+			left 170
 			top 5
 			width 90
@@ -63,5 +63,5 @@
 		element TextArea(Example/Visibility/ThresholdInfo): Example/Visibility/Templates/BasicText
 		{
-			left 155
+			left 170
 			top 20
 			width 90
@@ -79,5 +79,5 @@
 		element TextArea(Example/Visibility/UseOptimizationInfo): Example/Visibility/Templates/BasicText
 		{
-			left 155
+			left 170
 			top 35
 			width 90
@@ -95,5 +95,5 @@
 		element TextArea(Example/Visibility/UseDepthPassInfo): Example/Visibility/Templates/BasicText
 		{
-			left 155
+			left 170
 			top 50
 			width 90
@@ -107,10 +107,10 @@
 			width 90
 			height 30
-			caption [A] Assumed Visibility
+			caption [8][9] Assumed Visibility
 		}
 		
 		element TextArea(Example/Visibility/AssumedVisibilityInfo): Example/Visibility/Templates/BasicText
 		{
-			left 155
+			left 170
 			top 65
 			width 90
