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;
 }
