Index: trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h
===================================================================
--- trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h	(revision 120)
+++ trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h	(revision 121)
@@ -87,4 +87,7 @@
 	bool GetUseOptimization();
 
+	/** true if bounding box query is currently active. */
+    bool IsBoundingBoxQuery();
+
 protected:
 	/** materials for visualizing frustum and query culled nodes */
@@ -129,4 +132,5 @@
 	bool mOnlyShadowCasters;
 	bool mLeaveTransparentsInQueue;
+	bool mIsBoundingBoxQuery;
 };
 
Index: trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h	(revision 119)
+++ trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h	(revision 121)
@@ -59,6 +59,11 @@
 	void WriteLog();
 
-	/** Override pass so we can do the z-fail pass */
+	/** Override pass so we can do the z-fail pass. 
+	*/
 	Pass* setPass(Pass* pass);
+
+	/** Override from SceneManager so we can skip all but first pass for depth pass. 
+	*/
+	bool validatePassForRendering(Pass* pass);
 
 protected:
Index: trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 120)
+++ trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 121)
@@ -63,158 +63,9 @@
 }
 //-----------------------------------------------------------------------
-/*Pass *VisibilityTerrainSceneManager::setPass(Pass* pass)
-{
-	static bool myLastUsedVertexProgram = false;
-    static bool myLastUsedFragmentProgram = false;
-
-	// set depth fill pass only if depth write enabled
-	Pass *usedPass = pass;
-
-	if (mIsDepthPass && pass->getDepthWriteEnabled())
-	{
-		usedPass = mDepthPass;		
-	}
-
-    if (mIlluminationStage == IRS_RENDER_TO_TEXTURE)
-    {
-        // Derive a special shadow caster pass from this one
-        usedPass = deriveShadowCasterPass(usedPass);
-    }
-    else if (mIlluminationStage == IRS_RENDER_MODULATIVE_PASS)
-    {
-        usedPass = deriveShadowReceiverPass(usedPass);
-    }
-
-	bool passSurfaceAndLightParams = true;
-
-	// take original pass here
-    if (usedPass->hasVertexProgram())
-    {
-        mDestRenderSystem->bindGpuProgram(usedPass->getVertexProgram()->_getBindingDelegate());
-        // bind parameters later since they can be per-object
-        myLastUsedVertexProgram = true;
-        // does the vertex program want surface and light params passed to rendersystem?
-        passSurfaceAndLightParams = usedPass->getVertexProgram()->getPassSurfaceAndLightStates();
-    }
-    else
-    {
-        // Unbind program?
-        if (myLastUsedVertexProgram)
-        {
-            mDestRenderSystem->unbindGpuProgram(GPT_VERTEX_PROGRAM);
-            myLastUsedVertexProgram = false;
-        }
-        // Set fixed-function vertex parameters
-    }
-
-    if (passSurfaceAndLightParams)
-    {
-        // Set surface reflectance properties, only valid if lighting is enabled
-        if (usedPass->getLightingEnabled())
-        {
-            mDestRenderSystem->_setSurfaceParams( 
-                usedPass->getAmbient(), 
-                usedPass->getDiffuse(), 
-                usedPass->getSpecular(), 
-                usedPass->getSelfIllumination(), 
-                usedPass->getShininess(),
-				usedPass->getVertexColourTracking() );
-        }
-
-        // Dynamic lighting enabled?
-        mDestRenderSystem->setLightingEnabled(usedPass->getLightingEnabled());
-    }
-
-    // Using a fragment program?
-    if (usedPass->hasFragmentProgram())
-    {
-        mDestRenderSystem->bindGpuProgram(
-            usedPass->getFragmentProgram()->_getBindingDelegate());
-        // bind parameters later since they can be per-object
-        myLastUsedFragmentProgram = true;
-    }
-    else
-    {
-        // Unbind program?
-        if (myLastUsedFragmentProgram)
-        {
-            mDestRenderSystem->unbindGpuProgram(GPT_FRAGMENT_PROGRAM);
-            myLastUsedFragmentProgram = false;
-        }
-
-        // Set fixed-function fragment settings
-
-        // Fog (assumes we want pixel fog which is the usual)
-        // New fog params can either be from scene or from material
-        FogMode newFogMode;
-        ColourValue newFogColour;
-        Real newFogStart, newFogEnd, newFogDensity;
-        if (usedPass->getFogOverride())
-        {
-            // New fog params from material
-            newFogMode = usedPass->getFogMode();
-            newFogColour = usedPass->getFogColour();
-            newFogStart = usedPass->getFogStart();
-            newFogEnd = usedPass->getFogEnd();
-            newFogDensity = usedPass->getFogDensity();
-        }
-        else
-        {
-            // New fog params from scene
-            newFogMode = mFogMode;
-            newFogColour = mFogColour;
-            newFogStart = mFogStart;
-            newFogEnd = mFogEnd;
-            newFogDensity = mFogDensity;
-        }
-        mDestRenderSystem->_setFog(newFogMode, newFogColour, newFogDensity, newFogStart, newFogEnd);
-
-	}
-
-    // The rest of the settings are the same no matter whether we use programs or not
-
-    // Set scene blending
-    mDestRenderSystem->_setSceneBlending(
-        usedPass->getSourceBlendFactor(), usedPass->getDestBlendFactor());
-
-
-    // Texture unit settings
-
-    Pass::TextureUnitStateIterator texIter =  usedPass->getTextureUnitStateIterator();
-    size_t unit = 0;
-    while(texIter.hasMoreElements())
-    {
-        TextureUnitState* pTex = texIter.getNext();
-        mDestRenderSystem->_setTextureUnitSettings(unit, *pTex);
-        ++unit;
-    }
-    // Disable remaining texture units
-    mDestRenderSystem->_disableTextureUnitsFrom(usedPass->getNumTextureUnitStates());
-
-    // Set up non-texture related material settings
-    // Depth buffer settings
-    mDestRenderSystem->_setDepthBufferFunction(usedPass->getDepthFunction());
-    mDestRenderSystem->_setDepthBufferCheckEnabled(usedPass->getDepthCheckEnabled());
-    mDestRenderSystem->_setDepthBufferWriteEnabled(usedPass->getDepthWriteEnabled());
-    mDestRenderSystem->_setDepthBias(usedPass->getDepthBias());
-	// Alpha-reject settings
-	mDestRenderSystem->_setAlphaRejectSettings(
-		usedPass->getAlphaRejectFunction(), usedPass->getAlphaRejectValue());
-    // Set colour write mode
-    // Right now we only use on/off, not per-channel
-    bool colWrite = usedPass->getColourWriteEnabled();
-    mDestRenderSystem->_setColourBufferWriteEnabled(colWrite, colWrite, colWrite, colWrite);
-    // Culling mode
-    mDestRenderSystem->_setCullingMode(usedPass->getCullingMode());
-    // Shading
-    mDestRenderSystem->setShadingType(usedPass->getShadingMode());
-
-	return usedPass;
-}*/
-//-----------------------------------------------------------------------
 void VisibilityTerrainSceneManager::ShowVisualization(Camera *cam)
 {
 	// add player camera for visualization purpose
-	try {
+	try 
+	{
 		Camera *c;
 		if ((c = getCamera("PlayerCam")) != NULL)
@@ -223,5 +74,5 @@
 		}   
     }
-    catch(...)
+    catch (...)
     {
         // ignore
@@ -241,8 +92,8 @@
 		for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
 		{
-			
 			if (mRenderNodesForViz)
 			{
 				getRenderQueue()->addRenderable(*it);
+
 				// addbounding boxes instead of node itself
 				//(*it)->_addBoundingBoxToQueue(getRenderQueue());
@@ -250,17 +101,25 @@
 			if (mRenderNodesContentForViz) 
 			{
-				(*it)->_addToRenderQueue(cam, getRenderQueue(), false);
+				(*it)->_myAddToRenderQueue(cam, getRenderQueue(), false);
 			}
 		}
-	}
-	
+	}	
 }
 //-----------------------------------------------------------------------
 Pass *VisibilityTerrainSceneManager::setPass(Pass* pass)
 {
-	// setting vertex program is not efficient
-	//Pass *usedPass = ((mIsDepthPass && !pass->hasVertexProgram()) ? mDepthPass : pass);		
+	//Pass *usedPass = ((mIsDepthPass && !pass->hasVertexProgram()) ? mDepthPass : pass); // setting vertex program is not efficient
+	
 	// set depth fill pass only if depth write enabled
-	Pass *usedPass = (mIsDepthPass && pass->getDepthWriteEnabled() ? mDepthPass : pass);
+	Pass *usedPass = (mIsDepthPass && !mHierarchyInterface->IsBoundingBoxQuery() ? mDepthPass : pass);
+		
+	IlluminationRenderStage savedStage = mIlluminationStage; 
+	
+	// set illumination stage to NONE so no shadow material is used 
+	// for depth pass or for occlusion query
+	if (mIsDepthPass || mHierarchyInterface->IsBoundingBoxQuery())
+	{
+		mIlluminationStage = IRS_NONE;
+	}
 	
 	if (mIsDepthPass)
@@ -286,11 +145,12 @@
 		}
 	}
-	/*else if (mIsItemBufferPass)
-	{
-		usedPass = mItemBufferPass;
-	}*/
-	SceneManager::setPass(usedPass);
-
-	return usedPass;
+	//else if (mIsItemBufferPass) {usedPass = mItemBufferPass;}
+	
+	Pass *result = SceneManager::setPass(usedPass);
+
+	// reset illumination stage
+	mIlluminationStage = savedStage;
+
+	return result;
 }
 //-----------------------------------------------------------------------
@@ -302,23 +162,27 @@
 		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);
-	}
+	
+	mVisible.clear();
+	mBoxes.clear();
+	
+	//TerrainSceneManager::_findVisibleObjects(cam, onlyShadowCasters);		
+
+	// 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);
+
+	bool leaveTransparentsInQueue = mDelayRenderTransparents && !mUseDepthPass;
+
+	// possible two cameras (one for culling, one for rendering)
+	mHierarchyInterface->InitFrame(mOctree, cam, //mCameraInProgress, 
+						mCullCamera ? getCamera("CullCamera") : NULL,
+						leaveTransparentsInQueue);
 }
 //-----------------------------------------------------------------------
 void VisibilityTerrainSceneManager::_renderVisibleObjects()
 {
-	// create material for depth pass
-	InitDepthPass();
-
 	// visualization: apply standard rendering
 	if (mShowVisualization)
@@ -327,5 +191,7 @@
 		return;
 	}
-
+	
+	// create material for depth pass
+	InitDepthPass();
 
 	//-- hierarchical culling
@@ -333,12 +199,5 @@
 	// 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
+	// frame initialisation to reset culling manager stats
 	mVisibilityManager->GetCullingManager()->InitFrame(mVisualizeCulledNodes);
 	
@@ -351,5 +210,5 @@
 	setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
 
-	SceneManager::_renderVisibleObjects();
+	TerrainSceneManager::_renderVisibleObjects();
 
 
@@ -396,6 +255,7 @@
 	// for delayed rendering: transparents, overlay 
 	clearSpecialCaseRenderQueues();
-	SceneManager::_renderVisibleObjects();
-
+	TerrainSceneManager::_renderVisibleObjects();
+
+	getRenderQueue()->clear();
 	//WriteLog(); // write out stats
 }
@@ -517,4 +377,5 @@
             bool doLightIteration, const LightList* manualLightList)
 {
+	// for correct rendering, transparents must be rendered after hierarchical culling
 	if (!mSkipTransparents)
 	{
@@ -522,4 +383,14 @@
 	}
 }
+//-----------------------------------------------------------------------
+bool VisibilityTerrainSceneManager::validatePassForRendering(Pass* pass)
+{
+	// skip all but first pass if we are doing the depth pass
+	if (mIsDepthPass && pass->getIndex() > 0)
+	{
+		return false;
+	}
+	return SceneManager::validatePassForRendering(pass);
+}
 
 } // namespace Ogre
Index: trunk/VUT/Ogre/src/OgreVisibilitySceneManagerDll.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilitySceneManagerDll.cpp	(revision 120)
+++ trunk/VUT/Ogre/src/OgreVisibilitySceneManagerDll.cpp	(revision 121)
@@ -57,6 +57,4 @@
 
 	heightmapTerrainPageSource = new HeightmapTerrainPageSource();
-	visibilityTerrainPlugin->registerPageSource("Heightmap", heightmapTerrainPageSource);
-
 	
 	// Register
@@ -64,17 +62,25 @@
 	Root::getSingleton().setSceneManager(ST_EXTERIOR_CLOSE, visibilityTerrainPlugin);
 	//Root::getSingleton().setSceneManager(ST_GENERIC, occlusionDotPlugin);
+
+	visibilityTerrainPlugin->registerPageSource("Heightmap", heightmapTerrainPageSource);
 }
 //-----------------------------------------------------------------------
-extern "C" void dllStopPlugin(void)
+extern "C" void dllStopPlugin()
 {
 	delete heightmapTerrainPageSource;
-  
+
 	delete visibilityOctreePlugin;
 	delete visibilityTerrainPlugin;
 	//delete occlusionDotPlugin;
-
+	
 	delete visManager;
 	delete visEnv;
 }
+//-----------------------------------------------------------------------
+extern "C" void dllShutdownPlugin()
+{
+	visibilityTerrainPlugin->shutdown();
+	heightmapTerrainPageSource->shutdown();
+}
 
 } //namespace Ogre
Index: trunk/VUT/Ogre/src/OgreSceneContentGenerator.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreSceneContentGenerator.cpp	(revision 112)
+++ trunk/VUT/Ogre/src/OgreSceneContentGenerator.cpp	(revision 121)
@@ -160,5 +160,5 @@
 	char objName[100];
 
-	if(!ifstr.is_open())
+	if (!ifstr.is_open())
 		return false;
 
@@ -177,9 +177,8 @@
 					orientation.getRoll().valueDegrees());*/
 
-		(void)GenerateSceneObject(position, orientation, objName);
+		GenerateSceneObject(position, orientation, objName);
 		
-		std::stringstream d;
-		d << StringConverter::toString(position) << " " << StringConverter::toString(orientation);
-		LogManager::getSingleton().logMessage(d.str());
+		//std::stringstream d; d << StringConverter::toString(position) << " " << StringConverter::toString(orientation);
+		//LogManager::getSingleton().logMessage(d.str());
 	}
 	ifstr.close();
Index: trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 120)
+++ trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 121)
@@ -249,5 +249,5 @@
 	// for delayed rendering: transparents, overlay 
 	clearSpecialCaseRenderQueues();
-	SceneManager::_renderVisibleObjects();
+	OctreeSceneManager::_renderVisibleObjects();
 	
 	WriteLog(); // write out stats
Index: trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 120)
+++ trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 121)
@@ -9,6 +9,7 @@
 //-----------------------------------------------------------------------
 PlatformHierarchyInterface::PlatformHierarchyInterface(SceneManager *sm, RenderSystem *rsys):
-mSceneManager(sm), mRenderSystem(rsys), mSolidBoundingBox(NULL),
-mCamera(NULL), mCullCamera(NULL), mOnlyShadowCasters(false), mLeaveTransparentsInQueue(false)
+mSceneManager(sm), mRenderSystem(rsys), mSolidBoundingBox(NULL), mCamera(NULL), 
+mCullCamera(NULL), mOnlyShadowCasters(false), mLeaveTransparentsInQueue(false),
+mIsBoundingBoxQuery(false)
 {
 }
@@ -86,6 +87,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);
@@ -130,17 +131,15 @@
 {
 	GtpVisibility::HierarchyInterface::InitFrame(root);
+	
 	mPreviousNode = NULL;
 	mLeaveTransparentsInQueue = leaveTransparentsInQueue;
-	SetCamera(cam);
-
-	if (cullCam)
-	{
-		SetCullCamera(cullCam);
-	}
-	else
-	{
-		SetCullCamera(cam);
-	}
-
+
+	mCamera = cam;
+	mCullCamera = cullCam ? cullCam : cam;
+
+	if (mCullCamera != mCamera)
+		LogManager::getSingleton().logMessage("cullcamera is not camera!");
+
+	// create materials for node visualization
 	CreateNodeVizMaterials();
 }
@@ -186,6 +185,9 @@
 	else
 	{
+		mIsBoundingBoxQuery = true;
 		//LogManager::getSingleton().logMessage("render box\n");
 		RenderBoundingBox(GetBoundingBox(node));
+
+		mIsBoundingBoxQuery = false;
 	}
 
@@ -238,3 +240,8 @@
 	return mUseOptimization;
 }
+//-----------------------------------------------------------------------
+bool PlatformHierarchyInterface::IsBoundingBoxQuery()
+{
+	return mIsBoundingBoxQuery;
+}
 } // namespace Ogre
Index: trunk/VUT/Ogre/resources/VisibilityDemo.overlay
===================================================================
--- trunk/VUT/Ogre/resources/VisibilityDemo.overlay	(revision 120)
+++ trunk/VUT/Ogre/resources/VisibilityDemo.overlay	(revision 121)
@@ -306,4 +306,12 @@
 			caption [F2] Show / Hide stats
 		}
+		element TextArea(Example/Visibility/Help/AppStateInfo): Example/Visibility/Templates/BasicText
+		{
+			left 5
+			top 85
+			width 180
+			height 30
+			caption [F3] Toggle between interactive / recorded walkthrough
+		}
 		element TextArea(Example/Visibility/Help/RecordedInfo): Example/Visibility/Templates/BasicText
 		{
@@ -312,13 +320,5 @@
 			width 180
 			height 30
-			caption [F3] Start / End frame recording
-		}
-		element TextArea(Example/Visibility/Help/AppStateInfo): Example/Visibility/Templates/BasicText
-		{
-			left 5
-			top 85
-			width 180
-			height 30
-			caption [F4] Toggle between interactive / recorded walkthrough
+			caption [F4] Start / End frame recording
 		}
 		element TextArea(Example/Visibility/Help/ScreenshotsInfo): Example/Visibility/Templates/BasicText
