Index: trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h	(revision 159)
+++ trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h	(revision 164)
@@ -33,13 +33,13 @@
 	*/
 	
-	virtual bool setOption( const String &, const void * );
+	virtual bool setOption(const String &, const void *);
 	/** Gets the given option for the Scene VisibilityManager.
 	    @remarks
 		See setOption
 	*/
-	virtual bool getOption( const String &, void * );
+	virtual bool getOption(const String &, void *);
 
-	bool getOptionValues( const String & key, StringVector &refValueList );
-	bool getOptionKeys( StringVector &refKeys );
+	bool getOptionValues(const String & key, StringVector &refValueList);
+	bool getOptionKeys(StringVector &refKeys);
 
 	/** Sets the visibility manager.
@@ -75,5 +75,5 @@
 	/** Override from SceneManager so that sub entities can be assigned an id for item buffer.
 	*/
-	//Entity* createEntity(const String& entityName, const String& meshName);
+	Entity* createEntity(const String& entityName, const String& meshName);
 
 	/** Returns pointer to visibility manager.
@@ -85,4 +85,6 @@
 	OctreeHierarchyInterface *GetHierarchyInterface();
 
+	/** Inherited from scene manager. Neccesary to draw terrain properly.
+	*/
 	void endFrame();
 
Index: trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 160)
+++ trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 164)
@@ -37,7 +37,5 @@
 	mHierarchyInterface = new OctreeHierarchyInterface(this, mDestRenderSystem);
 	
-	//mDisplayNodes = true;
-	//mShowBoundingBoxes = true;
-	//mShowBoxes = true;
+	//mDisplayNodes = true;//mShowBoundingBoxes = true;//mShowBoxes = true;
 
 	// TODO: set maxdepth to reasonable value
@@ -120,5 +118,5 @@
 	if (mRenderNodesForViz || mRenderNodesContentForViz)
 	{
-		// change node material so it is better suited for visualization
+		// HACK: change node material so it is better suited for visualization
 		MaterialPtr nodeMat = MaterialManager::getSingleton().getByName("Core/NodeMaterial");
 		nodeMat->setAmbient(1, 1, 0);
@@ -130,7 +128,10 @@
 			if (mRenderNodesForViz)
 			{
-				if (((*it)->numAttachedObjects() > 0) && ((*it)->numChildren() == 0)
-					&& (*it)->getAttachedObject(0)->getMovableType() == "Entity")
-				getRenderQueue()->addRenderable((*it));
+				// render the leaf nodes
+				if (((*it)->numAttachedObjects() > 0) && ((*it)->numChildren() == 0) && 
+					 (*it)->getAttachedObject(0)->getMovableType() == "Entity")
+				{
+					getRenderQueue()->addRenderable((*it));
+				}
 
 				// addbounding boxes instead of node itself
@@ -508,5 +509,5 @@
 	}
 
-	// --- item buffer
+	//-- item buffer
 
     // Iterate through priorities
@@ -743,5 +744,4 @@
 */
 //-----------------------------------------------------------------------
-/*
 Entity* VisibilityTerrainSceneManager::createEntity(const String& entityName, 
 													const String& meshName)
@@ -752,4 +752,6 @@
 	{
 		ent->getSubEntity(i)->setId(mCurrentEntityId);
+		ent->getSubEntity(i)->setId((41 << 16) + (4 << 8) + 60);
+		//ent->getSubEntity(i)->setId((2 << 16) + (4 << 8) + 60);
 	}
 
@@ -759,4 +761,3 @@
 	return ent;
 }
-*/
 } // namespace Ogre
Index: trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 159)
+++ trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 164)
@@ -739,6 +739,5 @@
 }
 */
-//-----------------------------------------------------------------------
-/*
+/*//-----------------------------------------------------------------------
 Entity* VisibilityTerrainSceneManager::createEntity(const String& entityName, 
 													const String& meshName)
Index: trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp	(revision 159)
+++ trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp	(revision 164)
@@ -49,5 +49,5 @@
 	
 	// ---- capture visibility from all 6 directions
-	for (int i=0; i < 6; i++)	
+	for (int dir=0; dir < 6; dir++)	
 	{
 		sign *= -1;
@@ -60,5 +60,5 @@
 
 		// prevent from initialising geometry / node array again
-		if (i > 0)
+		if (dir > 0)
 		{
 			mWasInitialised = true;
@@ -70,8 +70,8 @@
 
 		// permute directions
-		Vector3 dir(0,0,0);
-		dir[i/2] = sign;
+		Vector3 direction(0,0,0);
+		direction[dir/2] = sign;
 
-		cam->setDirection(dir);
+		cam->setDirection(direction);
 	}
 	
Index: trunk/VUT/Ogre/src/OgreTerrainContentGenerator.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreTerrainContentGenerator.cpp	(revision 161)
+++ trunk/VUT/Ogre/src/OgreTerrainContentGenerator.cpp	(revision 164)
@@ -16,5 +16,5 @@
 RayQueryExecutor::~RayQueryExecutor()
 {
-	delete mRaySceneQuery;
+	OGRE_DELETE(mRaySceneQuery);
 }
 //-----------------------------------------------------------------------
Index: trunk/VUT/Ogre/src/OgreItemBufferQueryManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreItemBufferQueryManager.cpp	(revision 159)
+++ trunk/VUT/Ogre/src/OgreItemBufferQueryManager.cpp	(revision 164)
@@ -6,6 +6,8 @@
 
 namespace Ogre {
+
 //----------------------------------------------------------------------- 
-ItemBufferQueryManager::ItemBufferQueryManager(PlatformHierarchyInterface *hierarchyInterface, Viewport *vp,
+ItemBufferQueryManager::ItemBufferQueryManager(PlatformHierarchyInterface *hierarchyInterface, 
+											   Viewport *vp,
 											   const bool renderPatches):
 PlatformQueryManager(hierarchyInterface, vp), mRenderPatchesForItemBuffer(renderPatches)
@@ -13,5 +15,7 @@
 }
 //-----------------------------------------------------------------------
-bool ItemBufferQueryManager::ShootRay(const Ray &ray, std::vector<Mesh *> *visibleMeshes, bool isGlobalLine)
+bool ItemBufferQueryManager::ShootRay(const Ray &ray, 
+									  std::vector<Mesh *> *visibleMeshes, 
+									  bool isGlobalLine)
 {
     // run OGRE ray shooting query
@@ -50,8 +54,11 @@
 	ColourValue bg = mViewport->getBackgroundColour();
 	mViewport->setBackgroundColour(ColourValue(0, 0, 0, 0));
+	pfHierarchyInterface->GetRenderSystem()->clearFrameBuffer(FBT_COLOUR | FBT_DEPTH);
 
+	//-- render item buffer
+	sm->_renderScene(pCam, mViewport, false);
+    //mViewport->getTarget()->update();
 
-	// --- render item buffer
-	pfHierarchyInterface->GetSceneManager()->_renderScene(pCam, mViewport, false);
+//for(int j=0; j<10000000; j++)	printf("wait");
 
 
@@ -64,14 +71,20 @@
 
 	int n = mRenderPatchesForItemBuffer ? (int)visiblePatches->size() : (int)visibleGeometry->size();
-	
+
+	std::stringstream d; d << "dimx: " << dimx << ", dimy: " << dimy;
+	LogManager::getSingleton().logMessage(d.str());
+
 	// loop through frame buffer & collect visible pixels
 	for (int idx = 0; idx < dimy * dimx * 3; idx += 3)
 	{
-		// -- decode color code to receive id
+		//-- decode color code to receive id
 		int id = buf[idx] << 16;
 		id += buf[idx + 1] << 8;
 		id += buf[idx + 2];
 
-		// if valid id <= add visibility (id values start at 1
+		std::stringstream d; d << "myid: " << (int)buf[idx] << " " << (int)buf[idx + 1] << " " << (int)buf[idx + 2];
+		LogManager::getSingleton().logMessage(d.str());
+
+		// if valid id <= add visibility (id values start at 1)
 		if ((id > 0) && (id < n))
 		{
@@ -144,4 +157,6 @@
 				
 			subEnt->setId(id);
+			subEnt->setId((41 << 16) + (4 << 8) + 60);
+			//subEnt->setId((2 << 16) + (4 << 8) + 60);
 		}
 			
