Index: trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp	(revision 155)
+++ trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp	(revision 156)
@@ -453,5 +453,6 @@
 			++nodesSize;
 
-			std::stringstream d; d << "Node visibility: " << vis;
+			int id = sm->GetHierarchyInterface()->GetNodeId((*nodesIt).GetNode());
+			std::stringstream d; d << "Node " << id << " visibility: " << vis;
 			LogManager::getSingleton().logMessage(d.str());
 		}	
Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 155)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 156)
@@ -162,6 +162,6 @@
 
 		// the objects are generated on the whole terrain
-		mTerrainContentGenerator->GenerateScene(1500, "robot");
-		//mTerrainContentGenerator->GenerateScene(1500, "athene");
+		//mTerrainContentGenerator->GenerateScene(1500, "robot");
+		mTerrainContentGenerator->GenerateScene(1500, "athene");
 		//mTerrainContentGenerator->GenerateScene(500, "ninja");
 	}
Index: trunk/VUT/work/ogre_changes/OgreMain/include/OgreRenderTarget.h
===================================================================
--- trunk/VUT/work/ogre_changes/OgreMain/include/OgreRenderTarget.h	(revision 154)
+++ trunk/VUT/work/ogre_changes/OgreMain/include/OgreRenderTarget.h	(revision 156)
@@ -268,5 +268,5 @@
 
 #ifdef GTP_VISIBILITY_MODIFIED_OGRE
-		virtual uchar *getBufferContents(int &dimx, int &dimy) { return NULL; };
+		virtual uchar *getBufferContents(int &dimx, int &dimy) = 0;
 #endif // GTP_VISIBILITY_MODIFIED_OGRE
 
Index: trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreGLTexture.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreGLTexture.cpp	(revision 153)
+++ trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreGLTexture.cpp	(revision 156)
@@ -440,5 +440,4 @@
 	uchar *GLRenderTexture::getBufferContents(int &dimx, int &dimy)
 	{
-		ImageCodec::ImageData *imgData = new ImageCodec::ImageData();
 		dimx = mWidth;
 		dimy = mHeight;
Index: trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreWin32Window.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreWin32Window.cpp	(revision 153)
+++ trunk/VUT/work/ogre_changes/RenderSystems/GL/src/OgreWin32Window.cpp	(revision 156)
@@ -387,5 +387,4 @@
 	uchar *Win32Window::getBufferContents(int &dimx, int &dimy)
 	{
-		ImageCodec::ImageData *imgData = new ImageCodec::ImageData();
 		dimx = mWidth;
 		dimy = mHeight;
Index: trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h
===================================================================
--- trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h	(revision 155)
+++ trunk/VUT/Ogre/include/OgreVisibilityTerrainSceneManager.h	(revision 156)
@@ -11,4 +11,6 @@
 #include "OgrePlatformQueryManager.h"
 #include "VisibilityManager.h"
+#include <OgreSolidBoundingBox.h>
+
 
 namespace Ogre {
@@ -123,5 +125,7 @@
 	ShadowTechnique mSavedShadowTechnique;
 
-	bool mRenderOnlyBoundingBoxes;
+	bool mRenderHierarchyNodes;
+
+	std::vector<SolidBoundingBox *> mSolidBoxes;
 };
 
Index: trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h
===================================================================
--- trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h	(revision 155)
+++ trunk/VUT/Ogre/include/OgrePlatformHierarchyInterface.h	(revision 156)
@@ -106,12 +106,5 @@
     bool IsBoundingBoxQuery();
 
-	/** Finds visible objects of hierarchy node using occlusion queries
-	@param node the current scene node
-	@param objs the visible objects
-	@param includeChildren if the children of current scene nodes should also be traversed
-	*/
-	/*virtual bool FindVisibleObjects(GtpVisibility::HierarchyNode *node,	
-									InfoContainer<GtpVisibility::MeshInfo> *visibleGeometry, 
-									bool includeChildren = false) = 0;*/
+
 protected:
 	/** Renders geometry 
@@ -126,9 +119,4 @@
 	*/
 	void DeleteQueries();
-	
-	/** Renders bounding box of specified node.
-		@param box the bounding box of the scene node to be rendered 
-	*/
-	void RenderBoundingBox(AxisAlignedBox *box);
 
 	/** Returns pointer to current renderable bounding box geometry.
@@ -138,10 +126,11 @@
 	/** A pass that prepares an occlusion query.
 		@remark disables depth write, colour write, lighting,
-		vertex and fragment program.
-	*/
+		vertex and fragment program.*/
 	//void SetOcclusionPass();
 
-	/** Returns pointer to current renderable half bounding box geometry.*/
-	//SolidHalfBoundingBox *GetSolidHalfBoundingBox();
+	/** Renders given bounding box.
+		@param box the bounding box of the scene node to be rendered 
+	*/
+	void RenderBoundingBox(AxisAlignedBox *box);
 
 	/** Renderable of an aabb.
Index: trunk/VUT/Ogre/include/OgreSolidBoundingBox.h
===================================================================
--- trunk/VUT/Ogre/include/OgreSolidBoundingBox.h	(revision 117)
+++ trunk/VUT/Ogre/include/OgreSolidBoundingBox.h	(revision 156)
@@ -9,5 +9,5 @@
 	no colors, and no lighting.
 */
-class SolidBoundingBox : public SimpleRenderable
+class __declspec(dllexport) SolidBoundingBox : public SimpleRenderable
 {
 public:
@@ -30,23 +30,24 @@
 
 protected:
-		/** 
-			Sets the material used for occlusion queries. 
-			@remark the material is called "Visibility/QueryMaterial" and
-			uses no lighting, no depth write, and no colours
-		*/
-		void SetOcclusionQueryMaterial();
-        /** Override this method to prevent parent transforms (rotation,translation,scale)
-        */
-		void getWorldTransforms( Matrix4* xform ) const;
-        /** @copydoc Renderable::getWorldOrientation */
-        const Quaternion& getWorldOrientation(void) const;
-        /** @copydoc Renderable::getWorldPosition */
-        const Vector3& getWorldPosition(void) const;
+	/** 
+		Sets the material used for occlusion queries. 
+		@remark the material is called "Visibility/QueryMaterial" and
+		uses no lighting, no depth write, and no colours
+	*/
+	void SetOcclusionQueryMaterial();
+
+	/** Override this method to prevent parent transforms (rotation,translation,scale)
+    */
+	void getWorldTransforms( Matrix4* xform ) const;
+    /** @copydoc Renderable::getWorldOrientation */
+    const Quaternion& getWorldOrientation(void) const;
+    /** @copydoc Renderable::getWorldPosition */
+    const Vector3& getWorldPosition(void) const;
 		
-        /** Builds the wireframe line list.
-        */
-		void setupBoundingBoxVertices(const AxisAlignedBox& aab);
+    /** Builds the wireframe line list.
+    */
+	void setupBoundingBoxVertices(const AxisAlignedBox& aab);
 
-        Real mRadius;
+    Real mRadius;
 };
 
Index: trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 155)
+++ trunk/VUT/Ogre/src/OgreVisibilityTerrainSceneManager.cpp	(revision 156)
@@ -9,4 +9,5 @@
 #include <OgreEntity.h>
 #include <OgreSubEntity.h>
+
 
 namespace Ogre {
@@ -31,5 +32,6 @@
 mSkipTransparents(false),
 mSavedShadowTechnique(SHADOWTYPE_NONE),
-mRenderOnlyBoundingBoxes(false)
+mRenderHierarchyNodes(false)
+//mRenderHierarchyNodes(true)
 {
 	mHierarchyInterface = new OctreeHierarchyInterface(this, mDestRenderSystem);
@@ -147,5 +149,5 @@
 	//Pass *usedPass = ((mRenderDepthPass && !pass->hasVertexProgram()) ? mDepthPass : pass); 
 	
-	// set depth fill pass only if depth write enabled
+	// set depth fill pass if we currently do not make an aabb occlusion query
 	Pass *usedPass = (mRenderDepthPass && !mHierarchyInterface->IsBoundingBoxQuery() ? 
 					  mDepthPass : pass);
@@ -208,4 +210,10 @@
 void VisibilityTerrainSceneManager::_findVisibleObjects(Camera* cam, bool onlyShadowCasters)
 {
+	// clear list of solid boxes (used for item buffer hierarchy node rendering)
+	/*for (int i=0; i<(int)mSolidBoxes.size(); ++i)
+		delete mSolidBoxes[i];
+
+	mSolidBoxes.clear();*/
+
 	//-- show visible scene nodes and octree bounding boxes from last frame
 	if (mShowVisualization)
@@ -213,6 +221,28 @@
 		PrepareVisualization(cam);
 	}
-	else if (mRenderOnlyBoundingBoxes)
-	{
+	else if (mRenderHierarchyNodes)
+	{
+		AxisAlignedBox aab;
+		
+
+		// get rendered hierarchy nodes from last frame
+		GtpVisibility::HierarchyNodeList *nodeList = mHierarchyInterface->GetRenderedNodes();
+		GtpVisibility::HierarchyNodeList::iterator nodeIt, nodeIt_end = nodeList->end();
+
+		for (nodeIt = nodeList->begin(); nodeIt != nodeIt_end; ++nodeIt)		
+		{
+			SolidBoundingBox *solidBox = new SolidBoundingBox();
+			solidBox->SetupBoundingBox(aab);
+
+			Octree *octree = static_cast<Octree *>(*nodeIt);
+			solidBox->setId(octree->getId());
+			mSolidBoxes.push_back(solidBox);
+
+			aab = octree->_getWorldAABB();
+			std::stringstream d; d << "bounding box with id: " << octree->getId() << ", " << aab << "\n";
+			LogManager::getSingleton().logMessage(d.str());
+
+			getRenderQueue()->addRenderable(solidBox);	
+		}
 	}
 	else 
@@ -233,7 +263,7 @@
 	//TerrainSceneManager::_findVisibleObjects(cam, onlyShadowCasters);
 	
+	// -- delete lists stored for visualization
 	mVisible.clear();
 	mBoxes.clear();
-
 }
 //-----------------------------------------------------------------------
@@ -246,11 +276,12 @@
     // e.g., visualization, shadow pass
 
-	if (mShowVisualization || mRenderOnlyBoundingBoxes ||
-	   (mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE && mIlluminationStage == IRS_RENDER_TO_TEXTURE))
+	if (mShowVisualization || mRenderHierarchyNodes ||
+	   (mShadowTechnique == SHADOWTYPE_TEXTURE_MODULATIVE && 
+	    mIlluminationStage == IRS_RENDER_TO_TEXTURE))
 	{	
 		IlluminationRenderStage savedStage = mIlluminationStage; 
 	
-		if (mShowVisualization) 
-			// disable illumination stage because we want no shadows in visualization
+		if (mShowVisualization || mRenderHierarchyNodes) 
+			// disable illumination stage because we want no shadows
 			mIlluminationStage = IRS_NONE;
 
@@ -416,4 +447,8 @@
 		mUseItemBuffer = (*static_cast<const bool *>(val));
 		return true;
+	}
+	if (key == "RenderHierarchyNodes")
+	{
+		mRenderHierarchyNodes = (*static_cast<const bool *>(val));
 	}
 	return VisibilityOptionsManager(mVisibilityManager, mHierarchyInterface).
@@ -503,5 +538,6 @@
 	}
 
-	//--- item buffer
+	// --- item buffer
+
     // Iterate through priorities
     RenderQueueGroup::PriorityMapIterator groupIt = pGroup->getIterator();
@@ -604,11 +640,5 @@
 
 	Pass *usedPass = setPass(mItemBufferPass); 
-	
-	/*if (rend->getId() > 0)
-	{
-		std::stringstream d; d << "item buffer id: " << rend->getId() << ", col: " << col[0] <<
-			" " << col[1] << " " << col[2] << " " << col[3];
-		LogManager::getSingleton().logMessage(d.str());
-	}*/
+
 
 	// Render a single object, this will set up auto params if required
Index: trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 155)
+++ trunk/VUT/Ogre/src/OgreVisibilityOctreeSceneManager.cpp	(revision 156)
@@ -41,5 +41,4 @@
 	if (depthMat.isNull())
     {
-		// Init
 		depthMat = MaterialManager::getSingleton().create(
                 "Visibility/DepthPass",
Index: trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp	(revision 154)
+++ trunk/VUT/Ogre/src/OgrePlatformQueryManager.cpp	(revision 156)
@@ -57,9 +57,11 @@
 		// prevent from initialising geometry / node array again
 		if (i > 0)
+		{
 			mWasInitialised = true;
+		}
 
 		ComputeCameraVisibility(*cam, visibleNodes, visibleGeometry, relativeVisibility);
 		
-		//mViewport->getTarget()->update(); for(int j=0; j<10000000; j++)	printf("wait");
+		mViewport->getTarget()->update(); for(int j=0; j<10000000; j++)	printf("wait");
 
 		// permute directions
Index: trunk/VUT/Ogre/src/OgreItemBufferQueryManager.cpp
===================================================================
--- trunk/VUT/Ogre/src/OgreItemBufferQueryManager.cpp	(revision 155)
+++ trunk/VUT/Ogre/src/OgreItemBufferQueryManager.cpp	(revision 156)
@@ -32,5 +32,5 @@
 	SceneManager *sm = pfHierarchyInterface->GetSceneManager();
 
-	// ---- Render scene as item buffer (i.e., objects with their id as color codes)
+	// ---- Render scene with item buffer (i.e., objects with their id as color codes)
 
 	// const_cast allowed because camera is not changed in renderScene
@@ -52,15 +52,4 @@
 	// --- render item buffer
 	pfHierarchyInterface->GetSceneManager()->_renderScene(pCam, mViewport, false);
-	
-
-
-	// reset old overlay status
-	mViewport->setOverlaysEnabled(overlayEnabled);
-	
-	mViewport->setBackgroundColour(bg);
-
-	// don't need item buffer anymore
-	useItemBuffer = false;
-	sm->setOption("UseItemBuffer", &useItemBuffer);
 
 
@@ -88,6 +77,48 @@
 	}
 
+	delete [] buf;
+
+	// ---- render visible nodes and collect node visibility
+	bool renderBoxes = true;
+	sm->setOption("RenderHierarchyNodes", &renderBoxes);
+
+
+	// --- render item buffer for visible nodes only
+	pfHierarchyInterface->GetSceneManager()->_renderScene(pCam, mViewport, false);
+
+
+	// get frame buffer for node visibility
+	buf = mViewport->getTarget()->getBufferContents(dimx, dimy);
+
+	
+	// loop through frame buffer & collect visible pixels
+	for (int idx = 0; idx < dimy * dimx * 3; idx += 3)
+	{
+		// -- 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
+		if ((id > 0) && (id < (int)visibleNodes->size()))
+		{
+			((*visibleNodes)[id]).AddVisibility(1, 1);
+		}
+	}
+
+	// don't need item buffer anymore
+	useItemBuffer = false;
+	//sm->setOption("UseItemBuffer", &useItemBuffer);
+
+	renderBoxes = false;
+	//sm->setOption("RenderHierarchyNodes", &renderBoxes);
+
 	// reset initialised - flag
 	mWasInitialised = false;
+
+	// reset old overlay status
+	mViewport->setOverlaysEnabled(overlayEnabled);
+	
+	mViewport->setBackgroundColour(bg);
 
 	delete [] buf;
@@ -99,5 +130,4 @@
 	if (mWasInitialised)
 		return;
-
 
 	mWasInitialised = true;
@@ -122,6 +152,9 @@
 	GtpVisibility::HierarchyNode *node = NULL;
 	
+	int i = 1;
+
 	while (node = mHierarchyInterface->GetNextNode())
 	{
+		mHierarchyInterface->SetNodeId(node, i++);
 		visibleNodes->push_back(GtpVisibility::NodeInfo(node, 0, 0));
 	}
