Index: trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 130)
+++ trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 133)
@@ -25,5 +25,6 @@
 mUseDepthPass(false),
 mRenderItemBuffer(false),
-mCurrentEntityId(0)
+mCurrentEntityId(0),
+mEnableDepthWrite(true)
 {
 	mHierarchyInterface = new OctreeHierarchyInterface(this, mDestRenderSystem);
@@ -145,5 +146,6 @@
 	
 	// set depth fill pass only if depth write enabled
-	Pass *usedPass = (mRenderDepthPass && !mHierarchyInterface->IsBoundingBoxQuery() ? mDepthPass : pass);
+	Pass *usedPass = (mRenderDepthPass && !mHierarchyInterface->IsBoundingBoxQuery() ? 
+					  mDepthPass : pass);
 		
 	IlluminationRenderStage savedStage = mIlluminationStage; 
@@ -158,5 +160,5 @@
 	if (mRenderDepthPass)
 	{
-		// set vertex program of current pass
+		// --- set vertex program of current pass so z-buffer is updated correctly
         if (pass->hasVertexProgram())
 		{
@@ -178,7 +180,21 @@
 		}
 	}
+
+	bool IsDepthWrite = usedPass->getDepthWriteEnabled();
+	// global option which enables / disables depth writes
+	if (!mEnableDepthWrite)
+	{
+		usedPass->setDepthWriteEnabled(false);
+	}
+
 	//else if (mIsItemBufferPass) {usedPass = mItemBufferPass;}
 	
 	Pass *result = SceneManager::setPass(usedPass);
+
+	// reset depth write
+	if (!mEnableDepthWrite)
+	{
+		usedPass->setDepthWriteEnabled(IsDepthWrite);
+	}
 
 	// reset illumination stage
@@ -191,10 +207,7 @@
 {
 	// needs full ambient lighting for item colors to be exact
-	if (mRenderItemBuffer)
-	{
-		setAmbientLight(ColourValue(1,1,1,1));
-	}
-	//getRenderQueue()->clear();
-
+	/*if (mRenderItemBuffer)
+	{	setAmbientLight(ColourValue(1,1,1,1)); }*/
+	
 	//-- show visible scene nodes and octree bounding boxes from last frame
 	if (mShowVisualization)
@@ -374,4 +387,9 @@
 	{
 		TerrainRenderable::setCurrentRenderLevelIndex((*static_cast<const int *>(val)));
+		return true;
+	}
+	if (key == "DepthCheck")
+	{
+		mEnableDepthWrite = (*static_cast<const bool *>(val));
 		return true;
 	}
@@ -508,6 +526,6 @@
 	}
 
-	// ----- TRANSPARENT LOOP: must be handlet differently altough we don't really
-	// care about transparents for the item buffer
+	// ----- TRANSPARENT LOOP: must be handled differently 
+	// although we don't really	care about transparents for the item buffer
 	// TODO: HOW TO HANDLE OCCLUDED OBJECTS ????
 	RenderPriorityGroup::TransparentRenderablePassList
Index: trunk/VUT/Ogre/src/OgreVisibilitySceneManagerDll.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilitySceneManagerDll.cpp	(revision 121)
+++ trunk/VUT/Ogre/src/OgreVisibilitySceneManagerDll.cpp	(revision 133)
@@ -46,5 +46,5 @@
 
 //-----------------------------------------------------------------------
-extern "C" void dllStartPlugin(void)
+extern "C" void __declspec(dllexport) dllStartPlugin(void)
 {
 	visEnv = new GtpVisibility::VisibilityEnvironment();
@@ -66,5 +66,11 @@
 }
 //-----------------------------------------------------------------------
-extern "C" void dllStopPlugin()
+extern "C" void __declspec(dllexport) dllShutdownPlugin()
+{
+	visibilityTerrainPlugin->shutdown();
+	heightmapTerrainPageSource->shutdown();
+}
+//-----------------------------------------------------------------------
+extern "C" void __declspec(dllexport) dllStopPlugin()
 {
 	delete heightmapTerrainPageSource;
@@ -77,10 +83,5 @@
 	delete visEnv;
 }
-//-----------------------------------------------------------------------
-extern "C" void dllShutdownPlugin()
-{
-	visibilityTerrainPlugin->shutdown();
-	heightmapTerrainPageSource->shutdown();
-}
+
 
 } //namespace Ogre
Index: trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 130)
+++ trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 133)
@@ -188,4 +188,6 @@
 	else
 	{
+		// used by the scene manager, because bounding box rendering must be handled
+		// differently
 		mIsBoundingBoxQuery = true;
 
Index: trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp	(revision 133)
+++ trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp	(revision 133)
@@ -0,0 +1,181 @@
+#include "OgrePlatformQueryManager.h"
+#include "OcclusionQuery.h"
+#include <OgreSceneManager.h>
+#include <OgreLogManager.h>
+
+#include <vector>
+
+
+namespace Ogre {
+//----------------------------------------------------------------------- 
+PlatformQueryManager::PlatformQueryManager(PlatformHierarchyInterface *hierarchyInterface, Viewport *vp):
+QueryManager(hierarchyInterface), mViewport(vp)
+{
+}
+//-----------------------------------------------------------------------
+bool PlatformQueryManager::ShootRay(const Ray &ray, std::vector<Mesh *> *visibleMeshes, bool isGlobalLine)
+{
+    // run OGRE ray shooting query
+    return false;
+}
+//-----------------------------------------------------------------------
+void PlatformQueryManager::ComputeCameraVisibility(const Camera &camera,
+			    InfoContainer<GtpVisibility::NodeInfo> *visibleNodes,
+			    InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry,
+			    bool relativeVisibility)
+{
+	if (relativeVisibility)
+		LogManager::getSingleton().logMessage("relative");
+	else
+		LogManager::getSingleton().logMessage("absolute");
+
+	// we need access to the scene manager and the rendersystem
+	PlatformHierarchyInterface *pfHierarchyInterface = 
+		dynamic_cast<PlatformHierarchyInterface *>(mHierarchyInterface);
+
+	//-- Render scene to get conservative visibility and fill depth buffer 
+
+	// can do const_cast because Camera no changed in renderScene
+	Camera *pCam = const_cast<Camera *>(&camera); 
+
+	pfHierarchyInterface->GetSceneManager()->_renderScene(pCam, mViewport, false);
+
+	/* 
+		Test again to get exact visibility with regard to the current camera.
+		Issue all queries first to avoid starvation & stalls.
+	*/
+	GtpVisibility::QueryList queryList[2];
+	
+	// get rendered hierarchy nodes
+	GtpVisibility::HierarchyNodeList *nodeList = mHierarchyInterface->GetRenderedNodes();
+	// vector for storing entities of meshes
+	GtpVisibility::GeometryList geometryList;
+	
+	GtpVisibility::HierarchyNodeList::const_iterator nodeIt, nodeIt_end = nodeList->end();
+	// geometry list has still do be built
+	GtpVisibility::GeometryList::iterator geometryIt, geometryIt_end;
+
+	// for relative visibility we need 2 rendering passes
+	int n = relativeVisibility ? 2 : 1;
+	
+	for (int i=0; i<n; ++i)
+	{
+		//-- queries for hierarchy nodes
+		for (nodeIt = nodeList->begin(); nodeIt != nodeIt_end; ++nodeIt)
+		{
+			// TODO: DELETE QUERIES FROM PREVIOUS RENDER
+			queryList[i].push_back(mHierarchyInterface->IssueOcclusionQuery(*nodeIt, false));
+			
+			// store geometry of nodes in geometry list
+			mHierarchyInterface->GetGeometry(*nodeIt, &geometryList, false);
+		}
+
+		geometryIt_end = geometryList.end();
+
+		//-- add queries for geometry
+		for (geometryIt = geometryList.begin(); geometryIt != geometryIt_end; ++geometryIt)
+		{
+			queryList[i].push_back(mHierarchyInterface->IssueOcclusionQuery(*geometryIt));
+		}
+
+		// --- relative visiblity
+		/* 
+			1) get visible pixels count of objects
+			2) clear frame buffer
+			3) get projected visible pixels count:
+			   test all objects again without depth write (set as option in scene manager)
+			4) calculate ratio between visible vs. projected pixels
+		*/
+		if (i == n - 2) // if relativeVisibility, between first and second loop
+		{
+			pfHierarchyInterface->GetRenderSystem()->clearFrameBuffer(FBT_COLOUR | FBT_DEPTH);
+		}
+
+		// to obtain the correct number of projected pixels, depth write must be disabled
+		bool enableDepthCheck = (i != n - 2);
+
+		// this option must be provided by the scene manager
+		pfHierarchyInterface->GetSceneManager()->setOption("DepthCheck", &enableDepthCheck);
+	}
+
+
+	//---- collect results
+	unsigned int visiblePixels;
+
+	GtpVisibility::QueryList::iterator absQueryIt, relQueryIt;
+
+	absQueryIt = queryList[0].begin();
+	relQueryIt = queryList[1].begin();
+	
+	for (nodeIt = nodeList->begin(); nodeIt != nodeIt_end; ++nodeIt)
+	{
+		(*absQueryIt)->GetQueryResult(visiblePixels, true);
+
+		float vis = (float)visiblePixels;
+
+		if (relativeVisibility)
+		{
+			(*relQueryIt)->GetQueryResult(visiblePixels, true);
+			std::stringstream d; d << "relativ " << visiblePixels;
+			LogManager::getSingleton().logMessage(d.str());
+
+			if (visiblePixels > 0)
+			{				
+				vis /= (float) visiblePixels;
+			}
+			++relQueryIt;
+		}
+
+		++absQueryIt;
+
+		// leave nodes with visibilty 0 in queue: 
+		// happens if node is intersected by near plane
+		visibleNodes->push_back(GtpVisibility::NodeInfo(*nodeIt, vis));
+	}
+
+	//---- queries for geometry
+	geometryIt_end = geometryList.end();
+	
+	/*for (geometryIt = geometryList.begin(); geometryIt != geometryIt_end; ++geometryIt)
+	{
+		(*absQueryIt)->GetQueryResult(visiblePixels, true);
+
+		float vis = (float)visiblePixels;
+		bool isVisible = visiblePixels > 0;
+		
+		if (relativeVisibility)
+		{
+			(*relQueryIt)->GetQueryResult(visiblePixels, true);
+
+			if (visiblePixels)
+			{
+				vis /= (float) visiblePixels;
+			}
+			++relQueryIt;
+		}
+
+		++absQueryIt;
+
+		// approximate depth ordering during rendering => 
+		// geometry maybe occluded
+		if (isVisible)
+		{
+			visibleGeometry->push_back(GtpVisibility::MeshInfo(*geometryIt, vis));
+		}
+	}*/
+
+}
+//-----------------------------------------------------------------------
+void PlatformQueryManager::ComputeFromPointVisibility(const Vector3 &point,
+			       InfoContainer<GtpVisibility::NodeInfo> *visibleNodes,
+			       InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry,
+			       bool relativeVisibility)
+{
+	 visibleNodes->push_back(GtpVisibility::NodeInfo(mHierarchyInterface->GetSceneRoot(), 1.0f));
+}
+//-----------------------------------------------------------------------
+void PlatformQueryManager::SetViewport(Viewport *vp)
+{
+	mViewport = vp;
+}
+} // namespace Ogre
