Index: trunk/VUT/Ogre/include/OgreVisibilityOctreeSceneManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgreVisibilityOctreeSceneManager.h	(revision 115)
+++ trunk/VUT/Ogre/include/OgreVisibilityOctreeSceneManager.h	(revision 118)
@@ -28,8 +28,7 @@
 
 	/** Sets the given option for the SceneManager
-			    @remarks
-		Options are:
-		"Algorithm", int *;			
+		@remarks Options are: "Algorithm", int *;			
 	*/
+	
 	virtual bool setOption(const String &, const void *);
 	/** Gets the given option for the Scene VisibilityManager.
@@ -48,9 +47,5 @@
 	/** See set.
 	*/
-	GtpVisibility::VisibilityManager *getVisibilityManager( void );
-
-	/** Writes out stats into the Ogre log file.
-	*/
-	void WriteLog();
+	GtpVisibility::VisibilityManager *getVisibilityManager();
 	
 	/** Render a set of objects, see renderSingleObject for param definitions 
@@ -59,8 +54,16 @@
 	virtual void renderObjects(const RenderPriorityGroup::TransparentRenderablePassList& objs, 
             bool doLightIteration, const LightList* manualLightList = 0);
+
+	/** Writes out stats into the Ogre log file.
+	*/
+	void WriteLog();
+
+	/** Override pass so we can do the z-fail pass */
+	Pass* setPass(Pass* pass);
+
 protected:
-
+	/** Creates material for depth pass, e.g., a pass that only fills the depth buffer */
 	void CreateDepthPass();
-
+	void ShowVisualization(Camera *cam);
 	OctreeHierarchyInterface *mHierarchyInterface;
 	GtpVisibility::VisibilityManager *mVisibilityManager;
@@ -74,4 +77,5 @@
 	bool mDelayRenderTransparents;
 	bool mUseDepthPass;
+	bool mIsDepthPass;
 
 	Pass *mDepthPass;
Index: trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h	(revision 115)
+++ trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h	(revision 118)
@@ -3,7 +3,5 @@
 
 #include <OgreSceneNode.h>
-#include <OgreOctreeSceneManager.h>
 #include <OgreTerrainSceneManager.h>
-#include <OgreOctreeSceneQuery.h>
 #include <OgreOctreeNode.h>
 #include <OgreOctreeCamera.h>
@@ -25,5 +23,5 @@
 	~VisibilityTerrainSceneManager();
 
-	void _renderVisibleObjects(void);
+	void _renderVisibleObjects();
 	void _findVisibleObjects(Camera* cam, bool onlyShadowCasters);
 	void _updateSceneGraph(Camera* cam);
@@ -43,9 +41,9 @@
 	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();
Index: trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 117)
+++ trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 118)
@@ -1,4 +1,3 @@
 #include "OgreVisibilityTerrainSceneManager.h"
-#include "OgreOctreeHierarchyInterface.h"
 #include "OgreVisibilityOptionsManager.h"
 #include <OgreMath.h>
@@ -51,5 +50,4 @@
 		mDepthPass->setDepthWriteEnabled(true);
 		mDepthPass->setLightingEnabled(false);
-		//depthMat->load();
 	}
 	else
@@ -112,14 +110,24 @@
         // ignore
     }
-	
+	for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
+	{
+		getRenderQueue()->addRenderable(*it);
+	}
 	if (mRenderNodesForViz || mRenderNodesContentForViz)
 	{
+		// change node material so it is better suited for visualization
+		MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
+		nodeMat->setAmbient(1, 1, 0);
+		nodeMat->setLightingEnabled(true);
+		nodeMat->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
+
 		for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
 		{
+			
 			if (mRenderNodesForViz)
 			{
-				//getRenderQueue()->addRenderable(*it);
+				getRenderQueue()->addRenderable(*it);
 				// addbounding boxes instead of node itself
-				(*it)->_addBoundingBoxToQueue(getRenderQueue());
+				//(*it)->_addBoundingBoxToQueue(getRenderQueue());
 			}
 			if (mRenderNodesContentForViz) 
@@ -129,8 +137,5 @@
 		}
 	}
-	for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
-	{
-		getRenderQueue()->addRenderable(*it);
-	}
+	
 }
 //-----------------------------------------------------------------------
Index: trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 115)
+++ trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 118)
@@ -6,4 +6,6 @@
 #include <OgreCamera.h>
 #include <OgreLogManager.h>
+#include <OgreStringConverter.h>
+
 
 namespace Ogre {
@@ -15,6 +17,8 @@
 mVisibilityManager(visManager), 
 mUseDepthPass(false),
+mIsDepthPass(false),
 mShowVisualization(false),
 mRenderNodesForViz(false),
+mRenderNodesContentForViz(false),
 mVisualizeCulledNodes(false),
 mSkipTransparents(false),
@@ -45,6 +49,5 @@
                 ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
 
-		depthMat->createTechnique()->createPass();
-        mDepthPass = depthMat->getTechnique(0)->getPass(0);
+		mDepthPass = depthMat->getTechnique(0)->getPass(0);
 		mDepthPass->setColourWriteEnabled(false);
 		mDepthPass->setDepthWriteEnabled(true);
@@ -62,37 +65,137 @@
 }
 //-----------------------------------------------------------------------
+Pass *VisibilityOctreeSceneManager::setPass(Pass* pass)
+{
+	// setting vertex program is not efficient
+	//Pass *usedPass = ((mIsDepthPass && !pass->hasVertexProgram()) ? mDepthPass : pass);		
+	Pass *usedPass = (mIsDepthPass ? mDepthPass : pass);	
+
+	if (mIsDepthPass)
+	{
+		// set vertex program of current pass
+        if (pass->hasVertexProgram())
+		{
+			mDepthPass->setVertexProgram(pass->getVertexProgramName());
+
+			if (mDepthPass->hasVertexProgram())
+			{
+				const GpuProgramPtr& prg = mDepthPass->getVertexProgram();
+				// Load this program if not done already
+				if (!prg->isLoaded())
+					prg->load();
+				// Copy params
+				mDepthPass->setVertexProgramParameters(pass->getVertexProgramParameters());
+			}
+		}
+		else if (mDepthPass->hasVertexProgram())
+		{
+			mDepthPass->setVertexProgram("");
+		}
+	}
+
+	OctreeSceneManager::setPass(usedPass);
+
+	return usedPass;
+}
+//-----------------------------------------------------------------------
+void VisibilityOctreeSceneManager::ShowVisualization(Camera *cam)
+{
+	// add player camera for visualization purpose
+	try {
+		Camera *c;
+		if ((c = getCamera("PlayerCam")) != NULL)
+		{
+			getRenderQueue()->addRenderable(c);
+		}   
+    }
+    catch(...)
+    {
+        // ignore
+    }
+	for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
+{
+		getRenderQueue()->addRenderable(*it);
+	}
+	if (mRenderNodesForViz || mRenderNodesContentForViz)
+	{
+		// change node material so it is better suited for visualization
+		MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
+		nodeMat->setAmbient(1, 1, 0);
+		nodeMat->setLightingEnabled(true);
+		nodeMat->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();
+
+		for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
+	{
+
+			if (mRenderNodesForViz)
+			{
+				getRenderQueue()->addRenderable(*it);
+				// addbounding boxes instead of node itself
+				//(*it)->_addBoundingBoxToQueue(getRenderQueue());
+			}
+			if (mRenderNodesContentForViz) 
+			{
+				(*it)->_addToRenderQueue(cam, getRenderQueue(), false);
+			}
+		}
+	}
+	
+}
+//-----------------------------------------------------------------------
+void VisibilityOctreeSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
+{
+	//-- show visible scene nodes and octree bounding boxes from last frame
+	if (mShowVisualization)
+    {
+		ShowVisualization(cam);
+	}
+	else 
+	{	
+		mVisible.clear();
+	    mBoxes.clear();
+	
+		// if there is no depth pass =>
+		// we interleave identification and rendering of objects 
+		// in _renderVisibibleObjects
+
+		// only shadow casters will be rendered in shadow texture pass
+		mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
+	}
+}
+//-----------------------------------------------------------------------
 void VisibilityOctreeSceneManager::_renderVisibleObjects()
 {
-		if (!mShowVisualization)
-	{
-		// two cameras (one for culling, one for rendering)
-		mHierarchyInterface->InitFrame(mOctree, mCameraInProgress, 
-								mCullCamera ? getCamera("CullCamera") : NULL, mDelayRenderTransparents);
-
-		// call initframe to reset culling manager stats
-		mVisibilityManager->GetCullingManager()->InitFrame(mVisualizeCulledNodes);
-	}
-	
-	// standard terrain scenemanager rendering without hierarchical culling
-	if (!mUseDepthPass || mShowVisualization)
+	// visualization: apply standard rendering
+	if (mShowVisualization)
 	{	
 		OctreeSceneManager::_renderVisibleObjects();
-	
 		return;
 	} 
 	
+
 	//-- hierarchical culling
 	// the objects of different layers (e.g., background, scene, 
 	// overlay) must be identified and rendered one after another
 
+	bool leaveTransparentsInQueue = mDelayRenderTransparents && !mUseDepthPass;
+
+	// possible two cameras (one for culling, one for rendering)
+	mHierarchyInterface->InitFrame(mOctree, mCameraInProgress, 
+							mCullCamera ? getCamera("CullCamera") : NULL,
+							leaveTransparentsInQueue);
+
+	// call initframe to reset culling manager stats
+	mVisibilityManager->GetCullingManager()->InitFrame(mVisualizeCulledNodes);
+	
 	mSkipTransparents = false;
 
-	//-- render background
+	//-- render background, in case there is one
 	clearSpecialCaseRenderQueues();
 	addSpecialCaseRenderQueue(RENDER_QUEUE_BACKGROUND);
 	addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_EARLY);
-
 	setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
+
 	SceneManager::_renderVisibleObjects();
+
 
 #ifdef GTP_VISIBILITY_MODIFIED_OGRE
@@ -102,13 +205,19 @@
 	//-- render visible objects (i.e., all but overlay)
 	clearSpecialCaseRenderQueues();
+	addSpecialCaseRenderQueue(RENDER_QUEUE_SKIES_LATE);
 	addSpecialCaseRenderQueue(RENDER_QUEUE_OVERLAY);
 	setSpecialCaseRenderQueueMode(SceneManager::SCRQM_EXCLUDE);
 	
-	// render only solids in hierarchical algorithm. 
-	// Transparents need sorting, thus we render them afterwards
+	// transparents are skipped from hierarchical rendering
+	// => they need sorting, thus we render them afterwards
 	mSkipTransparents = mDelayRenderTransparents;
 
+	// set state for depth pass
+    mIsDepthPass = mUseDepthPass;
+	
 	/** 
 	* the hierarchical culling algorithm
+	  * for depth pass: will just find objects and update depth buffer
+	  * for delayed rendering: will render all but transparents
 	**/
 	
@@ -121,65 +230,22 @@
 	SceneManager::_renderVisibleObjects();
 
+	// for depth pass: add visible nodes found with the visibility culling
+	if (mUseDepthPass)
+{
+		for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
+	{
+			(*it)->_addToRenderQueue(mCameraInProgress, getRenderQueue(), false);
+	}
+		mIsDepthPass = false;
+        }
+	mSkipTransparents = false;
+		
+	//-- now we can render all remaining queue objects
+	// for depth pass: all
+	// for delayed rendering: transparents, overlay 
+	clearSpecialCaseRenderQueues();
+	SceneManager::_renderVisibleObjects();
+	
 	//WriteLog(); // write out stats
-}
-//-----------------------------------------------------------------------
-void VisibilityOctreeSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
-{
-	// only shadow casters will be rendered in shadow texture pass
-	mHierarchyInterface->SetOnlyShadowCasters(onlyShadowCasters);
-
-	// does nothing if hierarchical culling is used =>
-	// we interleave identification and rendering of objects 
-	// in _renderVisibibleObjects
-	if (!mUseDepthPass)
-	{
-		OctreeSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
-		return;	
-	}
-
-	//-- show visibile scene nodes and octree bounding boxes from last frame
-	if (mShowVisualization)
-    {
-		// add player camera for visualization purpose
-		try {
-			Camera *c;
-			if ((c = getCamera("PlayerCam")) != NULL)
-			{
-				getRenderQueue()->addRenderable(c);
-			}   
-        }
-        catch(...)
-        {
-            // ignore
-        }
-		
-		if (mRenderNodesForViz || mRenderNodesContentForViz)
-		{
-		/*	// change node material so it is better suited for visualization
-			MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
-			nodeMat->setAmbient(1, 1, 0);
-			nodeMat->setLightingEnabled(true);
-			nodeMat->getTechnique(0)->getPass(0)->removeAllTextureUnitStates();*/
-
-			for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
-			{
-				if (mRenderNodesForViz)
-				{
-					getRenderQueue()->addRenderable(*it);
-				}
-				if (mRenderNodesContentForViz) 
-				{
-					(*it)->_addToRenderQueue(cam, getRenderQueue(), false);
-				}
-			}
-		}
-		for (BoxList::iterator it = mBoxes.begin(); it != mBoxes.end(); ++it)
-		{
-			getRenderQueue()->addRenderable(*it);
-		}
-	}
-	
-	mVisible.clear();
-    mBoxes.clear();
 }
 //-----------------------------------------------------------------------
@@ -212,4 +278,9 @@
 		return true;
 	}
+	if (key == "RenderNodesContentForViz")
+	{
+		mRenderNodesContentForViz = (*static_cast<const bool *>(val));
+		return true;
+	}
 	if (key == "SkyBoxEnabled")
 	{
@@ -274,20 +345,11 @@
 }
 //-----------------------------------------------------------------------
-void VisibilityOctreeSceneManager::renderObjects(const RenderPriorityGroup::TransparentRenderablePassList& objs, 
-            bool doLightIteration, const LightList* manualLightList)
-{
-	if (!mSkipTransparents)
-	{
-		OctreeSceneManager::renderObjects(objs, doLightIteration, manualLightList);
-	}
-}
-//-----------------------------------------------------------------------
 void VisibilityOctreeSceneManager::WriteLog()
 {
 	std::stringstream d;
 
-	d << "Algorithm: " << mVisibilityManager->GetCullingManagerType() << ", "
-	  << "Hierarchy nodes: " << mNumOctreeNodes << ", " << "Traversed nodes: "
-	  << mHierarchyInterface->GetNumTraversedNodes() << ", "
+	d << "Algorithm type: " << mVisibilityManager->GetCullingManagerType() << ", "
+	  << "Hierarchy nodes: " << mNumOctreeNodes << ", " 
+	  << "Traversed nodes: " << mHierarchyInterface->GetNumTraversedNodes() << ", "
 	  << "Rendered nodes: " << mHierarchyInterface->GetNumRenderedNodes() << ", "
 	  << "Query culled nodes: " << mVisibilityManager->GetCullingManager()->GetNumQueryCulledNodes() << ", "
@@ -297,3 +359,13 @@
 	LogManager::getSingleton().logMessage(d.str());
 }
+//-----------------------------------------------------------------------
+void VisibilityOctreeSceneManager::renderObjects(const RenderPriorityGroup::TransparentRenderablePassList& objs, 
+            bool doLightIteration, const LightList* manualLightList)
+{
+	if (!mSkipTransparents)
+	{
+		OctreeSceneManager::renderObjects(objs, doLightIteration, manualLightList);
+	}
+}
+
 }   // namespace Ogre
Index: trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 115)
+++ trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 118)
@@ -86,6 +86,6 @@
 	
 	// set no depth write, no color, no lighting material
-	//mSceneManager->setPass(solidBox->getTechnique()->getPass(0));
-	SetOcclusionPass();
+	mSceneManager->setPass(solidBox->getTechnique()->getPass(0));
+	//SetOcclusionPass();
 
 	solidBox->SetupBoundingBoxVertices(*box);
Index: trunk/VUT/Ogre/src/OgreSolidBoundingBox.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreSolidBoundingBox.cpp	(revision 117)
+++ trunk/VUT/Ogre/src/OgreSolidBoundingBox.cpp	(revision 118)
@@ -2,9 +2,11 @@
 #include "OgreSolidBoundingBox.h"
 
-#include "OgreSimpleRenderable.h"
-#include "OgreHardwareBufferManager.h"
-#include "OgreCamera.h"
-#include "OgreMaterialManager.h"
-
+#include <OgreSimpleRenderable.h>
+#include <OgreHardwareBufferManager.h>
+#include <OgreCamera.h>
+#include <OgreMaterialManager.h>
+#include <OgreMaterial.h>
+#include <OgrePass.h>
+#include <OgreTechnique.h>
 
 namespace Ogre {
