Index: trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp	(revision 164)
+++ trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp	(revision 171)
@@ -5,10 +5,16 @@
 #include <OgreStringConverter.h>
 #include <vector>
+#include <OgreSubEntity.h>
 
 
 namespace Ogre {
+
 //----------------------------------------------------------------------- 
-PlatformQueryManager::PlatformQueryManager(PlatformHierarchyInterface *hierarchyInterface, Viewport *vp):
-QueryManager(hierarchyInterface), mViewport(vp), mWasInitialised(false)
+PlatformQueryManager::PlatformQueryManager(PlatformHierarchyInterface *hierarchyInterface, 
+										   Viewport *vp, bool renderPatches):
+QueryManager(hierarchyInterface), 
+mViewport(vp), 
+mWasInitialised(false),
+mRenderPatches(false)
 {
 }
@@ -65,6 +71,6 @@
 		}
 
-		ComputeCameraVisibility(*cam, visibleNodes, visibleGeometry, visiblePatches, relativeVisibility);
-		
+		ComputeCameraVisibility(*cam, visibleNodes, visibleGeometry, visiblePatches, 
+			                    relativeVisibility);
 		//mViewport->getTarget()->update(); for(int j=0; j<10000000; j++)	printf("wait");
 
@@ -80,4 +86,144 @@
 }
 //-----------------------------------------------------------------------
+void PlatformQueryManager::ComputeCameraVisibility(const Camera &camera,
+			    InfoContainer<GtpVisibility::NodeInfo> *visibleNodes,
+			    InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry,
+				InfoContainer<GtpVisibility::PatchInfo> *visiblePatches,
+			    bool relativeVisibility)
+{
+	// we need access to the scene manager and the rendersystem
+	PlatformHierarchyInterface *pfHierarchyInterface = 
+		dynamic_cast<PlatformHierarchyInterface *>(mHierarchyInterface);
+
+	SceneManager *sm = pfHierarchyInterface->GetSceneManager();
+
+
+	// const_cast allowed because camera is not changed in renderScene
+	Camera *pCam = const_cast<Camera *>(&camera); 
+
+	// disable overlays, reset them later
+	bool overlayEnabled = mViewport->getOverlaysEnabled();
+	mViewport->setOverlaysEnabled(false);
+	
+	// clear background with black (i.e., not a valid item id)
+	ColourValue bg = mViewport->getBackgroundColour();
+	mViewport->setBackgroundColour(ColourValue(0, 0, 0, 0));
+	//pfHierarchyInterface->GetRenderSystem()->clearFrameBuffer(FBT_COLOUR | FBT_DEPTH);
+
+	// initialise item buffer (if not already initialised)
+	InitItemBuffer(visibleGeometry, visiblePatches);
+
+	//-- render scene with item buffer (i.e., objects with their id as color codes)
+
+	// enable item buffer (must be provided by scene manager)
+	bool useItemBuffer = true;
+	sm->setOption("UseItemBuffer", &useItemBuffer);
+
+
+	sm->_renderScene(pCam, mViewport, false); // render item buffer
+   
+
+	//-- collect results
+
+	int dimx = 0;
+	int dimy = 0;
+
+	// copy frame buffer 
+	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());
+
+	// loop through frame buffer and collect visible pixels
+	for (int idx = 0; idx < dimy * dimx * 3; idx += 3)
+	{
+		//-- decode color code to receive id
+		int id = buf[idx] << 16;
+		id += buf[idx + 1] << 8;
+		id += buf[idx + 2];
+
+		//std::stringstream d; d << "myid: " << (int)buf[idx] << " " << (int)buf[idx + 1] << " " << (int)buf[idx + 2]; LogManager::getSingleton().logMessage(d.str());
+
+		// if valid id <= add visibility (id values start at 1)
+		if ((id > 0) && (id < n))
+		{
+			if (mRenderPatches)
+			{
+				((*visiblePatches)[id]).AddVisibility(1, 0);
+			}
+			else
+			{
+				((*visibleGeometry)[id]).AddVisibility(1, 0);
+			}
+		}
+	}
+
+
+	//-- 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
+}
+//-----------------------------------------------------------------------
+void PlatformQueryManager::InitItemBuffer(
+				InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry,
+				InfoContainer<GtpVisibility::PatchInfo> *visiblePatches)
+{
+	if (mWasInitialised)
+		return;
+
+	mWasInitialised = true;
+
+	SceneManager *sm = 
+		dynamic_cast<PlatformHierarchyInterface *>(mHierarchyInterface)->GetSceneManager();
+
+	SceneManager::EntityIterator it = sm->getEntityIterator();
+
+	// TODO: make the function more efficient
+
+	visibleGeometry->clear();
+	visiblePatches->clear();
+
+	int id = 0;
+
+	/* We can either use patches or meshes. If patches are used, an unique id must
+	   be given each patch. Otherwise the same id must be given to all patches belonging
+	   to the same mesh.
+    */
+	while (it.hasMoreElements())
+	{
+		Entity *ent = it.getNext();
+
+		for (int i = 0; i < (int)ent->getNumSubEntities(); ++i)
+		{
+			SubEntity *subEnt = ent->getSubEntity(i);
+
+			if (mRenderPatches)
+			{
+				++ id;
+				visiblePatches->push_back(GtpVisibility::PatchInfo(subEnt, 0, 0));
+			}
+				
+			subEnt->setId(id);
+			//subEnt->setId((41 << 16) + (4 << 8) + 60);
+			//subEnt->setId((2 << 16) + (4 << 8) + 60);
+		}
+			
+		if (!mRenderPatches)
+		{
+			visibleGeometry->push_back(GtpVisibility::MeshInfo(ent, 0, 0));
+			++ id;
+		}
+	}
+}
+//-----------------------------------------------------------------------
 void PlatformQueryManager::SetViewport(Viewport *vp)
 {
Index: trunk/VUT/Ogre/src/OgreItemBufferQueryManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreItemBufferQueryManager.cpp	(revision 166)
+++ trunk/VUT/Ogre/src/OgreItemBufferQueryManager.cpp	(revision 171)
@@ -15,12 +15,4 @@
 }
 //-----------------------------------------------------------------------
-bool ItemBufferQueryManager::ShootRay(const Ray &ray, 
-									  std::vector<Mesh *> *visibleMeshes, 
-									  bool isGlobalLine)
-{
-    // run OGRE ray shooting query
-    return false;
-}
-//-----------------------------------------------------------------------
 void ItemBufferQueryManager::ComputeCameraVisibility(const Camera &camera,
 			    InfoContainer<GtpVisibility::NodeInfo> *visibleNodes,
@@ -29,7 +21,4 @@
 			    bool relativeVisibility)
 {
-	// initialise item buffer (if not already initialised)
-	InitItemBuffer(visibleNodes, visibleGeometry, visiblePatches);
-
 	// we need access to the scene manager and the rendersystem
 	PlatformHierarchyInterface *pfHierarchyInterface = 
@@ -38,5 +27,4 @@
 	SceneManager *sm = pfHierarchyInterface->GetSceneManager();
 
-	// --- Render scene with item buffer (i.e., objects with their id as color codes)
 
 	// const_cast allowed because camera is not changed in renderScene
@@ -46,34 +34,36 @@
 	bool overlayEnabled = mViewport->getOverlaysEnabled();
 	mViewport->setOverlaysEnabled(false);
-
-	// set item buffer (must be provided by scene manager)
-	bool useItemBuffer = true;
-	sm->setOption("UseItemBuffer", &useItemBuffer);
 	
 	// clear background with black (i.e., not a valid item id)
 	ColourValue bg = mViewport->getBackgroundColour();
 	mViewport->setBackgroundColour(ColourValue(0, 0, 0, 0));
-	pfHierarchyInterface->GetRenderSystem()->clearFrameBuffer(FBT_COLOUR | FBT_DEPTH);
-
-	//-- render item buffer
-	sm->_renderScene(pCam, mViewport, false);
-    //mViewport->getTarget()->update();
-
-//for(int j=0; j<10000000; j++)	printf("wait");
+	//pfHierarchyInterface->GetRenderSystem()->clearFrameBuffer(FBT_COLOUR | FBT_DEPTH);
 
 
-	//---- collect results
+	//-- render scene with item buffer (i.e., objects with their id as color codes)
+
+	// enable item buffer (must be provided by scene manager)
+	bool useItemBuffer = true;
+	sm->setOption("UseItemBuffer", &useItemBuffer);
+	sm->_renderScene(pCam, mViewport, false); // render item buffer
+   
+
+	//-- collect results
+	
+	// initialise item buffer (if not already initialised)
+	InitItemBuffer(visibleNodes, visibleGeometry, visiblePatches);
+
 	int dimx = 0;
 	int dimy = 0;
 
-	// get frame buffer
+	// copy frame buffer 
 	uchar *buf = mViewport->getTarget()->getBufferContents(dimx, dimy);
 
-	int n = mRenderPatchesForItemBuffer ? (int)visiblePatches->size() : (int)visibleGeometry->size();
+	int n = mRenderPatchesForItemBuffer ? 
+		(int)visiblePatches->size() : (int)visibleGeometry->size();
 
-	std::stringstream d; d << "dimx: " << dimx << ", dimy: " << dimy;
-	LogManager::getSingleton().logMessage(d.str());
+	//std::stringstream d; d << "dimx: " << dimx << ", dimy: " << dimy; LogManager::getSingleton().logMessage(d.str());
 
-	// loop through frame buffer & collect visible pixels
+	// loop through frame buffer and collect visible pixels
 	for (int idx = 0; idx < dimy * dimx * 3; idx += 3)
 	{
@@ -83,6 +73,5 @@
 		id += buf[idx + 2];
 
-		//std::stringstream d; d << "myid: " << (int)buf[idx] << " " << (int)buf[idx + 1] << " " << (int)buf[idx + 2];
-		//LogManager::getSingleton().logMessage(d.str());
+		//std::stringstream d; d << "myid: " << (int)buf[idx] << " " << (int)buf[idx + 1] << " " << (int)buf[idx + 2]; LogManager::getSingleton().logMessage(d.str());
 
 		// if valid id <= add visibility (id values start at 1)
@@ -100,18 +89,15 @@
 	}
 
+
 	//-- reset options
 
-	// don't need item buffer anymore
-	useItemBuffer = false;
+	useItemBuffer = false; // don't need item buffer anymore
 	sm->setOption("UseItemBuffer", &useItemBuffer);
-	// reset initialised - flag
-	mWasInitialised = false;
-	// reset old overlay status
-	mViewport->setOverlaysEnabled(overlayEnabled);
-	// reset background color
-	mViewport->setBackgroundColour(bg);
 
-	// delete array storing the frame buffer
-	delete [] buf;
+	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
 }
 //-----------------------------------------------------------------------
Index: trunk/VUT/Ogre/src/OgreOcclusionQueriesQueryManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreOcclusionQueriesQueryManager.cpp	(revision 159)
+++ trunk/VUT/Ogre/src/OgreOcclusionQueriesQueryManager.cpp	(revision 171)
@@ -3,17 +3,44 @@
 #include <OgreStringConverter.h>
 #include <vector>
+#include <OgreSubEntity.h>
 
 
 namespace Ogre {
+
+/**
+   These functions necessary to enable sorting of node / mesh / patch info.
+*/
+//-----------------------------------------------------------------------
+inline bool nodeinfo_eq(const GtpVisibility::NodeInfo &info1, const GtpVisibility::NodeInfo &info2) 
+{ 
+	return info1.GetNode() == info2.GetNode(); 
+}
+//-----------------------------------------------------------------------
+inline bool meshinfo_eq(const GtpVisibility::MeshInfo &info1, const GtpVisibility::MeshInfo &info2) 
+{ 
+	return info1.GetMesh() == info2.GetMesh(); 
+}
+//-----------------------------------------------------------------------
+inline bool nodeinfo_lower(const GtpVisibility::NodeInfo &info1, const GtpVisibility::NodeInfo &info2) 
+{ 
+	return info1.GetNode() < info2.GetNode(); 
+}
+//-----------------------------------------------------------------------
+inline bool meshinfo_lower(const GtpVisibility::MeshInfo &info1, const GtpVisibility::MeshInfo &info2) 
+{ 
+	return info1.GetMesh() < info2.GetMesh(); 
+}
+
+
+
 //----------------------------------------------------------------------- 
-OcclusionQueriesQueryManager::OcclusionQueriesQueryManager(PlatformHierarchyInterface *hierarchyInterface, Viewport *vp):
-PlatformQueryManager(hierarchyInterface, vp)
-{
-}
-//-----------------------------------------------------------------------
-bool OcclusionQueriesQueryManager::ShootRay(const Ray &ray, std::vector<Mesh *> *visibleMeshes, bool isGlobalLine)
-{
-    // run OGRE ray shooting query
-    return false;
+OcclusionQueriesQueryManager::OcclusionQueriesQueryManager(
+							PlatformHierarchyInterface *hierarchyInterface, 
+							Viewport *vp,
+							bool renderPatches,
+							bool useItemBufferForGeometry):
+PlatformQueryManager(hierarchyInterface, vp, renderPatches),
+mUseItemBuffer(useItemBufferForGeometry)
+{
 }
 //-----------------------------------------------------------------------
@@ -24,22 +51,31 @@
 			    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);
 
-	//-- Render scene to get conservative visibility and fill depth buffer 
-
-	// const_cast allowed because camera is not changed in renderScene
-	Camera *pCam = const_cast<Camera *>(&camera); 
-
 	// disable overlays, reset them later
 	bool overlayEnabled = mViewport->getOverlaysEnabled();
-	mViewport->setOverlaysEnabled(false);
-
-	//-- render the scene once to update depth buffer
-	pfHierarchyInterface->GetSceneManager()->_renderScene(pCam, mViewport, false);
-
-	/* 
-		Two query lists for projected pixels and for visibile pixels:
+
+	//-- render scene with item buffer (i.e., objects with their id as color codes)
+	if (mUseItemBuffer)
+	{
+		PlatformQueryManager::ComputeCameraVisibility(camera, visibleNodes,
+			    visibleGeometry, visiblePatches, relativeVisibility);
+
+		mViewport->setOverlaysEnabled(false);
+	}
+	else
+	{
+   		// const_cast allowed because camera is not changed in renderScene
+		Camera *pCam = const_cast<Camera *>(&camera); 
+		mViewport->setOverlaysEnabled(false);
+
+		//-- Render scene to get conservative visibility and fill depth buffer
+		pfHierarchyInterface->GetSceneManager()->_renderScene(pCam, mViewport, false);
+	}
+
+	
+	/* 	Two query lists for projected pixels and for visibile pixels:
 		We test queries after a rendering pass 
 		to get exact visibility with regard to the current camera.
@@ -48,30 +84,39 @@
 	GtpVisibility::QueryList queryList[2];
 	
-	// get rendered hierarchy nodes
+	// rendered hierarchy nodes from previous render
 	GtpVisibility::HierarchyNodeList *nodeList = mHierarchyInterface->GetRenderedNodes();
+	GtpVisibility::HierarchyNodeList::iterator nodeIt, nodeIt_end = nodeList->end();
+
 	// vector for storing entities of meshes
 	GtpVisibility::GeometryList geometryList;
 	
-	GtpVisibility::HierarchyNodeList::iterator nodeIt, nodeIt_end = nodeList->end();
-	// geometry list has still do be built
-	GtpVisibility::GeometryList::iterator geometryIt, geometryIt_end;
+	// 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);
+	}
+
+	// geometry list iterator
+	GtpVisibility::GeometryList::iterator geometryIt, geometryIt_end = geometryList.end();
+
 
 	// to obtain the correct number of projected pixels, depth write must be disabled
 	bool enableDepthWrite = false;
-
 	// this option must be provided by the scene manager
 	pfHierarchyInterface->GetSceneManager()->setOption("DepthWrite", &enableDepthWrite);
 
+
 	/* relative visiblity:
-		1) get visible pixels count of objects
-		2) clear frame buffer
-		3) get projected visible pixels count:
-		   test all objects again without depth write (set as option in scene manager)
-		4) calculate ratio between visible vs. projected pixels
+	   1) get visible pixels count of objects
+	   2) clear frame buffer
+	   3) get projected visible pixels count:
+	      test all objects again without depth write (set as option in scene manager)
+	   4) calculate ratio between visible vs. projected pixels
 	*/
+		
 	// for relative visibility we need 2 rendering passes
 	int n = relativeVisibility ? 2 : 1;
 	
-	for (int i=0; i<n; ++i)
+	for (int i = 0; i < n; ++i)
 	{
 		//-- queries for hierarchy nodes
@@ -80,24 +125,41 @@
 			// TODO: DELETE QUERIES FROM PREVIOUS RENDER
 			queryList[i].push_back(mHierarchyInterface->IssueOcclusionQuery(*nodeIt, false));
-			
-			// store geometry of the hierarchy node in a geometry list (only once!)
-			if (i == 0)
+		}
+
+		//-- queries for geometry: if item buffer, capture only projected visibility
+		if (!(mUseItemBuffer && (i == 0)))
+		{
+			for (geometryIt = geometryList.begin(); geometryIt != geometryIt_end; ++geometryIt)
 			{
-				mHierarchyInterface->GetNodeGeometryList(*nodeIt, &geometryList, false);
+				queryList[i].push_back(mHierarchyInterface->IssueOcclusionQuery(*geometryIt));
 			}
 		}
 
-		geometryIt_end = geometryList.end();
-
-		//-- add queries for geometry
-		for (geometryIt = geometryList.begin(); geometryIt != geometryIt_end; ++geometryIt)
-		{
-			queryList[i].push_back(mHierarchyInterface->IssueOcclusionQuery(*geometryIt));
-		}
-
-	
 		pfHierarchyInterface->GetRenderSystem()->clearFrameBuffer(FBT_DEPTH);
 	}
 
+	
+	//-- collect results
+	GtpVisibility::QueryList::iterator visQueryIt = queryList[0].begin();
+	GtpVisibility::QueryList::iterator projQueryIt = queryList[1].begin();
+	
+	CollectNodeVisibility(visQueryIt, projQueryIt, nodeList, visibleNodes, relativeVisibility);
+
+	if (mUseItemBuffer)
+	{
+		if (relativeVisibility)
+		{
+			CollectRelativeGeometryVisibilityForItemBuffer(projQueryIt, &geometryList, visibleGeometry);
+		}
+    }
+	else
+	{
+		CollectGeometryVisibility(visQueryIt, projQueryIt, &geometryList, 
+			visibleGeometry, relativeVisibility);
+	    //CollectPatchVisibility(patchList, visiblePatches);
+	}
+
+
+	//-- reset options
 	enableDepthWrite = true;
 	// this option must be provided by the scene manager
@@ -106,12 +168,37 @@
 	mViewport->setOverlaysEnabled(overlayEnabled);
 
-	// ---- collect results
-	GtpVisibility::QueryList::iterator visQueryIt, projQueryIt;
-
-	visQueryIt = queryList[0].begin();
-	projQueryIt = queryList[1].begin();
-
-	
-	for (nodeIt = nodeList->begin(); nodeIt != nodeIt_end; ++nodeIt)
+	mWasInitialised = false;
+}
+//-----------------------------------------------------------------------
+void OcclusionQueriesQueryManager::ComputeFromPointVisibility(const Vector3 &point,
+			       InfoContainer<GtpVisibility::NodeInfo> *visibleNodes,
+			       InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry,
+				   InfoContainer<GtpVisibility::PatchInfo> *visiblePatches,
+			       bool relativeVisibility)
+{
+	PlatformQueryManager::ComputeFromPointVisibility(point, visibleNodes, 
+		visibleGeometry, visiblePatches, relativeVisibility);
+
+	// remove duplicates (duplicates occur if an object is on the edge of the viewport)
+	// before duplicates can be deleted we have to add up their visibility
+	RemoveDuplicateNodes(visibleNodes);
+	
+	if (!mUseItemBuffer)
+	{
+		RemoveDuplicateGeometry(visibleGeometry);
+	}
+}
+//-----------------------------------------------------------------------
+void  OcclusionQueriesQueryManager::CollectNodeVisibility(
+						GtpVisibility::QueryList::iterator &visQueryIt, 
+						GtpVisibility::QueryList::iterator &projQueryIt,
+						GtpVisibility::HierarchyNodeList *nodeList,  
+						InfoContainer<GtpVisibility::NodeInfo> *visibleNodes,
+						bool relativeVisibility)
+{
+	GtpVisibility::HierarchyNodeList::iterator nodeIt;
+
+	//-- queries for nodes
+	for (nodeIt = nodeList->begin(); nodeIt != nodeList->end(); ++nodeIt)
 	{
 		unsigned int visiblePixels = 0;
@@ -132,12 +219,69 @@
 		if (visiblePixels > 0)
 		{
-			visibleNodes->push_back(GtpVisibility::NodeInfo(*nodeIt, visiblePixels, projectedPixels));
-		}
-	}
-
-	// ---- queries for geometry
-	geometryIt_end = geometryList.end();
-	
-	for (geometryIt = geometryList.begin(); geometryIt != geometryIt_end; ++geometryIt)
+			visibleNodes->push_back(GtpVisibility::NodeInfo(*nodeIt, visiblePixels, 
+				projectedPixels));
+		}
+	}
+}
+//-----------------------------------------------------------------------
+void OcclusionQueriesQueryManager::CollectRelativeGeometryVisibilityForItemBuffer(
+									GtpVisibility::QueryList::iterator &projQueryIt,
+									GtpVisibility::GeometryList *geometryList, 
+									InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry)
+{
+	GtpVisibility::GeometryList::iterator geometryIt;
+
+	//-- queries for geometry
+	for (geometryIt = geometryList->begin(); geometryIt != geometryList->end(); ++ geometryIt)
+	{
+		unsigned int projectedPixels = 0;
+
+		(*projQueryIt)->GetQueryResult(projectedPixels, true);
+
+		++projQueryIt;
+		int id = (*geometryIt)->getSubEntity(0)->getId();
+
+		if ((id > 0) && (id < (int)visibleGeometry->size()))
+		{
+			(*visibleGeometry)[id].AddVisibility(0, projectedPixels);
+		}
+	}
+}
+//-----------------------------------------------------------------------
+void OcclusionQueriesQueryManager::CollectRelativePatchVisibilityForItemBuffer(
+									GtpVisibility::QueryList::iterator &projQueryIt,
+									GtpVisibility::PatchList *patchList, 
+									InfoContainer<GtpVisibility::PatchInfo> *visiblePatches)
+{
+	GtpVisibility::PatchList::iterator patchIt;
+
+	//-- queries for geometry
+	for (patchIt = patchList->begin(); patchIt != patchList->end(); ++ patchIt)
+	{
+		unsigned int projectedPixels = 0;
+
+		(*projQueryIt)->GetQueryResult(projectedPixels, true);
+
+		++projQueryIt;
+		int id = (*patchIt)->getId();
+
+		if ((id > 0) && (id < (int)visiblePatches->size()))
+		{
+			(*visiblePatches)[id].AddVisibility(0, projectedPixels);
+		}
+	}
+}
+//-----------------------------------------------------------------------
+void  OcclusionQueriesQueryManager::CollectGeometryVisibility(
+							GtpVisibility::QueryList::iterator &visQueryIt, 
+							GtpVisibility::QueryList::iterator &projQueryIt,
+							GtpVisibility::GeometryList *geometryList, 
+							InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry,
+							bool relativeVisibility)
+{
+	GtpVisibility::GeometryList::iterator geometryIt;
+
+	//-- queries for geometry
+	for (geometryIt = geometryList->begin(); geometryIt != geometryList->end(); ++ geometryIt)
 	{
 		unsigned int visiblePixels = 0;
@@ -149,9 +293,8 @@
 		{
 			(*projQueryIt)->GetQueryResult(projectedPixels, true);
-
-			++projQueryIt;
-		}
-
-		++visQueryIt;
+			++ projQueryIt;
+		}
+
+		++ visQueryIt;
 
 		// WARNING: approximate depth ordering during rendering => 
@@ -163,41 +306,44 @@
 		}
 	}
-
-}
-//-----------------------------------------------------------------------
-inline bool nodeinfo_eq(const GtpVisibility::NodeInfo &info1, const GtpVisibility::NodeInfo &info2) 
-{ 
-	return info1.GetNode() == info2.GetNode(); 
-}
-//-----------------------------------------------------------------------
-inline bool meshinfo_eq(const GtpVisibility::MeshInfo &info1, const GtpVisibility::MeshInfo &info2) 
-{ 
-	return info1.GetMesh() == info2.GetMesh(); 
-}
-//-----------------------------------------------------------------------
-inline bool nodeinfo_lower(const GtpVisibility::NodeInfo &info1, const GtpVisibility::NodeInfo &info2) 
-{ 
-	return info1.GetNode() < info2.GetNode(); 
-}
-//-----------------------------------------------------------------------
-inline bool meshinfo_lower(const GtpVisibility::MeshInfo &info1, const GtpVisibility::MeshInfo &info2) 
-{ 
-	return info1.GetMesh() < info2.GetMesh(); 
-}
-//-----------------------------------------------------------------------
-void OcclusionQueriesQueryManager::ComputeFromPointVisibility(const Vector3 &point,
-			       InfoContainer<GtpVisibility::NodeInfo> *visibleNodes,
-			       InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry,
-				   InfoContainer<GtpVisibility::PatchInfo> *visiblePatches,
-			       bool relativeVisibility)
-{
-	PlatformQueryManager::ComputeFromPointVisibility(point, visibleNodes, 
-		visibleGeometry, visiblePatches, relativeVisibility);
-
-	// --- remove duplicates (duplicates occur if an object is on the edge of the viewport)
-
-	// before duplicates can be deleted we have to add up their visibility
-
-	// --- visible nodes
+}
+//-----------------------------------------------------------------------
+void  OcclusionQueriesQueryManager::CollectPatchVisibility(
+							GtpVisibility::QueryList::iterator &visQueryIt, 
+							GtpVisibility::QueryList::iterator &projQueryIt,
+							GtpVisibility::PatchList *patchList, 
+							InfoContainer<GtpVisibility::PatchInfo> *visiblePatches,
+							bool relativeVisibility)
+{
+	GtpVisibility::PatchList::iterator patchIt;
+
+	//-- queries for patch
+	for (patchIt = patchList->begin(); patchIt != patchList->end(); ++ patchIt)
+	{
+		unsigned int visiblePixels = 0;
+		(*visQueryIt)->GetQueryResult(visiblePixels, true);
+		
+		unsigned int projectedPixels = 0;
+
+		if (relativeVisibility)
+		{
+			(*projQueryIt)->GetQueryResult(projectedPixels, true);
+			++ projQueryIt;
+		}
+
+		++ visQueryIt;
+
+		// WARNING: approximate depth ordering during rendering => 
+		// patch maybe occluded
+		if (visiblePixels > 0)
+		{			 
+			visiblePatches->push_back(GtpVisibility::PatchInfo(*patchIt, visiblePixels, 
+				projectedPixels));
+		}
+	}
+}
+//-----------------------------------------------------------------------
+void  OcclusionQueriesQueryManager::RemoveDuplicateNodes(
+					InfoContainer<GtpVisibility::NodeInfo> *visibleNodes)
+{
 	sort(visibleNodes->begin(), visibleNodes->end(), nodeinfo_lower);
 	InfoContainer<GtpVisibility::NodeInfo>::iterator visibleNodesIt,
@@ -217,5 +363,4 @@
 			nodeInfo->AddVisibility(*visibleNodesIt);
 		}
-
 	}
 	
@@ -223,13 +368,16 @@
 	visibleNodes->erase( std::unique(visibleNodes->begin(), 
 		visibleNodes->end(), nodeinfo_eq), visibleNodes->end());
-
-	// --- visible geometry
+}
+//-----------------------------------------------------------------------
+void  OcclusionQueriesQueryManager::RemoveDuplicateGeometry(
+				InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry)
+{
 	sort(visibleGeometry->begin(), visibleGeometry->end(), meshinfo_lower);
+	
 	InfoContainer<GtpVisibility::MeshInfo>::iterator visibleGeomIt,
-		visibleGeomIt_end = visibleGeometry->end();
+			visibleGeomIt_end = visibleGeometry->end();
 
 	GtpVisibility::MeshInfo *geomInfo = NULL;
 
-	int i=0;
 	for (visibleGeomIt = visibleGeometry->begin(); visibleGeomIt != visibleGeomIt_end; 
 		++visibleGeomIt)
