📄 ogrepaginglandscapepage.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 <OgreImage.h>
#include <OgreStringConverter.h>
#include <OgreConfigFile.h>
#include <OgreMaterialManager.h>
#include <OgreSceneNode.h>
#include <OgreTechnique.h>
#include <OgrePass.h>
#include <OgreTextureUnitState.h>
#include <OgreSceneManager.h>
#include <OgreCamera.h>
#include <OgreException.h>
#include <OgreRenderSystem.h>
#include <fstream>
#include "OgrePagingLandScapePage.h"
#include "OgrePagingLandScapeTileManager.h"
#include "OgrePagingLandScapePageData2D_HeightField.h"
#include "OgrePagingLandScapePageData2D_Spline.h"
namespace Ogre
{
PagingLandScapePage::PagingLandScapePage( int startx, int startz )
{
mIsLoaded = false;
mIsPreLoaded = false;
mOptions = PagingLandScapeOptions::getSingletonPtr();
mX = startx;
mZ = startz;
mPageNode = 0;
int size = mOptions->PageSize - 1;
// Boundaries of this page
// the middle page is at world coordinates 0,0
mIniX = (float)( mX + mX - mOptions->world_width) / 2.0 * ( size ) * mOptions->scale.x;
mIniZ = (float)( mZ + mZ - mOptions->world_height) / 2.0 * ( size ) * mOptions->scale.z;
float EndX = mIniX + size * mOptions->scale.x;
float EndZ = mIniZ + size * mOptions->scale.z;
//Change Zone of this page
// mZoneIniX = mIniX + mOptions->change_factor;
// mZoneEndX = mEndX - mOptions->change_factor;
// mZoneIniZ = mIniZ + mOptions->change_factor;
// mZoneEndZ = mEndZ - mOptions->change_factor;
mBoundsExt.setExtents( ( Real )( mIniX ), 0, ( Real )( mIniZ ),
( Real )( EndX ), 255 * mOptions->scale.y, ( Real )( EndZ ) );
mBoundsInt.setExtents( ( Real )( mIniX + mOptions->change_factor ), 0, ( Real )( mIniZ + mOptions->change_factor ),
( Real )( EndX - mOptions->change_factor ), 255 * mOptions->scale.y, ( Real )( EndZ - mOptions->change_factor ) );
for ( int i = 0; i < 4; i++ )
{
mNeighbors[ i ] = 0;
}
// get default material
mLandScapeMaterial = reinterpret_cast<Material *>(MaterialManager::getSingleton().getByName("BaseWhite"));
if (mOptions->data2DFormat == "HeightField" )
{
mData = new PagingLandScapePageData2D_HeightField();
}
else if (mOptions->data2DFormat == "SplineField" )
{
mData = new PagingLandScapePageData2D_Spline();
}
else
{
mData = 0;
Except(Exception::ERR_INVALIDPARAMS, "PageData2D not supplied!", "PagingLandScapePage::PagingLandScapePage" );
}
}
//-----------------------------------------------------------------------
PagingLandScapePage::~PagingLandScapePage()
{
mOptions = 0;
if ( mIsLoaded == true )
{
//unload();
// Unload the material
if ( mLandScapeMaterial != 0 )
{
//TODO: Revisar esto
//if ( mLandScapeMaterial->isLoaded() )
//{
// mLandScapeMaterial->unload();
//}
mLandScapeMaterial = 0;
}
// Unload the pages
int size = (int)mTiles.size();
for ( int i = 0; i < size; i++ )
{
for ( int j = 0; j < size; j++ )
{
mTiles[ i ][ j ] -> release();
mTiles[ i ][ j ] = 0;
}
}
if ( mData->isLoaded() )
{
mData->unload();
}
mIsLoaded = false;
}
if ( mPageNode != 0 )
{
mPageNode = 0;
}
if ( mData != 0 )
{
delete mData;
mData = 0;
}
}
//-----------------------------------------------------------------------
void PagingLandScapePage::load( SceneNode &PagingLandScapeRootNode )
{
if ( mIsPreLoaded == false )
{
return;
}
if ( mIsLoaded == true )
{
return;
}
//create a root landscape node.
mPageNode = PagingLandScapeRootNode.createChildSceneNode( "PagingLandScapePage." + StringConverter::toString( mX ) + "." + StringConverter::toString( mZ ) );
// Set node position
mPageNode -> setPosition ( (Real)mIniX , 0.0, (Real)mIniZ );
//setup the page array.
int tile_step = pow( 2, mOptions->InitLOD );
//JEFF was - mOptions->PageSize - 1
int num_tiles = ( mOptions->PageSize ) / tile_step;
int i, j;
for ( i = 0; i < num_tiles; i++ )
{
mTiles.push_back( PagingLandScapeTileRow() );
for ( j = 0; j < num_tiles; j++ )
{
mTiles[ i ].push_back( 0 );
}
}
char name[ 24 ];
int p = 0;
int q = 0;
for ( j = 0; j < (mOptions->PageSize - 1); j += tile_step )
{
p = 0;
for ( i = 0; i < (mOptions->PageSize - 1); i += tile_step )
{
sprintf( name, "page[%d,%d][%d,%d]", mX, mZ, p, q );
PagingLandScapeTile *tile = PagingLandScapeTileManager::getSingleton().getTile();
if ( tile != 0 )
{
mTiles[ p ][ q ] = tile;
//JEFF - tile has its own scene node
tile->init(*mPageNode, mData, mLandScapeMaterial, mIniX, mIniZ, i, j, mOptions->InitLOD );
}
else
{
String err = "Error: Invalid Tile: Make sure the default TileManager size is set to WorldWidth * WorldHeight * " +
StringConverter::toString(num_tiles) + "." + "Try increasing MaxNumTiles in the config file.";
Except( Exception::ERR_INVALIDPARAMS, err, "PagingLandScapePage::load" );
}
//JEFF - tile adds its own SceneNode to page node
//mPageNode -> attachObject( tile );
p++;
}
q++;
}
//setup the neighbor links.
for ( j = 0; j < q; j++ )
{
for ( i = 0; i < p; i++ )
{
if ( j == 0 ) // WEST
{
if ( mNeighbors[ PagingLandScapeTile::WEST ] != 0 )
{
if ( mNeighbors[ PagingLandScapeTile::WEST ] -> isLoaded( ) == true )
{
mTiles[ i ][ 0 ] -> setNeighbor( PagingLandScapeTile::WEST, mNeighbors[ PagingLandScapeTile::WEST ] -> mTiles[ i ][ q - 1 ] );
mNeighbors[ PagingLandScapeTile::WEST ] -> mTiles[ i ][ q - 1 ] -> setNeighbor( PagingLandScapeTile::EAST, mTiles[ i ][ 0 ] );
}
}
}
if ( j == ( q - 1 ) ) // EAST
{
if ( mNeighbors[ PagingLandScapeTile::EAST ] != 0 )
{
if ( mNeighbors[ PagingLandScapeTile::EAST ] -> isLoaded( ) == true )
{
mTiles[ i ][ q - 1 ] -> setNeighbor( PagingLandScapeTile::EAST, mNeighbors[ PagingLandScapeTile::EAST ] -> mTiles[ i ][ 0 ] );
mNeighbors[ PagingLandScapeTile::EAST ] -> mTiles[ i ][ 0 ] -> setNeighbor( PagingLandScapeTile::WEST, mTiles[ i ][ q - 1 ] );
}
}
}
if ( i == 0 ) // NORTH
{
if ( mNeighbors[ PagingLandScapeTile::NORTH ] != 0 )
{
if ( mNeighbors[ PagingLandScapeTile::NORTH ] -> isLoaded( ) == true )
{
mTiles[ 0 ][ j ] -> setNeighbor( PagingLandScapeTile::NORTH, mNeighbors[ PagingLandScapeTile::NORTH ] -> mTiles[ p - 1 ][ j ] );
mNeighbors[ PagingLandScapeTile::NORTH ] -> mTiles[ p - 1 ][ j ] -> setNeighbor( PagingLandScapeTile::SOUTH, mTiles[ 0 ][ j ] );
}
}
}
if ( i == ( p - 1 ) ) // SOUTH
{
if ( mNeighbors[ PagingLandScapeTile::SOUTH ] != 0 )
{
if ( mNeighbors[ PagingLandScapeTile::SOUTH ] -> isLoaded( ) == true )
{
mTiles[ p - 1 ][ j ] -> setNeighbor( PagingLandScapeTile::SOUTH, mNeighbors[ PagingLandScapeTile::SOUTH ] -> mTiles[ 0 ][ j ] );
mNeighbors[ PagingLandScapeTile::SOUTH ] -> mTiles[ 0 ][ j ] -> setNeighbor( PagingLandScapeTile::NORTH, mTiles[ p - 1 ][ j ] );
}
}
}
// REST
if ( j != q - 1 )
{
mTiles[ i ][ j ] -> setNeighbor( PagingLandScapeTile::EAST, mTiles[ i ][ j + 1 ] );
mTiles[ i ][ j + 1 ] -> setNeighbor( PagingLandScapeTile::WEST, mTiles[ i ][ j ] );
}
if ( i != p - 1 )
{
mTiles[ i ][ j ] -> setNeighbor( PagingLandScapeTile::SOUTH, mTiles[ i + 1 ][ j ] );
mTiles[ i + 1 ][ j ] -> setNeighbor( PagingLandScapeTile::NORTH, mTiles[ i ][ j ] );
}
}
}
// Not required?
mPageNode -> _update( true, true );
mIsLoaded = true;
}
//-----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -