Index: trunk/VUT/work/ogre_changes/OgreMain/include/OgreRenderQueueSortingGrouping.h
===================================================================
--- trunk/VUT/work/ogre_changes/OgreMain/include/OgreRenderQueueSortingGrouping.h	(revision 115)
+++ trunk/VUT/work/ogre_changes/OgreMain/include/OgreRenderQueueSortingGrouping.h	(revision 139)
@@ -197,7 +197,24 @@
         void clear(void);
 #ifdef GTP_VISIBILITY_MODIFIED_OGRE
+	
 		/** Clears solids in this group of renderables. Leaves transparents in group.
         */
         void clearSolids(void);
+
+		enum {SOLID_PASSES = 1,
+			  SOLID_PASSES_DECAL = 2,
+			  SOLID_PASSES_DIFFUSE_SPECULAR = 4,
+			  SOLID_PASSES_NOSHADOW = 8,
+			  TRANSPARENT_PASSES = 16};
+
+		/** Clears passes determined by parameter.
+			@param passes can be one or several (combined by |) of:
+			SOLID_PASSES 
+			SOLID_PASSES_DECAL 
+			SOLID_PASSES_DIFFUSE_SPECULAR
+			SOLID_PASSES_NOSHADOW
+			TRANSPARENT_PASSES
+		*/
+		void clear(const int leavePassesInQueue);
 #endif // GTP_VISIBILITY_MODIFIED_OGRE		
         /** Sets whether or not the queue will split passes by their lighting type,
@@ -306,4 +323,14 @@
         }
 #ifdef GTP_VISIBILITY_MODIFIED_OGRE	
+		void clear(int passes)
+        {
+            PriorityMap::iterator i, iend;
+            iend = mPriorityGroups.end();
+            for (i = mPriorityGroups.begin(); i != iend; ++i)
+            {
+                i->second->clear(passes);
+            }
+
+        }
 		/** Clears only solid renderables. Leaves transparents in queue. */
 		void clearSolids(void)
Index: trunk/VUT/work/ogre_changes/OgreMain/include/OgreSceneManager.h
===================================================================
--- trunk/VUT/work/ogre_changes/OgreMain/include/OgreSceneManager.h	(revision 131)
+++ trunk/VUT/work/ogre_changes/OgreMain/include/OgreSceneManager.h	(revision 139)
@@ -128,11 +128,12 @@
 			@param node scene node to be rendered
 			@param cam current camera
-			@param leaveTransparentsInQueue if only solid objects should be rendered
-		*/
-		void _renderSceneNode(Camera *cam, SceneNode *node, bool leaveTransparentsInQueue = false);
+			@param leavePassesInQueue list of passes which are left in queue
+		*/
+		void _renderSceneNode(Camera *cam, SceneNode *node, const int leavePassesInQueue = 0);
 		/** deletes all processed queues
-			@remark clears render queues after rendering scene node
-		*/
-		void _deleteRenderedQueueGroups(bool leaveTransparentsInQueue);
+			@param leavePassesInQueue pass list which is not deleted from queue
+			@remark used to clear render queues before rendering scene node
+		*/
+		void _deleteRenderedQueueGroups(int leavePassesInQueue = 0);
 		/** Internal method used by _renderVisibleObjects to deal with renderables
             which override the camera's own view / projection materices. 
@@ -156,5 +157,5 @@
 		/** Renders an Ogre MovableObject.
 		*/
-		void _renderMovableObject(MovableObject *mov, const bool leaveTransparentsInQueue);
+		void _renderMovableObject(MovableObject *mov, const int leavePassesInQueue);
 
 #endif // GTP_VISIBILITY_MODIFIED_OGRE
Index: trunk/VUT/work/ogre_changes/OgreMain/src/OgreRenderQueueSortingGrouping.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/OgreMain/src/OgreRenderQueueSortingGrouping.cpp	(revision 115)
+++ trunk/VUT/work/ogre_changes/OgreMain/src/OgreRenderQueueSortingGrouping.cpp	(revision 139)
@@ -280,5 +280,5 @@
     void RenderPriorityGroup::clearSolids(void)
     {
-      /*  SolidRenderablePassMap::iterator i, iend;
+        /*SolidRenderablePassMap::iterator i, iend;
         // Delete queue groups which are using passes which are to be
         // deleted, we won't need these any more and they clutter up 
@@ -313,4 +313,30 @@
         clearSolidPassMap(mSolidPassesNoShadow);
     }
+	//-----------------------------------------------------------------------
+    void RenderPriorityGroup::clear(const int leavePassesInQueue)
+    {
+		SolidRenderablePassMap::iterator i, iend;
+       
+		// -- standard method if no passes are left in queue
+		if (leavePassesInQueue == 0)
+		{
+			clear();
+			return;
+		}
+
+        // We do not clear the unchanged solid pass maps, only the contents of each list
+        // This is because we assume passes are reused a lot and it saves resorting
+		if (!(leavePassesInQueue & SOLID_PASSES))
+			clearSolidPassMap(mSolidPasses);
+		if (!(leavePassesInQueue & SOLID_PASSES_DECAL))
+			clearSolidPassMap(mSolidPassesDecal);
+		if (!(leavePassesInQueue & SOLID_PASSES_DIFFUSE_SPECULAR))
+			clearSolidPassMap(mSolidPassesDiffuseSpecular);
+		if (!(leavePassesInQueue & SOLID_PASSES_NOSHADOW))
+			clearSolidPassMap(mSolidPassesNoShadow);
+
+        if (!(leavePassesInQueue & TRANSPARENT_PASSES))
+			mTransparentPasses.clear();
+    }
 #endif // GTP_VISIBILITY_MODIFIED_OGRE		
 //-----------------------------------------------------------------------
Index: trunk/VUT/work/ogre_changes/OgreMain/src/OgreSceneManager.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/OgreMain/src/OgreSceneManager.cpp	(revision 135)
+++ trunk/VUT/work/ogre_changes/OgreMain/src/OgreSceneManager.cpp	(revision 139)
@@ -1909,6 +1909,5 @@
 
 	itransend = objs.end();
-	for (itrans = objs.begin(); 
-		itrans != itransend; ++itrans)
+	for (itrans = objs.begin(); itrans != itransend; ++itrans)
 	{
 		Renderable *r = itrans->renderable;
@@ -4197,8 +4196,8 @@
 #ifdef GTP_VISIBILITY_MODIFIED_OGRE
 //-----------------------------------------------------------------------
-void SceneManager::_renderSceneNode(Camera *cam, SceneNode *node, bool leaveTransparentsInQueue)
+void SceneManager::_renderSceneNode(Camera *cam, SceneNode *node, const int leavePassesInQueue)
 {
 	// delete previously rendered objects from renderqueue
-	_deleteRenderedQueueGroups(leaveTransparentsInQueue);
+	_deleteRenderedQueueGroups(leavePassesInQueue);
 
 	node->_findVisibleObjects(cam, getRenderQueue(), false, mDisplayNodes, false);
@@ -4206,5 +4205,5 @@
 }
 //-----------------------------------------------------------------------
-void SceneManager::_deleteRenderedQueueGroups(bool leaveTransparentsInQueue)
+void SceneManager::_deleteRenderedQueueGroups(const int leavePassesInQueue)
 {
 	RenderQueue::QueueGroupIterator queueIt = getRenderQueue()->_getQueueGroupIterator();
@@ -4218,12 +4217,8 @@
 		if (isRenderQueueToBeProcessed(qId))
 		{
-			if (leaveTransparentsInQueue)
-			{
-				pGroup->clearSolids();
-			}
-			else
-			{
-				pGroup->clear();
-			}
+			/*if (leaveTransparentsInQueue)
+			{	pGroup->clearSolids();	}	else
+			{	pGroup->clear(); }*/
+			pGroup->clear(leavePassesInQueue);
 		}
 	}
@@ -4245,8 +4240,8 @@
 }*/
 //-----------------------------------------------------------------------
-void SceneManager::_renderMovableObject(MovableObject *mov, const bool leaveTransparentsInQueue)
+void SceneManager::_renderMovableObject(MovableObject *mov, const int leavePassesInQueue)
 {
 	// delete previously rendered objects from renderqueue
-	_deleteRenderedQueueGroups(leaveTransparentsInQueue);
+	_deleteRenderedQueueGroups(leavePassesInQueue);
 
 	mov->_updateRenderQueue(getRenderQueue());
Index: trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/include/OgreOctreeSceneManager.h
===================================================================
--- trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/include/OgreOctreeSceneManager.h	(revision 115)
+++ trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/include/OgreOctreeSceneManager.h	(revision 139)
@@ -112,7 +112,8 @@
 		@param octree the octant to be rendered (without children)
 		@param onlyShadowCasters if only shadow casters are rendered
-		@param leaveTransparentsInQueue if transparemt should be left in queue for later processing
+		@param passes if passes should be left in queue for later processing
 	*/
-	void _renderOctant(Camera* cam, Octree *octree, bool onlyShadowCasters, bool leaveTransparentsInQueue = false);
+	void _renderOctant(Camera* cam, Octree *octree, bool onlyShadowCasters, 
+		const int leavePassesInQueue = 0);
 
 	/** Returns stored list of boxes */
Index: trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctree.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctree.cpp	(revision 135)
+++ trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctree.cpp	(revision 139)
@@ -36,4 +36,5 @@
 #include <OgreOctree.h>
 #include <OgreOctreeNode.h>
+#include <OgreLogManager.h>
 
 namespace Ogre
@@ -109,4 +110,5 @@
 		mDepth = 0;
 
+	_updateBounds();
 #endif //GTP_VISIBILITY_MODIFIED_OGRE
     mNumNodes = 0;
@@ -175,4 +177,5 @@
 	//mWireBoundingBox->setupBoundingBox(mBox);
 	mWireBoundingBox->setupBoundingBox(mWorldAABB);
+
     return mWireBoundingBox;
 }
@@ -229,5 +232,5 @@
 	// Reset bounds first
     mWorldAABB.setNull();
-
+  
     // Update bounds from own attached objects
 	NodeList::iterator it, it_end;
@@ -247,5 +250,5 @@
             for (int k = 0; k < 2; ++k)
             {
-                if (mChildren[i][j][k] != 0)
+                if (mChildren[i][j][k])
 				{
 					mWorldAABB.merge(mChildren[i][j][k]->_getWorldAABB());
@@ -254,4 +257,12 @@
         }
 	}
+	// HACK: clamp to bounds
+	AxisAlignedBox box;
+	_getCullBounds(&box);
+	mWorldAABB = mWorldAABB.intersection(box); 
+	
+	//std::stringstream d; d << "updating box: " << mWorldAABB << ", depth: " << mDepth << "null: " << mBox.isNull();
+	//LogManager::getSingleton().logMessage(d.str());
+
 	// recursively update parent bounds
 	if (mParent)
Index: trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreTerrainPageSource.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreTerrainPageSource.cpp	(revision 139)
+++ trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreTerrainPageSource.cpp	(revision 139)
@@ -0,0 +1,152 @@
+/*
+-----------------------------------------------------------------------------
+This source file is part of OGRE
+(Object-oriented Graphics Rendering Engine)
+For the latest info, see http://www.ogre3d.org/
+
+Copyright (c) 2000-2005 The OGRE Team
+Also see acknowledgements in Readme.html
+
+This program is free software; you can redistribute it and/or modify it under
+the terms of the GNU Lesser General Public License as published by the Free Software
+Foundation; either version 2 of the License, or (at your option) any later
+version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along with
+this program; if not, write to the Free Software Foundation, Inc., 59 Temple
+Place - Suite 330, Boston, MA 02111-1307, USA, or go to
+http://www.gnu.org/copyleft/lesser.txt.
+-----------------------------------------------------------------------------
+*/
+#include "OgreTerrainPageSource.h"
+#include "OgreTerrainPage.h"
+#include "OgreTerrainRenderable.h"
+#include "OgreSceneNode.h"
+#include "OgreTerrainSceneManager.h"
+#include "OgreLogManager.h"
+
+namespace Ogre {
+
+    //-----------------------------------------------------------------------
+    template<> TerrainPageSourceListenerManager* Singleton<TerrainPageSourceListenerManager>::ms_Singleton = 0;
+    TerrainPageSourceListenerManager* TerrainPageSourceListenerManager::getSingletonPtr(void)
+    {
+        return ms_Singleton;
+    }
+    TerrainPageSourceListenerManager& TerrainPageSourceListenerManager::getSingleton(void)
+    {  
+        assert( ms_Singleton );  return ( *ms_Singleton );  
+    }
+    //-------------------------------------------------------------------------
+	void TerrainPageSourceListenerManager::addListener(TerrainPageSourceListener* pl)
+	{
+        mPageSourceListeners.push_back(pl);
+	}
+    //-------------------------------------------------------------------------
+	void TerrainPageSourceListenerManager::removeListener(TerrainPageSourceListener* pl)
+	{
+        PageSourceListenerList::iterator i, iend;
+        iend = mPageSourceListeners.end();
+        for(i = mPageSourceListeners.begin(); i != iend; ++i)
+        {
+            if (*i == pl)
+            {
+                mPageSourceListeners.erase(i);
+                break;
+            }
+        }
+	}
+    //-------------------------------------------------------------------------
+	void TerrainPageSourceListenerManager::firePageConstructed(size_t pagex, size_t pagez, Real* heightData)
+	{
+        PageSourceListenerList::iterator i, iend;
+        iend = mPageSourceListeners.end();
+        for(i = mPageSourceListeners.begin(); i != iend; ++i)
+        {
+            (*i)->pageConstructed(pagex, pagez, heightData);
+        }
+	}
+	//-------------------------------------------------------------------------
+	TerrainPageSource::TerrainPageSource() : mSceneManager(0), mAsyncLoading(false) {
+	}
+	//-------------------------------------------------------------------------
+	TerrainPage* TerrainPageSource::buildPage(Real* heightData, const MaterialPtr& pMaterial)
+    {
+        String name;
+
+        // Create a Terrain Page
+        TerrainPage* page = new TerrainPage((mPageSize-1) / (mTileSize-1));
+        // Create a node for all tiles to be attached to
+        // Note we sequentially name since page can be attached at different points
+        // so page x/z is not appropriate
+        static size_t pageIndex = 0;
+		StringUtil::StrStreamType page_str;
+		page_str << pageIndex;
+        name = "page[";
+        name += page_str.str() + "]";
+        page->pageSceneNode = mSceneManager->createSceneNode(name);
+        
+        size_t q = 0;
+        for ( size_t j = 0; j < mPageSize - 1; j += ( mTileSize - 1 ) )
+        {
+            size_t p = 0;
+
+            for ( size_t i = 0; i < mPageSize - 1; i += ( mTileSize - 1 ) )
+            {
+				StringUtil::StrStreamType new_name_str;
+				
+                // Create scene node for the tile and the TerrainRenderable
+                new_name_str << "tile[" << pageIndex << "][" << (int)p << "," << (int)q << "]";
+				name = new_name_str.str();
+
+                SceneNode *c = page->pageSceneNode->createChildSceneNode( name );
+				
+                TerrainRenderable *tile = new TerrainRenderable(name);
+				// set queue
+				tile->setRenderQueueGroup(mSceneManager->getWorldGeometryRenderQueue());
+                // Initialise the tile
+				tile->setMaterial(pMaterial);
+				
+                tile->initialise(i, j, heightData);
+                // Attach it to the page
+                page->tiles[ p ][ q ] = tile;
+                // Attach it to the node
+                c ->attachObject( tile );
+                p++;
+			
+            }
+
+            q++;
+
+        }
+
+        pageIndex++;
+
+        // calculate neighbours for page
+        page->linkNeighbours();
+
+        return page;
+    }
+    //-------------------------------------------------------------------------
+    void TerrainPageSource::firePageConstructed(size_t pagex, size_t pagez, Real* heightData)
+    {
+		TerrainPageSourceListenerManager::getSingleton().firePageConstructed(pagex, pagez, heightData);
+    }
+    //-------------------------------------------------------------------------
+    void TerrainPageSource::addListener(TerrainPageSourceListener* pl)
+    {
+		TerrainPageSourceListenerManager::getSingleton().addListener(pl);
+    }
+    //-------------------------------------------------------------------------
+    void TerrainPageSource::removeListener(TerrainPageSourceListener* pl)
+    {
+		TerrainPageSourceListenerManager::getSingleton().removeListener(pl);
+
+    }
+    //-------------------------------------------------------------------------
+
+}
Index: trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreTerrainRenderable.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreTerrainRenderable.cpp	(revision 132)
+++ trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreTerrainRenderable.cpp	(revision 139)
@@ -388,14 +388,11 @@
             if (mLastNextLevel != nextLevel)
             {
-				//LogManager::getSingleton().logMessage("vbuffer binding");
-                if (nextLevel > 0)
-                {
-					//LogManager::getSingleton().logMessage("level > 0");
-                    mTerrain->vertexBufferBinding->setBinding(DELTA_BINDING, 
+				if (nextLevel > 0)
+                {
+					mTerrain->vertexBufferBinding->setBinding(DELTA_BINDING, 
                         mDeltaBuffers[nextLevel - 1]);
                 }
                 else
-                {
-					//LogManager::getSingleton().logMessage("level <= 0 (dummy b)");
+                {				
                     // bind dummy (incase bindings checked)
                     mTerrain->vertexBufferBinding->setBinding(DELTA_BINDING, 
@@ -406,11 +403,4 @@
 
         }
-		//else LogManager::getSingleton().logMessage("no lod morph");
-		/*std::stringstream d; 
-		d << "terrain: " << this->getName() << ", notify camera: " << cam->getName() << ", pos: " << StringConverter::toString(cam->getDerivedPosition()) << 
-			", distance: " << StringConverter::toString(L) << ", lod level: " << mLastNextLevel
-			<< ", render level: " << getRenderLevel() << ", morph factor: " << mLODMorphFactor;
-		LogManager::getSingleton().logMessage(d.str());*/
-
     }
     //-----------------------------------------------------------------------
Index: trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctreeSceneManager.cpp
===================================================================
--- trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctreeSceneManager.cpp	(revision 135)
+++ trunk/VUT/work/ogre_changes/Plugins/OctreeSceneManager/src/OgreOctreeSceneManager.cpp	(revision 139)
@@ -1176,8 +1176,8 @@
 //-----------------------------------------------------------------------
 void OctreeSceneManager::_renderOctant(Camera *cam, Octree *octant, bool onlyShadowCasters, 
-									   bool leaveTransparentsInQueue)//, bool useZPassQueue)
+									   const int leavePassesInQueue)
 {
 	// delete previously rendered objects from the render queue
-	_deleteRenderedQueueGroups(leaveTransparentsInQueue);
+	_deleteRenderedQueueGroups(leavePassesInQueue);
 
 	//Add stuff to be rendered;
