Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 98)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 99)
@@ -27,24 +27,25 @@
 	if(mTerrainContentGenerator)
 		delete mTerrainContentGenerator;
+	//if(mRenderTargetListener)	delete mRenderTargetListener;
 }
 //-----------------------------------------------------------------------
 void TestCullingTerrainApplication::createCamera()
 {
-	// Create the camera
-	mCamera = mSceneMgr->createCamera("CullCamera");
-	
-	// Set a nice viewpoint
+	// create the camera
+	mCamera = mSceneMgr->createCamera("PlayerCam");
+	
+	// set a nice viewpoint
 	mCamera->setPosition(707, 2500, 528);
 	mCamera->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
 	
-	//--create cull camera
-	mVisualizationCamera = mSceneMgr->createCamera("VisualizationCamera");
-	mVisualizationCamera->setPosition(707, 2500, 528);
-	mVisualizationCamera->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
+	//--create visualization camera
+	mVisualizationCamera = mSceneMgr->createCamera("VizCam");
+	mVisualizationCamera->setPosition(mCamera->getPosition());
+	mVisualizationCamera->setOrientation(mCamera->getOrientation());
 
 	mVisualizationCamera->setNearClipDistance(1);
 	mCamera->setNearClipDistance(1);
 
-	// Infinite far plane?
+	// infinite far plane?
 	if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_INFINITE_FAR_PLANE))
 	{
@@ -56,6 +57,20 @@
 		 mVisualizationCamera->setFarClipDistance(1000);
 		 mCamera->setFarClipDistance(1000);
-	}
-	
+	}	
+}
+
+//-----------------------------------------------------------------------
+bool TestCullingTerrainApplication::setup()
+{
+	bool result = ExampleApplication::setup();
+
+	createRenderTargetListener();
+
+	return result;
+}
+//-----------------------------------------------------------------------
+void TestCullingTerrainApplication::createRenderTargetListener()
+{
+	mWindow->addListener(new VisualizationRenderTargetListener(mSceneMgr));
 }
 //-----------------------------------------------------------------------
@@ -152,7 +167,6 @@
 mCurrentAlgorithm(GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING),
 mShowOctree(false),
-mUseCulling(true),
+mUseVisibilityCulling(true),
 mUseOptimization(true),
-mUseCullCamera(false),
 mVisualizationCamera(vizCamera)
 {
@@ -197,8 +211,7 @@
 
 	mSceneMgr->setOption("UseOptimization", &mUseOptimization);
-	mSceneMgr->setOption("UseCulling", &mUseCulling);
-	mSceneMgr->setOption("CullCamera", &mUseCullCamera);
+	mSceneMgr->setOption("UseVisibilityCulling", &mUseVisibilityCulling);
 	mSceneMgr->setOption("ShowOctree", &mShowOctree);
-
+	
     pOver->show();
 } 
@@ -241,6 +254,5 @@
      } // else if
 } // mousePressed
-
- //-----------------------------------------------------------------------
+//-----------------------------------------------------------------------
 void MouseQueryListener::mouseReleased(MouseEvent* e)
 {
@@ -316,6 +328,6 @@
 	KEY_PRESSED(KC_O, 0.3, toggleUseOptimization());
 	KEY_PRESSED(KC_Y, 0.3, toggleShowOctree());
-	KEY_PRESSED(KC_C, 0.3, toggleUseCulling());
-	KEY_PRESSED(KC_V, 0.3, toggleCullCamera());
+	KEY_PRESSED(KC_C, 0.3, toggleUseVisibilityCulling());
+	KEY_PRESSED(KC_V, 0.3, toggleShowViz());
 	KEY_PRESSED(KC_X, 0.3, FixVizCamera());
 
@@ -397,16 +409,17 @@
 }
 //-----------------------------------------------------------------------
-void MouseQueryListener::toggleUseCulling()
-{
-	mUseCulling = !mUseCulling;
-
-	mSceneMgr->setOption("UseCulling", &mUseCulling);
-}
-//-----------------------------------------------------------------------
-void MouseQueryListener::toggleCullCamera()
-{
-	mUseCullCamera = !mUseCullCamera;
-
-	if(mUseCullCamera)
+void MouseQueryListener::toggleUseVisibilityCulling()
+{
+	mUseVisibilityCulling = !mUseVisibilityCulling;
+
+	mSceneMgr->setOption("UseVisibilityCulling", &mUseVisibilityCulling);
+}
+//-----------------------------------------------------------------------
+void MouseQueryListener::toggleShowViz()
+{
+	mShowVisualization = !mShowVisualization;
+
+	// create viewport with priority 10: will be rendered over standard viewport
+	if(mShowVisualization)
 	{	
 		mWindow->addViewport(mVisualizationCamera, 10, 0.5, 0.5, 1, 1);
@@ -414,7 +427,7 @@
 	}
 	else
+	{
 		mWindow->removeViewport(10);
-
-	mSceneMgr->setOption("CullCamera", &mUseCullCamera);
+	}
 }
 //-----------------------------------------------------------------------
@@ -451,4 +464,25 @@
 	// Do nothing
 	e->consume();
+}
+/**************************************************************/
+/*      VisualizationRenderTargetListener implementation      */
+/**************************************************************/
+VisualizationRenderTargetListener::VisualizationRenderTargetListener(SceneManager *sceneMgr)
+:RenderTargetListener(), mSceneMgr(sceneMgr)
+{
+}
+//-----------------------------------------------------------------------
+void VisualizationRenderTargetListener::preViewportUpdate(const RenderTargetViewportEvent &evt)
+{
+	const bool b = evt.source->getZOrder() == 10; // visualization viewport
+			
+	mSceneMgr->setOption("ShowVisualization", &b);
+	
+	RenderTargetListener::preViewportUpdate(evt);
+}
+//-----------------------------------------------------------------------
+void VisualizationRenderTargetListener::postRenderTargetUpdate(const RenderTargetEvent &evt)
+{
+	RenderTargetListener::postRenderTargetUpdate(evt);
 }
 //-----------------------------------------------------------------------
Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.h
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.h	(revision 94)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.h	(revision 99)
@@ -7,4 +7,5 @@
 #include "TerrainContentGenerator.h"
 #include "VisibilityEnvironment.h"
+#include <OgreRenderTargetListener.h>
 
 Real timeDelay = 0;
@@ -23,4 +24,16 @@
 	"Stop and Wait Culling", 
 	"Coherent Hierarchical Culling"
+};
+
+class VisualizationRenderTargetListener: public RenderTargetListener
+{
+public:
+	VisualizationRenderTargetListener(SceneManager *sceneMgr);
+
+protected:
+	void preViewportUpdate (const RenderTargetViewportEvent &evt);
+	void postRenderTargetUpdate (const RenderTargetEvent &evt);
+
+	SceneManager *mSceneMgr;
 };
 
@@ -65,6 +78,6 @@
 	void toggleUseOptimization();
 	void toggleShowOctree();
-	void toggleUseCulling();
-	void toggleCullCamera();
+	void toggleUseVisibilityCulling();
+	void toggleShowViz();
 	void FixVizCamera();
 
@@ -100,6 +113,6 @@
 	bool mUseOptimization;
 	bool mShowOctree;
-	bool mUseCulling;
-	bool mUseCullCamera;
+	bool mUseVisibilityCulling;
+	bool mShowVisualization;
 
 	Camera *mVisualizationCamera;
@@ -113,8 +126,15 @@
 
 protected:
+	//-- inherited from ExampleApplication
+	bool setup();
 	void createScene();
 	void createFrameListener();
+	void createCamera();
+	//void createViewports();
+	
+	virtual void createRenderTargetListener();
+
+	/** cegui setup */
 	void setupGui();
-	virtual void createCamera();
 
 	CEGUI::OgreCEGUIRenderer *mGUIRenderer;
@@ -128,9 +148,9 @@
 
 	TerrainContentGenerator *mTerrainContentGenerator;
+	
+	Camera *mVisualizationCamera;
+	VisualizationRenderTargetListener *mRenderTargetListener;
 
-	Camera *mVisualizationCamera;
-	
 private:
 	void chooseSceneManager(void);
 };
-
Index: trunk/VUT/work/ogre_changes/OgreMain/src/OgreSceneManager.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/OgreMain/src/OgreSceneManager.cpp	(revision 61)
+++ trunk/VUT/work/ogre_changes/OgreMain/src/OgreSceneManager.cpp	(revision 99)
@@ -4178,5 +4178,5 @@
     }
 }
-#ifdef OGRE_COHERENT_OCCLUSION_CULLING
+#ifdef GTP_VISIBILITY_MODIFIED_OGRE
 //-----------------------------------------------------------------------
 void SceneManager::_renderSceneNode( Camera *cam, SceneNode *node )
@@ -4193,4 +4193,5 @@
 	RenderQueue::QueueGroupIterator queueIt = getRenderQueue()->_getQueueGroupIterator();
 
+	// find currently processed queue groups and delelete them from render queue
 	while (queueIt.hasMoreElements())
 	{
@@ -4198,5 +4199,4 @@
 		RenderQueueGroup* pGroup = queueIt.getNext();
 
-		// delete this queue group if already processed
 		if (isRenderQueueToBeProcessed(qId))
 			pGroup->clear();
@@ -4206,4 +4206,4 @@
 	// Pass::processPendingPassUpdates();
 }
-#endif //OGRE_COHERENT_OCCLUSION_CULLING
-}
+#endif //GTP_VISIBILITY_MODIFIED_OGRE
+}
Index: trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctreeSceneManager.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctreeSceneManager.cpp	(revision 61)
+++ trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctreeSceneManager.cpp	(revision 99)
@@ -316,7 +316,7 @@
 
     mOctree = new Octree( 0 );
-#ifdef OGRE_COHERENT_OCCLUSION_CULLING
+#ifdef GTP_VISIBILITY_MODIFIED_OGRE
 	mNumOctreeNodes = 1;
-#endif // OGRE_COHERENT_OCCLUSION_CULLING
+#endif // GTP_VISIBILITY_MODIFIED_OGRE
     mMaxDepth = depth;
     mBox = box;
@@ -461,7 +461,7 @@
         {
             octant -> mChildren[ x ][ y ][ z ] = new Octree( octant );
-#ifdef OGRE_COHERENT_OCCLUSION_CULLING
+#ifdef GTP_VISIBILITY_MODIFIED_OGRE
 	mNumOctreeNodes ++;
-#endif // OGRE_COHERENT_OCCLUSION_CULLING
+#endif // GTP_VISIBILITY_MODIFIED_OGRE
 
             const Vector3 *corners = octant -> mBox.getAllCorners();
@@ -1028,7 +1028,7 @@
 
     mOctree = new Octree( 0 );
-#ifdef OGRE_COHERENT_OCCLUSION_CULLING
+#ifdef GTP_VISIBILITY_MODIFIED_OGRE
 	mNumOctreeNodes = 1;
-#endif // OGRE_COHERENT_OCCLUSION_CULLING
+#endif // GTP_VISIBILITY_MODIFIED_OGRE
     mOctree->mBox = box;
 
@@ -1166,5 +1166,5 @@
     return q;
 }
-#ifdef OGRE_COHERENT_OCCLUSION_CULLING
+#ifdef GTP_VISIBILITY_MODIFIED_OGRE
 //-----------------------------------------------------------------------
 void OctreeSceneManager::_renderOctant(Camera *cam, Octree *octant)
@@ -1177,5 +1177,5 @@
 		OctreeNode *sn = *it;
 
-        mNumObjects++;
+        ++mNumObjects;
 
 		// check bounding box visibility of scene nodes
@@ -1187,21 +1187,28 @@
 
 			if (mDisplayNodes)
+			{
 				getRenderQueue()->addRenderable(sn);
+			}
 
 			// check if the scene manager or this node wants the bounding box shown.
 			if (sn->getShowBoundingBox() || mShowBoundingBoxes)
+			{
 				sn->_addBoundingBoxToQueue(getRenderQueue());
-
-			if (mShowBoxes)
-				getRenderQueue()->addRenderable(octant->getWireBoundingBox());
+			}
 		}
         ++it;
 	}
 
+	if (mShowBoxes)
+	{
+		getRenderQueue()->addRenderable(octant->getWireBoundingBox());
+		++mNumObjects;
+	}
+
 	SceneManager::_renderVisibleObjects();
 
-	// delete all rendered objects from renderqueue
+	// delete all rendered objects from the render queue
 	_deleteRenderedQueueGroups();
 }
-#endif // OGRE_COHERENT_OCCLUSION_CULLING
-}
+#endif // GTP_VISIBILITY_MODIFIED_OGRE
+}
Index: trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreGLHardwareOcclusionQuery.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreGLHardwareOcclusionQuery.cpp	(revision 97)
+++ trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreGLHardwareOcclusionQuery.cpp	(revision 99)
@@ -161,6 +161,6 @@
 
 	// Check for hardware occlusion support
-    // if( glDeleteOcclusionQueriesNV_ptr != 0 )	// This is a hack to see if hw occlusion is supported. pointer is 0 if it's not supported.
-	if( glDeleteQueriesARB_ptr != 0 )
+    if ( glDeleteOcclusionQueriesNV_ptr != 0 )	// This is a hack to see if hw occlusion is supported. pointer is 0 if it's not supported.
+	//if ( glDeleteQueriesARB_ptr != 0 )
 	{
 		m_bHWOcclusionSupport = true;
@@ -171,8 +171,8 @@
 	}
 
-	if( m_bHWOcclusionSupport )
-	{
-		//glGenOcclusionQueriesNV_ptr( 1, m_uintQuery );	
-		glGenQueriesARB_ptr(1, m_uintQuery);
+	if ( m_bHWOcclusionSupport )
+	{
+		glGenOcclusionQueriesNV_ptr( 1, m_uintQuery );	
+		//glGenQueriesARB_ptr(1, m_uintQuery);
 	}
 }
@@ -182,8 +182,8 @@
 GLHardwareOcclusionQuery::~GLHardwareOcclusionQuery() 
 { 
-	if( m_bHWOcclusionSupport )
-	{
-		//glDeleteOcclusionQueriesNV_ptr( 1, &m_uintQuery[0] );  
-		glDeleteQueriesARB_ptr(1, &m_uintQuery[0]);
+	if ( m_bHWOcclusionSupport )
+	{
+		glDeleteOcclusionQueriesNV_ptr( 1, &m_uintQuery[0] );  
+		//glDeleteQueriesARB_ptr(1, &m_uintQuery[0]);
 	}	
 }
@@ -192,9 +192,9 @@
 void GLHardwareOcclusionQuery::beginOcclusionQuery() 
 { 
-	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
+	if ( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
 	{
 		// do the actual occlusion query for this node
-		//glBeginOcclusionQueryNV_ptr( m_uintQuery[0] );
-		glBeginQueryARB_ptr(GL_SAMPLES_PASSED_ARB, m_uintQuery[0]);
+		glBeginOcclusionQueryNV_ptr( m_uintQuery[0] );
+		//glBeginQueryARB_ptr(GL_SAMPLES_PASSED_ARB, m_uintQuery[0]);
 	}
 }
@@ -202,8 +202,8 @@
 void GLHardwareOcclusionQuery::endOcclusionQuery() 
 { 
-	if( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
-	{
-		//glEndOcclusionQueryNV_ptr();
-		glEndQueryARB_ptr(GL_SAMPLES_PASSED_ARB);
+	if ( m_bHWOcclusionSupport )	// Make it fail silently if hardware occlusion isn't supported
+	{
+		glEndOcclusionQueryNV_ptr();
+		//glEndQueryARB_ptr(GL_SAMPLES_PASSED_ARB);
 	}
 }
@@ -211,19 +211,19 @@
 bool GLHardwareOcclusionQuery::pullOcclusionQuery( unsigned int * NumOfFragments, const bool waitForResult) 
 {
-	//unsigned int isAvailable = GL_TRUE;
-	int isAvailable = GL_TRUE;
+	unsigned int isAvailable = GL_TRUE;
+	//int isAvailable = GL_TRUE;
 
 	if( m_bHWOcclusionSupport )	
 	{
-		if(!waitForResult)
-		{
-			//glGetOcclusionQueryuivNV_ptr( m_uintQuery[0], GL_PIXEL_COUNT_AVAILABLE_NV, &isAvailable );
-			glGetQueryivARB_ptr(m_uintQuery[0], GL_QUERY_RESULT_AVAILABLE_ARB, &isAvailable);
-		}
-
-		if(isAvailable == GL_TRUE)
-		{
-			glGetQueryObjectuivARB_ptr( m_uintQuery[0], GL_QUERY_RESULT_ARB, NumOfFragments);
-			//glGetOcclusionQueryuivNV_ptr( m_uintQuery[0], GL_PIXEL_COUNT_NV, NumOfFragments );
+		if (!waitForResult)
+		{
+			glGetOcclusionQueryuivNV_ptr( m_uintQuery[0], GL_PIXEL_COUNT_AVAILABLE_NV, &isAvailable );
+			//glGetQueryivARB_ptr(m_uintQuery[0], GL_QUERY_RESULT_AVAILABLE_ARB, &isAvailable);
+		}
+
+		if (isAvailable == GL_TRUE)
+		{
+			glGetOcclusionQueryuivNV_ptr( m_uintQuery[0], GL_PIXEL_COUNT_NV, NumOfFragments );
+			//glGetQueryObjectuivARB_ptr( m_uintQuery[0], GL_QUERY_RESULT_ARB, NumOfFragments);
 		}
 	} 
Index: trunk/VUT/work/TestCulling/TestCullingApplication.cpp
===================================================================
--- trunk/VUT/work/TestCulling/TestCullingApplication.cpp	(revision 94)
+++ trunk/VUT/work/TestCulling/TestCullingApplication.cpp	(revision 99)
@@ -111,7 +111,7 @@
 mCurrentAlgorithm(GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING),
 mShowOctree(false),
-mUseCulling(true),
+mUseVisibilityCulling(true),
 mUseOptimization(true),
-mUseCullCamera(false)
+mShowVisualization(false)
 {
     // Reduce move speed
@@ -153,6 +153,6 @@
 	
 	mSceneMgr->setOption("UseOptimization", &mUseOptimization);
-	mSceneMgr->setOption("UseCulling", &mUseCulling);
-	mSceneMgr->setOption("CullCamera", &mUseCullCamera);
+	mSceneMgr->setOption("UseVisibilityCulling", &mUseVisibilityCulling);
+	mSceneMgr->setOption("ShowVizualisation", &mShowVisualization);
 	mSceneMgr->setOption("ShowOctree", &mShowOctree);
 
@@ -235,6 +235,6 @@
 	KEY_PRESSED(KC_ADD, 0, changeThreshold(10));
 	KEY_PRESSED(KC_O, 0.3, toggleUseOptimization());
-	KEY_PRESSED(KC_C, 0.3, toggleUseCulling());
-	KEY_PRESSED(KC_V, 0.3, toggleCullCamera());
+	KEY_PRESSED(KC_C, 0.3, toggleUseVisibilityCulling());
+	KEY_PRESSED(KC_V, 0.3, toggleShowViz());
 
 	updateStats();
@@ -281,16 +281,16 @@
 }
 //-----------------------------------------------------------------------
-void MouseQueryListener::toggleUseCulling()
-{
-	mUseCulling = !mUseCulling;
-
-	mSceneMgr->setOption("UseCulling", &mUseCulling);
-}
-//-----------------------------------------------------------------------
-void MouseQueryListener::toggleCullCamera()
-{
-	mUseCullCamera = !mUseCullCamera;
-
-	mSceneMgr->setOption("CullCamera", &mUseCullCamera);
+void MouseQueryListener::toggleUseVisibilityCulling()
+{
+	mUseVisibilityCulling = !mUseVisibilityCulling;
+
+	mSceneMgr->setOption("UseVisibilityCulling", &mUseVisibilityCulling);
+}
+//-----------------------------------------------------------------------
+void MouseQueryListener::toggleShowViz()
+{
+	mShowVisualization = !mShowVisualization;
+
+	mSceneMgr->setOption("CullCamera", &mShowVisualization);
 }
 //-----------------------------------------------------------------------
Index: trunk/VUT/work/TestCulling/TestCullingApplication.h
===================================================================
--- trunk/VUT/work/TestCulling/TestCullingApplication.h	(revision 94)
+++ trunk/VUT/work/TestCulling/TestCullingApplication.h	(revision 99)
@@ -66,6 +66,6 @@
 	void toggleUseOptimization();
 	void toggleShowOctree();
-	void toggleUseCulling();
-	void toggleCullCamera();
+	void toggleUseVisibilityCulling();
+	void toggleShowViz();
 
 protected:
@@ -94,6 +94,6 @@
 	bool mUseOptimization;
 	bool mShowOctree;
-	bool mUseCulling;
-	bool mUseCullCamera;
+	bool mUseVisibilityCulling;
+	bool mShowVisualization;
 
 	Camera *mVisualizationCamera;
