Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTree.h	(revision 1192)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTree.h	(revision 1195)
@@ -27,4 +27,5 @@
 #include <stack>
 
+#include "HierarchyInterface.h"
 
 
@@ -210,16 +211,18 @@
 
 			// Gets this node's parent (NULL if this is the root).
-			Node *getParent(void) const { return mParent; };
-			// Gets the nodes left & right child nodes, or NULL if not present
-			// or node is leaf
-			virtual Node *getLeftChild(void) const = 0;
-			virtual Node *getRightChild(void) const = 0;
-			
-			// returns the level the node is on
-			int getLevel(void) const { return mLevel; };
-
+			KdTree::Node *getParent(void) const { return mParent; };
+
+			// Gets the nodes left & right child nodes, or NULL if not present or node is leaf
+			virtual KdTree::Node *getLeftChild(void) const = 0;
+			virtual KdTree::Node *getRightChild(void) const = 0;
+
+			// add contained objects to render queue
 			virtual void queueVisibleObjects(unsigned long currentFrame, 
 				Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes) = 0;
+
+			// add contained geometry (Entities) to list
+			virtual void getGeometryList(GtpVisibility::GeometryVector *geometryList) = 0;
 			
+			// create (when necessary), setup and return wire bounding box representing the node
 			WireBoundingBox * getWireBoundingBox()
 			{
@@ -238,6 +241,8 @@
 				
 				return mWBB;
-
 			}
+
+			// returns the level the node is on
+			int getLevel(void) const { return mLevel; };
 
 			// functions for the CHC hierarchy interface
@@ -270,9 +275,9 @@
 			*/
 			virtual void _updateBounds(bool recurse = true) = 0;
-
+			
+			/** bounding box of the node**/
 			AxisAlignedBox mAABB;
 
-			// for the CHC hierarchy interface
-			/** the real extent of the node. */
+			/** mounding box of all objects inside the node */
 			AxisAlignedBox mWorldAABB;
 		protected:
@@ -283,4 +288,5 @@
 			WireBoundingBox * mWBB;
 			
+			// for the CHC hierarchy interface
 			unsigned int mLastRendered;
 			unsigned int mLastVisited;
@@ -317,6 +323,6 @@
 
 			// a branch should have at least one child
-			virtual Node * getLeftChild() const { return mLeft; };
-			virtual Node * getRightChild() const { return mRight; };
+			virtual KdTree::Node * getLeftChild() const { return mLeft; };
+			virtual KdTree::Node * getRightChild() const { return mRight; };
 
 			virtual void queueVisibleObjects(unsigned long currentFrame, 
@@ -327,4 +333,7 @@
 						queue->addRenderable(getWireBoundingBox());
 			}
+
+			// a branch has no geometry, do nothing
+			virtual void getGeometryList(GtpVisibility::GeometryVector *geometryList) { }
 
 			// branches do not posses geometry => just merge child aabbs
@@ -369,10 +378,12 @@
 
 			// a leaf never has children 
-			virtual Node * getLeftChild() const { return 0; };
-			virtual Node * getRightChild() const { return 0; };
+			virtual KdTree::Node * getLeftChild() const { return 0; };
+			virtual KdTree::Node * getRightChild() const { return 0; };
 
 			virtual void queueVisibleObjects(unsigned long currentFrame, 
 				Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes);
 
+			virtual void getGeometryList(GtpVisibility::GeometryVector *geometryList);
+
 			// update the world aabb based on the contained geometry
 			virtual void _updateBounds(bool recurse = true);
@@ -394,6 +405,8 @@
 		struct SplitCandidate
 		{
-			SplitCandidate(PlaneEventList * e, int n, AxisAlignedBox& a, KdTree::Branch * p, Real c, Real d, SplitInfo * b, PlaneEvent::Side s):
-				events(e), nObjects(n), aabb(a), parent(p), cost(c), decrease(d), best(b), side(s) { };
+			SplitCandidate(PlaneEventList * e, int n, AxisAlignedBox& a,
+				KdTree::Branch * p, Real c, Real d, SplitInfo * b, PlaneEvent::Side s):
+			events(e), nObjects(n), aabb(a), parent(p), cost(c), decrease(d), best(b), side(s)
+			{ };
 
 			bool operator < (const SplitCandidate& rhs) const
@@ -504,5 +517,9 @@
 
 		// test visibility of objects and add to render	queue
-		void queueVisibleObjects(Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes = false);
+		void queueVisibleObjects(Camera* cam, RenderQueue* queue, bool onlyShadowCasters, 
+			bool showBoxes, KdTree::NodeList& visibleNodes);
+
+		// find visible nodes & place in list
+		//void findVisibleNodes(NodeList& visibleNodes, Camera * cam);
 
 		// self-explanatory ...
@@ -536,6 +553,9 @@
 
 		// recursive rendering function
-		void recQueueVisibleObjects(KdTree::Node * node, unsigned long currentFrame, 
-			Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes);
+		void recQueueVisibleObjects(KdTree::Node * node, unsigned long currentFrame, Camera* cam,
+			RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, KdTree::NodeList& visibleNodes);
+
+		// recursively find visible nodes
+		//void recFindVisibleNodes(KdTree::Node * node, NodeList& visibleNodes, Camera * cam);
 
 		// the root node of the kdtree
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdRenderable.h
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdRenderable.h	(revision 1173)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdRenderable.h	(revision 1195)
@@ -12,5 +12,4 @@
 
 #include "OgreKdTree.h"
-#include "HierarchyInterface.h"
 
 namespace Ogre
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/scripts/Plugin_VisibilitySceneManager.vcproj
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/scripts/Plugin_VisibilitySceneManager.vcproj	(revision 1165)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/scripts/Plugin_VisibilitySceneManager.vcproj	(revision 1195)
@@ -21,5 +21,5 @@
 				Optimization="0"
 				AdditionalIncludeDirectories="&quot;$(OGRE_PATH)\PlugIns\OctreeSceneManager\include&quot;;&quot;$(OGRE_PATH)\Samples\Common\include&quot;;&quot;$(OGRE_PATH)\Dependencies\include\CEGUI&quot;;&quot;$(OGRE_PATH)\Samples\Common\CEGUIRenderer\include&quot;;&quot;$(OGRE_PATH)\OgreMain\include&quot;;&quot;$(OGRE_PATH)\Dependencies\include&quot;;..\include;..\..\..\OnlineCullingCHC\include;..\..\..\Preprocessing\src"
-				PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;PLUGIN_OCCLUSIONCULLINGSCENEMANAGER_EXPORTS;GTP_VISIBILITY_MODIFIED_OGRE;KDTREE_DEBUG"
+				PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;PLUGIN_OCCLUSIONCULLINGSCENEMANAGER_EXPORTS;GTP_VISIBILITY_MODIFIED_OGRE"
 				MinimalRebuild="TRUE"
 				BasicRuntimeChecks="3"
@@ -89,5 +89,5 @@
 				OptimizeForProcessor="2"
 				AdditionalIncludeDirectories="&quot;$(OGRE_PATH)\PlugIns\OctreeSceneManager\include&quot;;&quot;$(OGRE_PATH)\Samples\Common\include&quot;;&quot;$(OGRE_PATH)\Dependencies\include\CEGUI&quot;;&quot;$(OGRE_PATH)\Samples\Common\CEGUIRenderer\include&quot;;&quot;$(OGRE_PATH)\OgreMain\include&quot;;&quot;$(OGRE_PATH)\Dependencies\include&quot;;..\include;..\..\..\OnlineCullingCHC\include;..\..\..\Preprocessing\src"
-				PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;GTP_VISIBILITY_MODIFIED_OGRE;KDTREE_DEBUG"
+				PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;GTP_VISIBILITY_MODIFIED_OGRE"
 				StringPooling="TRUE"
 				RuntimeLibrary="2"
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeSceneManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeSceneManager.cpp	(revision 1192)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeSceneManager.cpp	(revision 1195)
@@ -539,6 +539,14 @@
 	{
 		getRenderQueue()->clear();
-		if (mKdTree)
-			mKdTree->queueVisibleObjects(cam, getRenderQueue(), onlyShadowCasters, mShowBoxes);
+		if (mShowVisualization)
+		{
+			PrepareVisualization(cam);
+		}
+		else
+		{
+			mVisibleNodes.clear();
+			if (mKdTree)
+				mKdTree->queueVisibleObjects(cam, getRenderQueue(), onlyShadowCasters, mShowBoxes, mVisibleNodes);
+		}
 	}
 	else
@@ -561,5 +569,5 @@
 				getRenderQueue()->clear();
 				if (mKdTree)
-					mKdTree->queueVisibleObjects(cam, getRenderQueue(), onlyShadowCasters, mShowBoxes);
+					mKdTree->queueVisibleObjects(cam, getRenderQueue(), onlyShadowCasters, mShowBoxes, mVisibleNodes);
 			}
 
@@ -571,7 +579,6 @@
 			//updatePvs(cam);
 		}
-	}
-
-	mVisibleNodes.clear();	
+		mVisibleNodes.clear();	
+	}
 }
 
@@ -726,16 +733,4 @@
 	mVisibleNodes.push_back(node);
 
-	//KdRenderableList::const_iterator it = nodelist.begin();
-	//KdRenderableList::const_iterator end = nodelist.end();
-	//while (it != end)
-	//{
-	//	if (!(*it)->isQueued(mHierarchyInterface->GetFrameId(), cam))
-	//	{
-	//		mVisibleNodes.push_back(*it);
-	//		(*it)->queueObjects(cam, getRenderQueue(), onlyShadowCasters);
-	//	}
-	//	it++;
-	//}
-
 	_renderQueueGroupObjects(currentGroup, QueuedRenderableCollection::OM_PASS_GROUP);
 }
@@ -937,20 +932,24 @@
 	if (mRenderNodesForViz || mRenderNodesContentForViz)
 	{
-		// HACK: change node material so it is better suited for visualization
-		//MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
+		RenderQueue * queue = getRenderQueue();
+		unsigned long frameid = 0;
+
+		if (mRenderMethod == KdTree::KDRM_INTERNAL)
+			frameid = Root::getSingleton().getCurrentFrameNumber();
+		else
+			frameid = mHierarchyInterface->GetFrameId();
 
 		for (KdTree::NodeList::iterator it = mVisibleNodes.begin(); it != mVisibleNodes.end(); ++it)
 		{
-			if (mRenderNodesForViz)
+			if (mRenderNodesForViz && (*it)->isLeaf())
 			{
 				WireBoundingBox * wirebox = (*it)->getWireBoundingBox();
-				if (wirebox)
-					getRenderQueue()->addRenderable(wirebox);
+				wirebox->setMaterial("KdTree/BoxViz");
+				getRenderQueue()->addRenderable(wirebox);
 			}
 			// add renderables itself
 			if (mRenderNodesContentForViz) 
 			{
-				(*it)->queueVisibleObjects(mHierarchyInterface->GetFrameId(), 
-					cam, getRenderQueue(), false, true);
+				(*it)->queueVisibleObjects(frameid, cam, queue, false, false);
 			}
 		}
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeHierarchyInterface.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeHierarchyInterface.cpp	(revision 1187)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeHierarchyInterface.cpp	(revision 1195)
@@ -42,9 +42,13 @@
 	else
 	{
-		KdTree::Branch * kdbranch = KDBRANCHPTR_CAST(node);
-		if (kdbranch->mLeft)
-			mDistanceQueue->push(kdbranch->mLeft);
-		if (kdbranch->mRight)
-			mDistanceQueue->push(kdbranch->mRight);
+		//KdTree::Branch * kdbranch = KDBRANCHPTR_CAST(node);
+		//if (kdbranch->mLeft)
+		//	mDistanceQueue->push(kdbranch->mLeft);
+		//if (kdbranch->mRight)
+		//	mDistanceQueue->push(kdbranch->mRight);
+		if (kdnode->getLeftChild())
+			mDistanceQueue->push(kdnode->getLeftChild());
+		if (kdnode->getRightChild())
+			mDistanceQueue->push(kdnode->getRightChild());
 	}
 }
@@ -133,5 +137,4 @@
 	}
 
-	// TODO: maybe boxlist?
 	dynamic_cast<KdTreeSceneManager *>(mSceneManager)->getRenderQueue()->addRenderable(box);
 }
@@ -141,16 +144,5 @@
 												   bool includeChildren) 
 {
-	KdTree::Node * kdnode = KDNODEPTR_CAST(node);
-	if (kdnode->isLeaf())
-	{
-		KdTree::Leaf * kdleaf = KDLEAFPTR_CAST(node);
-		KdRenderableList::iterator it = kdleaf->mKdRenderables.begin();
-		KdRenderableList::iterator end = kdleaf->mKdRenderables.end();
-		while (it != end)
-		{
-			(*it)->getGeometryList(geometryList);
-			it++;
-		}
-	}
+	KDNODEPTR_CAST(node)->getGeometryList(geometryList);
 }
 
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeSceneNode.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeSceneNode.cpp	(revision 1190)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeSceneNode.cpp	(revision 1195)
@@ -142,6 +142,4 @@
 			{
 				Entity *ent = static_cast<Entity *>(movable);
-				//std::stringstream d; d << "ent " << ent->getName(); 
-				//LogManager::getSingleton().logMessage(d.str());
 				geometryList->push_back(ent);
 			}
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTree.cpp	(revision 1192)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTree.cpp	(revision 1195)
@@ -307,5 +307,5 @@
 	mMaxDepth(maxdepth), 
 	mBuildMethod(bm), 
-	mHiLiteLevel(HILITE_OFF),
+	mHiLiteLevel(0),
 	mShowAllBoxes(false),
 	mShowNodes(true),
@@ -335,25 +335,30 @@
 	void KdTree::init()
 	{
-		// init visualization materials
-		MaterialPtr mphi = MaterialManager::getSingleton().getByName("KdTree/BoxHiLite");
-		if (mphi.isNull())
+		MaterialPtr mat;
+		TextureUnitState *tex;
+
+		// init visualization materials (unlit solid green/yellow)
+		mat = MaterialManager::getSingleton().getByName("KdTree/BoxHiLite");
+		if (mat.isNull())
 		{
 			ColourValue green(0, 1, 0);
-			mphi = MaterialManager::getSingleton().create("KdTree/BoxHiLite", "General");
-			mphi->setAmbient(green);
-			mphi->setDiffuse(green);
-			mphi->setLightingEnabled(true);
-			mphi->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
-		}
-
-		MaterialPtr mpviz = MaterialManager::getSingleton().getByName("KdTree/BoxViz");
-		if (mpviz.isNull())
+			mat = MaterialManager::getSingleton().create("KdTree/BoxHiLite", "General");
+			//mat->setAmbient(green);
+			//mat->setDiffuse(green);
+			mat->setLightingEnabled(false);
+			tex = mat->getTechnique(0)->getPass(0)->createTextureUnitState();
+			tex->setColourOperationEx(LBX_SOURCE2, LBS_CURRENT, LBS_MANUAL, green, green);
+		}
+
+		mat = MaterialManager::getSingleton().getByName("KdTree/BoxViz");
+		if (mat.isNull())
 		{
 			ColourValue yellow(1, 1, 0);
-			mpviz = MaterialManager::getSingleton().create("KdTree/BoxViz", "General");
-			mpviz->setAmbient(yellow);
-			mpviz->setDiffuse(yellow);
-			mpviz->setLightingEnabled(true);
-			mpviz->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
+			mat = MaterialManager::getSingleton().create("KdTree/BoxViz", "General");
+			//mat->setAmbient(yellow);
+			//mat->setDiffuse(yellow);
+			mat->setLightingEnabled(false);
+			tex = mat->getTechnique(0)->getPass(0)->createTextureUnitState();
+			tex->setColourOperationEx(LBX_SOURCE2, LBS_CURRENT, LBS_MANUAL, yellow, yellow);
 		}
 
@@ -1180,72 +1185,53 @@
 
 	//-------------------------------------------------------------------------
-	void KdTree::queueVisibleObjects(Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes)
+	void KdTree::queueVisibleObjects(Camera* cam, RenderQueue* queue, bool onlyShadowCasters, 
+		bool showBoxes, KdTree::NodeList& visibleNodes)
 	{
 		if (mKdRoot)
 			recQueueVisibleObjects(mKdRoot, Root::getSingleton().getCurrentFrameNumber(),
-				cam, queue, onlyShadowCasters, showBoxes);
+				cam, queue, onlyShadowCasters, showBoxes, visibleNodes);
 	}
 
 	//-------------------------------------------------------------------------
 	void KdTree::recQueueVisibleObjects(KdTree::Node * node, unsigned long currentFrame, 
-		Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes)
+		Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, KdTree::NodeList& visibleNodes)
 	{
 		// test visibility
 		if (cam->isVisible(node->mAABB))
 		{
-#if 0
+			visibleNodes.push_back(node);
+
 			node->queueVisibleObjects(currentFrame, cam, queue, onlyShadowCasters, showBoxes);
 
 			if (node->getLeftChild())
-				recQueueVisibleObjects(node->getLeftChild(), currentFrame, cam, queue, onlyShadowCasters, showBoxes);
+				recQueueVisibleObjects(node->getLeftChild(), currentFrame, 
+				cam, queue, onlyShadowCasters, showBoxes, visibleNodes);
 			if (node->getRightChild())
-				recQueueVisibleObjects(node->getRightChild(), currentFrame, cam, queue, onlyShadowCasters, showBoxes);
-#else
-#ifdef KDTREE_DEBUG
-			WireBoundingBox * wbb = 0;
-			if (showBoxes)
-				wbb = node->getWireBoundingBox();
-#endif
-
-			if (node->isLeaf())
-			{
-				KdTree::Leaf * leaf = KDLEAFPTR_CAST(node);
-				KdRenderableList::iterator it  = leaf->mKdRenderables.begin();
-				KdRenderableList::iterator end = leaf->mKdRenderables.end();
-				while (it != end)
-				{			
-					if (!(*it)->isQueued(currentFrame, cam))
-					{
-						(*it)->queueObjects(cam, queue, onlyShadowCasters);
-					}
-					it++;
-				}
-#ifdef KDTREE_DEBUG
-				if (wbb)
-					queue->addRenderable(wbb);
-#else
-				if (showBoxes)
-					queue->addRenderable(leaf->getWireBoundingBox());
-#endif
-			}
-			else
-			{
-				KdTree::Branch * branch = KDBRANCHPTR_CAST(node);
-#ifdef KDTREE_DEBUG
-				if (wbb)
-					queue->addRenderable(wbb);
-#else
-				if (showBoxes)
-					queue->addRenderable(branch->getWireBoundingBox());
-#endif
-
-				if (branch->mLeft)
-					recQueueVisibleObjects(branch->mLeft, currentFrame, cam, queue, onlyShadowCasters, showBoxes);
-				if (branch->mRight)
-					recQueueVisibleObjects(branch->mRight, currentFrame, cam, queue, onlyShadowCasters, showBoxes);
-			}
-#endif
-		}
-	}
+				recQueueVisibleObjects(node->getRightChild(), currentFrame, 
+				cam, queue, onlyShadowCasters, showBoxes, visibleNodes);
+		}
+	}
+
+	////-------------------------------------------------------------------------
+	//void KdTree::findVisibleNodes(NodeList& visibleNodes, Camera * cam)
+	//{
+	//	if (mKdRoot)
+	//		recFindVisibleNodes(mKdRoot, visibleNodes, cam);
+	//}
+
+	////-------------------------------------------------------------------------
+	//void KdTree::recFindVisibleNodes(KdTree::Node * node, NodeList& visibleNodes, Camera * cam)
+	//{
+	//	// test visibility
+	//	if (cam->isVisible(node->mAABB))
+	//	{
+	//		visibleNodes.push_back(node);
+
+	//		if (node->getLeftChild())
+	//			recFindVisibleNodes(node->getLeftChild(), visibleNodes, cam);
+	//		if (node->getRightChild())
+	//			recFindVisibleNodes(node->getRightChild(), visibleNodes, cam);
+	//	}
+	//}
 
 	/************************************************************************/
@@ -1356,4 +1342,5 @@
 	}
 
+	//-------------------------------------------------------------------------
 	void KdTree::Leaf::queueVisibleObjects(unsigned long currentFrame, 
 		Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes)
@@ -1376,4 +1363,16 @@
 
 	//-------------------------------------------------------------------------
+	void KdTree::Leaf::getGeometryList(GtpVisibility::GeometryVector *geometryList)
+	{
+		KdRenderableList::iterator it = mKdRenderables.begin();
+		KdRenderableList::iterator end = mKdRenderables.end();
+		while (it != end)
+		{
+			(*it)->getGeometryList(geometryList);
+			it++;
+		}
+	}
+
+	//-------------------------------------------------------------------------
 	// update the world aabb based on the contained geometry
 	void KdTree::Leaf::_updateBounds(bool recurse)
