📄 ogrepaginglandscapepagemanager.cpp
字号:
/***************************************************************************
OgrePagingLandScapePageManager.cpp - description
-------------------
begin : Sat May 01 2004
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 "OgrePagingLandScapeOptions.h"
#include "OgrePagingLandScapeCamera.h"
#include "OgrePagingLandScapePage.h"
#include "OgrePagingLandScapePageManager.h"
#include "OgrePagingLandScapeRenderableManager.h"
#include "OgrePagingLandScapeData2DManager.h"
#include "OgrePagingLandScapeTileInfo.h"
#include "OgrePagingLandScapeTile.h"
namespace Ogre
{
//-----------------------------------------------------------------------
template<> PagingLandScapePageManager* Singleton<PagingLandScapePageManager>::ms_Singleton = 0;
PagingLandScapePageManager* PagingLandScapePageManager::getSingletonPtr(void)
{
return ms_Singleton;
}
PagingLandScapePageManager& PagingLandScapePageManager::getSingleton(void)
{
assert( ms_Singleton ); return ( *ms_Singleton );
}
//-----------------------------------------------------------------------
PagingLandScapePageManager::PagingLandScapePageManager(SceneNode* rootNode)
{
mSceneRoot = rootNode;
mCurrentCameraPageX = 0;
mCurrentCameraPageZ = 0;
mLastCameraPageState = PAGE_CHANGE;
mLastCameraPos = Vector3(-999999,9999999,-999999);
mPause = 99;
mWidth = PagingLandScapeOptions::getSingleton().world_width;
mHeigth = PagingLandScapeOptions::getSingleton().world_height;
//setup the page array.
// mPages.reserve (mWidth);
// mPages.resize (mWidth);
for (uint i = 0; i < mWidth; i++ )
{
mPages.push_back( PagingLandScapePageRow() );
// mPages[ i ].reserve (mHeigth);
// mPages[ i ].resize (mHeigth);
for (uint j = 0; j < mHeigth; j++ )
{
mPages[i].push_back( 0);
}
}
// populate the page array
for (uint j = 0; j < mHeigth; j++ )
{
for (uint i = 0; i < mWidth; i++ )
{
mPages[ i ][ j ] = new PagingLandScapePage( i, j );
}
}
for (uint j = 0; j < mHeigth; j++ )
{
for (uint i = 0; i < mWidth; i++ )
{
if ( j != mHeigth - 1)
{
mPages[ i ][ j ]-> _setNeighbor( SOUTH, mPages[ i ][ j + 1 ] );
mPages[ i ][ j + 1 ] -> _setNeighbor( NORTH, mPages[ i ][ j ] );
}
if ( i != mWidth - 1)
{
mPages[ i ][ j ] -> _setNeighbor( EAST, mPages[ i + 1 ][ j ] );
mPages[ i + 1 ][ j ] -> _setNeighbor( WEST, mPages[ i ][ j ] );
}
}
}
}
//-----------------------------------------------------------------------
PagingLandScapePageManager::~PagingLandScapePageManager()
{
for (uint i = 0; i < mWidth; i++ )
{
for (uint j = 0; j < mHeigth; j++ )
{
assert (mPages[i][j]);
delete mPages[i][j];
mPages[i][j] = 0;
}
}
// PagingLandScapePages::iterator iend = mPages.end();
// for (PagingLandScapePages::iterator i = mPages.begin(); i != iend; ++i)
// {
// PagingLandScapePageRow::iterator jend = i->end();
// for (PagingLandScapePageRow::iterator j = i->begin(); j != jend; ++j)
// {
// delete *j;
// *j = 0;
// }
// }
// for (PagingLandScapePages::iterator i = mPages.begin(); i != mPages.end(); ++i)
// {
// i->clear();
// }
// mPages.clear();
}
//-----------------------------------------------------------------------
void PagingLandScapePageManager::update( Camera* cam )
{
// Here we have to look if we have to load, unload any of the LandScape Pages
//Vector3 pos = cam->getPosition();
// Fix from Praetor, so the camera used gives you "world-relative" coordinates
Vector3 pos = cam->getDerivedPosition();
//updateStats( pos );
// Update only if the camera was moved
// make sure in the bounding box of landscape
pos.y = 127.0f;
if ( PagingLandScapeOptions::getSingleton().cameraThreshold <= (mLastCameraPos - pos).squaredLength() )
{
// Check for the camera position in the LandScape Pages list, we check if we are in the inside the security zone
// if not, launch the change routine and update the camera position mCurrentCameraX, mCurrentCameraY.
if ( mPages[ mCurrentCameraPageX ][ mCurrentCameraPageZ ] &&
mPages[ mCurrentCameraPageX ][ mCurrentCameraPageZ ]-> isCameraIn( pos ) != PAGE_INSIDE )
{
// JEFF
// convert camera pos to page index
uint i, j;
PagingLandScapeData2DManager::getSingleton().getPageIndices (pos, i, j);
if (((mCurrentCameraPageX != i) || (mCurrentCameraPageZ != j)) &&
(mPages[ i ][ j ] -> isCameraIn ( pos ) == PAGE_INSIDE) )
{
const uint adjpages = PagingLandScapeOptions::getSingleton().max_adjacent_pages;
const uint prepages = PagingLandScapeOptions::getSingleton().max_preload_pages;
const uint w = mWidth;
const uint h = mHeigth;
// We must load the next visible landscape pages,
// and unload the last visibles
// check the landscape boundaries
uint iniX = ((int)(i - adjpages) > 0)? i - adjpages: 0;
uint iniZ = ((int)(j - adjpages) > 0)? j - adjpages: 0;
uint finX = (i + adjpages >= w )? w - 1: i + adjpages;
uint finZ = (j + adjpages >= h )? h - 1: j + adjpages;
uint preIniX = ((int)(i - prepages) > 0)? i - prepages: 0;
uint preIniZ = ((int)(j - prepages) > 0)? j - prepages: 0;
uint preFinX = (i + prepages >= w)? w - 1: i + prepages;
uint preFinZ = (j + prepages >= h )? h - 1: j + prepages;
// update the camera page position
mCurrentCameraPageX = i;
mCurrentCameraPageZ = j;
// Have the current page be loaded now
if ( mPages[ mCurrentCameraPageX ][ mCurrentCameraPageZ ]->isPreLoaded() == false )
{
mPages[ mCurrentCameraPageX ][ mCurrentCameraPageZ ]->preload();
}
if ( mPages[ mCurrentCameraPageX ][ mCurrentCameraPageZ ]->isLoaded() == false )
{
mPages[ mCurrentCameraPageX ][ mCurrentCameraPageZ ]->load( * mSceneRoot );
}
// Queue the rest
// Loading and unloading must be done one by one to avoid FPS drop, so they are queued.
// No need to queue for preload since _ProcessLoading will do it in Load if required.
// post unload as required
for( j = 0; j < preIniZ; j++)
{
for( i = 0 ; i < preIniX; i++)
{
mPagePostunloadQueue.push( mPages[ i ][ j ] );
}
}
for( j = preFinZ + 1; j < h; j++)
{
for( i = preFinX + 1; i < w; i++)
{
mPagePostunloadQueue.push( mPages[ i ][ j ] );
}
}
// Preload as required
for ( j = preIniZ; j < iniZ; j++ )
{
for ( i = preIniX; i < iniX; i++ )
{
mPagePreloadQueue.push( mPages[ i ][ j ] );
}
}
for ( j = finZ; j <= preFinZ; j++ )
{
for ( i = finX; i <= preFinX; i++ )
{
mPagePreloadQueue.push( mPages[ i ][ j ] );
}
}
// load as required
for ( j = iniZ; j <= finZ; j++ )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -