Index: /GTP/trunk/App/Demos/Vis/KdTreeDemo/README.txt
===================================================================
--- /GTP/trunk/App/Demos/Vis/KdTreeDemo/README.txt	(revision 1296)
+++ /GTP/trunk/App/Demos/Vis/KdTreeDemo/README.txt	(revision 1300)
@@ -31,5 +31,6 @@
   -b, --burnin
     Run a burn-in demo, i.e. loop the demo until ESC is pressed. Requires
-    --demomode. If --logfile is specified, the result of each run will be saved.
+    --demomode. When --logfile is specified, the result of each run will be
+    saved.
 
   -s <sm_id>, --scenemgr <sm_id>
@@ -52,4 +53,13 @@
     no effect. For supported scene managers, render mode switching is also
     available ingame (see INGAME COMMANDS).
+    
+  -v [mode], --viewcells [mode]
+    Set view cell mode. Applies only to scenes where view cells can be loaded
+    (see CONFIG FILE), otherwise has no effect. [mode] is optional.
+    Valid values for [mode] are:
+      0 ... do not use view cells for visibility (same as ommiting -v)
+      1 ... use view cells for visibility (same as -v without parameter)
+      2 ... use view cells and visibility filter
+    These setting can also be changeg ingame (see INGAME COMMANDS).
 
   --comment
@@ -107,7 +117,7 @@
             following formats:
               .iv, .wrl:  Mesh files like the vienna scene.
-              .cfg:       Config files for terrain creation. You need to specify
-                          an appropriate scene manager which supports terrain
-                          world geometry rendering (KTE, OCM, TER).
+              .cfg:       Config files for terrain creation. You need to
+                          specify an appropriate scene manager which supports
+                          terrain world geometry rendering (KTE, OCM, TER).
               .txt:       Text file created by the application which store
                           positions of entites (small movable objects)
@@ -134,6 +144,6 @@
             on enables shadows
     selectentities:
-            Sum up the following numbers to select which entities will appear in
-            the scene:
+            Sum up the following numbers to select which entities will appear
+            in the scene:
               1  robots in grid
               2  randomly placed robots
@@ -186,5 +196,5 @@
     R     Toggle OpenGL/Direct3D render mode (Full, Line, Point)
     T     Toggle OpenGL/Direct3D texture filtering (Bilinear, Trilinear, Aniso)
-    C     Toggle use view cells from PVS (need to load them first)
+    C     Toggle use view cells from PVS (load on demand)
     X     Toggle use visibility filter (whatever that is)
 
@@ -212,14 +222,14 @@
   If the demo shall be permanently stored, the program must be launched with
   the --outfile option, specifying a file to which the demo will be saved. The
-  file name must have on of the above mentioned extensions. If the file allready
-  exists and a demo is saved, it will be overwritten. To save a recorded demo,
-  simply press F6 to store it in the file. If a demo has been allready stored in
-  this session, it will be overwritten.
+  file name must have on of the above mentioned extensions. If the file
+  allready exists and a demo is saved, it will be overwritten. To save a
+  recorded demo, simply press F6 to store it in the file. If a demo has been
+  allready stored in this session, it will be overwritten.
   It is recommended to limit the framerate of the application (e.g. by enabling
   vsync) when recording a demo, since it will produce smaller files.
   To load a demo from a file, specify it with the --infile command line option.
   To run a benchmark, a prerecorded demo must be loaded. Also, a logfile has to
-  be specified using the --logfile option. Finally the --demomode option must be
-  set. The program starts with disabled user input and no overlays. After an
+  be specified using the --logfile option. Finally the --demomode option must
+  be set. The program starts with disabled user input and no overlays. After an
   initial 5 second delay, to stabilize the framerate and preload the scene, the
   demo is played back and the framerate and other statistics are recored. Then
@@ -237,14 +247,18 @@
   If the specified logfile does not exist, it is created including the headers.
   If a log file with the specified name does exists, it is checked if the demo
-  file used in the logfile corresponds with the demo file used in this benchmark
-  run. If the names do not match, or if the logfile cannot be read or written
-  to, the demo is executed but no stats are written. Additionaly, an error
-  message is written in "Ogre.log". However, if the log file exists and the demo
-  name matches, the resulst are appended to the existing logfile, allowing for
-  easy comparison of different settings and options.
+  file used in the logfile corresponds with the demo file used in this
+  benchmark run. If the names do not match, or if the logfile cannot be read or
+  written to, the demo is executed but no stats are written. Additionaly, an
+  error message is written to "Ogre.log". However, if the log file exists and
+  the demo name matches, the resulst are appended to the existing logfile,
+  allowing for easy comparison of different settings and options.
 
 BUGS
-  Kills my PC occasionally when running a long demo with KTE and CHC in the
-  terrain scene. Reason completely mysterious. Happend twice so far.
+  Mysterious and serious (sometimes bluescreen) crashes due to an error in the
+  nVidia display driver (nv4_disp.dll). Occur with the following combination:
+  Direct3D Render Manager + terrain rendering with OCM or KTE + 
+  hardware occlusion culling enabled (CHC, possibly also SWC) + demo playback.
+  Not tested on other hardware (e.g. ATI cards). Not exactly repeatable,
+  crashes in different places during playback or not at all.
 
 AUTHOR
Index: /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/include/TestKdTree.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/include/TestKdTree.h	(revision 1296)
+++ /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/include/TestKdTree.h	(revision 1300)
@@ -50,4 +50,6 @@
 	void createTerrainScene(Real max_x, Real max_y, Real max_z);
 	Vector3 clampToGround(const Vector3& vect, Real offset, Real cap);
+
+	const String& getViewCellsFileName() const { return mViewCells; }
 protected:
 	KdTreeAppListener *mFrameListener;
Index: /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/include/TestKdTreeAppListener.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/include/TestKdTreeAppListener.h	(revision 1296)
+++ /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/include/TestKdTreeAppListener.h	(revision 1300)
@@ -100,4 +100,13 @@
 	};
 
+	enum ViewCellsMode
+	{
+		VCM_OFF,
+		VCM_ON,
+		VCM_FILTER,
+		VCM_SIZE,
+		VCM_NOTSET
+	};
+
 	enum ShowTree
 	{
@@ -128,4 +137,5 @@
 		mBuildMethod(KdTree::KDBM_PRIORITYQUEUE),
 		mSceneType(ST_SIMPLE),
+		mViewCells(VCM_OFF),
 		mFastStart(false),
 		mBurnIn(false),
@@ -153,4 +163,5 @@
 		int mBuildMethod;
 		int mSceneType;
+		int mViewCells;
 		bool mFastStart;
 		bool mBurnIn;
@@ -248,4 +259,5 @@
 
 	// view cells
+	bool mViewCellsLoaded;
 	bool mUseViewCells;
 	bool mUseVisibilityFilter;
@@ -268,5 +280,7 @@
 	Overlay *mDebugOverlay;
 	Overlay *mKdTreeOverlay;
-
+	Overlay *mLoadingOverlay;
+
+	OverlayElement *mMyLoadingInfo;
 	OverlayElement *mDemoStatus;
 	TextAreaOverlayElement *mDemoTime;
Index: /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp	(revision 1296)
+++ /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTree.cpp	(revision 1300)
@@ -41,5 +41,6 @@
 				.addOpt("","savesceneto", ARGUMENT_REQUIRED)
 				.addOpt("f","faststart", ARGUMENT_NONE)
-				.addOpt("b","burnin", ARGUMENT_NONE);
+				.addOpt("b","burnin", ARGUMENT_NONE)
+				.addOpt("v","viewcells", ARGUMENT_OPTIONAL);
 
 
@@ -135,4 +136,19 @@
 			}
 
+			if (cmdparser.getOpt("v",tmp))
+			{
+				if (tmp == "0")
+					options.mViewCells = KdTreeAppListener::VCM_OFF;
+				else if (tmp == "" || tmp == "1")
+					options.mViewCells = KdTreeAppListener::VCM_ON;
+				else if (tmp == "2")
+					options.mViewCells = KdTreeAppListener::VCM_FILTER;
+				else
+				{
+					MessageBox(NULL, ("Invalid argument for option --viewcells: " + tmp).c_str(), "Error", MB_OK | MB_ICONERROR );
+					return -1;
+				}
+
+			}
 		}
 		catch (std::string s)
@@ -328,7 +344,7 @@
 			mTopCam->setPosition(Vector3(1232, 3990, -1477));
 
-			// try loading view cells if specified and if scene manager supports it
-			if (!mViewCells.empty())
-				mSceneMgr->setOption("LoadViewCells", mViewCells.c_str());
+			//// try loading view cells if specified and if scene manager supports it
+			//if (!mViewCells.empty())
+			//	mSceneMgr->setOption("LoadViewCells", mViewCells.c_str());
 
 			// sky box
Index: /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTreeAppListener.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTreeAppListener.cpp	(revision 1296)
+++ /GTP/trunk/App/Demos/Vis/KdTreeDemo/OGRE/src/TestKdTreeAppListener.cpp	(revision 1300)
@@ -114,4 +114,5 @@
 mShowTree(SHOWTREE_OFF),
 // view cells
+mViewCellsLoaded(false),
 mUseViewCells(false),
 mUseVisibilityFilter(false),
@@ -167,7 +168,4 @@
 	mCamNode = sm->getSceneNode("PlayerCamNode");
 
-	sm->getOption("UseViewCells", &mUseViewCells);
-	sm->getOption("UseVisibilityFilter", &mUseVisibilityFilter);
-
 	mRaySceneQuery = mSceneMgr->createRayQuery(Ray(mCamNode->getPosition(), Vector3::NEGATIVE_UNIT_Y));
 
@@ -178,6 +176,28 @@
 	mDemoTime = 0;
 
+	// loading view cells overlay
+	mLoadingOverlay = OverlayManager::getSingleton().getByName("Example/Visibility/LoadingOverlay");
+	mMyLoadingInfo = OverlayManager::getSingleton().getOverlayElement("Example/Visibility/Loading/MyLoadingInfo");
+	mMyLoadingInfo->setCaption("loading view cells ...");
+
+	mLoadingOverlay->hide();
+
 	initKdTreeOverlay();
 	initStatsOverlay();
+
+	// set view cells
+	if (mOptions.mSceneType == ST_GEOMETRY)
+	{
+		switch(mOptions.mViewCells)
+		{
+		case VCM_ON:
+			toggleUseViewCells();
+			break;
+		case VCM_FILTER:
+			toggleUseViewCells();
+			toggleUseVisibilityFilter();
+			break;
+		}
+	}
 
 	if (!mOptions.mDemoInfileName.empty())
@@ -326,5 +346,5 @@
 		sTC = ": Free";
 
-	if (mSceneMgr->getOption("UseViewCells", &mUseViewCells))
+	if (mSceneMgr->getOption("UseViewCells", &mUseViewCells) && mOptions.mSceneType == ST_GEOMETRY)
 	{
 		if (mUseViewCells)
@@ -335,8 +355,18 @@
 	else
 	{
-		sVC = NA;
-	}
-
-	if (mSceneMgr->getOption("UseVisibilityFilter", &mUseVisibilityFilter))
+		if (mOptions.mSceneType == ST_GEOMETRY && mOptions.myApp->getViewCellsFileName() != "" && 
+			(mOptions.mSceneManager == SM_KDT || 
+			 mOptions.mSceneManager == SM_KTE || 
+			 mOptions.mSceneManager == SM_OCM))
+		{
+			sVC = ": off";
+		}
+		else
+		{
+			sVC = NA;
+		}
+	}
+
+	if (mSceneMgr->getOption("UseVisibilityFilter", &mUseVisibilityFilter) && mOptions.mSceneType == ST_GEOMETRY)
 	{
 		if (mUseVisibilityFilter)
@@ -347,5 +377,15 @@
 	else
 	{
-		sVF = NA;
+		if (mOptions.mSceneType == ST_GEOMETRY && mOptions.myApp->getViewCellsFileName() != "" && 
+			(mOptions.mSceneManager == SM_KDT || 
+			 mOptions.mSceneManager == SM_KTE || 
+			 mOptions.mSceneManager == SM_OCM))
+		{
+			sVF = ": off";
+		}
+		else
+		{
+			sVF = NA;
+		}
 	}
 
@@ -387,5 +427,29 @@
 void KdTreeAppListener::toggleUseViewCells()
 {
+	// viewcells apply only for scenes loaded from geometry
+	if (mOptions.mSceneType != ST_GEOMETRY)
+		return;
+
 	mUseViewCells = !mUseViewCells;
+
+
+	// load on demand
+	if (mUseViewCells && !mViewCellsLoaded)
+	{
+		mLoadingOverlay->show();
+
+		// call once to load view cell loading overlay
+		mWindow->update();
+		mViewCellsLoaded = mSceneMgr->setOption("LoadViewCells", mOptions.myApp->getViewCellsFileName().c_str());
+
+		if (!mViewCellsLoaded)
+		{
+			LogManager::getSingleton().logMessage("#@#@ Error: loading view cells from " + 
+				mOptions.myApp->getViewCellsFileName() + " failed.");
+		}
+
+		mLoadingOverlay->hide();
+	}
+
 	if (mSceneMgr->setOption("UseViewCells", &mUseViewCells))
 	{
