Index: trunk/VUT/Ogre/include/OgreVisibilitySceneManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgreVisibilitySceneManager.h	(revision 99)
+++ trunk/VUT/Ogre/include/OgreVisibilitySceneManager.h	(revision 103)
@@ -56,5 +56,7 @@
 	SceneNodeHierarchyInterface *mHierarchyInterface;
 	GtpVisibility::VisibilityManager *mVisibilityManager;
+
 	bool mUseVisibilityCulling;
+	bool mShowVisualization;
 };
 } // namespace Ogre
Index: trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h
===================================================================
--- trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h	(revision 94)
+++ trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h	(revision 103)
@@ -44,9 +44,10 @@
 	void SetCullCamera(Camera *cullCam);
 	/** Initialises this scene traverser for the current frame.
-	@param root root of the hierarchy
-		@param cam current camera
-		@param cullCam the camera used for culling. If null, the current camera is used
-		@remark convenience method wich calls VisibilitySceneTraverser::initFrame,
-		sets the current camera, and initialises the distance queue.
+		@remark This is a convenience method which resets the stats, 
+				sets the current camera, and initialises the distance queue.
+		@param root root of the hierarchy
+		@param cam the actual camera definding the view frustum
+		@param cullCam the camera used for culling. 
+		@remark If null, the actual camera is used for both viewing and culling		
 	*/
 	void InitFrame(GtpVisibility::HierarchyNode *root, Camera *cam, Camera *cullCam = NULL);
@@ -60,5 +61,5 @@
 	*/
 	void SetSceneManager(SceneManager *sm);
-	/** Sets pointer to the current render system 
+	/** Sets pointer to the current render system .
 		@param rsys the rendersystem 
 	*/
@@ -77,5 +78,16 @@
 		GtpVisibility::HierarchyNode *node, const bool wasVisible);
 
+	/** If true, the interface finds and renders only objects which are marked as shadow casters.
+		@remark This is important for the shadow texture pass
+	*/
+	void SetOnlyShadowCasters(bool onlyShadowCasters);
+
 protected:
+	/** A pass that prepares an occlusion query.
+		@remark disables depth write, colour write, lighting,
+		vertex and fragment program.
+	*/
+	void SetOcclusionPass();
+
 	/** Deletes all occlusion queries.
 	*/
@@ -85,12 +97,14 @@
 	*/
 	void RenderBoundingBox(AxisAlignedBox *box);
-	/** Returns pointer to current renderable half bounding box geometry
-	*/
-	//SolidHalfBoundingBox *GetSolidHalfBoundingBox();
+
 	/** Returns pointer to current renderable bounding box geometry
 	*/
 	SolidBoundingBox *GetSolidBoundingBox();
 
-	/** one renderable half of an aabb. 
+	/** Returns pointer to current renderable half bounding box geometry
+	*/
+	//SolidHalfBoundingBox *GetSolidHalfBoundingBox();
+
+	/** Renderable of an aabb
 	*/
 	SolidBoundingBox *mSolidBoundingBox;
@@ -105,4 +119,6 @@
 	
 	std::vector<PlatformOcclusionQuery *> mOcclusionQueries;
+
+	bool mOnlyShadowCasters;
 };
 
Index: trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h	(revision 100)
+++ trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h	(revision 103)
@@ -30,7 +30,5 @@
 
 	/** Sets the given option for the SceneManager
-			    @remarks
-		Options are:
-		"Algorithm", int *;			
+		@remarks Options are: "Algorithm", int *;			
 	*/
 	
@@ -45,22 +43,18 @@
 	bool getOptionKeys( StringVector &refKeys );
 
-	/** Sets the visibility manager.
+	/** Sets the visibility manager
 		@param visManager the visibility manager
 	*/
 	void setVisibilityManager(GtpVisibility::VisibilityManager *visManager);
-	/** See set.
+	/** See set
 	*/
 	GtpVisibility::VisibilityManager *getVisibilityManager();
 	
-	//writes out stats into the Ogre log file
-	void WriteLog();
-	void SetShowVisualization(bool showViz);
-
-/*	void SetSkyBoxEnabled(bool enabled);
-	void SetSkyPlaneEnabled(bool enabled);
-	void SetSkyDomeEnabled(bool enabled);*/
-
 protected:
 	
+	/** Writes out stats into the Ogre log file
+	*/
+	void WriteLog();
+
 	OctreeHierarchyInterface *mHierarchyInterface;
 	GtpVisibility::VisibilityManager *mVisibilityManager;
@@ -68,4 +62,5 @@
 	bool mUseVisibilityCulling;
 	bool mShowVisualization;
+	bool mRenderNodesForViz;
 };
 
Index: trunk/VUT/Ogre/src/OgreOctreeHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreOctreeHierarchyInterface.cpp	(revision 101)
+++ trunk/VUT/Ogre/src/OgreOctreeHierarchyInterface.cpp	(revision 103)
@@ -106,5 +106,6 @@
 		octant->setLastRendered(mFrameId);
 
-		static_cast<OctreeSceneManager *>(mSceneManager)->_renderOctant(mCamera, octant);
+		static_cast<OctreeSceneManager *>(mSceneManager)->_renderOctant(mCamera, 
+			octant, mOnlyShadowCasters);
 
 		mNumRenderedNodes ++;
Index: trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 100)
+++ trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 103)
@@ -36,13 +36,15 @@
 	mHierarchyInterface->InitFrame(mOctree, mCameraInProgress, 
 							mCullCamera ? getCamera("CullCamera") : NULL);
-	//mVisibilityManager->GetCullingManager()->InitFrame();
 
-	// standard scenemanager rendering without hierarchical culling
-	if(!mUseVisibilityCulling)
+	// call initframe to reset culling manager stats
+	mVisibilityManager->GetCullingManager()->InitFrame();
+
+	// standard terrain scenemanager rendering without hierarchical culling
+	if (!mUseVisibilityCulling || mShowVisualization)
 	{	
 		OctreeSceneManager::_renderVisibleObjects();
 		return;
 	} 
-    
+	
 	//-- hierarchical culling
 	// the objects of different layers (e.g., background, scene, 
@@ -77,5 +79,5 @@
 #endif
 
-	//-- render remaining objects, e.g., overlay
+	//-- render overlay
 	clearSpecialCaseRenderQueues();
 	SceneManager::_renderVisibleObjects();
@@ -86,4 +88,7 @@
 void VisibilityOctreeSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
 {
+	// if shadow pass => render only shadow casters
+	mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
+
 	// does nothing if hierarchical culling is used =>
 	// we interleave identification and rendering of objects 
@@ -104,6 +109,5 @@
 	}
 
-	getRenderQueue()->clear();
-    mBoxes.clear();
+	mBoxes.clear();
     mVisible.clear();
 }
@@ -156,5 +160,5 @@
 {
 	return  VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
-		getOptionKeys ( refKeys ) || OctreeSceneManager::getOptionKeys(refKeys);
+		getOptionKeys (refKeys) || OctreeSceneManager::getOptionKeys(refKeys);
 }
 //-----------------------------------------------------------------------
@@ -174,5 +178,6 @@
 
 	d << "Algorithm: " << mVisibilityManager->GetCullingManagerType() << ", "
-	  << "Hierarchy nodes: " << mNumOctreeNodes << ", " << "Traversed nodes: " << mHierarchyInterface->GetNumTraversedNodes() << ", "
+	  << "Hierarchy nodes: " << mNumOctreeNodes << ", " << "Traversed nodes: "
+	  << mHierarchyInterface->GetNumTraversedNodes() << ", "
 	  << "Rendered nodes: " << mHierarchyInterface->GetNumRenderedNodes() << ", "
 	  << "Query culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumQueryCulledNodes() << ", "
Index: trunk/VUT/Ogre/src/OgreSceneNodeHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreSceneNodeHierarchyInterface.cpp	(revision 100)
+++ trunk/VUT/Ogre/src/OgreSceneNodeHierarchyInterface.cpp	(revision 103)
@@ -115,4 +115,5 @@
 AxisAlignedBox *SceneNodeHierarchyInterface::GetBoundingBox(GtpVisibility::HierarchyNode *node)
 {
+	// only create renderable bounding box for new node
 	if (node != mPreviousNode)
 	{
Index: trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 100)
+++ trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 103)
@@ -1,9 +1,7 @@
 #include <OgreCamera.h>
 #include <OgreLogManager.h>
-//#include "OgreSolidHalfBoundingBox.h"
 #include "OgreSolidBoundingBox.h"
 #include "OgrePlatformHierarchyInterface.h"
 #include "OgrePlatformOcclusionQuery.h"
-#include <windows.h>
 
 namespace Ogre {
@@ -12,5 +10,5 @@
 PlatformHierarchyInterface::PlatformHierarchyInterface(SceneManager *sm, RenderSystem *rsys):
 mSceneManager(sm), mRenderSystem(rsys), mSolidBoundingBox(NULL),
-mCamera(NULL), mCullCamera(NULL)
+mCamera(NULL), mCullCamera(NULL), mOnlyShadowCasters(false)
 {
 }
@@ -36,10 +34,10 @@
 	static RenderOperation ro;
 
-	//TODO: this should be the full bounding box
 	SolidBoundingBox *solidBox = GetSolidBoundingBox();
 	
 	mRenderSystem->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
 	mSceneManager->useRenderableViewProjMode(solidBox);
-    mSceneManager->setPass(solidBox->getTechnique()->getPass(0));
+	//mSceneManager->setOcclusionPass(solidBox->getTechnique()->getPass(0));
+	SetOcclusionPass();
 
 	solidBox->SetupBoundingBoxVertices(*box);
@@ -80,5 +78,6 @@
 }
 //-----------------------------------------------------------------------
-void PlatformHierarchyInterface::InitFrame(GtpVisibility::HierarchyNode *root, Camera *cam, Camera *cullCam)
+void PlatformHierarchyInterface::InitFrame(GtpVisibility::HierarchyNode *root, 
+										   Camera *cam, Camera *cullCam)
 {
 	GtpVisibility::HierarchyInterface::InitFrame(root);
@@ -88,7 +87,11 @@
 
 	if (cullCam)
+	{
 		SetCullCamera(cullCam);
+	}
 	else
+	{
 		SetCullCamera(cam);
+	}
 }
 //-----------------------------------------------------------------------
@@ -141,4 +144,26 @@
 }
 //-----------------------------------------------------------------------
+void PlatformHierarchyInterface::SetOcclusionPass()
+{
+    // disable vertex and fragment program
+	mRenderSystem->unbindGpuProgram(GPT_VERTEX_PROGRAM);
+	mRenderSystem->unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
+	
+	// disable lighting
+	mRenderSystem->setLightingEnabled(false);
+
+    // Disable remaining texture units
+    mRenderSystem->_disableTextureUnitsFrom(0);
+
+    //--Set up non-texture related material settings
+   
+	// Depth buffer settings
+	mRenderSystem->_setDepthBufferParams(true, false, CMPF_LESS_EQUAL);
+	// Set colour write mode off
+	mRenderSystem->_setColourBufferWriteEnabled(false, false, false, false);
+	// Set colour write mode pff
+    mRenderSystem->_setColourBufferWriteEnabled(false, false, false, false);
+}
+//-----------------------------------------------------------------------
 SolidBoundingBox *PlatformHierarchyInterface::GetSolidBoundingBox()
 {
@@ -148,4 +173,8 @@
 	return mSolidBoundingBox;
 }
-
+//-----------------------------------------------------------------------
+void PlatformHierarchyInterface::SetOnlyShadowCasters(bool onlyShadowCasters)
+{
+	mOnlyShadowCasters = onlyShadowCasters;
+}
 } // namespace Ogre
Index: trunk/VUT/Ogre/src/OgreSolidHalfBoundingBox.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreSolidHalfBoundingBox.cpp	(revision 100)
+++ trunk/VUT/Ogre/src/OgreSolidHalfBoundingBox.cpp	(revision 103)
@@ -6,5 +6,5 @@
 #include "OgreCamera.h"
 #include "OgreMaterialManager.h"
-//#include <windows.h>
+
 
 namespace Ogre {
Index: trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 101)
+++ trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 103)
@@ -16,6 +16,8 @@
 VisibilityTerrainSceneManager::VisibilityTerrainSceneManager(
 	GtpVisibility::VisibilityManager *visManager)
-: mVisibilityManager(visManager), mUseVisibilityCulling(true),
-mShowVisualization(false)
+: mVisibilityManager(visManager), 
+mUseVisibilityCulling(true),
+mShowVisualization(false),
+mRenderNodesForViz(false)
 {
 	mHierarchyInterface = 
@@ -24,4 +26,6 @@
 	//mDisplayNodes = true;
 	//mShowBoundingBoxes = true;
+
+	// TODO: set maxdepth to reasonable value
 	mMaxDepth = 50;
 }
@@ -32,13 +36,59 @@
 }
 //-----------------------------------------------------------------------
+void VisibilityTerrainSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
+{
+	mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
+	// does nothing if hierarchical culling is used =>
+	// we interleave identification and rendering of objects 
+	// in _renderVisibibleObjects
+	if (!mUseVisibilityCulling)
+	{
+		OctreeSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
+		return;	
+	}
+
+	//-- show visibile scene nodes and octree bounding boxes from last frame
+	if (mShowVisualization)
+    {
+		// add player camera for visualization purpose
+		Camera *c;
+		if ((c = getCamera("PlayerCam")) != NULL)
+		{
+			getRenderQueue()->addRenderable(c);
+		}
+		
+		for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
+		{
+			getRenderQueue()->addRenderable(*it);
+
+			if (mRenderNodesForViz)
+			{
+				(*it)->_addToRenderQueue(cam, getRenderQueue(), false);
+			}
+		}
+		for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
+		{
+			getRenderQueue()->addRenderable(*it);
+		}
+	}
+	
+	mVisible.clear();
+    mBoxes.clear();
+}
+//-----------------------------------------------------------------------
 void VisibilityTerrainSceneManager::_renderVisibleObjects()
 {
-	// two cameras (one for culling, one for rendering)
-	mHierarchyInterface->InitFrame(mOctree, mCameraInProgress, 
-							mCullCamera ? getCamera("CullCamera") : NULL);
-	//mVisibilityManager->GetCullingManager()->InitFrame();
-	
+	if (!mShowVisualization)
+	{
+		// two cameras (one for culling, one for rendering)
+		mHierarchyInterface->InitFrame(mOctree, mCameraInProgress, 
+								mCullCamera ? getCamera("CullCamera") : NULL);
+
+		// call initframe to reset culling manager stats
+		mVisibilityManager->GetCullingManager()->InitFrame();
+	}
+
 	// standard terrain scenemanager rendering without hierarchical culling
-	if(!mUseVisibilityCulling || mShowVisualization)
+	if (!mUseVisibilityCulling || mShowVisualization)
 	{	
 		TerrainSceneManager::_renderVisibleObjects();
@@ -85,47 +135,6 @@
 }
 //-----------------------------------------------------------------------
-void VisibilityTerrainSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
-{
-	// does nothing if hierarchical culling is used =>
-	// we interleave identification and rendering of objects 
-	// in _renderVisibibleObjects
-	getRenderQueue()->clear();
-	
-	if (!mUseVisibilityCulling)
-	{
-		OctreeSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
-		return;	
-	}
-	
-	//-- show visibile scene nodes and octree bounding boxes from last frame
-	if (mShowVisualization)
-    {
-		// add cull camera for visualization purpose
-		if (mCullCamera)
-		{
-			Camera *c;
-			if ((c = getCamera("CullCamera")) != NULL)
-			{
-				getRenderQueue()->addRenderable(c);
-			}
-		}
-
-		for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
-		{
-			getRenderQueue()->addRenderable(*it);
-		}
-		for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
-		{
-			getRenderQueue()->addRenderable(*it);
-		}
-	}
-	
-	mVisible.clear();
-    mBoxes.clear();
-}
-//-----------------------------------------------------------------------
 void VisibilityTerrainSceneManager::_updateSceneGraph(Camera* cam)
 {
-	// mHierarchyInterface->setNumSceneNodes((int)mSceneNodes.size());
 	mVisibilityManager->GetCullingManager()->SetHierarchyInterface(mHierarchyInterface);
 	mHierarchyInterface->SetRenderSystem(mDestRenderSystem);
@@ -149,4 +158,9 @@
 		return true;
 	}
+	if (key == "RenderNodesForViz")
+	{
+		mRenderNodesForViz = (*static_cast<const bool *>(val));
+		return true;
+	}
 	if (key == "SkyBoxEnabled")
 	{
@@ -180,5 +194,5 @@
 }
 //-----------------------------------------------------------------------
-bool VisibilityTerrainSceneManager::getOptionValues(const String & key, StringVector  &refValueList)
+bool VisibilityTerrainSceneManager::getOptionValues(const String & key, StringVector &refValueList)
 {
 	return TerrainSceneManager::getOptionValues( key, refValueList);
@@ -200,25 +214,5 @@
 	return mVisibilityManager;
 }
-//-----------------------------------------------------------------------
-void VisibilityTerrainSceneManager::SetShowVisualization(bool showViz)
-{
-	mShowVisualization = showViz;
-}
-/*
-//-----------------------------------------------------------------------
-void SetSkyBoxEnabled(bool enabled)
-{
-	mSkyBoxEnabled = enabled;
-}
-//-----------------------------------------------------------------------
-void SetSkyPlaneEnabled(bool enabled)
-{
-	mSkyPlaneEnabled = enabled;
-}
-//-----------------------------------------------------------------------
-void SetSkyDomeEnabled(bool enabled)
-{
-	mSkyDomeEnabled = enabled;
-} */
+
 //-----------------------------------------------------------------------
 void VisibilityTerrainSceneManager::WriteLog()
@@ -236,3 +230,5 @@
 	LogManager::getSingleton().logMessage(d.str());
 }
+
+
 } // namespace Ogre
Index: trunk/VUT/Ogre/src/OgreSolidBoundingBox.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreSolidBoundingBox.cpp	(revision 100)
+++ trunk/VUT/Ogre/src/OgreSolidBoundingBox.cpp	(revision 103)
@@ -74,4 +74,5 @@
 	 //setMaterial("BaseWhiteNoLighting");
 }
+//-----------------------------------------------------------------------
 SolidBoundingBox::~SolidBoundingBox() 
 {
Index: trunk/VUT/Ogre/src/OgreVisibilitySceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilitySceneManager.cpp	(revision 100)
+++ trunk/VUT/Ogre/src/OgreVisibilitySceneManager.cpp	(revision 103)
@@ -12,7 +12,8 @@
 //-----------------------------------------------------------------------
 VisibilitySceneManager::VisibilitySceneManager(GtpVisibility::VisibilityManager *visManager)
-:mVisibilityManager(visManager), mUseVisibilityCulling(true)
+:mVisibilityManager(visManager), mUseVisibilityCulling(true), mShowVisualization(false)
 {
-	mHierarchyInterface = new SceneNodeHierarchyInterface(this, mDestRenderSystem);
+	mHierarchyInterface = 
+		new SceneNodeHierarchyInterface(this, mDestRenderSystem);
 }
 //-----------------------------------------------------------------------
@@ -24,10 +25,11 @@
 void VisibilitySceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
 {
-	// empty if hierarchical culling is used =>
+	// does nothing if hierarchical culling is used =>
 	// we interleave identification and rendering of objects 
-	// in _renderVisibibleObjects 
+	// in _renderVisibibleObjects
 	if (!mUseVisibilityCulling)
 	{
 		SceneManager::_findVisibleObjects(cam, onlyShadowCasters);
+		return;	
 	}
 }
@@ -35,8 +37,12 @@
 void VisibilitySceneManager::_renderVisibleObjects()
 {
+	// two cameras (one for culling, one for rendering)
 	mHierarchyInterface->InitFrame(mSceneRoot, mCameraInProgress);
+
+	// call initframe to reset culling manager stats
 	mVisibilityManager->GetCullingManager()->InitFrame();
 
-	if (!mUseVisibilityCulling)
+	// standard terrain scenemanager rendering without hierarchical culling
+	if (!mUseVisibilityCulling || mShowVisualization)
 	{	
 		SceneManager::_renderVisibleObjects();
@@ -55,4 +61,5 @@
 	setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
 	SceneManager::_renderVisibleObjects();
+
 #ifdef GTP_VISIBILITY_MODIFIED_OGRE
 	_deleteRenderedQueueGroups();
@@ -64,6 +71,10 @@
 	setSpecialCaseRenderQueueMode(SceneManager::SCRQM_EXCLUDE);
 
-	//-- the hierarchical culling algorithm
+
+	/** 
+	* the hierarchical culling algorithm
+	**/
 	mVisibilityManager->ApplyVisibilityCulling();
+
 
 #ifdef GTP_VISIBILITY_MODIFIED_OGRE
@@ -74,4 +85,6 @@
 	clearSpecialCaseRenderQueues();
 	SceneManager::_renderVisibleObjects();
+
+	//WriteLog(); // write out stats
 }
 //-----------------------------------------------------------------------
@@ -123,4 +136,3 @@
 }
 
-//} // namespace GtpVisibility
 } // namespace Ogre
