Index: trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp	(revision 171)
+++ trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp	(revision 173)
@@ -16,5 +16,5 @@
 mViewport(vp), 
 mWasInitialised(false),
-mRenderPatches(false)
+mRenderPatches(renderPatches)
 {
 }
@@ -125,5 +125,20 @@
 
 	//-- collect results
-
+	CollectItemBufferResults(visibleGeometry, visiblePatches);
+
+	//-- reset options
+
+	useItemBuffer = false; // don't need item buffer anymore
+	sm->setOption("UseItemBuffer", &useItemBuffer);
+
+	mWasInitialised = false; // reset initialised - flag
+	mViewport->setOverlaysEnabled(overlayEnabled); // reset old overlay status
+	mViewport->setBackgroundColour(bg); // reset background color
+}
+//-----------------------------------------------------------------------
+void PlatformQueryManager::CollectItemBufferResults(
+				InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry,
+				InfoContainer<GtpVisibility::PatchInfo> *visiblePatches)
+{
 	int dimx = 0;
 	int dimy = 0;
@@ -151,5 +166,5 @@
 		{
 			if (mRenderPatches)
-			{
+			{	
 				((*visiblePatches)[id]).AddVisibility(1, 0);
 			}
@@ -161,16 +176,7 @@
 	}
 
-
-	//-- reset options
-
-	useItemBuffer = false; // don't need item buffer anymore
-	sm->setOption("UseItemBuffer", &useItemBuffer);
-
-	mWasInitialised = false; // reset initialised - flag
-	mViewport->setOverlaysEnabled(overlayEnabled); // reset old overlay status
-	mViewport->setBackgroundColour(bg); // reset background color
-
-	delete [] buf; 	// delete copy of the frame buffer
-}
+	delete [] buf;
+}
+
 //-----------------------------------------------------------------------
 void PlatformQueryManager::InitItemBuffer(
@@ -215,5 +221,4 @@
 			subEnt->setId(id);
 			//subEnt->setId((41 << 16) + (4 << 8) + 60);
-			//subEnt->setId((2 << 16) + (4 << 8) + 60);
 		}
 			
Index: trunk/VUT/Ogre/src/OgreOcclusionQueriesQueryManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreOcclusionQueriesQueryManager.cpp	(revision 171)
+++ trunk/VUT/Ogre/src/OgreOcclusionQueriesQueryManager.cpp	(revision 173)
@@ -22,4 +22,9 @@
 }
 //-----------------------------------------------------------------------
+inline bool patchinfo_eq(const GtpVisibility::PatchInfo &info1, const GtpVisibility::PatchInfo &info2) 
+{ 
+	return info1.GetPatch() == info2.GetPatch(); 
+}
+//-----------------------------------------------------------------------
 inline bool nodeinfo_lower(const GtpVisibility::NodeInfo &info1, const GtpVisibility::NodeInfo &info2) 
 { 
@@ -31,5 +36,9 @@
 	return info1.GetMesh() < info2.GetMesh(); 
 }
-
+//-----------------------------------------------------------------------
+inline bool patchinfo_lower(const GtpVisibility::PatchInfo &info1, const GtpVisibility::PatchInfo &info2) 
+{ 
+	return info1.GetPatch() < info2.GetPatch(); 
+}
 
 
@@ -100,4 +109,7 @@
 	GtpVisibility::GeometryList::iterator geometryIt, geometryIt_end = geometryList.end();
 
+	// vector for storing subentities of meshes
+	GtpVisibility::PatchList patchList;
+	GtpVisibility::PatchList::iterator patchIt, patchIt_end = patchList.end();
 
 	// to obtain the correct number of projected pixels, depth write must be disabled
@@ -128,9 +140,15 @@
 
 		//-- queries for geometry: if item buffer, capture only projected visibility
+		for (geometryIt = geometryList.begin(); geometryIt != geometryIt_end; ++geometryIt)
+		{
+			queryList[i].push_back(mHierarchyInterface->IssueOcclusionQuery(*geometryIt));
+		}
+		
+		//-- queries for patches: if item buffer, capture only projected visibility
 		if (!(mUseItemBuffer && (i == 0)))
 		{
-			for (geometryIt = geometryList.begin(); geometryIt != geometryIt_end; ++geometryIt)
+			for (patchIt = patchList.begin(); patchIt != patchIt_end; ++patchIt)
 			{
-				queryList[i].push_back(mHierarchyInterface->IssueOcclusionQuery(*geometryIt));
+				queryList[i].push_back(mHierarchyInterface->IssueOcclusionQuery(*patchIt));
 			}
 		}
@@ -150,15 +168,14 @@
 		if (relativeVisibility)
 		{
-			CollectRelativeGeometryVisibilityForItemBuffer(projQueryIt, &geometryList, visibleGeometry);
+			CollectRelativePatchVisibilityForItemBuffer(projQueryIt, &patchList, visiblePatches);
 		}
     }
 	else
 	{
-		CollectGeometryVisibility(visQueryIt, projQueryIt, &geometryList, 
-			visibleGeometry, relativeVisibility);
-	    //CollectPatchVisibility(patchList, visiblePatches);
-	}
-
-
+		CollectPatchVisibility(visQueryIt, projQueryIt, &patchList, visiblePatches, relativeVisibility);
+	}	
+	
+	CollectGeometryVisibility(visQueryIt, projQueryIt, &geometryList, visibleGeometry, relativeVisibility);
+		
 	//-- reset options
 	enableDepthWrite = true;
@@ -397,4 +414,31 @@
 		meshinfo_eq), visibleGeometry->end());
 }
-
+//-----------------------------------------------------------------------
+void  OcclusionQueriesQueryManager::RemoveDuplicatePatches(
+				InfoContainer<GtpVisibility::PatchInfo> *visiblePatches)
+{
+	sort(visiblePatches->begin(), visiblePatches->end(), patchinfo_lower);
+	
+	InfoContainer<GtpVisibility::PatchInfo>::iterator visiblePatchIt,
+			visiblePatchIt_end = visiblePatches->end();
+
+	GtpVisibility::PatchInfo *patchInfo = NULL;
+
+	for (visiblePatchIt = visiblePatches->begin(); visiblePatchIt != visiblePatchIt_end; 
+		++visiblePatchIt)
+	{
+		if (!patchInfo || (patchInfo->GetPatch() != (*visiblePatchIt).GetPatch()))
+		{
+			patchInfo = &(*visiblePatchIt);
+		}
+		else // info points to same mesh, just add visibility
+		{
+			patchInfo->AddVisibility(*visiblePatchIt);
+		}
+	}
+
+	// physically delete duplicates
+	visiblePatches->erase(std::unique(visiblePatches->begin(), visiblePatches->end(), 
+		patchinfo_eq), visiblePatches->end());
+}
 } // namespace Ogre
