Index: trunk/VUT/GtpVisibility/include/HierarchyInterface.h
===================================================================
--- trunk/VUT/GtpVisibility/include/HierarchyInterface.h	(revision 85)
+++ trunk/VUT/GtpVisibility/include/HierarchyInterface.h	(revision 86)
@@ -36,5 +36,6 @@
 		@returns occlusion query for this node
 	*/
-	virtual OcclusionQuery *IssueOcclusionQuery(HierarchyNode *node) = 0;
+	virtual OcclusionQuery *IssueOcclusionQuery(HierarchyNode *node, 
+		const bool wasVisible = false) = 0;
 	/** Sets the root of the scene hierarchy.
 		@param root the hierarchy root
@@ -105,7 +106,13 @@
 	*/
 	unsigned int GetNumRenderedNodes();
+	/** Optimization when issuing occlusion test: test is done with actual geometry 
+		rather than the bounding box
+		@param useOptimization if the optimization should be used
+	*/
+	void SetUseOptimization(bool useOptimization);
 
 protected:
 	
+	bool mUseOptimization;
 	unsigned int mFrameId;
 
Index: trunk/VUT/GtpVisibility/include/OcclusionQuery.h
===================================================================
--- trunk/VUT/GtpVisibility/include/OcclusionQuery.h	(revision 59)
+++ trunk/VUT/GtpVisibility/include/OcclusionQuery.h	(revision 86)
@@ -9,14 +9,12 @@
 {
 public:
-//	OcclusionQuery();
 	virtual ~OcclusionQuery() {};
 	/** Returns the result of an occlusion query in terms of visible pixels.
-		@returns number of visible pixels
+		@param queryResult the number of visible pixels if the result was available
+		@param waitForResult if we should wait for the result until available
+		@returns if the result was already available
 	*/
-	virtual unsigned int GetQueryResult() const = 0;
-	/** Returns true if the result of the query is available, false otherwise.
-		@returns if result is available
-	*/
-	virtual bool ResultAvailable() const = 0;
+	virtual bool GetQueryResult(unsigned int &queryResult, 
+		const bool waitForResult) const = 0;
 	/** Begins occlusion query.
 		@remark the query counts the number of visible pixels between it's begin and end
Index: trunk/VUT/GtpVisibility/src/FrustumCullingManager.cpp
===================================================================
--- trunk/VUT/GtpVisibility/src/FrustumCullingManager.cpp	(revision 85)
+++ trunk/VUT/GtpVisibility/src/FrustumCullingManager.cpp	(revision 86)
@@ -1,4 +1,4 @@
 #include "FrustumCullingManager.h"
-//#include <windows.h>
+#include <windows.h>
 
 namespace GtpVisibility {
Index: trunk/VUT/GtpVisibility/src/CoherentHierarchicalCullingManager.cpp
===================================================================
--- trunk/VUT/GtpVisibility/src/CoherentHierarchicalCullingManager.cpp	(revision 85)
+++ trunk/VUT/GtpVisibility/src/CoherentHierarchicalCullingManager.cpp	(revision 86)
@@ -1,4 +1,4 @@
 #include "CoherentHierarchicalCullingManager.h"
-//#include <windows.h>
+#include <windows.h>
 
 namespace GtpVisibility {
@@ -8,19 +8,19 @@
 {
 	mNumFrustumCulledNodes = mNumQueryCulledNodes = 0;
-	//OutputDebugString("Coherent Culling\n");
+//	OutputDebugString("Coherent Culling\n");
 
 	QueryQueue queryQueue;
-	
+	unsigned int visiblePixels = 0;
+	bool isAvailable = false;
+
 	//-- PART 1: process finished occlusion queries
 	while (!mHierarchyInterface->GetQueue()->empty() || !queryQueue.empty())
 	{
 		while (!queryQueue.empty() && 
-			   (queryQueue.front().second->ResultAvailable() || mHierarchyInterface->GetQueue()->empty()))
+			   queryQueue.front().second->GetQueryResult(visiblePixels, 
+									mHierarchyInterface->GetQueue()->empty()))
 		{
 			HierarchyNode *node = queryQueue.front().first;
-					
-			// wait until result available
-			unsigned int visiblePixels = queryQueue.front().second->GetQueryResult();
-			
+						
 			queryQueue.pop();
 
@@ -67,5 +67,6 @@
 				
 				// identify nodes that we cannot skip queries for
-				bool mustQuery = !wasVisible || mHierarchyInterface->HasGeometry(node) || mHierarchyInterface->IsLeaf(node);
+				bool mustQuery = !wasVisible || mHierarchyInterface->HasGeometry(node) || 
+					mHierarchyInterface->IsLeaf(node);
 
 				// reset node's visibility classification 
@@ -78,5 +79,6 @@
 				if (mustQuery)
 				{
-					queryQueue.push(QueryPair(node, mHierarchyInterface->IssueOcclusionQuery(node)));
+					queryQueue.push(QueryPair(node, 
+						mHierarchyInterface->IssueOcclusionQuery(node, wasVisible)));
 				}
 					
Index: trunk/VUT/GtpVisibility/src/HierarchyInterface.cpp
===================================================================
--- trunk/VUT/GtpVisibility/src/HierarchyInterface.cpp	(revision 85)
+++ trunk/VUT/GtpVisibility/src/HierarchyInterface.cpp	(revision 86)
@@ -8,5 +8,5 @@
 HierarchyInterface::HierarchyInterface(): 
 mFrameId(0), mNumTraversedNodes(0), mNumRenderedNodes(0), 
-mSceneRoot(0), mPreviousNode(0), mCurrentTestIdx(0)
+mSceneRoot(0), mPreviousNode(0), mCurrentTestIdx(0), mUseOptimization(true)
 {	
 	mDistanceQueue = new DistanceQueue(GreaterDistance<HierarchyNode *>(this));
@@ -64,3 +64,9 @@
 	return mNumRenderedNodes;
 }
+
+void HierarchyInterface::SetUseOptimization(bool useOptimization)
+{
+	mUseOptimization = useOptimization;
+}
+
 } // namespace GtpVisibility
Index: trunk/VUT/GtpVisibility/src/StopAndWaitCullingManager.cpp
===================================================================
--- trunk/VUT/GtpVisibility/src/StopAndWaitCullingManager.cpp	(revision 85)
+++ trunk/VUT/GtpVisibility/src/StopAndWaitCullingManager.cpp	(revision 86)
@@ -1,4 +1,4 @@
 #include "StopAndWaitCullingManager.h"
-//#include <windows.h>
+#include <windows.h>
 
 namespace GtpVisibility {
@@ -35,7 +35,10 @@
 			}
 
+			unsigned int visiblePixels = 0;
+
+			mHierarchyInterface->IssueOcclusionQuery(node)->GetQueryResult(visiblePixels, true);
+
 			// node visible
-			if (mHierarchyInterface->IssueOcclusionQuery(node)->GetQueryResult() > 
-				mVisibilityThreshold)
+			if (visiblePixels > mVisibilityThreshold)
 			{
 				mHierarchyInterface->TraverseNode(node);
Index: trunk/VUT/chcdemo/RenderTraverser.cpp
===================================================================
--- trunk/VUT/chcdemo/RenderTraverser.cpp	(revision 43)
+++ trunk/VUT/chcdemo/RenderTraverser.cpp	(revision 86)
@@ -56,5 +56,5 @@
 
 /**
-This is the standard render traversal algorithm doing only frustum culling
+	This is the standard render traversal algorithm doing only frustum culling
 */
 void RenderTraverser::RenderCullFrustum()
@@ -144,5 +144,5 @@
 	{
 		while(!queryQueue.empty() && 
-			  (ResultAvailable(queryQueue.front()) || 	mDistanceQueue.empty()))
+			  (ResultAvailable(queryQueue.front()) || mDistanceQueue.empty()))
 		{
 			HierarchyNode *node = queryQueue.front();
@@ -327,5 +327,5 @@
 }
 
-void RenderTraverser::IssueOcclusionQuery(HierarchyNode *node, bool wasVisible)
+void RenderTraverser::IssueOcclusionQuery(HierarchyNode *node, const bool wasVisible)
 {
 	// get next available test id
Index: trunk/VUT/chcdemo/HierarchyNode.h
===================================================================
--- trunk/VUT/chcdemo/HierarchyNode.h	(revision 10)
+++ trunk/VUT/chcdemo/HierarchyNode.h	(revision 86)
@@ -34,6 +34,6 @@
 
 /** This class represents a node in a k-d tree hierarchy. A
-node has two children. The node can be either an interior node (i.e., left and right child
-NULL) or a leaf node, which holds the actual geometry.
+	node has two children. The node can be either an interior node (i.e., left and right child
+	NULL) or a leaf node, which holds the actual geometry.
 */
 
Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 85)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 86)
@@ -96,5 +96,8 @@
 
 	mTerrainContentGenerator = new TerrainContentGenerator(mSceneMgr);
+	
+	mTerrainContentGenerator->GenerateScene(500, "ninja.mesh");
 	mTerrainContentGenerator->GenerateScene(500, "robot.mesh");
+
 	// no limitations needed anymore: the user can set 
 	// objects also on peaks of terrain
@@ -137,20 +140,19 @@
 									   CEGUI::Renderer *renderer,
 									   TerrainContentGenerator *sceneGenerator): 
-ExampleFrameListener(win, cam, false, true), mGUIRenderer(renderer), 
-mShutdownRequested(false)
-{
-	// Setup default variables
-	mCurrentObject = NULL;
-	mLMouseDown = false;
-	mRMouseDown = false;
-	mSceneMgr = sceneManager;
-	mTerrainContentGenerator = sceneGenerator;
-
-    // Reduce move speed
+ExampleFrameListener(win, cam, false, true), 
+mGUIRenderer(renderer), 
+mShutdownRequested(false), 
+mUseOptimization(false),
+mLMouseDown(false),
+mRMouseDown(false),
+mSceneMgr(sceneManager),
+mCurrentObject(NULL),
+mTerrainContentGenerator(sceneGenerator),
+mVisibilityThreshold(0),
+mCurrentAlgorithm(GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING)
+{
+	// Reduce move speed
 	mMoveSpeed = 50;
 	mRotateSpeed *= 2;
-
-	mCurrentAlgorithm = GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING;
-	mVisibilityThreshold = 0;
     
 	// Register this so that we get mouse events.
@@ -173,4 +175,5 @@
 	mRenderedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/RenderedNodesInfo");
 	mNumObjectsInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/NumObjectsInfo");
+	mUseOptimizationInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/UseOptimizationInfo");
 
 	mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
@@ -182,6 +185,9 @@
 	mRenderedNodesInfo->setCaption(": 0");
 	mNumObjectsInfo->setCaption(": 0");
+	mUseOptimizationInfo->setCaption(": true");
 
 	setAlgorithm(mCurrentAlgorithm);
+	toggleUseOptimization();
+	toggleShowOctree();
 
     pOver->show();
@@ -293,4 +299,6 @@
 	KEY_PRESSED(KC_SUBTRACT, 0, changeThreshold(-10));
 	KEY_PRESSED(KC_ADD, 0, changeThreshold(10));
+	KEY_PRESSED(KC_O, 0.3, toggleUseOptimization());
+	KEY_PRESSED(KC_S, 0.3, toggleShowOctree());
 	//KEY_PRESSED(KC_T, 1, change);
       
@@ -321,4 +329,5 @@
 void MouseQueryListener::setAlgorithm(int algorithm)
 {
+	//OutputDebugString("changing algorithm\n");
 	mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
 	mSceneMgr->setOption("Algorithm", &mCurrentAlgorithm);
@@ -347,4 +356,23 @@
 	sprintf(str,": %d", mTerrainContentGenerator->GetObjectCount()); 
 	mNumObjectsInfo->setCaption(str);
+}
+//-----------------------------------------------------------------------
+void MouseQueryListener::toggleUseOptimization()
+{
+	mUseOptimization = !mUseOptimization;
+
+	mSceneMgr->setOption("UseOptimization", &mUseOptimization);
+
+	if(mUseOptimization)
+		mUseOptimizationInfo->setCaption(": true");
+	else
+		mUseOptimizationInfo->setCaption(": false");
+}
+//-----------------------------------------------------------------------
+void MouseQueryListener::toggleShowOctree()
+{
+	mShowOctree = !mShowOctree;
+
+	mSceneMgr->setOption("ShowOctree", &mShowOctree);
 }
 //-----------------------------------------------------------------------
Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.h
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.h	(revision 85)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.h	(revision 86)
@@ -63,4 +63,6 @@
 	void changeThreshold(int incr);
 	void changeStats();
+	void toggleUseOptimization();
+	void toggleShowOctree();
 
 protected:
@@ -80,5 +82,5 @@
     OverlayElement *mTraversedNodesInfo;
 	OverlayElement *mHierarchyNodesInfo;
-	//OverlayElement *mSceneNodesInfo;
+	OverlayElement *mUseOptimizationInfo;
 	OverlayElement *mRenderedNodesInfo;
 	OverlayElement *mNumObjectsInfo;
@@ -89,4 +91,7 @@
 	RayQueryExecutor *mRayQueryExecutor;
 	TerrainContentGenerator *mTerrainContentGenerator;
+
+	bool mUseOptimization;
+	bool mShowOctree;
 };
 
Index: trunk/VUT/work/TestCulling/SceneContentGenerator.cpp
===================================================================
--- trunk/VUT/work/TestCulling/SceneContentGenerator.cpp	(revision 85)
+++ trunk/VUT/work/TestCulling/SceneContentGenerator.cpp	(revision 86)
@@ -20,5 +20,6 @@
 mMaxPos(Vector3(70.0f, 70.0f, 600.0f)), 
 mMinAngle(Vector3(0.0f, 0.0f, 0.0f)),
-mMaxAngle(Vector3(360, 360, 360))
+mMaxAngle(Vector3(360, 360, 360)),
+mScale(0.05, 0.05, 0.05)
 {
 }
@@ -67,5 +68,5 @@
 
 	currentObject->attachObject(ent);
-	currentObject->setScale(0.1f, 0.1f, 0.1f);
+	currentObject->setScale(mScale);
 		
 	currentObject->yaw(Degree(rotation.x));
@@ -106,3 +107,8 @@
 	return mCount;
 }
+//-----------------------------------------------------------------------
+void SceneContentGenerator::SetScale(Vector3 scale)
+{
+	mScale = scale;
+}
 } // namespace Ogre
Index: trunk/VUT/work/TestCulling/TestCullingApplication.cpp
===================================================================
--- trunk/VUT/work/TestCulling/TestCullingApplication.cpp	(revision 85)
+++ trunk/VUT/work/TestCulling/TestCullingApplication.cpp	(revision 86)
@@ -54,5 +54,5 @@
 
 	mSceneContentGenerator = new SceneContentGenerator(mSceneMgr);
-	mSceneContentGenerator->GenerateScene(330, "robot.mesh");
+	mSceneContentGenerator->GenerateScene(3000, "sphere.mesh");
 
 	// Create a skybox
@@ -101,23 +101,19 @@
 									   CEGUI::Renderer *renderer,
 									   SceneContentGenerator *sceneContentGenerator)
-        : ExampleFrameListener(win, cam, false, true), mGUIRenderer(renderer), 
-		mShutdownRequested(false)
-{
-
-	// Setup default variables
-	//mOgreHead = NULL;
-	mLMouseDown = false;
-	mRMouseDown = false;
-	mSceneMgr = sceneManager;
-
-	mSceneContentGenerator = sceneContentGenerator;
-
+: ExampleFrameListener(win, cam, false, true),
+mSceneMgr(sceneManager),
+mGUIRenderer(renderer), 
+mShutdownRequested(false), 
+mUseOptimization(false),
+mLMouseDown(false),
+mRMouseDown(false),
+mSceneContentGenerator(sceneContentGenerator),
+mVisibilityThreshold(0),
+mCurrentAlgorithm(GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING)
+{
     // Reduce move speed
 	mMoveSpeed = 50;
 	mRotateSpeed *= 2;
-
-	mCurrentAlgorithm = GtpVisibility::VisibilityEnvironment::COHERENT_HIERARCHICAL_CULLING;
-	mVisibilityThreshold = 0;
-    
+ 
 	// Register this so that we get mouse events.
 	mEventProcessor->addMouseListener(this);
@@ -137,4 +133,5 @@
 	mRenderedNodesInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/RenderedNodesInfo");
 	mNumObjectsInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/NumObjectsInfo");
+	mUseOptimizationInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/UseOptimizationInfo");
 
 	mAlgorithmInfo->setCaption(": " + mCurrentAlgorithmCaptions[mCurrentAlgorithm]);
@@ -146,6 +143,8 @@
 	mRenderedNodesInfo->setCaption(": 0");
 	mNumObjectsInfo->setCaption(": 0");
+	mUseOptimizationInfo->setCaption(": true");
 
 	setAlgorithm(mCurrentAlgorithm);
+	toggleUseOptimization();
 
     pOver->show();
@@ -226,4 +225,5 @@
 	KEY_PRESSED(KC_SUBTRACT, 0, changeThreshold(-10));
 	KEY_PRESSED(KC_ADD, 0, changeThreshold(10));
+	KEY_PRESSED(KC_O, 0.3, toggleUseOptimization());
 	//KEY_PRESSED(KC_T, 1, change);
       
@@ -250,4 +250,23 @@
 
 	setAlgorithm(mCurrentAlgorithm);
+}
+//-----------------------------------------------------------------------
+void MouseQueryListener::toggleUseOptimization()
+{
+	mUseOptimization = !mUseOptimization;
+
+	mSceneMgr->setOption("UseOptimization", &mUseOptimization);
+
+	if(mUseOptimization)
+		mUseOptimizationInfo->setCaption(": true");
+	else
+		mUseOptimizationInfo->setCaption(": false");
+}
+//-----------------------------------------------------------------------
+void MouseQueryListener::toggleShowOctree()
+{
+	mShowOctree = !mShowOctree;
+
+	mSceneMgr->setOption("ShowOctree", &mShowOctree);
 }
 //-----------------------------------------------------------------------
Index: trunk/VUT/work/TestCulling/SceneContentGenerator.h
===================================================================
--- trunk/VUT/work/TestCulling/SceneContentGenerator.h	(revision 85)
+++ trunk/VUT/work/TestCulling/SceneContentGenerator.h	(revision 86)
@@ -35,4 +35,5 @@
 	void SetMaxPos(Vector3 maxPos);
 	int GetObjectCount();
+	void SetScale(Vector3 scale);
 
 protected:
@@ -42,5 +43,6 @@
 	Vector3 mMaxPos;
 	Vector3 mMinPos;
-	
+	Vector3 mScale;
+
 	SceneManager *mSceneMgr;
 	int mCount;		// The number of objects on the screen
Index: trunk/VUT/work/TestCulling/TestCullingApplication.h
===================================================================
--- trunk/VUT/work/TestCulling/TestCullingApplication.h	(revision 85)
+++ trunk/VUT/work/TestCulling/TestCullingApplication.h	(revision 86)
@@ -64,4 +64,6 @@
 	void changeThreshold(int incr);
 	void changeStats();
+	void toggleUseOptimization();
+	void toggleShowOctree();
 
 protected:
@@ -83,6 +85,10 @@
 	OverlayElement *mRenderedNodesInfo;
 	OverlayElement *mNumObjectsInfo;
+	OverlayElement *mUseOptimizationInfo;
 
 	SceneContentGenerator *mSceneContentGenerator;
+
+	bool mUseOptimization;
+	bool mShowOctree;
 };
 
Index: trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h
===================================================================
--- trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h	(revision 85)
+++ trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h	(revision 86)
@@ -65,7 +65,9 @@
 	/** Issue a occlusion query for this node. 
 		@param node the current hierarchy node
+		@param wasVisible if the node was visible in the last frame
 		@returns occlusion query for this node
 	*/
-	GtpVisibility::OcclusionQuery *IssueOcclusionQuery(GtpVisibility::HierarchyNode *node);
+	GtpVisibility::OcclusionQuery *IssueOcclusionQuery(
+		GtpVisibility::HierarchyNode *node, const bool wasVisible);
 
 protected:
Index: trunk/VUT/Ogre/include/OgreSolidHalfBoundingBox.h
===================================================================
--- trunk/VUT/Ogre/include/OgreSolidHalfBoundingBox.h	(revision 85)
+++ trunk/VUT/Ogre/include/OgreSolidHalfBoundingBox.h	(revision 86)
@@ -16,5 +16,5 @@
 
 	SolidHalfBoundingBox();
-	/** Sets up the first or second half of a solid bounding box.
+	/** Sets up the first or second half of a the solid box.
 		@param aab the axis aligned bounding box
 		@param isFirstHalf if it is the first or the second half
@@ -22,10 +22,12 @@
 	void SetupBoundingBox(const AxisAlignedBox& aabb, const bool isFirstHalf);
 	
-protected:
-    /** Builds the wireframe line list.
+	/** Builds the wireframe line list.
 		@param aab the axis aligned bounding box for setting up the vertices
 		@param first or second half of the box
     */
 	void SetupBoundingBoxVertices(const AxisAlignedBox& aab, const bool isFirstHalf);
+
+protected:
+   
 	/** 
 		Sets the material used for occlusion queries. 
Index: trunk/VUT/Ogre/include/OgrePlatformOcclusionQuery.h
===================================================================
--- trunk/VUT/Ogre/include/OgrePlatformOcclusionQuery.h	(revision 59)
+++ trunk/VUT/Ogre/include/OgrePlatformOcclusionQuery.h	(revision 86)
@@ -20,6 +20,6 @@
 	virtual ~PlatformOcclusionQuery();
 
-	virtual unsigned int GetQueryResult() const;
-	virtual bool ResultAvailable() const;
+	virtual bool GetQueryResult(unsigned int &queryResult,
+		const bool waitForResult) const;
 	virtual void BeginQuery() const;
 	virtual void EndQuery() const;
Index: trunk/VUT/Ogre/src/OgreOctreeHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreOctreeHierarchyInterface.cpp	(revision 85)
+++ trunk/VUT/Ogre/src/OgreOctreeHierarchyInterface.cpp	(revision 86)
@@ -57,11 +57,6 @@
 												  GtpVisibility::HierarchyNode *node2) const
 {
-	// matt: change this (inefficient)
-	AxisAlignedBox box1, box2;
-
-	static_cast<Octree *>(node1)->_getCullBounds(&box1);
-	static_cast<Octree *>(node2)->_getCullBounds(&box2);
-
-	return GetSquaredViewDepth(mCamera, &box1) > GetSquaredViewDepth(mCamera, &box2);
+	return GetSquaredViewDepth(mCamera, &static_cast<Octree *>(node1)->mBox) > 
+		   GetSquaredViewDepth(mCamera, &static_cast<Octree *>(node2)->mBox);
 }
 //-----------------------------------------------------------------------
@@ -70,5 +65,12 @@
 {
 	Vector3 mid = ((box->getMinimum() - box->getMaximum()) * 0.5) + box->getMinimum();
-	return (cam->getDerivedPosition() - mid).squaredLength();                                                                   
+	// use nearest point rather than midpoint
+	Vector3 camPos = cam->getDerivedPosition();
+
+	Vector3 minPos(camPos.x < mid.x ? box->getMinimum().x : box->getMaximum().x,
+				   camPos.y < mid.y ? box->getMinimum().y : box->getMaximum().y,
+				   camPos.z < mid.z ? box->getMinimum().z : box->getMaximum().z);
+	
+	return (camPos - minPos).squaredLength();                                                                   
 }
 //-----------------------------------------------------------------------
Index: trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 74)
+++ trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 86)
@@ -18,7 +18,7 @@
 
 	//mDisplayNodes = true;
-	mShowBoundingBoxes = true;
-	mShowBoxes = true;
-	//mMaxDepth = 20;
+	//mShowBoundingBoxes = true;
+	//mShowBoxes = true;
+	mMaxDepth = 20;
 }
 //-----------------------------------------------------------------------
Index: trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 85)
+++ trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 86)
@@ -6,4 +6,5 @@
 #include "OgrePlatformHierarchyInterface.h"
 #include "OgrePlatformOcclusionQuery.h"
+#include <windows.h>
 
 namespace Ogre {
@@ -33,20 +34,21 @@
 void PlatformHierarchyInterface::RenderBoundingBox(AxisAlignedBox *box)
 {
+	static RenderOperation ro;
+
+	//TODO: this should be the full bounding box
+	SolidHalfBoundingBox *halfbox = GetSolidHalfBoundingBox();
+	
+	mRenderSystem->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
+	mSceneManager->useRenderableViewProjMode(halfbox);
+    mSceneManager->setPass(halfbox->getTechnique()->getPass(0));
+
 	// Render two halfes of the bounding box (using triangle fans)
 	for(int halfIdx = 0; halfIdx < 2; ++halfIdx)
 	{
-		//TODO: this should be the full bounding box
-		SolidHalfBoundingBox *halfbox = GetSolidHalfBoundingBox();
-		halfbox->SetupBoundingBox(*box, halfIdx == 1);
+		halfbox->SetupBoundingBoxVertices(*box, halfIdx == 1);
 				
-		mRenderSystem->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
-		
-		static RenderOperation ro;
-
-        mSceneManager->useRenderableViewProjMode(halfbox);
-		mSceneManager->setPass(GetSolidHalfBoundingBox()->getTechnique()->getPass(0));
 		halfbox->getRenderOperation(ro);
 		ro.srcRenderable = halfbox;
-		mRenderSystem->_render(ro);
+		mRenderSystem->_render(ro);	
 	}
 }
@@ -70,4 +72,5 @@
 {
 	GtpVisibility::HierarchyInterface::InitFrame(root);
+	mPreviousNode = NULL;
 	SetCamera(cam);
 }
@@ -94,5 +97,5 @@
 //-----------------------------------------------------------------------
 GtpVisibility::OcclusionQuery *PlatformHierarchyInterface::IssueOcclusionQuery(
-	GtpVisibility::HierarchyNode *node) 
+	GtpVisibility::HierarchyNode *node, const bool wasVisible) 
 {
 	// get next available test id
@@ -102,5 +105,16 @@
 	query->BeginQuery();
 			
-	RenderBoundingBox(GetBoundingBox(node));
+	// if leaf and was visible => will be rendered anyway, thus we
+	// can also test with the real geometry 
+	if(mUseOptimization && wasVisible && IsLeaf(node))
+	{
+	//	OutputDebugString("Using optimization!!\n");
+		RenderNode(node);
+	}
+	else
+	{
+	//	OutputDebugString("Not optimized!!\n");
+		RenderBoundingBox(GetBoundingBox(node));
+	}
 
 	query->EndQuery();
Index: trunk/VUT/Ogre/src/OgreSolidHalfBoundingBox.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreSolidHalfBoundingBox.cpp	(revision 85)
+++ trunk/VUT/Ogre/src/OgreSolidHalfBoundingBox.cpp	(revision 86)
@@ -26,6 +26,6 @@
 	Vector3 vmin = aab.getMinimum();
 		
-	Real sqLen = std::max(vmax.squaredLength(), vmin.squaredLength());
-    mRadius = Math::Sqrt(sqLen);
+	//Real sqLen = std::max(vmax.squaredLength(), vmin.squaredLength());
+    //mRadius = Math::Sqrt(sqLen);
 	
 	Real maxx = vmax.x;
@@ -90,5 +90,5 @@
 
     // setup the bounding box of this SimpleRenderable
-	setBoundingBox(aabb);
+	//setBoundingBox(aabb);
 }
 //-----------------------------------------------------------------------
Index: trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 85)
+++ trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 86)
@@ -12,11 +12,10 @@
 //namespace GtpVisibility {
 //-----------------------------------------------------------------------
-VisibilityTerrainSceneManager::VisibilityTerrainSceneManager(GtpVisibility::VisibilityManager *visManager)
-:mVisibilityManager(visManager)
+VisibilityTerrainSceneManager::VisibilityTerrainSceneManager(
+	GtpVisibility::VisibilityManager *visManager): mVisibilityManager(visManager)
 {
 	mHierarchyInterface = 
 		new OctreeHierarchyInterface(this, mDestRenderSystem);
 
-	setVisibilityManager(visManager);
 	//mShowBoxes = true;
 	//mDisplayNodes = true;
Index: trunk/VUT/Ogre/src/OgrePlatformOcclusionQuery.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformOcclusionQuery.cpp	(revision 59)
+++ trunk/VUT/Ogre/src/OgrePlatformOcclusionQuery.cpp	(revision 86)
@@ -1,4 +1,4 @@
 #include "OgrePlatformOcclusionQuery.h"
-
+#include <windows.h>
 namespace Ogre {
 
@@ -24,20 +24,8 @@
 }
 //-----------------------------------------------------------------------
-unsigned int PlatformOcclusionQuery::GetQueryResult() const
+bool PlatformOcclusionQuery::GetQueryResult(unsigned int &visiblePixels, 
+											const bool waitForResult) const
 {
-	unsigned int visiblePixels = 0;
-	// wait if result not available
-	mHardwareOcclusionQuery->pullOcclusionQuery(&visiblePixels);
-
-	return visiblePixels;
-}
-//-----------------------------------------------------------------------
-bool PlatformOcclusionQuery::ResultAvailable() const
-{
-#ifdef GTP_VISIBILITY_MODIFIED_OGRE
-	return mHardwareOcclusionQuery->resultAvailable();
-#else
-	return true;
-#endif
+	return mHardwareOcclusionQuery->pullOcclusionQuery(&visiblePixels, waitForResult);
 }
 
Index: trunk/VUT/Ogre/src/OgreVisibilityOptionsManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityOptionsManager.cpp	(revision 75)
+++ trunk/VUT/Ogre/src/OgreVisibilityOptionsManager.cpp	(revision 86)
@@ -20,5 +20,9 @@
 	{
 		mVisibilityManager->SetVisibilityCullingThreshold(*static_cast<const int *>(val));
-
+		return true;
+	}
+	if (key == "UseOptimization")
+	{
+		mHierarchyInterface->SetUseOptimization(*static_cast<const bool *>(val));
 		return true;
 	}
@@ -53,4 +57,5 @@
 		return true;
 	}
+	
 	return false;
 }
Index: trunk/VUT/Ogre/resources/VisibilityDemo.overlay
===================================================================
--- trunk/VUT/Ogre/resources/VisibilityDemo.overlay	(revision 84)
+++ trunk/VUT/Ogre/resources/VisibilityDemo.overlay	(revision 86)
@@ -10,5 +10,5 @@
 		top 5
 		width 450
-		height 55
+		height 60
 		material Core/StatsBlockCenter
         	border_size 1 1 1 1
@@ -69,4 +69,30 @@
 			left 155
 			top 20
+			width 90
+			height 30
+			font_name TrebuchetMSBold
+			char_height 16
+			caption :
+			colour_top 0.5 0.7 0.5
+			colour_bottom 0.3 0.5 0.3
+		}
+		element TextArea(Example/Visibility/UseOptimization)
+		{
+			metrics_mode pixels
+			left 5
+			top 35
+			width 90
+			height 30
+			font_name TrebuchetMSBold
+			char_height 16
+			caption [O] Optimization
+			colour_top 0.5 0.7 0.5
+			colour_bottom 0.3 0.5 0.3
+		}
+		element TextArea(Example/Visibility/UseOptimizationInfo)
+		{
+			metrics_mode pixels
+			left 155
+			top 35
 			width 90
 			height 30
