⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ogrepaginglandscapepage.cpp

📁 使用stl技术,(还没看,是听说的)
💻 CPP
字号:
/***************************************************************************
	OgrePagingLandScapePage.cpp  -  description
	-------------------
	begin                : Sat Mar 08 2003
	copyright            : (C) 2003 by Jose A. Milan
	email                : spoke2@supercable.es
***************************************************************************/

/***************************************************************************
*                                                                         *
*   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.                       *
*                                                                         *
***************************************************************************/

#include "OgreVector3.h"
#include "OgreColourValue.h"

#include "OgreMovableObject.h"
#include "OgreAxisAlignedBox.h"

#include "OgreCamera.h"

#include "OgreStringConverter.h"
#include "OgreSceneNode.h"
#include "OgreException.h"

#include "OgrePagingLandScapeOptions.h"
#include "OgrePagingLandScapeCamera.h"
#include "OgrePagingLandScapePageManager.h"
#include "OgrePagingLandScapeRenderableManager.h"
#include "OgrePagingLandScapeData2DManager.h"
#include "OgrePagingLandScapeTileInfo.h"
#include "OgrePagingLandScapeTile.h"
#include "OgrePagingLandScapeTileManager.h"
#include "OgrePagingLandScapeTextureManager.h"

#include "OgrePagingLandScapePage.h"

namespace Ogre
{

PagingLandScapePage::PagingLandScapePage( uint tableX, uint tableZ )
{
	mIsLoaded = false;
	mIsPreLoaded = false;

	mTableX = tableX;
	mTableZ = tableZ;

    mNumTiles = (uint) ((Real) PagingLandScapeOptions::getSingleton().PageSize / PagingLandScapeOptions::getSingleton().TileSize);
	mPageNode = 0;

	uint size = PagingLandScapeOptions::getSingleton().PageSize - 1;
	// Boundaries of this page
	// the middle page is at world coordinates 0,0
    Real factorX = size * PagingLandScapeOptions::getSingleton().scale.x;
    Real factorZ = size * PagingLandScapeOptions::getSingleton().scale.z;
	mIniX = ((Real)( (int)(mTableX + mTableX - PagingLandScapeOptions::getSingleton().world_width))) / 2.0 * factorX ;		
	mIniZ = ((Real)( (int)(mTableZ + mTableZ - PagingLandScapeOptions::getSingleton().world_height))) / 2.0 * factorZ ;		
	float EndX = mIniX + factorX;
	float EndZ = mIniZ + factorZ;
	float MaxHeight = PagingLandScapeData2DManager::getSingleton().getMaxHeight(tableX, tableZ);
    Real chgfactor = PagingLandScapeOptions::getSingleton().change_factor;
	mBoundsExt.setExtents( ( Real )( mIniX ), 
                            0, 
                           ( Real )( mIniZ ), 
						   ( Real )( EndX ), 
                           MaxHeight, 
                           ( Real )( EndZ ) );
	//Change Zone of this page
	mBoundsInt.setExtents( ( Real )( mIniX + chgfactor ), 
                            0, 
                           ( Real )( mIniZ + chgfactor ),
						   ( Real )( EndX - chgfactor ), 
                            MaxHeight, 
                            ( Real )( EndZ - chgfactor )	);
    for ( uint i = 0; i < 4; i++ )
    {
        mNeighbors[ i ] = 0;
    }
}

//-----------------------------------------------------------------------
PagingLandScapePage::~PagingLandScapePage()
{
	unload();	 
    postUnload ();
}

//-----------------------------------------------------------------------
void PagingLandScapePage::preload( )
{
	if ( mIsPreLoaded == true )
	{
		return;
	}

	PagingLandScapeData2DManager::getSingleton().load(mTableX, mTableZ);

	mIsPreLoaded = true;
}

//-----------------------------------------------------------------------
void PagingLandScapePage::load( SceneNode &PagingLandScapeRootNode )
{
	if ( mIsPreLoaded == false )
	{
		return;
	}

	if ( mIsLoaded == true )
	{
		return;
	}
	PagingLandScapeTextureManager::getSingleton().load(mTableX, mTableZ);
	//create a root landscape node.
	mPageNode = PagingLandScapeRootNode.createChildSceneNode( "PagingLandScapePage." + StringConverter::toString( mTableX ) + "." + StringConverter::toString( mTableZ ) );
	// Set node position
	mPageNode->setPosition( (Real)mIniX , 0.0, (Real)mIniZ );

	for (uint  i = 0; i < mNumTiles; i++ )
	{
		mTiles.push_back( PagingLandScapeTileRow() );

		for (uint  j = 0; j < mNumTiles; j++ )
		{
			mTiles[ i ].push_back( 0 );
		}
	}

	for (uint  j = 0; j < mNumTiles; j++ )
	{
		for (uint  i = 0; i < mNumTiles; i++ )
        {
            char name[ 24 ];
			sprintf( name, "page[%d,%d][%d,%d]", mTableX, mTableZ, i, j );

			PagingLandScapeTile *tile = PagingLandScapeTileManager::getSingleton().getTile();
			if ( tile != 0 )
			{
				mTiles[ i ][ j ] = tile;
				tile->init( *mPageNode, mTableX, mTableZ, i, j );
			}
			else
			{
				String err = "Error: Invalid Tile: Make sure the default TileManager size is set to WorldWidth * WorldHeight * 4. Try increasing MaxNumTiles in the configuration file.";
				Except( Exception::ERR_INVALIDPARAMS, err, "PagingLandScapePage::load" );
			}
		}
	}
    for (uint  j = 0; j < mNumTiles; j++ )
	{
		for (uint  i = 0; i < mNumTiles; i++ )
		{		
                if ( j != mNumTiles - 1 )
                {
                    mTiles[ i ][ j ]-> _setNeighbor( SOUTH, mTiles[ i ][ j + 1 ] );
                    mTiles[ i ][ j + 1 ]  -> _setNeighbor( NORTH, mTiles[ i ][ j ] );
                }
                if ( i != mNumTiles - 1 )
                {
                    mTiles[ i ][ j ] -> _setNeighbor( EAST, mTiles[ i + 1 ][ j ] );
                    mTiles[ i + 1 ][ j ] -> _setNeighbor( WEST, mTiles[ i ][ j ] );    
                }           
		}
	}
    if (mNeighbors[EAST] && mNeighbors[EAST]->isLoaded())
    {
        uint i = mNumTiles - 1;
        for (uint  j = 0; j < mNumTiles; j++ )
        {	
            PagingLandScapeTile *t_nextpage =  mNeighbors[EAST]->getTile  ( 0 , j );
            PagingLandScapeTile *t_currpage =  mTiles[ i ][ j ];
            assert(t_nextpage && t_currpage);
            t_currpage -> _setNeighbor( EAST, t_nextpage );
            t_nextpage -> _setNeighbor( WEST, t_currpage );
        }
    }
    if (mNeighbors[WEST] && mNeighbors[WEST]->isLoaded())
    {
        uint i = mNumTiles - 1;
        for (uint  j = 0; j < mNumTiles; j++ )
        {	
            PagingLandScapeTile *t_nextpage =  mNeighbors[WEST]->getTile  ( i , j );
            PagingLandScapeTile *t_currpage =  mTiles[ 0 ][ j ];
            assert(t_nextpage && t_currpage);
            t_currpage -> _setNeighbor( WEST, t_nextpage );
            t_nextpage -> _setNeighbor( EAST, t_currpage );
        }
    }

    if (mNeighbors[SOUTH] && mNeighbors[SOUTH]->isLoaded())
    {       
        uint j = mNumTiles - 1;
        for (uint  i = 0; i < mNumTiles; i++ )
        {	
            PagingLandScapeTile *t_nextpage =  mNeighbors[SOUTH]->getTile  ( i , 0 );
            PagingLandScapeTile *t_currpage =  mTiles[ i ][ j ];
            assert(t_nextpage && t_currpage);
            t_currpage -> _setNeighbor( SOUTH, t_nextpage);
            t_nextpage -> _setNeighbor( NORTH, t_currpage );
        }
    }
    if (mNeighbors[NORTH] && mNeighbors[NORTH]->isLoaded())
    {       
        uint j = mNumTiles - 1;
        for (uint  i = 0; i < mNumTiles; i++ )
        {	
            PagingLandScapeTile *t_nextpage =  mNeighbors[NORTH]->getTile  ( i , j );
            PagingLandScapeTile *t_currpage =  mTiles[ i ][ 0 ];
            assert(t_nextpage && t_currpage);
            t_currpage -> _setNeighbor(NORTH, t_nextpage );
            t_nextpage -> _setNeighbor(SOUTH, t_currpage );
        }
    }
    mPageNode->_update( true, true );
	mIsLoaded = true;
}

//-----------------------------------------------------------------------
void PagingLandScapePage::unload( )
{
	if ( mIsLoaded == false )
		return;
    
    if (mNeighbors[SOUTH])
        mNeighbors[SOUTH]->_setNeighbor (NORTH, 0);
    if (mNeighbors[NORTH])
        mNeighbors[NORTH]->_setNeighbor (SOUTH, 0);
    if (mNeighbors[EAST])
        mNeighbors[EAST]->_setNeighbor (WEST, 0);
    if (mNeighbors[WEST])
        mNeighbors[WEST]->_setNeighbor (EAST, 0);
    for ( uint i = 0; i < 4; i++ )
    {
        mNeighbors[ i ] = 0;
    }

    // Unload the Tiles
    for ( uint i = 0; i < mNumTiles; i++ )
    {
        for ( uint j = 0; j < mNumTiles; j++ )
        {
            assert (mTiles[ i ][ j ]);
            mTiles[ i ][ j ] -> release();
            mTiles[ i ][ j ] = 0;
        }
        //mTiles[ i ].clear();
    }
    //mTiles.clear ();

	if ( mPageNode  )
	{
		// Unload the nodes
		mPageNode->detachAllObjects();
		mPageNode->removeAndDestroyAllChildren();

		//mPageNode->getParent()->removeChild( mPageNode->getName() );
		//delete mPageNode;
		static_cast<SceneNode*>( mPageNode->getParent() )->removeAndDestroyChild( mPageNode->getName() );
		mPageNode = 0;
	}
    PagingLandScapeTextureManager::getSingleton().unload(mTableX, mTableZ);
    mIsLoaded = false;
}

//-----------------------------------------------------------------------
void PagingLandScapePage::postUnload( )
{
	if ( mIsLoaded == true )
	{
		return;
	}
	if ( mIsPreLoaded == true )
	{
		mIsPreLoaded = false;

		PagingLandScapeData2DManager::getSingleton().unload( mTableX, mTableZ );
	}
}

//-----------------------------------------------------------------------
bool PagingLandScapePage::isLoaded()
{
	return mIsLoaded;
}

//-----------------------------------------------------------------------
bool PagingLandScapePage::isPreLoaded( )
{
	return mIsPreLoaded;
}

//-----------------------------------------------------------------------
int PagingLandScapePage::isCameraIn( const Vector3 & pos )
{
	if ( mBoundsExt.intersects( pos ) == true )
	{
		if ( mBoundsInt.intersects( pos ) == true )
		{
			// Full into this page
			return PAGE_INSIDE;
		}
		else
		{
			// Over the change zone
			return PAGE_CHANGE;
		}
	}
	else
	{
		// Not in this page
		return PAGE_OUTSIDE;
	}
}

//-----------------------------------------------------------------------
void PagingLandScapePage::_Notify(const Vector3 &pos, PagingLandscapeCamera* Cam)
{
	if (mIsLoaded
        && Cam->getVisibility (mPageNode->_getWorldAABB ()))
        //&& Cam->getVisibility (mBoundsExt))
	{
        
		for ( uint i = 0; i < mNumTiles; i++ )
		{
			for ( uint j = 0; j < mNumTiles; j++ )
			{
				mTiles[ i ][ j]->_Notify( pos, Cam);
			}
		}
	}
}

//-----------------------------------------------------------------------
void PagingLandScapePage::getPagingLandScapeRenderOpsInBox(const AxisAlignedBox& box, std::list<RenderOperation>& opList)
{
	if ( box.intersects( mBoundsExt ) )
	{
		for ( uint i = 0; i < mNumTiles; i++ )
		{
			for ( uint j = 0; j < mNumTiles; j++ )
			{
				mTiles[ i ][ j]->getPagingLandScapeRenderOpsInBox( box, opList );
			}
		}
	}
}

PagingLandScapeTile *PagingLandScapePage::getTile(const Vector3& pos)
{
    Vector3 TileRefPos = pos;

    TileRefPos.x = (pos.x / PagingLandScapeOptions::getSingleton().scale.x / (PagingLandScapeOptions::getSingleton().TileSize));
    TileRefPos.z = (pos.z / PagingLandScapeOptions::getSingleton().scale.z / (PagingLandScapeOptions::getSingleton().TileSize));

    int x = (int) TileRefPos.x;
    int z = (int) TileRefPos.z;

    if (mTiles[x][z] && mTiles[x][z]-> isLoaded ())
        return mTiles[x][z];
    else
        return 0;
}
} //namespace

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -