Index: trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp	(revision 133)
+++ trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp	(revision 135)
@@ -16,11 +16,18 @@
 
 
-String mCurrentAlgorithmCaptions[GtpVisibility::VisibilityEnvironment::NUM_CULLING_MANAGERS] = 
-{
-    "View Frustum Culling", 
+String currentAlgorithmCaptions[GtpVisibility::VisibilityEnvironment::NUM_CULLING_MANAGERS] = 
+{
+	"View Frustum Culling", 
 	"Stop and Wait Culling", 
 	"Coherent Hierarchical Culling"
 };
 
+String queryTypeCaptions[4] = 
+{
+	"from camera, visible pixels", 
+	"from viewpoint, visible pixels", 
+	"from camera, relative visibility",
+	"from viewpoint, relative visibility"
+};
 
 //-----------------------------------------------------------------------
@@ -73,9 +80,10 @@
 mVisualizeCulledNodes(false),
 mSunLight(sunLight),
-mShiftPressed(false)
+mShiftPressed(false),
+mShowQueryStats(false)
 {
 	//mInputDevice = PlatformManager::getSingleton().createInputReader();
-	
 	//mInputDevice->initialise(win, true, true);
+
 	mEventProcessor = new EventProcessor();
 	
@@ -94,5 +102,5 @@
 	mHelpOverlay = OverlayManager::getSingleton().getByName("Example/Visibility/HelpOverlay"); 
 	mDebugOverlay = OverlayManager::getSingleton().getByName("Core/DebugOverlay");
-
+	mQueryOverlay = OverlayManager::getSingleton().getByName("Example/Visibility/QueryOverlay"); 
 
 	//-- visibility culling stats overlay
@@ -112,5 +120,5 @@
 	mQueriesIssuedInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/QueriesIssuedInfo");
 	
-	mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
+	mAlgorithmInfo->setCaption(": " + currentAlgorithmCaptions[mCurrentAlgorithm]);
 	mThresholdInfo->setCaption(": 0");
 	mFrustumCulledNodesInfo->setCaption(": 0");
@@ -124,4 +132,12 @@
 	mQueriesIssuedInfo->setCaption(": 0");
 
+
+	//-- visibility query stats overlay
+	mQueryTypeInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Query/QueryTypeInfo");
+	mQueryVisibleNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Query/VisibleNodesInfo");
+    mQueryVisibleGeometryInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Query/VisibleGeometryInfo");
+	mQueryNodeVisibilityInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Query/NodeVisibilityInfo");
+	mQueryGeometryVisibilityInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Query/GeometryVisibilityInfo");
+
 	// show stats overlays
 	showStats(true);
@@ -145,5 +161,5 @@
 	mSceneMgr->setOption("ShowVisualization", &mShowVisualization);
 
-	// TODO: change this
+	// TODO: change this (does not work with other scene manager plugins)
 	mVisibilityManager = dynamic_cast<VisibilityTerrainSceneManager *>(mSceneMgr)->
 		GetVisibilityManager();
@@ -155,6 +171,4 @@
 TerrainFrameListener::~TerrainFrameListener()
 {
-	//PlatformManager::getSingleton().destroyInputReader(mInputDevice);
-    
 	delete mRayQueryExecutor;
 	delete mEventProcessor;
@@ -182,5 +196,5 @@
 		 // Get results, create a node/entity on the position
 		 mCurrentObject = mTerrainContentGenerator->GenerateSceneObject(
-			 mouseRay.getOrigin(), Vector3::ZERO, "robot", false);
+			 mouseRay.getOrigin(), Vector3::ZERO, "robot");
 		
          mLMouseDown = true; 
@@ -341,4 +355,7 @@
 	if (fromPoint)
 	{
+		Vector3 point;
+		mVisibilityManager->GetQueryManager()->
+		ComputeFromPointVisibility(point, &visibleNodes, &visibleGeometry, relativeVisibility);
 	}
 	else
@@ -348,15 +365,60 @@
 	}
 
+	float averageNodeVis = 0;
+	float averageGeometryVis = 0;
+
 	for (int i=0; i < visibleGeometry.size(); ++i)
 	{
-		std::stringstream d; d << "Geometry visibility: " << visibleGeometry[i].GetVisibiliy();
+		averageGeometryVis += visibleGeometry[i].GetVisibility();
+
+		std::stringstream d; d << "Geometry visibility: " << visibleGeometry[i].GetVisibility();
 		LogManager::getSingleton().logMessage(d.str());
 	}
 	for (int i=0; i < visibleNodes.size(); ++i)
 	{
-		std::stringstream d; d << "Node visibility: " << visibleNodes[i].GetVisibiliy();
+		averageNodeVis += visibleNodes[i].GetVisibility();
+
+		std::stringstream d; d << "Node visibility: " << visibleNodes[i].GetVisibility();
 		LogManager::getSingleton().logMessage(d.str());
 	}
 
+	averageNodeVis /= (float)visibleNodes.size();
+	averageGeometryVis /= (float)visibleGeometry.size();
+
+	char str[100];
+
+	//-- update visibility queries stats
+    try 
+	{
+		//-- visibility queries stats
+		int idx = fromPoint ?  1 : 0;
+		idx += relativeVisibility ? 2 : 0;
+
+		sprintf(str, ": %s", queryTypeCaptions[idx].c_str()); 
+		mQueryTypeInfo->setCaption(str);
+
+		sprintf(str, ": %d", (int)visibleNodes.size()); 
+		mQueryVisibleNodesInfo->setCaption(str);
+	
+		sprintf(str,": %d", (int)visibleGeometry.size()); 
+		mQueryVisibleGeometryInfo->setCaption(str);
+		
+		sprintf(str,": %3.3f", averageNodeVis); 
+		mQueryNodeVisibilityInfo->setCaption(str);
+
+		sprintf(str,": %3.3f", averageGeometryVis); 
+		mQueryGeometryVisibilityInfo->setCaption(str);
+	}
+	catch (...)
+	{
+		// ignore
+	}
+
+	// show the results
+	if (!mShowQueryStats)
+	{
+		mQueryOverlay->show();
+		mShowQueryStats = true;
+	}
 }
 //-----------------------------------------------------------------------
@@ -399,5 +461,5 @@
 
 	it_end = mFrameInfo.end();
-	for(it = mFrameInfo.begin(); it < it_end; ++it)
+	for (it = mFrameInfo.begin(); it < it_end; ++it)
 	{
 		ofstr << StringConverter::toString((*it).position) << " " 
@@ -422,6 +484,6 @@
 		ifstr.getline(line, 256);
 		sscanf(line, "%f %f %f %f %f %f %f %f", &info.position.x, &info.position.y, &info.position.z,
-				&info.orientation.w, &info.orientation.x, &info.orientation.y, &info.orientation.z,
-				&info.timeElapsed);
+			   &info.orientation.w, &info.orientation.x, &info.orientation.y, &info.orientation.z,
+			   &info.timeElapsed);
 		
 		mFrameInfo.push_back(info);
@@ -447,5 +509,5 @@
 		//-- write out stats for recorded walkthrough
 		std::stringstream d;
-		d << "Algorithm: " << mCurrentAlgorithmCaptions[mCurrentAlgorithm] << "\n"
+		d << "Algorithm: " << currentAlgorithmCaptions[mCurrentAlgorithm] << "\n"
 		  << "avg. FPS: " << mWindow->getAverageFPS() << "\n"
 		  << "best FPS: " << mWindow->getBestFPS() << "\n"
@@ -532,5 +594,5 @@
 void TerrainFrameListener::setAlgorithm(int algorithm)
 {
-	mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
+	mAlgorithmInfo->setCaption(": " + currentAlgorithmCaptions[mCurrentAlgorithm]);
 	mSceneMgr->setOption("Algorithm", &mCurrentAlgorithm);
 }
@@ -548,5 +610,6 @@
 
 	// update stats when necessary
-    try {
+    try 
+	{
 		OverlayElement* guiAvg = OverlayManager::getSingleton().getOverlayElement("Core/AverageFps");
 		OverlayElement* guiCurr = OverlayManager::getSingleton().getOverlayElement("Core/CurrFps");
@@ -591,7 +654,7 @@
 		mObjectsInfo->setCaption(str);
 	}
-	catch(...)
-	{
-            // ignore
+	catch (...)
+	{
+		// ignore
 	}
 }
@@ -787,5 +850,5 @@
 
 	case KC_LSHIFT:
-		mShiftPressed = !mShiftPressed;
+		mShiftPressed = true;
 		break;
 	//KEY_PRESSED(KC_F3, 0.3, writeFrames());
@@ -798,6 +861,6 @@
     {
         // Print camera details
-        mWindow->setDebugText("P: " + StringConverter::toString(mCamera->getDerivedPosition()) + " " + 
-            "O: " + StringConverter::toString(mCamera->getDerivedOrientation()));
+        mWindow->setDebugText("P: " + StringConverter::toString(mCamera->getDerivedPosition()) + 
+			" " + "O: " + StringConverter::toString(mCamera->getDerivedOrientation()));
     }
 
@@ -809,4 +872,9 @@
 void TerrainFrameListener::keyReleased(KeyEvent* e)
 {
+	if (e->getKey() == KC_LSHIFT)
+	{
+		mShiftPressed = false;
+	}
+	
 	CEGUI::System::getSingleton().injectKeyUp(e->getKey());
 	e->consume();
@@ -867,19 +935,17 @@
 bool TerrainFrameListener::processUnbufferedKeyInput(const FrameEvent& evt)
 {
-	/*if (mInputDevice->isKeyDown(KC_ESCAPE))
-    {            
-        return false;
-    }*/
-
+	bool cursorPressed = false;
+	
 	/* Move camera forward by keypress. */
     if (mInputDevice->isKeyDown(KC_UP))
 	{
 		mTranslateVector.z = -mMoveScale;
-	}
-
+		cursorPressed = true;
+	}
     /* Move camera backward by keypress. */
     if (mInputDevice->isKeyDown(KC_DOWN))
     {
 		mTranslateVector.z = mMoveScale;
+		cursorPressed = true;
     }
 
@@ -887,4 +953,5 @@
     {
         mCamNode->yaw(-mRotScale);
+		cursorPressed = true;
     }
 	
@@ -892,4 +959,5 @@
     {
         mCamNode->yaw(mRotScale);
+		cursorPressed = true;
     }
 	// visualization camera
@@ -903,4 +971,10 @@
 	}
 
+	// show the results
+	if (cursorPressed && mShowQueryStats)
+	{
+		mQueryOverlay->hide();
+		mShowQueryStats = false;
+	}
 
     // Return true to continue rendering
Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 133)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 135)
@@ -162,6 +162,6 @@
 	if (!mTerrainContentGenerator->LoadObjects("objects.out"))
 	{
-		// height is restricted to 50, so no objects appear on peaks 
-		// => there is much occlusion
+		// to provide much occlusion,
+		// height is restricted to 50, so no objects appear on peaks
 		mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 50.0f, 3000.0f));
 		mTerrainContentGenerator->SetOffset(0);
Index: trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.h
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.h	(revision 133)
+++ trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.h	(revision 135)
@@ -121,4 +121,6 @@
 	*/
 	void ApplyVisibilityQuery(bool fromPoint, bool relativeVisibility);
+
+	void toggleShowQueryStats();
 
 protected:
@@ -156,4 +158,11 @@
 	OverlayElement *mObjectsInfo;
 	OverlayElement *mQueriesIssuedInfo;
+
+	OverlayElement *mQueryTypeInfo;
+	OverlayElement *mQueryVisibleNodesInfo;
+    OverlayElement *mQueryVisibleGeometryInfo;
+	OverlayElement *mQueryVisiblityInfo;
+	OverlayElement *mQueryNodeVisibilityInfo;
+	OverlayElement *mQueryGeometryVisibilityInfo;
 
 	RayQueryExecutor *mRayQueryExecutor;
@@ -173,5 +182,5 @@
 	bool mShutdownRequested;
 	bool mDisplayCameraDetails;
-
+	
 	Real mVizCameraHeight;
 
@@ -208,4 +217,5 @@
 	Overlay* mHelpOverlay;
 	Overlay* mCullStatsOverlay;
+	Overlay* mQueryOverlay;
 
 	Light *mSunLight;
@@ -214,4 +224,6 @@
 	
 	bool mShiftPressed;
+	bool mShowQueryStats;
+
 	//bool mUseBufferedInputKeys, mUseBufferedInputMouse, mInputTypeSwitchingOn;
 };
Index: trunk/VUT/work/ogre_changes/OgreMain/src/OgreSceneManager.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/OgreMain/src/OgreSceneManager.cpp	(revision 131)
+++ trunk/VUT/work/ogre_changes/OgreMain/src/OgreSceneManager.cpp	(revision 135)
@@ -779,6 +779,5 @@
 void SceneManager::_renderScene(Camera* camera, Viewport* vp, bool includeOverlays)
 {
-	LogManager::getSingleton().logMessage("***********RENDER SCENE************");
-    Root::getSingleton()._setCurrentSceneManager(this);
+	Root::getSingleton()._setCurrentSceneManager(this);
     // Prep Pass for use in debug shadows
     initShadowVolumeMaterials();
@@ -4200,9 +4199,9 @@
 void SceneManager::_renderSceneNode(Camera *cam, SceneNode *node, bool leaveTransparentsInQueue)
 {
+	// delete previously rendered objects from renderqueue
+	_deleteRenderedQueueGroups(leaveTransparentsInQueue);
+
 	node->_findVisibleObjects(cam, getRenderQueue(), false, mDisplayNodes, false);
 	SceneManager::_renderVisibleObjects();
-
-	// delete all rendered objects from renderqueue
-	_deleteRenderedQueueGroups(leaveTransparentsInQueue);
 }
 //-----------------------------------------------------------------------
@@ -4248,10 +4247,9 @@
 void SceneManager::_renderMovableObject(MovableObject *mov, const bool leaveTransparentsInQueue)
 {
+	// delete previously rendered objects from renderqueue
+	_deleteRenderedQueueGroups(leaveTransparentsInQueue);
+
 	mov->_updateRenderQueue(getRenderQueue());
 	SceneManager::_renderVisibleObjects();
-	
-	// delete rendered objects from renderqueue
-	//TODO: should be first
-	_deleteRenderedQueueGroups(leaveTransparentsInQueue);
 }
 #endif //GTP_VISIBILITY_MODIFIED_OGRE
Index: trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/include/OgreOctree.h
===================================================================
--- trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/include/OgreOctree.h	(revision 104)
+++ trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/include/OgreOctree.h	(revision 135)
@@ -157,40 +157,48 @@
 
 #ifdef GTP_VISIBILITY_MODIFIED_OGRE
-	public:
-		/** Returns last visited frame id. */
-		int lastVisited(void);
-		/** Set to current frame id.
-        @param current frame id.
-        */
-		void setLastVisited(int frameid);
-		/** Makes this octree become visible / invisble.
-        @param visible Whether this node is to be made visible or invisible
-        */
-		void setOctreeVisible(bool visible);
-		/** Returns true if this node is marked visible, false otherwise. 
-		*/
-		bool isOctreeVisible(void);
-		/** Gets this node's parent (NULL if this is the root).
-		*/
-		Octree *getParent();
-		/** Frame id when this octree was last rendered. 
-		@return last rendered frame id
-		*/	
-		int lastRendered(void);
-		/** Sets frame id when this octree was last rendered. 
-		@param last rendered frame id
-		*/
-		void setLastRendered(int frameid);
-		/** Returns current depth of octree 
-		@return current depth
-		*/
-		int getDepth();
-
-	protected:
-
-		int mLastRendered;
-		int mLastVisited;
-		bool mVisible;
-		int mDepth;
+public:
+	/** Returns last visited frame id. */
+	int lastVisited(void);
+	/** Set to current frame id.
+    @param current frame id.
+    */
+	void setLastVisited(int frameid);
+	/** Makes this octree become visible / invisble.
+    @param visible Whether this node is to be made visible or invisible
+    */
+	void setOctreeVisible(bool visible);
+	/** Returns true if this node is marked visible, false otherwise. 
+	*/
+	bool isOctreeVisible(void);
+	/** Gets this node's parent (NULL if this is the root).
+	*/
+	Octree *getParent();
+	/** Frame id when this octree was last rendered. 
+	@return last rendered frame id
+	*/	
+	int lastRendered(void);
+	/** Sets frame id when this octree was last rendered. 
+	@param last rendered frame id
+	*/
+	void setLastRendered(int frameid);
+	/** Returns current depth of octree 
+	@return current depth
+	*/
+	int getDepth();
+	/** Returns real extent of the octree, i.e., the merged extent of the bounding boxes. */
+	AxisAlignedBox _getWorldAABB(void) const;
+
+	/** Updates bounds of real aabb of octree. */
+	void _updateBounds();
+
+protected:
+	
+	/** the real extent of the octree. */
+	AxisAlignedBox mWorldAABB;
+
+	int mLastRendered;
+	int mLastVisited;
+	bool mVisible;
+	int mDepth;
 
 #endif // GTP_VISIBILITY_MODIFIED_OGRE
Index: trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctree.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctree.cpp	(revision 115)
+++ trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctree.cpp	(revision 135)
@@ -142,4 +142,8 @@
     _ref();
 
+#ifdef GTP_VISIBILITY_MODIFIED_OGRE
+	_updateBounds();
+#endif
+
 }
 
@@ -151,4 +155,8 @@
     //update total counts.
     _unref();
+
+#ifdef GTP_VISIBILITY_MODIFIED_OGRE
+	_updateBounds();
+#endif
 }
 
@@ -165,5 +173,6 @@
         mWireBoundingBox = new WireBoundingBox();
 
-	mWireBoundingBox->setupBoundingBox(mBox);
+	//mWireBoundingBox->setupBoundingBox(mBox);
+	mWireBoundingBox->setupBoundingBox(mWorldAABB);
     return mWireBoundingBox;
 }
@@ -171,5 +180,5 @@
 #ifdef GTP_VISIBILITY_MODIFIED_OGRE
 //-----------------------------------------------------------------------	
-int Octree::lastVisited(void)
+int Octree::lastVisited()
 {
 	return mLastVisited;
@@ -181,5 +190,5 @@
 }
 //-----------------------------------------------------------------------	
-int Octree::lastRendered(void)
+int Octree::lastRendered()
 {
 	return mLastRendered;
@@ -196,5 +205,5 @@
 }
 //-----------------------------------------------------------------------	
-bool Octree::isOctreeVisible(void)
+bool Octree::isOctreeVisible()
 {
 	return mVisible;
@@ -210,4 +219,45 @@
 	return mDepth;
 }
+//-----------------------------------------------------------------------	
+AxisAlignedBox Octree::_getWorldAABB(void) const
+{
+	return mWorldAABB;
+}
+//-----------------------------------------------------------------------
+void Octree::_updateBounds()
+{
+	// Reset bounds first
+    mWorldAABB.setNull();
+
+    // Update bounds from own attached objects
+	NodeList::iterator it, it_end;
+	it_end = mNodes.end();
+
+    for (it = mNodes.begin(); it != it_end; ++it)
+    {
+        // Merge world bounds of each object
+		mWorldAABB.merge((*it)->_getWorldAABB());
+    }
+
+    // Merge with children
+	for (int i = 0; i < 2; ++i)
+    {
+        for (int j = 0; j < 2; ++j)
+        {
+            for (int k = 0; k < 2; ++k)
+            {
+                if (mChildren[i][j][k] != 0)
+				{
+					mWorldAABB.merge(mChildren[i][j][k]->_getWorldAABB());
+				}
+            }
+        }
+	}
+	// recursively update parent bounds
+	if (mParent)
+	{
+		mParent->_updateBounds();
+	}
+ }
 #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 115)
+++ trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctreeSceneManager.cpp	(revision 135)
@@ -428,4 +428,10 @@
             _addOctreeNode( onode, mOctree );
     }
+#ifdef GTP_VISIBILITY_MODIFIED_OGRE
+	else // still update because bounding box may be different
+	{
+		onode -> getOctant() -> _updateBounds();
+	}
+#endif // GTP_VISIBILITY_MODIFIED_OGRE
 }
 
@@ -1172,4 +1178,7 @@
 									   bool leaveTransparentsInQueue)//, bool useZPassQueue)
 {
+	// delete previously rendered objects from the render queue
+	_deleteRenderedQueueGroups(leaveTransparentsInQueue);
+
 	//Add stuff to be rendered;
     NodeList::iterator it = octant->mNodes.begin();
@@ -1211,6 +1220,4 @@
 	//-- the actual rendering
 	SceneManager::_renderVisibleObjects();
-	// delete all rendered objects from the render queue
-	_deleteRenderedQueueGroups(leaveTransparentsInQueue);
 }
 #endif // GTP_VISIBILITY_MODIFIED_OGRE
