Index: trunk/VUT/Ogre/src/OgreOcclusionQueriesQueryManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreOcclusionQueriesQueryManager.cpp	(revision 173)
+++ trunk/VUT/Ogre/src/OgreOcclusionQueriesQueryManager.cpp	(revision 174)
@@ -47,8 +47,8 @@
 							PlatformHierarchyInterface *hierarchyInterface, 
 							Viewport *vp,
-							bool renderPatches,
-							bool useItemBufferForGeometry):
-PlatformQueryManager(hierarchyInterface, vp, renderPatches),
-mUseItemBuffer(useItemBufferForGeometry)
+							int queryModes,
+							int itemBufferMode):
+PlatformQueryManager(hierarchyInterface, vp, queryModes), 
+mItemBufferMode(itemBufferMode)
 {
 }
@@ -60,5 +60,5 @@
 			    bool relativeVisibility)
 {
-	 // we need access to the scene manager and the rendersystem
+	// we need access to the scene manager and the rendersystem
 	PlatformHierarchyInterface *pfHierarchyInterface = 
 		dynamic_cast<PlatformHierarchyInterface *>(mHierarchyInterface);
@@ -68,10 +68,15 @@
 
 	//-- render scene with item buffer (i.e., objects with their id as color codes)
-	if (mUseItemBuffer)
-	{
+	if ((mItemBufferMode && mQueryModes) != 0)
+	{
+		int savedQueryModes = mQueryModes;
+		mQueryModes = mItemBufferMode;
+		
 		PlatformQueryManager::ComputeCameraVisibility(camera, visibleNodes,
 			    visibleGeometry, visiblePatches, relativeVisibility);
-
+		
+		// overlays cause false visibility
 		mViewport->setOverlaysEnabled(false);
+		mQueryModes = savedQueryModes;
 	}
 	else
@@ -93,17 +98,32 @@
 	GtpVisibility::QueryList queryList[2];
 	
-	// rendered hierarchy nodes from previous render
-	GtpVisibility::HierarchyNodeList *nodeList = mHierarchyInterface->GetRenderedNodes();
-	GtpVisibility::HierarchyNodeList::iterator nodeIt, nodeIt_end = nodeList->end();
+	// rendered visibile hierarchy nodes from previous rendering
+	GtpVisibility::HierarchyNodeList *visNodes = mHierarchyInterface->GetVisibleNodes();
+	GtpVisibility::HierarchyNodeList::iterator visNodesIt, visNodesIt_end = visNodes->end();
+
+    GtpVisibility::HierarchyNodeList nodeList;
+
+	if (mQueryModes && NODE_VISIBILITY)
+	{
+		for (visNodesIt = visNodes->begin(); visNodesIt != visNodesIt_end; ++visNodesIt)
+		{
+			nodeList.push_back((*visNodesIt));
+		}
+	}
+
+	GtpVisibility::HierarchyNodeList::iterator nodeIt, nodeIt_end = nodeList.end();
 
 	// vector for storing entities of meshes
 	GtpVisibility::GeometryList geometryList;
 	
-	// store geometry of the hierarchy node in a geometry list (only once!)
-	for (nodeIt = nodeList->begin(); nodeIt != nodeIt_end; ++nodeIt)		
-	{
-		mHierarchyInterface->GetNodeGeometryList(*nodeIt, &geometryList, false);
-	}
-
+	// store geometry of the hierarchy nodes in a geometry list
+	if (mQueryModes && GEOMETRY_VISIBILITY)
+	{	
+		for (visNodesIt = visNodes->begin(); visNodesIt != visNodesIt_end; ++visNodesIt)	
+		{
+			mHierarchyInterface->GetNodeGeometryList(*visNodesIt, &geometryList, false);
+		}
+	}
+	
 	// geometry list iterator
 	GtpVisibility::GeometryList::iterator geometryIt, geometryIt_end = geometryList.end();
@@ -111,4 +131,14 @@
 	// vector for storing subentities of meshes
 	GtpVisibility::PatchList patchList;
+
+	// store patchges of the geometries in a patch list
+	if (mQueryModes && PATCH_VISIBILITY)
+	{
+		for (visNodesIt = visNodes->begin(); visNodesIt != visNodesIt_end; ++visNodesIt)	
+		{
+			GetNodePatchList(*visNodesIt, &patchList);
+		}
+	}
+
 	GtpVisibility::PatchList::iterator patchIt, patchIt_end = patchList.end();
 
@@ -133,5 +163,5 @@
 	{
 		//-- queries for hierarchy nodes
-		for (nodeIt = nodeList->begin(); nodeIt != nodeIt_end; ++nodeIt)		
+		for (nodeIt = nodeList.begin(); nodeIt != nodeIt_end; ++nodeIt)		
 		{
 			// TODO: DELETE QUERIES FROM PREVIOUS RENDER
@@ -140,11 +170,14 @@
 
 		//-- 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));
+		if ((mItemBufferMode != GEOMETRY_VISIBILITY) || (i == 1))
+		{
+			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)))
+		if ((mItemBufferMode != PATCH_VISIBILITY) || (i == 1))
 		{
 			for (patchIt = patchList.begin(); patchIt != patchIt_end; ++patchIt)
@@ -162,19 +195,33 @@
 	GtpVisibility::QueryList::iterator projQueryIt = queryList[1].begin();
 	
-	CollectNodeVisibility(visQueryIt, projQueryIt, nodeList, visibleNodes, relativeVisibility);
-
-	if (mUseItemBuffer)
-	{
-		if (relativeVisibility)
-		{
-			CollectRelativePatchVisibilityForItemBuffer(projQueryIt, &patchList, visiblePatches);
-		}
-    }
+	// collect occlusion queries for hierarchy nodes
+	CollectNodeVisibility(visQueryIt, projQueryIt, &nodeList, visibleNodes, relativeVisibility);
+
+
+
+	// collect occlusion queries for geometry
+	if ((mItemBufferMode == GEOMETRY_VISIBILITY) && relativeVisibility)
+	{
+		// if visibility was established using the item buffer,
+		// the array is organized different (e.g., ordered by id, all possible objects)
+		CollectRelativeGeometryVisibilityForItemBuffer(projQueryIt, &geometryList, visibleGeometry);
+	}
+	else
+	{
+		CollectGeometryVisibility(visQueryIt, projQueryIt, &geometryList, visibleGeometry, 
+								  relativeVisibility);
+	}
+
+
+	// collect occlusion queries for patches
+	if ((mItemBufferMode == PATCH_VISIBILITY) && relativeVisibility)
+	{
+		CollectRelativePatchVisibilityForItemBuffer(projQueryIt, &patchList, visiblePatches);
+	}
 	else
 	{
 		CollectPatchVisibility(visQueryIt, projQueryIt, &patchList, visiblePatches, relativeVisibility);
 	}	
-	
-	CollectGeometryVisibility(visQueryIt, projQueryIt, &geometryList, visibleGeometry, relativeVisibility);
+
 		
 	//-- reset options
@@ -201,7 +248,12 @@
 	RemoveDuplicateNodes(visibleNodes);
 	
-	if (!mUseItemBuffer)
+	if (mItemBufferMode != GEOMETRY_VISIBILITY)
 	{
 		RemoveDuplicateGeometry(visibleGeometry);
+	}
+	
+	if (mItemBufferMode != PATCH_VISIBILITY)
+	{
+		RemoveDuplicatePatches(visiblePatches);
 	}
 }
@@ -442,3 +494,22 @@
 		patchinfo_eq), visiblePatches->end());
 }
+//-----------------------------------------------------------------------
+void OcclusionQueriesQueryManager::GetNodePatchList(GtpVisibility::HierarchyNode *node,	
+					 			 					GtpVisibility::PatchList *patchList)
+{
+	GtpVisibility::GeometryList geomList;
+	mHierarchyInterface->GetNodeGeometryList(node, &geomList, false);
+
+	// geometry list iterator
+	GtpVisibility::GeometryList::iterator geomIt, geomIt_end = geomList.end();
+
+	for (geomIt = geomList.begin(); geomIt != geomIt_end; ++geomIt)
+	{
+		for	(int i = 0; i < (int)(*geomIt)->getNumSubEntities(); ++i)
+		{
+			patchList->push_back((*geomIt)->getSubEntity(i));
+		}
+	}
+}
+
 } // namespace Ogre
