Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreOcclusionCullingSceneManager.h
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreOcclusionCullingSceneManager.h	(revision 2164)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreOcclusionCullingSceneManager.h	(revision 2171)
@@ -124,4 +124,6 @@
 	bool LoadViewCells(const String &filename);
 
+	void RenderDepthForQuery(Camera* camera, Viewport* vp);
+
 protected:
 	
@@ -130,4 +132,5 @@
 	void SetObjectVisible(GtpVisibilityPreprocessor::Intersectable *entry, 
 						  const bool visible);
+
 	void ShowViewCellsGeometry();
 
@@ -180,7 +183,12 @@
 	void loadConfig(DataStreamPtr& stream);
 
-	void RenderDepthPass();
+	void RenderDepthPass(const bool fillRenderQueue = true);
 
 	void _renderScene(Camera* camera, Viewport* vp, bool includeOverlays);
+
+	int FindExactVisibleObjects(Camera *camera,
+								Viewport* vp,
+								const bool fromPoint,
+								const bool nodeVisibility);	
 
 	//////////////////////////////////////////
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreOcclusionQueriesQueryManager.h
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreOcclusionQueriesQueryManager.h	(revision 925)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreOcclusionQueriesQueryManager.h	(revision 2171)
@@ -19,8 +19,8 @@
     
     void ComputeCameraVisibility(const Camera &camera,
-			    GtpVisibility::NodeInfoContainer *visibleNodes,
-			    GtpVisibility::MeshInfoContainer *visibleGeometry,
-				GtpVisibility::PatchInfoContainer *visiblePatches,
-			    bool relativeVisibility = false);
+								 GtpVisibility::NodeInfoContainer *visibleNodes,
+								 GtpVisibility::MeshInfoContainer *visibleGeometry,
+								 GtpVisibility::PatchInfoContainer *visiblePatches,
+								 bool relativeVisibility = false);
     
     /** 
@@ -29,8 +29,8 @@
     */
     void ComputeFromPointVisibility(const Vector3 &point,
-			       GtpVisibility::NodeInfoContainer *visibleNodes,
-			       GtpVisibility::MeshInfoContainer *visibleGeometry,
-				   GtpVisibility::PatchInfoContainer *visiblePatches,
-			       bool relativeVisibility = false);
+									GtpVisibility::NodeInfoContainer *visibleNodes,
+									GtpVisibility::MeshInfoContainer *visibleGeometry,
+									GtpVisibility::PatchInfoContainer *visiblePatches,
+									bool relativeVisibility = false);
     
 
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreVisibilityOptionsManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreVisibilityOptionsManager.cpp	(revision 1486)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreVisibilityOptionsManager.cpp	(revision 2171)
@@ -16,7 +16,8 @@
 	{
 		// delete old queries (not needed for e.g., view frustum culling)
-		// note cannot be deleted because of new ogre occlusion query implementation
-		// there we cannot just delete the queries, so they would stay!
-		//mHierarchyInterface->ResetQueries();
+		// note cannot be deleted because of new ogre occlusion query 
+		// implementation there we cannot just delete the queries, so 
+		// they would stay!
+		// mHierarchyInterface->ResetQueries();
 		mVisibilityManager->SetCullingManager(*static_cast<const 
 			GtpVisibility::VisibilityEnvironment::CullingManagerType *>(val));
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgrePlatformQueryManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgrePlatformQueryManager.cpp	(revision 2066)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgrePlatformQueryManager.cpp	(revision 2171)
@@ -27,13 +27,14 @@
 }
 //-----------------------------------------------------------------------
-void PlatformQueryManager::ComputeFromPointVisibility(const Vector3 &point,
-			       GtpVisibility::NodeInfoContainer *visibleNodes,
-			       GtpVisibility::MeshInfoContainer *visibleGeometry,
-				   GtpVisibility::PatchInfoContainer *visiblePatches,
-			       bool relativeVisibility)
-{
-	SceneManager *sm = //static_cast<PlatformHierarchyInterface *>
+void PlatformQueryManager::ComputeFromPointVisibility(
+					const Vector3 &point,
+					GtpVisibility::NodeInfoContainer *visibleNodes,
+					GtpVisibility::MeshInfoContainer *visibleGeometry,
+					GtpVisibility::PatchInfoContainer *visiblePatches,
+					bool relativeVisibility)
+{
+	SceneManager *sm =
 		static_cast<PlatformHierarchyInterface *>
-		(mHierarchyInterface)->GetSceneManager();
+								(mHierarchyInterface)->GetSceneManager();
 
 	// create a camera for the point query
@@ -43,5 +44,8 @@
 	Camera *savedCam = mViewport->getCamera();
 	
-	// --- initialise new camera
+	
+	//////////////
+	//-- initialise new camera
+
 	mViewport->setCamera(cam);
 	cam->setPosition(point);
@@ -56,5 +60,7 @@
 	int sign = -1;
 	
-	// ---- capture visibility from all 6 directions
+	///////////////
+	//-- capture visibility from all 6 directions
+
 	for (int dir=0; dir < 6; dir++)	
 	{
@@ -62,8 +68,11 @@
 		
 		// Print camera details
-      	std::stringstream d;
-		d << "Point query camera: " + StringConverter::toString(cam->getDerivedPosition()) + 
-			" " + "O: " + StringConverter::toString(cam->getDerivedOrientation());
-		LogManager::getSingleton().logMessage(d.str());
+      	if (0)
+		{
+			std::stringstream d;
+			d << "Point query camera: " + StringConverter::toString(cam->getDerivedPosition()) + 
+				" " + "O: " + StringConverter::toString(cam->getDerivedOrientation());
+			LogManager::getSingleton().logMessage(d.str());
+		}
 
 		// prevent from initialising geometry / node array again
@@ -73,5 +82,8 @@
 		}
 
-		ComputeCameraVisibility(*cam, visibleNodes, visibleGeometry, visiblePatches, 
+		ComputeCameraVisibility(*cam, 
+								visibleNodes, 
+								visibleGeometry, 
+								visiblePatches, 
 			                    relativeVisibility);
 		//mViewport->getTarget()->update(); for(int j=0; j<10000000; j++)	printf("wait");
@@ -79,5 +91,5 @@
 		// permute directions
 		Vector3 direction(0,0,0);
-		direction[dir/2] = sign;
+		direction[dir / 2] = sign;
 
 		cam->setDirection(direction);
@@ -88,13 +100,13 @@
 }
 //-----------------------------------------------------------------------
-void PlatformQueryManager::ComputeCameraVisibility(const Camera &camera,
-			    GtpVisibility::NodeInfoContainer *visibleNodes,
-			    GtpVisibility::MeshInfoContainer *visibleGeometry,
-				GtpVisibility::PatchInfoContainer *visiblePatches,
-			    bool relativeVisibility)
+void PlatformQueryManager::ComputeCameraVisibility(
+								const Camera &camera,
+								GtpVisibility::NodeInfoContainer *visibleNodes,
+								GtpVisibility::MeshInfoContainer *visibleGeometry,
+								GtpVisibility::PatchInfoContainer *visiblePatches,
+								bool relativeVisibility)
 {
 	// we need access to the scene manager and the rendersystem
 	PlatformHierarchyInterface *pfHierarchyInterface = 
-		//static_cast<PlatformHierarchyInterface *>(mHierarchyInterface);
 		static_cast<PlatformHierarchyInterface *>(mHierarchyInterface);
 
@@ -113,8 +125,10 @@
 	mViewport->setBackgroundColour(ColourValue(0, 0, 0, 0));
 	//pfHierarchyInterface->GetRenderSystem()->clearFrameBuffer(FBT_COLOUR | FBT_DEPTH);
+
 #ifdef ITEM_BUFFER
 	// initialise item buffer (if not already initialised)
 	InitItemBuffer(visibleGeometry, visiblePatches);
 
+	//////////
 	//-- render scene with item buffer (i.e., objects with their id as color codes)
 
@@ -127,9 +141,10 @@
    
 #ifdef ITEM_BUFFER
+	/////////
 	//-- collect results
 	CollectItemBufferResults(visibleGeometry, visiblePatches);
 
-	//-- reset options
-
+	/////////
+	//-- reset state
 	useItemBuffer = false; // don't need item buffer anymore
 	sm->setOption("UseItemBuffer", &useItemBuffer);
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/Plugin_VisibilitySceneManager.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/Plugin_VisibilitySceneManager.vcproj	(revision 2168)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/Plugin_VisibilitySceneManager.vcproj	(revision 2171)
@@ -112,5 +112,5 @@
 				AdditionalLibraryDirectories="&quot;$(OGRE_PATH)\PlugIns\OctreeSceneManager\bin\$(ConfigurationName)&quot;;&quot;$(OGRE_PATH)\OgreMain\lib\$(ConfigurationName)&quot;;&quot;$(OGRE_PATH)\Samples\Common\CEGUIRenderer\lib&quot;;&quot;$(OGRE_PATH)\Dependencies\lib\$(ConfigurationName)&quot;;&quot;..\..\..\Preprocessing\lib\$(ConfigurationName)&quot;;..\..\..\..\..\..\..\NonGTP\Xerces\xercesc\lib;..\..\..\Preprocessing\src\GL;..\..\..\..\..\..\..\NonGTP\Zlib\lib;&quot;..\..\lib\$(ConfigurationName)&quot;;&quot;..\..\..\..\..\Lib\Vis\OnlineCullingCHC\IVReader\lib\$(ConfigurationName)&quot;;&quot;..\..\..\..\..\Lib\Vis\OnlineCullingCHC\ObjReader\lib\$(ConfigurationName)&quot;"
 				ModuleDefinitionFile="..\misc\OgreVisibilitySceneManager.def"
-				GenerateDebugInformation="TRUE"
+				GenerateDebugInformation="FALSE"
 				SubSystem="2"
 				OptimizeReferences="2"
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOcclusionCullingSceneManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOcclusionCullingSceneManager.cpp	(revision 2170)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOcclusionCullingSceneManager.cpp	(revision 2171)
@@ -24,5 +24,5 @@
 #include "IVReader.h"
 #include "ObjReader.h"
-
+#include "OgreOcclusionQueriesQueryManager.h"
 
 
@@ -194,5 +194,6 @@
 	String val;
 
-	LogManager::getSingleton().logMessage("****** OcclusionCullingSceneManager Options ********");
+	LogManager::getSingleton().
+		logMessage("****** OcclusionCullingSceneManager Options ********");
 	config.load(stream);
 
@@ -717,5 +718,5 @@
 		// because shadows have low occlusion anyway
 		if ((mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE && 
-			mIlluminationStage == IRS_RENDER_TO_TEXTURE) ||
+			 mIlluminationStage == IRS_RENDER_TO_TEXTURE) ||
 			mNormalExecution)
 		{
@@ -772,5 +773,5 @@
 	{
 		// note matt: this is also called in TerrainSceneManager: really necessary?
-		mDestRenderSystem -> setLightingEnabled(false);
+		mDestRenderSystem->setLightingEnabled(false);
 
 		if (mUseItemBuffer)
@@ -1077,6 +1078,21 @@
 		* static_cast<unsigned int *>(val) = (unsigned int)algorithm;
 
-		getRenderQueue()->clear(true);
-
+		//getRenderQueue()->clear();
+
+		return true;
+	}
+	if (key == "VisibleObjects")
+	{ 
+		if (mNormalExecution)
+			return false;
+
+		const bool fromPoint = true;
+		const bool nodeVisibility = true;
+		
+		* static_cast<unsigned int *>(val) = 
+			(unsigned int)FindExactVisibleObjects(mCameraInProgress, 
+												  mCurrentViewport, 
+												  fromPoint, 
+												  nodeVisibility);
 		return true;
 	}
@@ -1818,6 +1834,12 @@
 }
 //-----------------------------------------------------------------------
-void OcclusionCullingSceneManager::RenderDepthPass()
-{
+void OcclusionCullingSceneManager::RenderDepthPass(const bool fillRenderQueue)
+{
+	if (mNormalExecution)
+	{
+		// not in the normal workflow
+		return;
+	}
+
 	// create material for depth pass
 	InitDepthPass();
@@ -1853,12 +1875,13 @@
 	mLeavePassesInQueue = 0;
 		
-	// the shaded geometry is rendered in a second pass
-	// add visible nodes found by the visibility culling algorithm
-	NodeList::const_iterator it, it_end = mVisible.end();
-
-	if(1) 
-	for (it = mVisible.begin(); it != it_end; ++ it)
-	{
-		(*it)->_addToRenderQueue(mCameraInProgress, getRenderQueue(), false);
+	if (fillRenderQueue)
+	{
+		// the shaded geometry is rendered in a second pass
+		// add visible nodes found by the visibility culling algorithm
+		NodeList::const_iterator it, it_end = mVisible.end();
+        for (it = mVisible.begin(); it != it_end; ++ it)
+		{
+			(*it)->_addToRenderQueue(mCameraInProgress, getRenderQueue(), false);
+		}
 	}
 }
@@ -1887,5 +1910,7 @@
 
 //-----------------------------------------------------------------------
-void OcclusionCullingSceneManager::_renderScene(Camera* camera, Viewport* vp, bool includeOverlays)
+void OcclusionCullingSceneManager::_renderScene(Camera* camera, 
+												Viewport* vp, 
+												bool includeOverlays)
 {
     Root::getSingleton()._setCurrentSceneManager(this);
@@ -2057,5 +2082,208 @@
     camera->_notifyRenderedFaces(mDestRenderSystem->_getFaceCount());
 }
-
+//-----------------------------------------------------------------------
+int OcclusionCullingSceneManager::FindExactVisibleObjects(Camera *camera,
+														  Viewport* vp,
+														  const bool fromPoint,
+														  const bool nodeVisibility)
+{
+	const int itemBufferMode = 0;
+	const bool relativeVisibility = false;
+
+	int queryModes = 0;
+
+	if (nodeVisibility)
+		queryModes |= GtpVisibility::QueryManager::NODE_VISIBILITY;
+	else
+		queryModes |= GtpVisibility::QueryManager::GEOMETRY_VISIBILITY;
+
+	OcclusionQueriesQueryManager *queryManager = 
+		new OcclusionQueriesQueryManager(mHierarchyInterface,
+										 vp,
+										 queryModes, 
+										 itemBufferMode);
+
+	mVisibilityManager->SetQueryManager(queryManager);
+
+	GtpVisibility::NodeInfoContainer visibleNodes;
+	GtpVisibility::MeshInfoContainer visibleGeometry;
+	GtpVisibility::PatchInfoContainer visiblePatches;
+
+	if (fromPoint)
+	{
+		queryManager->ComputeFromPointVisibility(camera->getDerivedPosition(), 
+												 &visibleNodes, 
+												 &visibleGeometry, 
+												 &visiblePatches, 
+												 relativeVisibility);
+	}
+	else
+	{
+		queryManager->ComputeCameraVisibility(*camera,
+											  &visibleNodes, 
+											  &visibleGeometry, 
+											  &visiblePatches, 
+											  relativeVisibility);
+	}
+		
+	if (0)
+	{
+		std::stringstream d; 
+		d << "Query mode: " << queryModes 
+		  << " visible nodes: " << (int)visibleNodes.size() 
+		  << " visible geometry: " << (int)visibleGeometry.size();
+
+		LogManager::getSingleton().logMessage(d.str());
+	}
+
+	///////////////////////
+	//-- put items in render queue
+	//////////
+
+	getRenderQueue()->clear();
+
+	//////////////////////
+	//-- apply queries on geometry level
+
+	if (!nodeVisibility)
+	{
+		GtpVisibility::MeshInfoContainer::iterator geomIt, geomIt_end = visibleGeometry.end();
+
+		for (geomIt = visibleGeometry.begin(); geomIt != geomIt_end; ++geomIt)
+		{
+			MovableObject *mo = (*geomIt).GetSource();
+
+			// add if not 0
+			if (!(*geomIt).GetVisiblePixels())
+				continue;
+
+			mo->_notifyCurrentCamera(camera);
+
+			if (mo->isVisible())
+			{
+				mo->_updateRenderQueue(getRenderQueue());
+			}
+		}
+	}
+	else
+	{
+		////////////////
+		//-- apply queries on node level
+
+		GtpVisibility::NodeInfoContainer::iterator nodesIt, nodesIt_end = visibleNodes.end();
+
+		for (nodesIt = visibleNodes.begin(); nodesIt != nodesIt_end; ++ nodesIt)
+		{
+			if (!(*nodesIt).GetVisiblePixels())
+				continue;
+
+			Octree *octree = static_cast<Octree *>((*nodesIt).GetSource());
+
+			NodeList::iterator nIt, nIt_end = octree->mNodes.end();
+
+			for (nIt = octree->mNodes.begin(); nIt != nIt_end; ++ nIt)
+			{
+				(*nIt)->_addToRenderQueue(camera, getRenderQueue(), false);
+			}
+		}
+	}
+
+    delete queryManager;
+
+	if (nodeVisibility)
+		return (int)visibleNodes.size();
+	else
+		return (int)visibleGeometry.size();
+}
+//-----------------------------------------------------------------------
+void OcclusionCullingSceneManager::RenderDepthForQuery(Camera* camera, 
+													   Viewport* vp)
+{
+    Root::getSingleton()._setCurrentSceneManager(this);
+	mActiveQueuedRenderableVisitor->targetSceneMgr = this;
+
+    mCameraInProgress = camera;
+
+    // Update scene graph for this camera (can happen multiple times per frame)
+    _updateSceneGraph(camera);
+
+    // Invert vertex winding?
+    if (camera->isReflected())
+    {
+        mDestRenderSystem->setInvertVertexWinding(true);
+    }
+    else
+    {
+        mDestRenderSystem->setInvertVertexWinding(false);
+    }
+
+    // Tell params about viewport
+    mAutoParamDataSource.setCurrentViewport(vp);
+    // Set the viewport
+    setViewport(vp);
+
+    // Tell params about camera
+    mAutoParamDataSource.setCurrentCamera(camera);
+    // Set autoparams for finite dir light extrusion
+    mAutoParamDataSource.setShadowDirLightExtrusionDistance(mShadowDirLightExtrudeDist);
+
+    // Tell params about current ambient light
+    mAutoParamDataSource.setAmbientLightColour(mAmbientLight);
+	// Tell rendersystem
+	mDestRenderSystem->setAmbientLight(mAmbientLight.r, mAmbientLight.g, mAmbientLight.b);
+
+    // Tell params about render target
+    mAutoParamDataSource.setCurrentRenderTarget(vp->getTarget());
+
+
+    // Set camera window clipping planes (if any)
+    if (mDestRenderSystem->getCapabilities()->hasCapability(RSC_USER_CLIP_PLANES))
+    {
+        if (camera->isWindowSet())  
+        {
+            const std::vector<Plane>& planeList = 
+                camera->getWindowPlanes();
+            for (ushort i = 0; i < 4; ++i)
+            {
+                mDestRenderSystem->enableClipPlane(i, true);
+                mDestRenderSystem->setClipPlane(i, planeList[i]);
+            }
+        }
+        else
+        {
+            for (ushort i = 0; i < 4; ++i)
+            {
+                mDestRenderSystem->enableClipPlane(i, false);
+            }
+        }
+    }
+
+	// Prepare render queue for receiving new objects
+	prepareRenderQueue();
+
+	// Begin the frame
+    mDestRenderSystem->_beginFrame();
+
+    // Set rasterisation mode
+    mDestRenderSystem->_setPolygonMode(camera->getPolygonMode());
+
+	// Set initial camera state
+	mDestRenderSystem->_setProjectionMatrix(mCameraInProgress->getProjectionMatrixRS());
+	mDestRenderSystem->_setViewMatrix(mCameraInProgress->getViewMatrix(true));
+
+	// // Render scene content in order fill depth buffer
+	bool savedUseDepthPass = mUseDepthPass;
+	mUseDepthPass = true;
+
+	// don't need shading, render only depth pass
+	const bool fillQueue = false;
+	RenderDepthPass(fillQueue);
+	// _renderVisibleObjects();
+    
+	mUseDepthPass = savedUseDepthPass;
+   
+    // End frame
+    mDestRenderSystem->_endFrame();
+}
 //-----------------------------------------------------------------------
 const String OcclusionCullingSceneManagerFactory::FACTORY_TYPE_NAME = "OcclusionCullingSceneManager";
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOcclusionQueriesQueryManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOcclusionQueriesQueryManager.cpp	(revision 2168)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreOcclusionQueriesQueryManager.cpp	(revision 2171)
@@ -5,4 +5,8 @@
 #include <OgreSubEntity.h>
 #include "OgrePlatformHierarchyInterface.h"
+#include "OgreOcclusionCullingSceneManager.h"
+#include "VisibilityEnvironment.h"
+
+
 
 namespace Ogre {
@@ -19,28 +23,33 @@
 }
 //-----------------------------------------------------------------------
-void OcclusionQueriesQueryManager::ComputeCameraVisibility(const Camera &camera,
-														   GtpVisibility::NodeInfoContainer *visibleNodes,
-														   GtpVisibility::MeshInfoContainer *visibleGeometry,
-														   GtpVisibility::PatchInfoContainer *visiblePatches,
-														   bool relativeVisibility)
+void OcclusionQueriesQueryManager::ComputeCameraVisibility(
+											const Camera &camera,
+											GtpVisibility::NodeInfoContainer *visibleNodes,
+											GtpVisibility::MeshInfoContainer *visibleGeometry,
+											GtpVisibility::PatchInfoContainer *visiblePatches,
+											bool relativeVisibility)
 {
 	// we need access to the scene manager and the rendersystem
 	PlatformHierarchyInterface *pfHierarchyInterface = 
-		//static_cast<PlatformHierarchyInterface *>(mHierarchyInterface);
 		static_cast<PlatformHierarchyInterface *>(mHierarchyInterface);
 
 	// disable overlays, reset them later
 	bool overlayEnabled = mViewport->getOverlaysEnabled();
-Ogre::LogManager::getSingleton().logMessage("here020202");
+
 	///////////
 	//-- render scene with item buffer (i.e., objects with their id as color codes)
-//mItemBufferMode = 0;
-	if ((mItemBufferMode && mQueryModes) != 0)
+
+	//mItemBufferMode = 0;
+	if (0 && 
+		(mItemBufferMode && mQueryModes) != 0)
 	{
 		int savedQueryModes = mQueryModes;
 		mQueryModes = mItemBufferMode;
 		
-		PlatformQueryManager::ComputeCameraVisibility(camera, visibleNodes,
-			    visibleGeometry, visiblePatches, relativeVisibility);
+		PlatformQueryManager::ComputeCameraVisibility(camera, 
+													  visibleNodes,
+													  visibleGeometry, 
+													  visiblePatches, 
+													  relativeVisibility);
 		
 		// overlays cause false visibility
@@ -54,10 +63,36 @@
 		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:
+
+		OcclusionCullingSceneManager *occlusionSceneMgr = 
+			static_cast<OcclusionCullingSceneManager *>(pfHierarchyInterface->GetSceneManager());
+
+		// no visibility manager available => no visibility scene manager, return
+		GtpVisibility::VisibilityManager *visManager = NULL;	
+		
+		if (!occlusionSceneMgr->getOption("VisibilityManager", &visManager))
+		{
+			Ogre::LogManager::getSingleton().logMessage("no vismanager found");
+			return;
+		}
+
+		// use stop and wait culling for from view point queries because probably
+		// no temporal coherence
+		GtpVisibility::VisibilityEnvironment::CullingManagerType occlusionType = 
+			visManager->GetCullingManagerType();
+
+		visManager->SetCullingManager(GtpVisibility::VisibilityEnvironment::STOP_AND_WAIT_CULLING);
+
+		if (1)
+			occlusionSceneMgr->RenderDepthForQuery(pCam, mViewport);
+		else
+			pfHierarchyInterface->GetSceneManager()->_renderScene(pCam, mViewport, false);
+
+		visManager->SetCullingManager(occlusionType);
+	}
+
+	
+	/*	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.
@@ -88,5 +123,5 @@
 	if (mQueryModes & GEOMETRY_VISIBILITY)
 	{	
-		for (visNodesIt = visNodes->begin(); visNodesIt != visNodesIt_end; ++visNodesIt)	
+		for (visNodesIt = visNodes->begin(); visNodesIt != visNodesIt_end; ++ visNodesIt)	
 		{
 			mHierarchyInterface->GetNodeGeometryList(*visNodesIt, &geometryList, false);
@@ -128,7 +163,7 @@
 	int n = relativeVisibility ? 2 : 1;
 	if (mItemBufferMode > 0)
-		LogManager::getSingleton().logMessage("item buffer");
-
-	for (int i = 0; i < n; ++i)
+		LogManager::getSingleton().logMessage("using item buffer");
+
+	for (int i = 0; i < n; ++ i)
 	{
 		//-- queries for hierarchy nodes
@@ -139,8 +174,10 @@
 		}
 
+		////////////////////////////
 		//-- queries for geometry: if item buffer, capture only projected visibility
+
 		if ((mItemBufferMode != GEOMETRY_VISIBILITY) || (i == 1))
 		{			
-			for (geometryIt = geometryList.begin(); geometryIt != geometryIt_end; ++geometryIt)
+			for (geometryIt = geometryList.begin(); geometryIt != geometryIt_end; ++ geometryIt)
 			{
 				queryList[i].push_back(pfHierarchyInterface->IssueMeshOcclusionQuery(*geometryIt));
@@ -148,5 +185,7 @@
 		}
 		
+		////////////////////////
 		//-- queries for patches: if item buffer, capture only projected visibility
+
 		if ((mItemBufferMode != PATCH_VISIBILITY) || (i == 1))
 		{
@@ -160,11 +199,16 @@
 	}
 
-	
+	///////////////	
 	//-- collect results
+
 	GtpVisibility::QueryList::iterator visQueryIt = queryList[0].begin();
 	GtpVisibility::QueryList::iterator projQueryIt = queryList[1].begin();
 	
 	// collect occlusion queries for hierarchy nodes
-	CollectNodeVisibility(visQueryIt, projQueryIt, &nodeList, visibleNodes, relativeVisibility);
+	CollectNodeVisibility(visQueryIt, 
+						  projQueryIt, 
+						  &nodeList, 
+						  visibleNodes, 
+						  relativeVisibility);
 
 
@@ -174,9 +218,14 @@
 		// 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);
+		CollectRelativeGeometryVisibilityForItemBuffer(projQueryIt, 
+													   &geometryList, 
+													   visibleGeometry);
 	}
 	else if (mItemBufferMode != GEOMETRY_VISIBILITY)
 	{
-		CollectGeometryVisibility(visQueryIt, projQueryIt, &geometryList, visibleGeometry, 
+		CollectGeometryVisibility(visQueryIt, 
+								  projQueryIt, 
+								  &geometryList, 
+								  visibleGeometry, 
 								  relativeVisibility);
 	}
@@ -186,13 +235,20 @@
 	if ((mItemBufferMode == PATCH_VISIBILITY) && relativeVisibility)
 	{
-		CollectRelativePatchVisibilityForItemBuffer(projQueryIt, &patchList, visiblePatches);
+		CollectRelativePatchVisibilityForItemBuffer(projQueryIt, 
+													&patchList, 
+													visiblePatches);
 	}
 	else if (mItemBufferMode != PATCH_VISIBILITY)
 	{
-		CollectPatchVisibility(visQueryIt, projQueryIt, &patchList, visiblePatches, relativeVisibility);
+		CollectPatchVisibility(visQueryIt, 
+							   projQueryIt, 
+							   &patchList, 
+							   visiblePatches, 
+							   relativeVisibility);
 	}	
 
-		
-	//-- reset options
+	////////////
+	//-- reset state
+
 	enableDepthWrite = true;
 	// this option must be provided by the scene manager
@@ -204,22 +260,26 @@
 }
 //-----------------------------------------------------------------------
-void OcclusionQueriesQueryManager::ComputeFromPointVisibility(const Vector3 &point,
-			       GtpVisibility::NodeInfoContainer *visibleNodes,
-			       GtpVisibility::MeshInfoContainer *visibleGeometry,
-				   GtpVisibility::PatchInfoContainer *visiblePatches,
-			       bool relativeVisibility)
-{Ogre::LogManager::getSingleton().logMessage("here5");
-	PlatformQueryManager::ComputeFromPointVisibility(point, visibleNodes, 
-		visibleGeometry, visiblePatches, relativeVisibility);
+void OcclusionQueriesQueryManager::ComputeFromPointVisibility(
+								const Vector3 &point,
+								GtpVisibility::NodeInfoContainer *visibleNodes,
+								GtpVisibility::MeshInfoContainer *visibleGeometry,
+								GtpVisibility::PatchInfoContainer *visiblePatches,
+								bool relativeVisibility)
+{
+	PlatformQueryManager::ComputeFromPointVisibility(point, 
+													 visibleNodes, 
+													 visibleGeometry, 
+													 visiblePatches, 
+													 relativeVisibility);
 
 	// adds up their visibility and removes duplicates 
 	// (duplicates occur if an object is on the edge of the viewport)
 	RemoveDuplicateNodes(visibleNodes);
-	Ogre::LogManager::getSingleton().logMessage("here6");
+
 	if (mItemBufferMode != GEOMETRY_VISIBILITY)
 	{
 		RemoveDuplicateGeometry(visibleGeometry);
 	}
-	Ogre::LogManager::getSingleton().logMessage("here7");
+
 	if (mItemBufferMode != PATCH_VISIBILITY)
 	{
@@ -228,5 +288,5 @@
 }
 //------------------------------------------------------------------------
-void  OcclusionQueriesQueryManager::CollectNodeVisibility(
+void OcclusionQueriesQueryManager::CollectNodeVisibility(
 						GtpVisibility::QueryList::iterator &visQueryIt, 
 						GtpVisibility::QueryList::iterator &projQueryIt,
@@ -238,5 +298,5 @@
 
 	//-- queries for nodes
-	for (nodeIt = nodeList->begin(); nodeIt != nodeList->end(); ++nodeIt)
+	for (nodeIt = nodeList->begin(); nodeIt != nodeList->end(); ++ nodeIt)
 	{
 		unsigned int visiblePixels = 0;
@@ -248,15 +308,14 @@
 		{
 			(*projQueryIt)->GetQueryResult(projectedPixels, true);
-		
-			++projQueryIt;
-		}
-
-		++visQueryIt;
+			++ projQueryIt;
+		}
+
+		++ visQueryIt;
 		
 		// node with visibilty 0 in queue (e.g., if node is intersected by near plane)
 		if (visiblePixels > 0)
 		{
-			visibleNodes->push_back(GtpVisibility::NodeInfo(*nodeIt, visiblePixels, 
-				projectedPixels));
+			visibleNodes->push_back(
+				GtpVisibility::NodeInfo(*nodeIt, visiblePixels, projectedPixels));
 		}
 	}
@@ -384,5 +443,5 @@
 					GtpVisibility::NodeInfoContainer *visibleNodes)
 {
-	stable_sort(visibleNodes->begin(), visibleNodes->end()/*, nodeinfo_lt*/);
+	sort(visibleNodes->begin(), visibleNodes->end());
 
 	GtpVisibility::NodeInfoContainer::iterator visibleNodesIt,
@@ -412,5 +471,5 @@
 				GtpVisibility::MeshInfoContainer *visibleGeometry)
 {
-	stable_sort(visibleGeometry->begin(), visibleGeometry->end());
+	sort(visibleGeometry->begin(), visibleGeometry->end());
 	
 	GtpVisibility::MeshInfoContainer::iterator visibleGeomIt,
@@ -440,5 +499,5 @@
 				GtpVisibility::PatchInfoContainer *visiblePatches)
 {
-	stable_sort(visiblePatches->begin(), visiblePatches->end());
+	sort(visiblePatches->begin(), visiblePatches->end());
 	
 	GtpVisibility::PatchInfoContainer::iterator visiblePatchIt,
