Index: trunk/VUT/Ogre/include/OgreVisibilityOptionsManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgreVisibilityOptionsManager.h	(revision 130)
+++ trunk/VUT/Ogre/include/OgreVisibilityOptionsManager.h	(revision 187)
@@ -6,5 +6,5 @@
 #include <OgreStringVector.h>
 #include "VisibilityManager.h"
-#include "HierarchyInterface.h"
+#include "OgrePlatformHierarchyInterface.h"
 
 
@@ -25,5 +25,5 @@
 	*/
 	VisibilityOptionsManager(GtpVisibility::VisibilityManager *visManager, 
-							 GtpVisibility::HierarchyInterface *hierarchyInterface);
+							 PlatformHierarchyInterface *hierarchyInterface);
 	
 	/** Sets the given option for the scene traverser.
@@ -42,5 +42,5 @@
 protected:
 	GtpVisibility::VisibilityManager *mVisibilityManager;
-	GtpVisibility::HierarchyInterface *mHierarchyInterface;
+	PlatformHierarchyInterface *mHierarchyInterface;
 };
 
Index: trunk/VUT/Ogre/include/OgreVisibilityOctreeSceneManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgreVisibilityOctreeSceneManager.h	(revision 159)
+++ trunk/VUT/Ogre/include/OgreVisibilityOctreeSceneManager.h	(revision 187)
@@ -83,4 +83,6 @@
 	OctreeHierarchyInterface *GetHierarchyInterface();
 
+	void renderAdditiveStencilShadowedQueueGroupObjects(RenderQueueGroup* pGroup);
+	void renderModulativeStencilShadowedQueueGroupObjects(RenderQueueGroup* pGroup);
 
 protected:
@@ -146,4 +148,6 @@
 	/// Always execute the vertex program of a pass, e.g., for the depth pass or item buffer
 	bool mExecuteVertexProgramForAllPasses;
+
+	bool mIsHierarchicalCulling;
 };
 
Index: trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h
===================================================================
--- trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h	(revision 175)
+++ trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h	(revision 187)
@@ -108,4 +108,8 @@
 	GtpVisibility::OcclusionQuery *IssuePatchOcclusionQuery(GtpVisibility::Patch *patch);
 
+	/** Deletes all occlusion queries.
+	*/
+	void DeleteQueries();
+
 protected:
 	/** Renders the given geometry 
@@ -120,8 +124,4 @@
 	*/
 	void CreateNodeVizMaterials();
-
-	/** Deletes all occlusion queries.
-	*/
-	void DeleteQueries();
 
 	/** Returns pointer to current renderable bounding box geometry.
Index: trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 175)
+++ trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 187)
@@ -32,10 +32,12 @@
 mSkipTransparents(false),
 mSavedShadowTechnique(SHADOWTYPE_NONE),
-mRenderTransparentsForItemBuffer(false),
-mExecuteVertexProgramForAllPasses(false)
+mRenderTransparentsForItemBuffer(true),
+mExecuteVertexProgramForAllPasses(true)
 {
 	mHierarchyInterface = new OctreeHierarchyInterface(this, mDestRenderSystem);
 	
-	//mDisplayNodes = true;//mShowBoundingBoxes = true;//mShowBoxes = true;
+	//mDisplayNodes = true;
+	//mShowBoundingBoxes = true;
+	//mShowBoxes = true;
 
 	// TODO: set maxdepth to reasonable value
@@ -209,5 +211,6 @@
 }
 //-----------------------------------------------------------------------
-void VisibilityTerrainSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
+void VisibilityTerrainSceneManager::_findVisibleObjects(Camera* cam,
+														bool onlyShadowCasters)
 {
 	//-- show visible scene nodes and octree bounding boxes from last frame
@@ -345,4 +348,5 @@
 
 	getRenderQueue()->clear(); // finally clear render queue
+	OGRE_DELETE(mRenderQueue); // HACK: should be cleared before...
 	//WriteLog(); // write out stats
 }
@@ -423,4 +427,9 @@
 	{ 
 		mRenderTransparentsForItemBuffer  = (*static_cast<const bool *>(val));
+		return true;
+	}
+	if (key == "NodeVizScale")
+	{ 
+		OctreeNode::setVizScale(*static_cast<const float *>(val));
 		return true;
 	}
@@ -663,7 +672,8 @@
 	if (!mUseDepthPass || !mUseItemBuffer)
 	{
-		if (mShadowTechnique == SHADOWTYPE_STENCIL_ADDITIVE)
+		if (mSavedShadowTechnique == SHADOWTYPE_STENCIL_ADDITIVE)
 		{
 			// TODO: remove this pass because it should be processed during hierarchical culling
+			//mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES;
 			mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW;
 
@@ -676,5 +686,5 @@
 		}
 	
-		if (mShadowTechnique == SHADOWTYPE_STENCIL_MODULATIVE)
+		if (mSavedShadowTechnique == SHADOWTYPE_STENCIL_MODULATIVE)
 		{
 			mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW;
Index: trunk/VUT/Ogre/src/OgreVisibilityOptionsManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityOptionsManager.cpp	(revision 155)
+++ trunk/VUT/Ogre/src/OgreVisibilityOptionsManager.cpp	(revision 187)
@@ -4,6 +4,6 @@
 //-----------------------------------------------------------------------
 VisibilityOptionsManager::VisibilityOptionsManager(
-	GtpVisibility::VisibilityManager *visManager, 
-	GtpVisibility::HierarchyInterface *hierarchyInterface):
+							GtpVisibility::VisibilityManager *visManager, 
+							PlatformHierarchyInterface *hierarchyInterface):
 mVisibilityManager(visManager), 
 mHierarchyInterface(hierarchyInterface)
@@ -11,8 +11,9 @@
 }
 //-----------------------------------------------------------------------
-bool VisibilityOptionsManager::setOption( const String & key, const void * val )
+bool VisibilityOptionsManager::setOption(const String & key, const void * val)
 {
 	if (key == "Algorithm")
 	{
+		mHierarchyInterface->DeleteQueries();
 		mVisibilityManager->SetCullingManager(*static_cast<const 
 			GtpVisibility::VisibilityEnvironment::CullingManagerType *>(val));
Index: trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 175)
+++ trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 187)
@@ -33,6 +33,7 @@
 mSkipTransparents(false),
 mSavedShadowTechnique(SHADOWTYPE_NONE),
-mRenderTransparentsForItemBuffer(false),
-mExecuteVertexProgramForAllPasses(false)
+mRenderTransparentsForItemBuffer(true),
+mExecuteVertexProgramForAllPasses(true),
+mIsHierarchicalCulling(false)
 {
 	mHierarchyInterface = new OctreeHierarchyInterface(this, mDestRenderSystem);
@@ -318,4 +319,6 @@
 		mRenderItemBuffer = false;
 		mSkipTransparents = false;
+		mIsHierarchicalCulling = false;
+
 		mLeavePassesInQueue = 0;
 		mShadowTechnique = mSavedShadowTechnique;
@@ -342,4 +345,5 @@
 	
 	getRenderQueue()->clear(); // finally clear render queue
+	OGRE_DELETE(mRenderQueue); // HACK: should be cleared before...
 	//WriteLog(); // write out stats
 }
@@ -422,5 +426,9 @@
 		return true;
 	}
-
+	if (key == "NodeVizScale")
+	{ 
+		OctreeNode::setVizScale(*static_cast<const float *>(val));
+		return true;
+	}
 	return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
 		setOption(key, val) || OctreeSceneManager::setOption(key, val);
@@ -639,12 +647,14 @@
 
 	// render standard solids without shadows during hierarchical culling pass
-	if ((mShadowTechnique == SHADOWTYPE_STENCIL_ADDITIVE) ||
+	/*if ((mShadowTechnique == SHADOWTYPE_STENCIL_ADDITIVE) ||
 	    (mShadowTechnique == SHADOWTYPE_STENCIL_MODULATIVE))
 	{	
 		mShadowTechnique = SHADOWTYPE_NONE;
-	}
+	}*/
 
 	// set depth pass flag before rendering
 	mRenderDepthPass = mUseDepthPass;
+
+	mIsHierarchicalCulling = true; // during hierarchical culling
 
 	// item buffer needs full ambient lighting to use item colors as unique id
@@ -662,10 +672,10 @@
 	mLeavePassesInQueue = 0;
 
-	if (!mUseDepthPass || !mUseItemBuffer)
-	{
-		if (mShadowTechnique == SHADOWTYPE_STENCIL_ADDITIVE)
+	if (!mUseDepthPass && !mUseItemBuffer)
+	{
+		if (mSavedShadowTechnique == SHADOWTYPE_STENCIL_ADDITIVE)
 		{
 			// TODO: remove this pass because it should be processed during hierarchical culling
-			mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW;
+			//mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW;
 
 			mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_DECAL;
@@ -675,7 +685,8 @@
 			// just render ambient passes
 			mIlluminationStage = IRS_AMBIENT;
-		}
-	
-		if (mShadowTechnique == SHADOWTYPE_STENCIL_MODULATIVE)
+			getRenderQueue()->setSplitPassesByLightingType(true);
+		}
+	
+		if (mSavedShadowTechnique == SHADOWTYPE_STENCIL_MODULATIVE)
 		{
 			mLeavePassesInQueue |= RenderPriorityGroup::SOLID_PASSES_NOSHADOW;
@@ -712,29 +723,125 @@
 }
 //-----------------------------------------------------------------------
-/*void VisibilityOctreeSceneManager::renderBasicQueueGroupObjects(RenderQueueGroup* pGroup)
-{
-    // Basic render loop: Iterate through priorities
-    RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
-
-    while (groupIt.hasMoreElements())
-    {
-        RenderPriorityGroup* pPriorityGrp = groupIt.getNext();
-
-        // Sort the queue first
-        pPriorityGrp->sort(mCameraInProgress);
-
-		// Do solids
-		// TODO: render other solid passes for shadows
-        renderObjects(pPriorityGrp->_getSolidPassesNoShadows(), true);
-
-		// do solid passes no shadows if addititive stencil shadows
-		if (mSavedShadowTechnique == SHADOWTYPE_STENCIL_ADDITIVE)
-			renderObjects(pPriorityGrp->_getSolidPassesNoShadows(), true);
+void VisibilityOctreeSceneManager::renderAdditiveStencilShadowedQueueGroupObjects(RenderQueueGroup* pGroup)
+{
+	// only render solid passes during hierarchical culling
+	if (mIsHierarchicalCulling)
+	{
+		RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
+	    LightList lightList;
+
+		while (groupIt.hasMoreElements())
+		{
+			RenderPriorityGroup* pPriorityGrp = groupIt.getNext();
+
+			// Sort the queue first
+			pPriorityGrp->sort(mCameraInProgress);
+
+			// Clear light list
+			lightList.clear();
+
+			// Render all the ambient passes first, no light iteration, no lights
+			mIlluminationStage = IRS_AMBIENT;
+
+			OctreeSceneManager::renderObjects(pPriorityGrp->_getSolidPasses(), false, &lightList);
+			// Also render any objects which have receive shadows disabled
+			OctreeSceneManager::renderObjects(pPriorityGrp->_getSolidPassesNoShadow(), true);
 		
-        // Do transparents
-        renderObjects(pPriorityGrp->_getTransparentPasses(), true);
-
-
-    }// for each priority
-}*/
+			/*std::stringstream d; 
+			d << " solid size: " << (int)pPriorityGrp->_getSolidPasses().size()
+				<< " solid no shadow size: " << (int)pPriorityGrp->_getSolidPassesNoShadow().size()
+				<< "difspec size: " << (int)pPriorityGrp->_getSolidPassesDiffuseSpecular().size()
+				<< " decal size: " << (int)pPriorityGrp->_getSolidPassesDecal().size();
+			LogManager::getSingleton().logMessage(d.str());*/
+		}
+	}
+	else
+	{
+		OctreeSceneManager::renderAdditiveStencilShadowedQueueGroupObjects(pGroup);
+
+		/*RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
+	    LightList lightList;
+		
+		while (groupIt.hasMoreElements())
+		{
+		
+			RenderPriorityGroup* pPriorityGrp = groupIt.getNext();
+
+			// Sort the queue first
+			pPriorityGrp->sort(mCameraInProgress);
+
+			// Clear light list
+			lightList.clear();
+
+			// Now iterate per light
+			mIlluminationStage = IRS_PER_LIGHT;
+	
+		    // Iterate over lights, render all volumes to stencil
+	        LightList::const_iterator li, liend;
+		    liend = mLightsAffectingFrustum.end();
+	
+		    for (li = mLightsAffectingFrustum.begin(); li != liend; ++li)
+			{
+				LogManager::getSingleton().logMessage("iterating over lights");
+				Light* l = *li;
+				// Set light state
+
+				if (l->getCastShadows())
+				{
+					// Clear stencil
+					mDestRenderSystem->clearFrameBuffer(FBT_STENCIL);
+					renderShadowVolumesToStencil(l, mCameraInProgress);
+					// turn stencil check on
+					mDestRenderSystem->setStencilCheckEnabled(true);
+					// NB we render where the stencil is equal to zero to render lit areas
+					mDestRenderSystem->setStencilBufferParams(CMPF_EQUAL, 0);
+				}
+
+				// render lighting passes for this light
+				if (lightList.empty())
+					lightList.push_back(l);
+				else
+					lightList[0] = l;
+				OctreeSceneManager::renderObjects(pPriorityGrp->_getSolidPassesDiffuseSpecular(), false, &lightList);
+
+				// Reset stencil params
+				mDestRenderSystem->setStencilBufferParams();
+				mDestRenderSystem->setStencilCheckEnabled(false);
+				mDestRenderSystem->_setDepthBufferParams();
+
+			}// for each light
+
+			// Now render decal passes, no need to set lights as lighting will be disabled
+			mIlluminationStage = IRS_DECAL;
+			OctreeSceneManager::renderObjects(pPriorityGrp->_getSolidPassesDecal(), false);
+	    }// for each priority
+	
+		// reset lighting stage
+	    mIlluminationStage = IRS_NONE;*/
+	}
+}
+//-----------------------------------------------------------------------
+void VisibilityOctreeSceneManager::renderModulativeStencilShadowedQueueGroupObjects(RenderQueueGroup* pGroup)
+{
+   if (mIsHierarchicalCulling)
+   {
+	   // Iterate through priorities
+	   RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
+
+	   while (groupIt.hasMoreElements())
+	   {
+		   RenderPriorityGroup* pPriorityGrp = groupIt.getNext();
+
+		   // Sort the queue first
+		   pPriorityGrp->sort(mCameraInProgress);
+
+		   // Do (shadowable) solids
+		   OctreeSceneManager::renderObjects(pPriorityGrp->_getSolidPasses(), true);
+	   }
+   }
+   else
+   {
+	   SceneManager::renderModulativeStencilShadowedQueueGroupObjects(pGroup);
+   }
+}
 }   // namespace Ogre
Index: trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 183)
+++ trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 187)
@@ -68,14 +68,14 @@
 	DeleteQueries();
 
-	if (mSolidBoundingBox)
-		delete mSolidBoundingBox;
+	OGRE_DELETE(mSolidBoundingBox);
 }
 //-----------------------------------------------------------------------
 void PlatformHierarchyInterface::DeleteQueries()
 {
-	for (int i=0; i < (int)mOcclusionQueries.size(); ++i)
-		delete mOcclusionQueries[i];
-
-	mOcclusionQueries.clear();
+	for (int i = 0; i < (int)mOcclusionQueries.size(); ++ i)
+		OGRE_DELETE(mOcclusionQueries[i]);
+
+	mCurrentTestIdx = 0;
+    mOcclusionQueries.clear();
 }
 //-----------------------------------------------------------------------
