Index: GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/include/TestKdTreeAppListener.h
===================================================================
--- GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/include/TestKdTreeAppListener.h	(revision 1220)
+++ GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/include/TestKdTreeAppListener.h	(revision 1250)
@@ -114,5 +114,6 @@
 		mBuildMethod(KdTree::KDBM_PRIORITYQUEUE),
 		mDemoMode(false),
-		mEnhancedVisibility(false)
+		mEnhancedVisibility(false),
+		mDemoInterval(1.0f)
 		{
 
@@ -134,5 +135,14 @@
 		bool mDemoMode;
 		bool mEnhancedVisibility;
-	};
+		Real mDemoInterval;
+	};
+
+	struct DemoStats
+	{
+		unsigned int mTotalNumFrames;
+		unsigned int mNumFrames;
+		Real mTotalEllapsedTime;
+		Real mEllapsedTime;
+	} mDemoStats;
 
     // Constructor takes a RenderWindow because it uses that to determine input context
Index: GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp	(revision 1220)
+++ GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp	(revision 1250)
@@ -37,5 +37,6 @@
 				.addOpt("s","scenemgr", ARGUMENT_REQUIRED)
 				.addOpt("","comment", ARGUMENT_REQUIRED)
-				.addOpt("e","enhancevis", ARGUMENT_NONE);
+				.addOpt("e","enhancevis", ARGUMENT_NONE)
+				.addOpt("","logint",ARGUMENT_REQUIRED);
 
 
@@ -87,4 +88,11 @@
 				s << tmp;
 				s >> options.mKI;
+				s.clear();
+			}
+
+			if (cmdparser.getOpt("logint", tmp))
+			{
+				s << tmp;
+				s >> options.mDemoInterval;
 				s.clear();
 			}
@@ -417,5 +425,5 @@
 		KdTreeAppListener::SCENEMANAGERNAME[mOptions.mSceneManager],
 		"MySceneManager");
-	// set params depending on scene manager
+	// set params for kdtree scene manager
 	if (mOptions.mSceneManager == KdTreeAppListener::SM_KDT)
 	{
@@ -426,7 +434,10 @@
 		mSceneMgr->setOption("RenderMethod", &mOptions.mRenderMethod);
 		mSceneMgr->setOption("EnhancedVisibility", &mOptions.mEnhancedVisibility);
+		// fix
+		bool depthpass = false;
+		mSceneMgr->setOption("UseDepthPass", &depthpass);
 	}
 	// set algorithm when scene manager is OCM - numbering is different though
-	if (mOptions.mSceneManager == KdTreeAppListener::SM_OCM)
+	else if (mOptions.mSceneManager == KdTreeAppListener::SM_OCM)
 	{
 		int alg = CONV_KDT_TO_OCM_ALG(mOptions.mRenderMethod);
Index: GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTreeAppListener.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTreeAppListener.cpp	(revision 1220)
+++ GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTreeAppListener.cpp	(revision 1250)
@@ -131,5 +131,5 @@
 mTimeUntilNextToggle(0.0f),
 // stuff for walkthrough recording/playback
-mTimeUntilNextLogWrite(0.0f),
+mTimeUntilNextLogWrite(options.mDemoInterval),
 mTimeRemaining(0.0f),
 mWaitBeforeDemoStart(0.0f),
@@ -1063,4 +1063,10 @@
 		mTimeRemaining -= evt.timeSinceLastFrame;
 
+		// update stats
+		++ mDemoStats.mNumFrames;
+		++ mDemoStats.mTotalNumFrames;
+		mDemoStats.mEllapsedTime += evt.timeSinceLastFrame;
+		mDemoStats.mTotalEllapsedTime += evt.timeSinceLastFrame;
+
 		// store fps when in demo mode
 		if (mOptions.mDemoMode)
@@ -1070,7 +1076,10 @@
 			if (mTimeUntilNextLogWrite <= 0.0)
 			{
-				const RenderTarget::FrameStats& stats = mWindow->getStatistics();
-				mTimeUntilNextLogWrite += 1.0;
-				mDemoFPS.push_back(stats.lastFPS);
+				mTimeUntilNextLogWrite += mOptions.mDemoInterval;
+				//const RenderTarget::FrameStats& stats = mWindow->getStatistics();
+				//mDemoFPS.push_back(stats.lastFPS);
+				mDemoFPS.push_back(mDemoStats.mNumFrames / mDemoStats.mEllapsedTime);
+				mDemoStats.mNumFrames = 0;
+				mDemoStats.mEllapsedTime = 0.0f;
 			}
 		}
@@ -1256,4 +1265,10 @@
 		mCurrFrame = mFrameList.begin();
 		mTimeRemaining = mCurrFrame->mElapsedTime;
+
+		// reset stats
+		mDemoStats.mNumFrames = 0;
+		mDemoStats.mTotalNumFrames = 0;
+		mDemoStats.mEllapsedTime = 0.0f;
+		mDemoStats.mTotalEllapsedTime = 0.0f;
 	}
 	// stop playback
@@ -1452,5 +1467,7 @@
 	// stats
 	Real minFPS = Math::POS_INFINITY, maxFPS = 0.0, avgFPS = 0.0;
-	String demofile;
+	String line, storedname;
+	String demopath, demoname;
+	StringUtil::splitFilename(mOptions.mDemoInfileName, demoname, demopath);
 	
 	// check if logfile exists
@@ -1458,12 +1475,13 @@
 	if (logread.is_open())
 	{
-		// read first item, it's the name of the demo
+		// read first line, shuold start with the name of the demo
 		// if matches, OK, otherwise do something
-		logread >> demofile;
-		if (!StringUtil::startsWith(demofile, mOptions.mDemoInfileName))
+		logread >> line;
+		storedname = line.substr(0,line.find_first_of(','));
+		if (storedname != demoname)
 		{
 			LogManager::getSingleton().logMessage(
 				"##Error##: Saved demo stats do not match the current demo: " +
-				demofile + " != " + mOptions.mDemoInfileName);
+				storedname + " != " + demoname);
 			logread.close();
 			return;
@@ -1477,5 +1495,5 @@
 		{
 			// demo title
-			logheader << mOptions.mDemoInfileName << fs;
+			logheader << demoname << fs;
 			// seconds
 			for (size_t i = 0; i < mDemoFPS.size(); i ++)
@@ -1484,5 +1502,6 @@
 			}
 			// minFPS, avgFPS, maxFPS, comment, record separator
-			logheader << "min FPS" << fs << "avg FPS" << fs << "max FPS" << fs << "Comment" << rs;
+			logheader << "\"min FPS\"" << fs << "\"avg FPS\"" << fs << "\"max FPS\"" << fs 
+				<< "\"# Frames \"" << fs << "\"Total Time\"" << fs << "\"Comment\"" << rs;
 			logheader.close();
 		}
@@ -1502,5 +1521,5 @@
 	{
 		// demo settings
-		logwrite << SCENEMANAGER[mOptions.mSceneManager];
+		logwrite << "\"" << SCENEMANAGER[mOptions.mSceneManager];
 		if (mOptions.mSceneManager == SM_KDT)
 		{
@@ -1512,5 +1531,5 @@
 			logwrite << ds << RENDERMETHOD[(mOptions.mRenderMethod+3)%3];
 		}
-		logwrite << fs;
+		logwrite << "\"" << fs;
 		// per second stats
 		for (std::list<Real>::iterator it = mDemoFPS.begin(); it != mDemoFPS.end(); it ++)
@@ -1522,12 +1541,15 @@
 				maxFPS = *it;
 
-			avgFPS += *it;
+			//avgFPS += *it;
 
 			logwrite << (int)(*it) << fs;
 
 		}
-		avgFPS /= mDemoFPS.size();
+		//avgFPS /= mDemoFPS.size();
+		avgFPS = mDemoStats.mTotalNumFrames / mDemoStats.mTotalEllapsedTime;
 		// minFPS, avgFPS, maxFPS, comment, record separator
-		logwrite << (int)minFPS << fs << (int)avgFPS << fs << (int)maxFPS << fs << mOptions.mComment << rs;
+		logwrite << (int)minFPS << fs << (int)avgFPS << fs << (int)maxFPS << fs 
+			<< mDemoStats.mTotalNumFrames << fs << mDemoStats.mTotalEllapsedTime << fs 
+			<< "\"" << mOptions.mComment << "\"" << rs;
 		logwrite.close();
 	}
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTree.h
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTree.h	(revision 1220)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTree.h	(revision 1250)
@@ -213,4 +213,6 @@
 			virtual bool isEmpty() const = 0;
 			virtual bool hasGeometry() const = 0;
+			
+			virtual void mergeLeaves(std::set<Leaf *>& leaves) = 0;
 
 			// Gets this node's parent (NULL if this is the root).
@@ -223,5 +225,5 @@
 			// add contained objects to render queue
 			virtual void queueVisibleObjects(unsigned long currentFrame, 
-				Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes) = 0;
+				Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis = false) = 0;
 
 			// add contained geometry (Entities) to list
@@ -326,4 +328,10 @@
 			virtual bool hasGeometry() const { return false; };
 
+			virtual void mergeLeaves(std::set<Leaf *>& leaves)
+			{
+				for (std::set<Leaf *>::iterator it = mLeaves.begin(); it != mLeaves.end(); it++)
+					leaves.insert(*it);
+			}
+
 			// a branch should have at least one child
 			virtual KdTree::Node * getLeftChild() const { return mLeft; };
@@ -331,9 +339,13 @@
 
 			virtual void queueVisibleObjects(unsigned long currentFrame, 
-				Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes)
+				Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis = false)
 			{
 				if (showBoxes)
 					if (mLevel == mOwner->getHiLiteLevel() || mOwner->getShowAllBoxes())
 						queue->addRenderable(getWireBoundingBox());
+
+				if (fullVis)
+					for (std::set<Leaf *>::iterator it = mLeaves.begin(); it != mLeaves.end(); it ++)
+						(*it)->queueVisibleObjects(currentFrame, cam, queue, onlyShadowCasters, showBoxes, fullVis);
 			}
 
@@ -347,8 +359,15 @@
 				mWorldAABB.setNull();
 
+				// merge box & leaves
 				if (mLeft)
+				{
 					mWorldAABB.merge(mLeft->mWorldAABB);
+					mLeft->mergeLeaves(mLeaves);
+				}
 				if (mRight)
+				{
 					mWorldAABB.merge(mRight->mWorldAABB);
+					mRight->mergeLeaves(mLeaves);
+				}
 
 				// update parent recursively
@@ -361,4 +380,6 @@
 			Plane  * mSplitPlane;
 			PlaneEvent::Side mPlaneSide;
+		protected:
+			std::set<Leaf *> mLeaves;
 		};
 
@@ -373,11 +394,14 @@
 
 			// a leaf is a leaf, dammit
-			virtual bool isLeaf() const { return true; };
+			virtual bool isLeaf() const { return true; }
 
 			// a leaf is empty when it does not posses renderables
-			virtual bool isEmpty() const { return mKdRenderables.empty(); };
+			virtual bool isEmpty() const { return mKdRenderables.empty(); }
 
 			// a leaf has geometry when it has renderables
-			virtual bool hasGeometry() const { return !mKdRenderables.empty(); };
+			virtual bool hasGeometry() const { return !mKdRenderables.empty(); }
+
+			// a leaf adds itself to the leaf set
+			virtual void mergeLeaves(std::set<Leaf *>& leaves)	{ leaves.insert(this); }
 
 			// a leaf never has children 
@@ -386,5 +410,5 @@
 
 			virtual void queueVisibleObjects(unsigned long currentFrame, 
-				Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes);
+				Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis = false);
 
 			virtual void getGeometryList(GtpVisibility::GeometryVector *geometryList);
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTreeCamera.h
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTreeCamera.h	(revision 1212)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/include/OgreKdTreeCamera.h	(revision 1250)
@@ -51,4 +51,7 @@
 	// enhanced visibility
 	NodeVisibility getVisibilityEnhanced(const AxisAlignedBox& box) const;
+
+	// DEBUG
+	//mutable unsigned int mNumVisQueries;
 protected:
 
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeCamera.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeCamera.cpp	(revision 1212)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTreeCamera.cpp	(revision 1250)
@@ -28,8 +28,4 @@
 {
 	mEnhancedVisMode = enhanced;
-	//if (mEnhancedVisMode)
-	//	getVisibility = getVisibilityEnhanced;
-	//else
-	//	getVisibility = getVisibilitySimple;
 }
 
@@ -43,4 +39,5 @@
 KdTreeCamera::NodeVisibility KdTreeCamera::getVisibilityEnhanced(const AxisAlignedBox& box) const
 {
+	//++ mNumVisQueries;
 	// Null boxes always invisible
 	if ( box.isNull() )
@@ -101,4 +98,6 @@
 KdTreeCamera::NodeVisibility KdTreeCamera::getVisibilitySimple(const AxisAlignedBox& box) const
 {
+	//++ mNumVisQueries;
+
 	// dummy
 	FrustumPlane* culledBy = 0;
Index: GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTree.cpp	(revision 1211)
+++ GTP/trunk/Lib/Vis/OnlineCullingCHC/OGRE/src/OgreKdTree.cpp	(revision 1250)
@@ -1188,7 +1188,13 @@
 		bool showBoxes, KdTree::NodeList& visibleNodes)
 	{
+		// debug
+		//cam->mNumVisQueries = 0;
+
 		if (mKdRoot)
 			recQueueVisibleObjects(mKdRoot, Root::getSingleton().getCurrentFrameNumber(),
 				cam, queue, onlyShadowCasters, showBoxes, visibleNodes);
+
+		//mBuildLog->logMessage("Frame # " + StringConverter::toString(Root::getSingleton().getCurrentFrameNumber()) +
+		//	" ," + StringConverter::toString(cam->mNumVisQueries) + " vis queries");
 	}
 
@@ -1207,14 +1213,17 @@
 			visibleNodes.push_back(node);
 
-			node->queueVisibleObjects(currentFrame, cam, queue, onlyShadowCasters, showBoxes);
-
-			if (node->getLeftChild())
-				recQueueVisibleObjects(node->getLeftChild(), currentFrame, 
-				cam, queue, onlyShadowCasters, showBoxes, visibleNodes, 
-				(fullVis || vis == KdTreeCamera::KDNV_FULL) ? true : false);
-			if (node->getRightChild())
-				recQueueVisibleObjects(node->getRightChild(), currentFrame, 
-				cam, queue, onlyShadowCasters, showBoxes, visibleNodes, 
-				(fullVis || vis == KdTreeCamera::KDNV_FULL) ? true : false);
+			bool v = (fullVis || vis == KdTreeCamera::KDNV_FULL);
+			node->queueVisibleObjects(currentFrame, cam, queue, onlyShadowCasters, showBoxes, v);
+
+			if (!v)
+			{
+
+				if (node->getLeftChild())
+					recQueueVisibleObjects(node->getLeftChild(), currentFrame, 
+					cam, queue, onlyShadowCasters, showBoxes, visibleNodes, v);
+				if (node->getRightChild())
+					recQueueVisibleObjects(node->getRightChild(), currentFrame, 
+					cam, queue, onlyShadowCasters, showBoxes, visibleNodes, v);
+			}
 		}
 	}
@@ -1366,5 +1375,5 @@
 	//-------------------------------------------------------------------------
 	void KdTree::Leaf::queueVisibleObjects(unsigned long currentFrame, 
-		Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes)
+		Camera* cam, RenderQueue* queue, bool onlyShadowCasters, bool showBoxes, bool fullVis)
 	{
 		KdRenderableList::iterator it  = mKdRenderables.begin();
Index: GTP/trunk/Lib/Vis/GtpVisibility.sln
===================================================================
--- GTP/trunk/Lib/Vis/GtpVisibility.sln	(revision 1239)
+++ GTP/trunk/Lib/Vis/GtpVisibility.sln	(revision 1250)
@@ -10,5 +10,4 @@
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "IVReader", "OnlineCullingCHC\IVReader\src\IVReader.vcproj", "{7319E499-473D-4CE5-9983-725D6E68A55D}"
 	ProjectSection(ProjectDependencies) = postProject
-		{80DECC17-BDDD-4412-8CF8-F7C1C17A7436} = {80DECC17-BDDD-4412-8CF8-F7C1C17A7436}
 	EndProjectSection
 EndProject
@@ -30,4 +29,6 @@
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestCullingTerrain", "..\..\App\Demos\Vis\HillyTerrain\OGRE\TestCullingTerrain.vcproj", "{248F19A6-2FE0-4F5D-8928-E0EA10609887}"
 	ProjectSection(ProjectDependencies) = postProject
+		{80DECC17-BDDD-4412-8CF8-F7C1C17A7436} = {80DECC17-BDDD-4412-8CF8-F7C1C17A7436}
+		{7319E499-473D-4CE5-9983-725D6E68A55D} = {7319E499-473D-4CE5-9983-725D6E68A55D}
 	EndProjectSection
 EndProject
@@ -35,4 +36,5 @@
 	ProjectSection(ProjectDependencies) = postProject
 		{80DECC17-BDDD-4412-8CF8-F7C1C17A7436} = {80DECC17-BDDD-4412-8CF8-F7C1C17A7436}
+		{7319E499-473D-4CE5-9983-725D6E68A55D} = {7319E499-473D-4CE5-9983-725D6E68A55D}
 	EndProjectSection
 EndProject
