Index: trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp	(revision 158)
+++ trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp	(revision 159)
@@ -103,5 +103,7 @@
 mVisibilityManager(NULL),
 mDelayedQueriesIssued(0.0),
-mDelayedTraversedNodes(0.0)
+mDelayedTraversedNodes(0.0),
+mUseItemBuffer(false),
+mRenderPatchesForItemBuffer(false)
 {
 	//mInputDevice = PlatformManager::getSingleton().createInputReader();
@@ -162,8 +164,12 @@
 	//-- visibility query stats overlay
 	mQueryTypeInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Query/QueryTypeInfo");
+	
 	mQueryVisibleNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Query/VisibleNodesInfo");
     mQueryVisibleGeometryInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Query/VisibleGeometryInfo");
+	mQueryVisiblePatchInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Query/VisiblePatchInfo");
+	
 	mQueryNodeVisibilityInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Query/NodeVisibilityInfo");
 	mQueryGeometryVisibilityInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Query/GeometryVisibilityInfo");
+	mQueryPatchVisibilityInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Query/PatchVisibilityInfo");
 
 	// show stats overlays
@@ -294,6 +300,5 @@
 
 	//-- IMPORTANT: must be set, otherwise terrain is not rendered correctly
-	int terrainLevelIdx = 0;
-	mSceneMgr->setOption("TerrainLevelIdx", &terrainLevelIdx);
+	mSceneMgr->endFrame();
 
 	//-- setup what is needed for immediate mouse/key movement
@@ -392,5 +397,5 @@
 	{
 		mQueryManager = new ItemBufferQueryManager(sm->GetHierarchyInterface(), 
-			mWindow->getViewport(0));
+			mWindow->getViewport(0), mRenderPatchesForItemBuffer);
 	}
 	else
@@ -404,4 +409,5 @@
 	InfoContainer<GtpVisibility::NodeInfo> visibleNodes;
 	InfoContainer<GtpVisibility::MeshInfo> visibleGeometry;
+	InfoContainer<GtpVisibility::PatchInfo> visiblePatches;
 
 	if (fromPoint)
@@ -409,16 +415,14 @@
 		mQueryManager->
 			ComputeFromPointVisibility(mCamNode->getPosition(), &visibleNodes, 
-									   &visibleGeometry, relativeVisibility);
+									   &visibleGeometry, &visiblePatches, relativeVisibility);
 	}
 	else
 	{
 		mQueryManager->ComputeCameraVisibility(*mCamera,
-			    &visibleNodes, &visibleGeometry, relativeVisibility);
-	}
-
-	float averageNodeVis = 0;
-	float averageGeometryVis = 0;
-	int geomSize = 0;
-	int nodesSize = 0;
+			    &visibleNodes, &visibleGeometry, &visiblePatches, relativeVisibility);
+	}
+
+	float averageNodeVis = 0, averageGeometryVis = 0, averagePatchVis = 0;
+	int geomSize = 0, nodesSize = 0, patchSize = 0;
 
 	InfoContainer<GtpVisibility::MeshInfo>::iterator geomIt, geomIt_end = visibleGeometry.end();
@@ -458,8 +462,28 @@
 	}
 
+	InfoContainer<GtpVisibility::PatchInfo>::iterator patchIt, patchIt_end = visiblePatches.end();
+
+	for (patchIt = visiblePatches.begin(); patchIt != patchIt_end; ++patchIt)
+	{
+		// add if not 0
+		if ((*patchIt).GetVisiblePixels())
+		{
+			float vis = relativeVisibility ? 
+				(*patchIt).ComputeRelativeVisibility() : (float)(*patchIt).GetVisiblePixels();
+		
+			averagePatchVis += vis;
+			++patchSize;
+
+			std::stringstream d; d << "Patch visibility: " << vis;
+			LogManager::getSingleton().logMessage(d.str());
+		}	
+	}
+
 	if (nodesSize)
 		averageNodeVis /= (float)nodesSize;
 	if (geomSize)
 		averageGeometryVis /= (float)geomSize;
+	if (patchSize)
+		averagePatchVis /= (float)patchSize;
 
 	//-- update visibility queries stats
@@ -481,4 +505,7 @@
 		mQueryVisibleGeometryInfo->setCaption(str);
 		
+		sprintf(str,": %d", (int)patchSize); 
+		mQueryVisiblePatchInfo->setCaption(str);
+
 		sprintf(str,": %3.3f", averageNodeVis); 
 		mQueryNodeVisibilityInfo->setCaption(str);
@@ -486,4 +513,7 @@
 		sprintf(str,": %3.3f", averageGeometryVis); 
 		mQueryGeometryVisibilityInfo->setCaption(str);
+
+		sprintf(str,": %3.3f", averagePatchVis); 
+		mQueryPatchVisibilityInfo->setCaption(str);
 	}
 	catch (...)
@@ -909,5 +939,5 @@
 		break;
 	
-	case KC_F:
+    case KC_F:
 		nextFilter();
 		break;
@@ -929,4 +959,12 @@
 	case KC_S:
 		toggleShowShadows();
+		break;
+
+	case KC_I:
+		mUseItemBuffer = !mUseItemBuffer;
+		break;
+
+	case KC_C:
+		mRenderPatchesForItemBuffer = !mRenderPatchesForItemBuffer;
 		break;
 
@@ -959,15 +997,10 @@
 		break;
 	case KC_F5:
-		ApplyVisibilityQuery(false, mShiftPressed, false);
+		ApplyVisibilityQuery(false, mShiftPressed, mUseItemBuffer);
 		break;
 	case KC_F6:
-		ApplyVisibilityQuery(true, mShiftPressed, false);
-		break;
-	case KC_F7:
-		ApplyVisibilityQuery(false, mShiftPressed, true);
-		break;
-	case KC_F8:
-		ApplyVisibilityQuery(true, mShiftPressed, true);
-		break;
+		ApplyVisibilityQuery(true, mShiftPressed, mUseItemBuffer);
+		break;
+	
 	case KC_F11:
 		takeScreenShot();
Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 156)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 159)
@@ -127,6 +127,6 @@
 	setupGui();
 
-	// Floor plane
-	/*
+	
+	/* // occluder plane to test visibility
 	Plane plane;
 	plane.normal = Vector3::UNIT_Y;
@@ -138,10 +138,9 @@
 	pPlaneEnt->setMaterialName("Examples/Rockwall");
 	pPlaneEnt->setCastShadows(true);
-	mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(pPlaneEnt);
-	*/
+	mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(pPlaneEnt);	*/
+
 	mSceneMgr->setShadowTextureSettings(1024, 2);
 	mSceneMgr->setShadowColour(ColourValue(0.5, 0.5, 0.5));
-    //mSceneMgr->setShowDebugShadows(true);
-
+   
 
 	//-- terrain content setup
Index: trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.h
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.h	(revision 155)
+++ trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.h	(revision 159)
@@ -169,8 +169,11 @@
 	OverlayElement *mQueryTypeInfo;
 	OverlayElement *mQueryVisibleNodesInfo;
-    OverlayElement *mQueryVisibleGeometryInfo;
+	OverlayElement *mQueryVisibleGeometryInfo;
+	OverlayElement *mQueryVisiblePatchInfo;
+
 	OverlayElement *mQueryVisiblityInfo;
 	OverlayElement *mQueryNodeVisibilityInfo;
 	OverlayElement *mQueryGeometryVisibilityInfo;
+	OverlayElement *mQueryPatchVisibilityInfo;
 
 	RayQueryExecutor *mRayQueryExecutor;
@@ -190,5 +193,6 @@
 	bool mShutdownRequested;
 	bool mDisplayCameraDetails;
-	
+	bool mUseItemBuffer;
+
 	Real mVizCameraHeight;
 
@@ -236,4 +240,5 @@
 	unsigned long mAvgFps;
 	unsigned long mFrameCount;
+
 	//bool mUseBufferedInputKeys, mUseBufferedInputMouse, mInputTypeSwitchingOn;
 	PlatformQueryManager *mQueryManager;
@@ -241,4 +246,6 @@
 	float mDelayedQueriesIssued;
 	float mDelayedTraversedNodes;
+
+	bool mRenderPatchesForItemBuffer;
 };
 
