//-----------------------------------------------------------------------
	void PagingLandScapeRenderableManager::processTileUnload()
	{
		if (mTilesLoadRenderableQueue.empty())
			return;

		PagingLandScapeTile *tile;
		for (PagingLandScapeQueue<PagingLandScapeTile>::MsgQueType::iterator itq = mTilesLoadRenderableQueue.begin(); 
			itq != mTilesLoadRenderableQueue.end();)
		{
			tile = *itq;
			assert (tile != 0);

			assert (tile->isLoading());
			assert (tile->getRenderable ());
			assert (tile->getRenderable ()->mQueued);

			assert (tile->isLoaded ());       
			assert (tile->getSceneNode());
			assert (!tile->getRenderable ()->isLoaded ()); 

			if (!tile->getRenderable ()->isInUse ())
            {
				tile->setLoading (false);
				tile->getRenderable ()->mQueued = false;
                tile->unload (); 
				itq = mTilesLoadRenderableQueue.erase (itq);
			}
			else
			{
				++itq;
			}
		}
	}
        //-----------------------------------------------------------------------
		// Update using page local tile coordinates.
		void UpdateTile( int tileX, int tileZ )
		{
			PagingLandScapeTile* tile = page_->getTile( tileX, tileZ );
			if( tile != tile_ )
			{
				tile_ = tile;
				bool tileLoaded = tile_ && tile_->isLoaded() && tile_->getRenderable();
				renderLevel_.SetRenderLevel( (tileLoaded) ? tile_->getRenderable()->getRenderLevel() : 0 );
			}
        }
	//-----------------------------------------------------------------------
	PagingLandScapeTile* PagingLandScapePage::getTile(const Vector3& pos) const
	{
		const size_t x = static_cast<unsigned int>(pos.x / mPageMgr.getOptions()->scale.x / (mPageMgr.getOptions()->TileSize));
		const size_t z = static_cast<unsigned int>(pos.z / mPageMgr.getOptions()->scale.z / (mPageMgr.getOptions()->TileSize));

		PagingLandScapeTile* tile = getTile(x, z);
		if (tile && tile->isLoaded()) {
			return tile;
		} else {
			return 0;
		}
	}
    //-----------------------------------------------------------------------
    bool PagingLandScapeRenderableManager::executeRenderableLoading(void)
    {
        if (mTilesLoadQueue.empty())
            return true;	
        else
        { 
            const uint k = PagingLandScapeOptions::getSingleton ().num_renderables_loading;
            for (uint i = 0; i < k; i++ )
            {
                PagingLandScapeTile* tile = mTilesLoadQueue.pop ();

                // no more in queues.
                if (tile == 0)
                    return true;

                assert (tile->isLoaded ());           
                           
                PagingLandScapeRenderable* rend = tile->getRenderable();

                assert (!rend->isLoaded ());      
                SceneNode * tileSceneNode = tile->getTileNode ();

                assert (rend != 0 && tileSceneNode != 0);

                tileSceneNode->attachObject( rend );
                // renderables need to be attached (BBox compute)
                if (rend->load())
                {
                    tile->_linkRenderableNeighbor ();
                }
                else
                {
                    // renderable have been unloaded since.
                    tile->unload ();
                }
                tileSceneNode->needUpdate();
            }
        }
        return false;
    }
    //-----------------------------------------------------------------------
    bool PagingLandScapeData2D::setHeight( const uint x, const uint z,
                                           const uint Pos, const Real h )
    {
        if (mHeightData[Pos] != h)
        {
	        mHeightData[ Pos ] = h;    

		    uint tileposx = x;
		    uint tileposz = z;
		// Make position local to tiles.
		// and return a Tile.
	        PagingLandScapeTile *t = 
		    PagingLandScapePageManager::getSingleton().getTilePage(tileposx, tileposz, 
									mPageX, mPageZ);
	        if (t && t->isLoaded ())
	        { 
		        // tells what tile portion needs to be updated.
	            PagingLandScapeRenderable * const r = t->getRenderable ();                
		        r->adjustDeformationRectangle (tileposx, tileposz); 
        		
		        const uint tSize = PagingLandScapeOptions::getSingleton ().TileSize - 1;
		        const uint NumTiles = PagingLandScapeOptions::getSingleton ().NumTiles;

                const PagingLandScapeTileInfo * const info =  t->getInfo();
                const uint tX = info->tileX;
                const uint tZ = info->tileZ;

                // If we're on a page edge, we must duplicate the change on the 
                // neighbour tile (if it has one...)
                // could be a direct neighbour or a diagonal one (in a corner.)
                const bool left  = (tileposx == 0     && tX != 0);
                const bool right = (tileposx == tSize && tX != NumTiles - 1);
                const bool down  = (tileposz == 0     && tZ != 0);
                const bool up    = (tileposz == tSize && tZ != NumTiles - 1);


		        if (left)
		        { 
			        PagingLandScapeRenderable * const rn = r->_getNeighbor(WEST);
                    if (rn)
                    {
                        if (down)
                        {
                            PagingLandScapeRenderable * const rnUp = rn->_getNeighbor(NORTH);
                            if (rnUp && rnUp->isLoaded ())
	                        { 
                                rnUp->adjustDeformationRectangle (tSize, tSize); 
                            }
                        }
                        else  if (up)
                        {
                            PagingLandScapeRenderable * const rnDown = rn->_getNeighbor(SOUTH);
                            if (rnDown && rnDown->isLoaded ())
	                        { 
                                rnDown->adjustDeformationRectangle (tSize, 0); 
                            }
                        }
			            if (rn->isLoaded ())
			            { 
			                rn->adjustDeformationRectangle (tSize, tileposz); 
			            }
                    }
		        }
                else  if (right)
                {
                    PagingLandScapeRenderable * const rn = r->_getNeighbor(EAST);
                    if (rn)
	                { 
                        if (down)
                        {
                            PagingLandScapeRenderable * const rnUp = rn->_getNeighbor(NORTH);
                            if (rnUp && rnUp->isLoaded ())
	                        { 
                                rnUp->adjustDeformationRectangle (0, tSize); 
                            }
                        }
                        else  if (up)
                        {
                            PagingLandScapeRenderable * const rnDown = rn->_getNeighbor(SOUTH);
                            if (rnDown && rnDown->isLoaded ())
	                        { 
                                rnDown->adjustDeformationRectangle (0, 0); 
                            }
                        }
                        if (rn)
	                    { 
                            rn->adjustDeformationRectangle (0, tileposz); 
                        }
                    }
                }
                if (down)
                {
                    PagingLandScapeRenderable * const rn = r->_getNeighbor(NORTH);
                    if (rn && rn->isLoaded ())
	                { 
                        rn->adjustDeformationRectangle (tileposx, tSize); 
                    }
                }
                else  if (up)
                {
                    PagingLandScapeRenderable * const rn = r->_getNeighbor(SOUTH);
                    if (rn && rn->isLoaded ())
	                { 
                        rn->adjustDeformationRectangle (tileposx, 0); 
                    }
                }
	        }
    	
	        adjustDeformationRectangle (x, z);
            return true;
        }
       return false;
    }