Index: trunk/VUT/Ogre/include/OgreVisibilityOctreeSceneManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgreVisibilityOctreeSceneManager.h	(revision 119)
+++ trunk/VUT/Ogre/include/OgreVisibilityOctreeSceneManager.h	(revision 122)
@@ -77,5 +77,5 @@
 	bool mDelayRenderTransparents;
 	bool mUseDepthPass;
-	bool mIsDepthPass;
+	bool mRenderDepthPass;
 
 	Pass *mDepthPass;
Index: trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h	(revision 121)
+++ trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h	(revision 122)
@@ -67,8 +67,20 @@
 	bool validatePassForRendering(Pass* pass);
 
+	void RenderItemBuffer(RenderPriorityGroup* pGroup);
+	void RenderSingleObjectForItemBuffer(Renderable *rend);
+	void renderQueueGroupObjects(RenderQueueGroup* pGroup);
+
+	/** Override from SceneManager so that sub entities can be assigned an id for item buffer */
+	Entity* createEntity(const String& entityName, const String& meshName);
+
 protected:
-	/** Creates material for depth pass, e.g., a pass that only fills the depth buffer */
+	
+	/** Creates material for depth pass, e.g., a pass that only fills the depth buffer. */
 	void InitDepthPass();
+	/** Creates material for item buffer. */
+	void InitItemBufferPass();
+	/** Fills render queue so that a visualization can be rendered. */
 	void ShowVisualization(Camera *cam);
+
 	OctreeHierarchyInterface *mHierarchyInterface;
 	GtpVisibility::VisibilityManager *mVisibilityManager;
@@ -82,7 +94,11 @@
 	bool mDelayRenderTransparents;
 	bool mUseDepthPass;
-	bool mIsDepthPass;
+	bool mRenderDepthPass;
+	bool mRenderItemBuffer;
 
 	Pass *mDepthPass;
+	Pass *mItemBufferPass;
+
+	int mCurrentEntityId;
 };
 
Index: trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 121)
+++ trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 122)
@@ -7,5 +7,6 @@
 #include <OgreLogManager.h>
 #include <OgreStringConverter.h>
-
+#include <OgreEntity.h>
+#include <OgreSubEntity.h>
 
 namespace Ogre {
@@ -15,5 +16,5 @@
 	GtpVisibility::VisibilityManager *visManager): 
 mVisibilityManager(visManager), 
-mIsDepthPass(false),
+mRenderDepthPass(false),
 mShowVisualization(false),
 mRenderNodesForViz(false),
@@ -22,5 +23,7 @@
 mSkipTransparents(false),
 mDelayRenderTransparents(true),
-mUseDepthPass(false)
+mUseDepthPass(false),
+mRenderItemBuffer(true),
+mCurrentEntityId(0)
 {
 	mHierarchyInterface = new OctreeHierarchyInterface(this, mDestRenderSystem);
@@ -54,4 +57,27 @@
 }
 //-----------------------------------------------------------------------
+void VisibilityTerrainSceneManager::InitItemBufferPass()
+{
+	MaterialPtr itemBufferMat = MaterialManager::getSingleton().
+		getByName("Visibility/ItemBufferPass");
+
+	if (itemBufferMat.isNull())
+    {
+		// Init
+		itemBufferMat = MaterialManager::getSingleton().create("Visibility/ItemBufferPass",
+                ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
+
+		mItemBufferPass = itemBufferMat->getTechnique(0)->getPass(0);
+		mItemBufferPass->setColourWriteEnabled(true);
+		mItemBufferPass->setDepthWriteEnabled(true);
+		mItemBufferPass->setLightingEnabled(true);
+	}
+	else
+	{
+		mItemBufferPass = itemBufferMat->getTechnique(0)->getPass(0);
+	}
+	mItemBufferPass->setAmbient(0, 0, 0);
+}
+//-----------------------------------------------------------------------
 VisibilityTerrainSceneManager::~VisibilityTerrainSceneManager()
 {
@@ -65,4 +91,5 @@
 void VisibilityTerrainSceneManager::ShowVisualization(Camera *cam)
 {
+	LogManager::getSingleton().logMessage("***********VISUALIZATION************");
 	// add player camera for visualization purpose
 	try 
@@ -101,5 +128,5 @@
 			if (mRenderNodesContentForViz) 
 			{
-				(*it)->_myAddToRenderQueue(cam, getRenderQueue(), false);
+				(*it)->_addToRenderQueue(cam, getRenderQueue(), false);
 			}
 		}
@@ -109,8 +136,8 @@
 Pass *VisibilityTerrainSceneManager::setPass(Pass* pass)
 {
-	//Pass *usedPass = ((mIsDepthPass && !pass->hasVertexProgram()) ? mDepthPass : pass); // setting vertex program is not efficient
+	//Pass *usedPass = ((mRenderDepthPass && !pass->hasVertexProgram()) ? mDepthPass : pass); // setting vertex program is not efficient
 	
 	// set depth fill pass only if depth write enabled
-	Pass *usedPass = (mIsDepthPass && !mHierarchyInterface->IsBoundingBoxQuery() ? mDepthPass : pass);
+	Pass *usedPass = (mRenderDepthPass && !mHierarchyInterface->IsBoundingBoxQuery() ? mDepthPass : pass);
 		
 	IlluminationRenderStage savedStage = mIlluminationStage; 
@@ -118,10 +145,10 @@
 	// set illumination stage to NONE so no shadow material is used 
 	// for depth pass or for occlusion query
-	if (mIsDepthPass || mHierarchyInterface->IsBoundingBoxQuery())
+	if (mRenderDepthPass || mHierarchyInterface->IsBoundingBoxQuery())
 	{
 		mIlluminationStage = IRS_NONE;
 	}
 	
-	if (mIsDepthPass)
+	if (mRenderDepthPass)
 	{
 		// set vertex program of current pass
@@ -157,4 +184,13 @@
 void VisibilityTerrainSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
 {
+	// needs full ambient lighting for item colors to be exact
+	if (mRenderItemBuffer)
+	{
+		setAmbientLight(ColourValue(1,1,1,1));
+	}
+
+	LogManager::getSingleton().logMessage("***********FIND OBJECTS************");
+	getRenderQueue()->clear();
+
 	//-- show visible scene nodes and octree bounding boxes from last frame
 	if (mShowVisualization)
@@ -163,9 +199,9 @@
 	}
 	
+	//TerrainSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
+	
 	mVisible.clear();
 	mBoxes.clear();
 	
-	//TerrainSceneManager::_findVisibleObjects(cam, onlyShadowCasters);		
-
 	// if there is no depth pass =>
 	// we interleave identification and rendering of objects 
@@ -185,13 +221,28 @@
 void VisibilityTerrainSceneManager::_renderVisibleObjects()
 {
+    std::stringstream d;
+	d << "Terrain render level: " << TerrainRenderable::getCurrentRenderLevelIndex();
+	LogManager::getSingleton().logMessage(d.str());
+
+	// increase terrain renderlevel
+	int renderLevel = TerrainRenderable::getCurrentRenderLevelIndex() + 1;
+
+	if (TerrainRenderable::getCurrentRenderLevelIndex() >= 10)
+	{   // max. 10 different renderlevels
+		renderLevel = 0;
+	}
+
 	// visualization: apply standard rendering
 	if (mShowVisualization)
 	{	
 		TerrainSceneManager::_renderVisibleObjects();
+		TerrainRenderable::setCurrentRenderLevelIndex(renderLevel);
 		return;
 	}
-	
-	// create material for depth pass
-	InitDepthPass();
+
+	LogManager::getSingleton().logMessage("***********RENDER OBJECTS************");
+
+	InitDepthPass(); 	// create material for depth pass
+	InitItemBufferPass(); // create material for item buffer pass
 
 	//-- hierarchical culling
@@ -228,5 +279,5 @@
 
 	// set state for depth pass
-    mIsDepthPass = mUseDepthPass;
+    mRenderDepthPass = mUseDepthPass;
 	
 	/** 
@@ -242,9 +293,9 @@
 	if (mUseDepthPass)
 	{
-		/*for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
+		for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
 		{
 			(*it)->_addToRenderQueue(mCameraInProgress, getRenderQueue(), false);
-		}*/
-		mIsDepthPass = false;
+		}
+		mRenderDepthPass = false;
 	}
 
@@ -257,7 +308,8 @@
 	TerrainSceneManager::_renderVisibleObjects();
 
-	getRenderQueue()->clear();
+	TerrainRenderable::setCurrentRenderLevelIndex(renderLevel);
 	//WriteLog(); // write out stats
 }
+
 //-----------------------------------------------------------------------
 void VisibilityTerrainSceneManager::_updateSceneGraph(Camera* cam)
@@ -318,4 +370,10 @@
 		mDelayRenderTransparents = (*static_cast<const bool *>(val));
 		return true;
+	}
+	// notifiy that frame has ended so terrain render level can be reset for correct
+	// terrain rendering
+	if (key == "TerrainLevelIdx")
+	{
+		TerrainRenderable::setCurrentRenderLevelIndex((*static_cast<const int *>(val)));
 	}
 	return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
@@ -387,5 +445,5 @@
 {
 	// skip all but first pass if we are doing the depth pass
-	if (mIsDepthPass && pass->getIndex() > 0)
+	if ((mRenderDepthPass || mRenderItemBuffer) && pass->getIndex() > 0)
 	{
 		return false;
@@ -393,4 +451,95 @@
 	return SceneManager::validatePassForRendering(pass);
 }
+//-----------------------------------------------------------------------
+void VisibilityTerrainSceneManager::renderQueueGroupObjects(RenderQueueGroup* pGroup)
+{
+	if (!mRenderItemBuffer)
+	{
+		TerrainSceneManager::renderQueueGroupObjects(pGroup);
+		return;
+	}
+
+	//--- item buffer
+    // Iterate through priorities
+    RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
+
+	while (groupIt.hasMoreElements())
+    {
+		RenderItemBuffer(groupIt.getNext());
+	}
+}
+//-----------------------------------------------------------------------
+void VisibilityTerrainSceneManager::RenderItemBuffer(RenderPriorityGroup* pGroup)
+{
+	// Do solids
+	RenderPriorityGroup::SolidRenderablePassMap solidObjs = pGroup->_getSolidPasses();
+
+	// ----- SOLIDS LOOP -----
+	RenderPriorityGroup::SolidRenderablePassMap::const_iterator ipass, ipassend;
+	ipassend = solidObjs.end();
+
+	for (ipass = solidObjs.begin(); ipass != ipassend; ++ipass)
+	{
+		// Fast bypass if this group is now empty
+		if (ipass->second->empty()) 
+			continue;
+
+		// Render only first pass
+		if (ipass->first->getIndex() > 0)
+			continue;
+
+		RenderPriorityGroup::RenderableList* rendList = ipass->second;
+		
+		RenderPriorityGroup::RenderableList::const_iterator irend, irendend;
+		irendend = rendList->end();
+			
+		for (irend = rendList->begin(); irend != irendend; ++irend)
+		{
+			RenderSingleObjectForItemBuffer(*irend);
+		}
+	}
+
+	// ----- TRANSPARENT LOOP
+	RenderPriorityGroup::TransparentRenderablePassList
+		transpObjs = pGroup->_getTransparentPasses();
+	RenderPriorityGroup::TransparentRenderablePassList::const_iterator itrans, itransend;
+
+	itransend = transpObjs.end();
+	for (itrans = transpObjs.begin(); itrans != itransend; ++itrans)
+	{
+		// Render only first pass
+		if (itrans->pass->getIndex() == 0)
+		{			
+			RenderSingleObjectForItemBuffer(itrans->renderable);
+		}
+	} 
+}
+//-----------------------------------------------------------------------
+void VisibilityTerrainSceneManager::RenderSingleObjectForItemBuffer(Renderable *rend)
+{
+	static LightList nullLightList;
+	Real col = (Real)rend->getId() / (Real)mCurrentEntityId;
+	mItemBufferPass->setAmbient(col, 0, 0);
+
+	setPass(mItemBufferPass);
+
+	std::stringstream d;
+	d << "item buffer id: " << rend->getId() << ", col: " << col;
+	LogManager::getSingleton().logMessage(d.str());
+	// Render a single object, this will set up auto params if required
+	renderSingleObject(rend, mItemBufferPass, true, &nullLightList);
+}
+//-----------------------------------------------------------------------
+Entity* VisibilityTerrainSceneManager::createEntity(const String& entityName, const String& meshName)
+{
+	Entity *ent = SceneManager::createEntity(entityName, meshName);
+
+	for (int i = 0; i < ent->getNumSubEntities(); ++i)
+	{
+		ent->getSubEntity(i)->setId(mCurrentEntityId ++);
+	}
+
+	return ent;
+}
 
 } // namespace Ogre
Index: trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 121)
+++ trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 122)
@@ -16,5 +16,5 @@
 : 
 mVisibilityManager(visManager), 
-mIsDepthPass(false),
+mRenderDepthPass(false),
 mShowVisualization(false),
 mRenderNodesForViz(false),
@@ -69,11 +69,12 @@
 {
 	// setting vertex program is not efficient
-	Pass *usedPass = ((mIsDepthPass && pass->getDepthWriteEnabled() && !pass->hasVertexProgram()) ? mDepthPass : pass);		
+	Pass *usedPass = ((mRenderDepthPass && pass->getDepthWriteEnabled() && 
+		!pass->hasVertexProgram()) ? mDepthPass : pass);		
 
 	/*
 	// set depth fill pass only if depth write enabled
-	Pass *usedPass = (mIsDepthPass && pass->getDepthWriteEnabled() ? mDepthPass : pass);
-
-    if (mIsDepthPass && pass->hasVertexProgram())
+	Pass *usedPass = (mRenderDepthPass && pass->getDepthWriteEnabled() ? mDepthPass : pass);
+
+    if (mRenderDepthPass && pass->hasVertexProgram())
     {
 		// set vertex program of current pass to depth pass
@@ -127,5 +128,5 @@
 
 		for (NodeList::iterator it = mVisible.begin(); it != mVisible.end(); ++it)
-	{
+		{
 
 			if (mRenderNodesForViz)
@@ -169,5 +170,5 @@
 	// create material for depth pass
 	InitDepthPass();
-
+	
 	// visualization: apply standard rendering
 	if (mShowVisualization)
@@ -175,6 +176,5 @@
 		OctreeSceneManager::_renderVisibleObjects();
 		return;
-	} 
-	
+	} 	
 
 	//-- hierarchical culling
@@ -218,5 +218,5 @@
 
 	// set state for depth pass
-    mIsDepthPass = mUseDepthPass;
+    mRenderDepthPass = mUseDepthPass;
 	
 	/** 
@@ -241,5 +241,5 @@
 			(*it)->_addToRenderQueue(mCameraInProgress, getRenderQueue(), false);
 		}
-		mIsDepthPass = false;
+		mRenderDepthPass = false;
     }
 	mSkipTransparents = false;
Index: trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 121)
+++ trunk/VUT/Ogre/src/OgrePlatformHierarchyInterface.cpp	(revision 122)
@@ -173,10 +173,10 @@
 	//-- the actual query test
 	query->BeginQuery();
-			
+	
 	// if node is leaf and was visible => will be rendered anyway.
 	// In this case we can also test with the real geometry.
 	// If camera for culling is different from camera for rendering or only solids 
 	// will be rendereded => cannot optimize
-	if (mUseOptimization && (mCamera == mCullCamera) && wasVisible && IsLeaf(node) ) 
+	if (mUseOptimization && (mCamera == mCullCamera) && wasVisible && IsLeaf(node)) 
 	{
 		//LogManager::getSingleton().logMessage("render node\n");
