Index: trunk/VUT/Ogre/include/OgreOcclusionQueriesQueryManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgreOcclusionQueriesQueryManager.h	(revision 173)
+++ trunk/VUT/Ogre/include/OgreOcclusionQueriesQueryManager.h	(revision 174)
@@ -15,8 +15,6 @@
 		on different hierarchy types, while reusing the implementation of the query methods.
     */
-	OcclusionQueriesQueryManager(PlatformHierarchyInterface *hierarchyInterface, 
-		                         Viewport *vp,
-								 bool renderPatches,
-		                         bool useItemBufferForGeometry);
+	OcclusionQueriesQueryManager(PlatformHierarchyInterface *hierarchyInterface, Viewport *vp,
+								 int queryModes, int itemBufferMode);
     
     void ComputeCameraVisibility(const Camera &camera,
@@ -70,5 +68,12 @@
 	void RemoveDuplicatePatches(InfoContainer<GtpVisibility::PatchInfo> *visiblePatches);
 
-	bool mUseItemBuffer;
+	/** Returns the patches of the geometry of a given node. 
+		@param node the node containing the patches
+		@param patchList the patches are returned in this list
+	*/
+	virtual void GetNodePatchList(GtpVisibility::HierarchyNode *node,	
+					 			  GtpVisibility::PatchList *patchList);
+
+	int mItemBufferMode;
 };
 
Index: trunk/VUT/Ogre/include/OgreOctreeHierarchyInterface.h
===================================================================
--- trunk/VUT/Ogre/include/OgreOctreeHierarchyInterface.h	(revision 158)
+++ trunk/VUT/Ogre/include/OgreOctreeHierarchyInterface.h	(revision 174)
@@ -48,7 +48,7 @@
 
 	void GetNodeGeometryList(GtpVisibility::HierarchyNode *node,
-					 GtpVisibility::GeometryList *geometryList, 
-					 bool includeChildren);
-
+							 GtpVisibility::GeometryList *geometryList, 
+							 bool includeChildren);
+	
 protected:
 	/** Returns pointer to the bounding box of the node.
Index: trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h
===================================================================
--- trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h	(revision 156)
+++ trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h	(revision 174)
@@ -106,4 +106,5 @@
     bool IsBoundingBoxQuery();
 
+	GtpVisibility::OcclusionQuery *IssueOcclusionQuery(GtpVisibility::Patch *patch);
 
 protected:
@@ -111,4 +112,8 @@
 	*/
 	void RenderGeometry(GtpVisibility::Mesh *geom);
+
+	/** Renders a patch 
+	*/
+	void RenderPatch(GtpVisibility::Patch *patch);
 
 	/** Materials for visualizing frustum and query culled nodes.
Index: trunk/VUT/Ogre/include/OgrePlatformQueryManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgrePlatformQueryManager.h	(revision 173)
+++ trunk/VUT/Ogre/include/OgrePlatformQueryManager.h	(revision 174)
@@ -18,6 +18,5 @@
     */
 	PlatformQueryManager(PlatformHierarchyInterface *hierarchyInterface, 
-		                 Viewport *vp,
-						 bool renderPatches);
+		                 Viewport *vp, int queryModes);
     
     /** 
@@ -42,5 +41,5 @@
 	*/
 	void SetViewport(Viewport *vp);
-	
+
 protected:
 
@@ -53,6 +52,6 @@
 
 	Viewport *mViewport;
+	
 	bool mWasInitialised;
-	bool mRenderPatches;
 };
 
Index: trunk/VUT/Ogre/src/OgreSceneNodeHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreSceneNodeHierarchyInterface.cpp	(revision 158)
+++ trunk/VUT/Ogre/src/OgreSceneNodeHierarchyInterface.cpp	(revision 174)
@@ -45,5 +45,5 @@
 	{
 		sceneNode->setLastRendered(sceneNode->lastVisited());
-		mRenderedNodes.push_back(node);
+		mVisibleNodes.push_back(node);
 
 		mSceneManager->_renderSceneNode(mCamera, sceneNode, mLeavePassesInQueue);
Index: trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 166)
+++ trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 174)
@@ -14,6 +14,6 @@
 
 //-----------------------------------------------------------------------
-VisibilityTerrainSceneManager::VisibilityTerrainSceneManager(GtpVisibility::
-															 VisibilityManager *visManager): 
+VisibilityTerrainSceneManager::VisibilityTerrainSceneManager(
+							GtpVisibility::VisibilityManager *visManager): 
 mVisibilityManager(visManager), 
 mShowVisualization(false),
@@ -752,6 +752,4 @@
 	{
 		ent->getSubEntity(i)->setId(mCurrentEntityId);
-		//ent->getSubEntity(i)->setId((41 << 16) + (4 << 8) + 60);
-		//ent->getSubEntity(i)->setId((2 << 16) + (4 << 8) + 60);
 	}
 
Index: trunk/VUT/Ogre/src/OgreOctreeHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreOctreeHierarchyInterface.cpp	(revision 159)
+++ trunk/VUT/Ogre/src/OgreOctreeHierarchyInterface.cpp	(revision 174)
@@ -103,5 +103,5 @@
 			octant, mOnlyShadowCasters, mLeavePassesInQueue);
 
-		mRenderedNodes.push_back(node);
+		mVisibleNodes.push_back(node);
 	}
 #endif
@@ -167,3 +167,4 @@
 	}
 }
+
 } // namespace Ogre
Index: trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 164)
+++ trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 174)
@@ -737,21 +737,4 @@
 
     }// for each priority
-}
-*/
-/*//-----------------------------------------------------------------------
-Entity* VisibilityTerrainSceneManager::createEntity(const String& entityName, 
-													const String& meshName)
-{
-	Entity *ent = SceneManager::createEntity(entityName, meshName);
-
-	for (int i = 0; i < (int)ent->getNumSubEntities(); ++i)
-	{
-		ent->getSubEntity(i)->setId(mCurrentEntityId);
-	}
-
-	// increase counter of entity id values
-	++ mCurrentEntityId;
-
-	return ent;
 }*/
 }   // namespace Ogre
Index: trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 159)
+++ trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 174)
@@ -201,4 +201,20 @@
 //-----------------------------------------------------------------------
 GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueOcclusionQuery(
+	GtpVisibility::Patch *patch)
+{
+	// get next available test id
+	GtpVisibility::OcclusionQuery *query = GetNextOcclusionQuery();
+
+	//-- the actual query test
+	query->BeginQuery();
+	
+	RenderPatch(patch);
+
+	query->EndQuery();
+
+	return query;
+}
+//-----------------------------------------------------------------------
+GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueOcclusionQuery(
 	GtpVisibility::Mesh *mesh)
 {
@@ -269,4 +285,11 @@
 }
 //-----------------------------------------------------------------------
+void PlatformHierarchyInterface::RenderPatch(GtpVisibility::Patch *patch)
+{
+	Pass *pass = patch->getTechnique()->getPass(0);
+	mSceneManager->setPass(pass);
+	mSceneManager->renderSingleObject(patch, pass, false);
+}
+//-----------------------------------------------------------------------
 SceneManager *PlatformHierarchyInterface::GetSceneManager()
 {
@@ -278,3 +301,4 @@
 	return mRenderSystem;
 }
+
 } // namespace Ogre
Index: trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp	(revision 173)
+++ trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp	(revision 174)
@@ -12,9 +12,8 @@
 //----------------------------------------------------------------------- 
 PlatformQueryManager::PlatformQueryManager(PlatformHierarchyInterface *hierarchyInterface, 
-										   Viewport *vp, bool renderPatches):
-QueryManager(hierarchyInterface), 
+										   Viewport *vp, int queryModes):
+QueryManager(hierarchyInterface, queryModes), 
 mViewport(vp), 
-mWasInitialised(false),
-mRenderPatches(renderPatches)
+mWasInitialised(false)
 {
 }
@@ -147,7 +146,4 @@
 	uchar *buf = mViewport->getTarget()->getBufferContents(dimx, dimy);
 
-	int n = mRenderPatches ? 
-		(int)visiblePatches->size() : (int)visibleGeometry->size();
-
 	//std::stringstream d; d << "dimx: " << dimx << ", dimy: " << dimy; LogManager::getSingleton().logMessage(d.str());
 
@@ -163,11 +159,14 @@
 
 		// if valid id <= add visibility (id values start at 1)
-		if ((id > 0) && (id < n))
-		{
-			if (mRenderPatches)
-			{	
+		if (mQueryModes == PATCH_VISIBILITY)
+		{	
+			if ((id > 0) && (id < (int)visiblePatches->size()))
+			{
 				((*visiblePatches)[id]).AddVisibility(1, 0);
 			}
-			else
+		}
+		else if (mQueryModes == GEOMETRY_VISIBILITY)
+		{
+			if ((id > 0) && (id < (int)visibleGeometry->size()))
 			{
 				((*visibleGeometry)[id]).AddVisibility(1, 0);
@@ -213,5 +212,5 @@
 			SubEntity *subEnt = ent->getSubEntity(i);
 
-			if (mRenderPatches)
+			if (mQueryModes == PATCH_VISIBILITY)
 			{
 				++ id;
@@ -223,5 +222,5 @@
 		}
 			
-		if (!mRenderPatches)
+		if (mQueryModes == GEOMETRY_VISIBILITY)
 		{
 			visibleGeometry->push_back(GtpVisibility::MeshInfo(ent, 0, 0));
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
