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

📄 ogrepaginglandscapedata2dmanager.cpp

📁 使用stl技术,(还没看,是听说的)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************
	OgrePagingLandScapeData2DManager.cpp  -  description
	-------------------
	begin                : Mon Jun 16 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 "OgreSimpleRenderable.h"
#include "OgreException.h"

#include "OgrePagingLandScapeOptions.h"
#include "OgrePagingLandScapeCamera.h"

#include "OgrePagingLandScapeData2DManager.h"
#include "OgrePagingLandScapeData2D.h"

// Data loaders implementations
#include "OgrePagingLandScapeData2D_HeightField.h"
#include "OgrePagingLandScapeData2D_HeightFieldTC.h"
#include "OgrePagingLandScapeData2D_Spline.h"

// needed to get RenderLevel for RealHeight
#include "OgrePagingLandScapeRenderable.h"
#include "OgrePagingLandScapeTile.h"
#include "OgrePagingLandScapeTileInfo.h"
#include "OgrePagingLandScapePage.h"
#include "OgrePagingLandScapePageManager.h"

namespace Ogre
{

//-----------------------------------------------------------------------
template<> PagingLandScapeData2DManager* Singleton<PagingLandScapeData2DManager>::ms_Singleton = 0;
PagingLandScapeData2DManager* PagingLandScapeData2DManager::getSingletonPtr(void)
{
	return ms_Singleton;
}
PagingLandScapeData2DManager& PagingLandScapeData2DManager::getSingleton(void)
{  
	assert( ms_Singleton );  return ( *ms_Singleton );  
}

//-----------------------------------------------------------------------
PagingLandScapeData2DManager::PagingLandScapeData2DManager( )
{
    uint w = PagingLandScapeOptions::getSingleton().world_width;
    uint h = PagingLandScapeOptions::getSingleton().world_height;
    //Setup the page array
    uint i, j;
	for ( i = 0; i < w; i++)
	{
		mData2D.push_back( PagingLandScapeData2DRow() );
		for ( j = 0; j < h; j++ )
		{
			mData2D[i].push_back( 0 );
		}
	}
    //Populate the page array
    if ( PagingLandScapeOptions::getSingleton().data2DFormat == "HeightField" )
    {
        for ( j = 0; j < h; j++ )
        {
            for ( i = 0; i < w; i++ )
            {
                PagingLandScapeData2D* data;

                data = new PagingLandScapeData2D_HeightField();
                mData2D[ i ][ j ] = data;
            }
        }

    }
     else if ( PagingLandScapeOptions::getSingleton().data2DFormat == "HeightFieldTC" )
     {
         for ( j = 0; j < h; j++ )
         {
             for ( i = 0; i < w; i++ )
             {
                    PagingLandScapeData2D* data;

                    data	= new PagingLandScapeData2D_HeightFieldTC();
                    mData2D[ i ][ j ] = data;
             }
        }
     }
    else if ( PagingLandScapeOptions::getSingleton().data2DFormat == "SplineField" )
    {
        for ( j = 0; j < h; j++ )
        {
            for ( i = 0; i < w; i++ )
            {
                PagingLandScapeData2D* data;

                data = new PagingLandScapeData2D_Spline();
                mData2D[ i ][ j ] = data;
            }
        }
    }
    else
    {
       
	Except( Exception::ERR_INVALIDPARAMS, "PageData2D not supplied!",
		"PagingLandScapePageManager::PagingLandScapePageManager" );
        
    }
    // when data is not yet loaded it gives the absolute maximum possible
    mMaxHeight = mData2D[ 0 ][ 0 ]->getMaxHeight ();
    
}
//-----------------------------------------------------------------------

PagingLandScapeData2DManager::~PagingLandScapeData2DManager( )
{    
    for (uint j = 0; j < PagingLandScapeOptions::getSingleton().world_height; j++ )
    {
        for (uint i = 0; i < PagingLandScapeOptions::getSingleton().world_width; i++ )
        {
            assert (mData2D[ i ][ j ]);
            delete  mData2D[ i ][ j ];
            mData2D[ i ][ j ] = 0;
        }
    }

//    PagingLandScapeData2DPages::iterator iend = mData2D.end();
//    for (PagingLandScapeData2DPages::iterator i = mData2D.begin(); i != iend; ++i)
//    {
//        PagingLandScapeData2DRow::iterator jend  = i->end();
//        for (PagingLandScapeData2DRow::iterator j = i->begin(); j != jend; ++j)
//        {
//            delete *j;
//            *j = 0;
//        }       
//    }
//    for (PagingLandScapeData2DPages::iterator i = mData2D.begin(); i != iend; ++i)
//    {
//        i->clear();    
//    } 
//	mData2D.clear();
}

//-----------------------------------------------------------------------
void PagingLandScapeData2DManager::load( const uint dataX, const uint dataZ )
{
	PagingLandScapeData2D* data = mData2D[ dataX ][ dataZ ];
	if ( !data->isLoaded() )
	{
        data->load( dataX, dataZ );
	}
}
//-----------------------------------------------------------------------
void PagingLandScapeData2DManager::unload( const uint dataX, const uint dataZ )
{
	PagingLandScapeData2D* data = mData2D[ dataX ][ dataZ ];
	if ( data->isLoaded() )
	{
		data->unload();
	}
}

//-----------------------------------------------------------------------
bool PagingLandScapeData2DManager::isLoaded( const uint dataX, const uint dataZ )
{
	return mData2D[ dataX ][ dataZ ]->isLoaded();
}
//-----------------------------------------------------------------------
const ColourValue PagingLandScapeData2DManager::getCoverageAt( const uint dataX, const uint dataZ, const float x, const float z )
{
    PagingLandScapeData2D* data = mData2D[ dataX ][ dataZ ];
    if ( data->isLoaded() )
    {
        // TODO check it the terrain height is modified
        return data->getCoverage(x, z);
    }
    return ColourValue::White;
}
//-----------------------------------------------------------------------
const ColourValue PagingLandScapeData2DManager::getBaseAt( const uint dataX, const uint dataZ, const float x, const float z )
{
    PagingLandScapeData2D* data = mData2D[ dataX ][ dataZ ];
    if ( data->isLoaded() )
    {
        // TODO check it the terrain height is modified
        return data->getBase(x, z);
   }
    return ColourValue::White;
}
//-----------------------------------------------------------------------
const float PagingLandScapeData2DManager::getHeight( const uint dataX, 
                                                    const uint dataZ, 
                                                    const float x, 
                                                    const float z )
{
    
	PagingLandScapeData2D* data = mData2D[ dataX ][ dataZ ];
	if ( data->isLoaded() )
	{
		// TODO check it the terrain height is modified
		return data->getHeight(x, z);
	}
	return 0.0f;
}
//-----------------------------------------------------------------------
const float PagingLandScapeData2DManager::getHeight( const uint dataX, 
                                                    const uint dataZ, 
                                                    const uint x, 
                                                    const uint z )
{
    
	PagingLandScapeData2D* data = mData2D[ dataX ][ dataZ ];
	if ( data->isLoaded() )
	{
		// TODO check it the terrain height is modified
		return data->getHeight(x, z);
	}
	return 0.0f;
}
//-----------------------------------------------------------------------
void PagingLandScapeData2DManager::DeformHeight(const Vector3 &deformationPoint,
                                                const Real &modificationHeight,
                                                PagingLandScapeTileInfo* info)
{
    uint pSize = PagingLandScapeOptions::getSingleton().PageSize;
    uint pX = info->pageX;
    uint pZ = info->pageZ;
    uint wL = PagingLandScapeOptions::getSingleton().world_width;
    uint hL = PagingLandScapeOptions::getSingleton().world_height;
    // adjust x and z to be local to page
    uint x = uint ((deformationPoint.x ) 
            - ((pX - wL * 0.5f) * (pSize)));
    uint z = uint ((deformationPoint.z ) 
            - ((pZ - hL * 0.5f) * (pSize)));

    PagingLandScapeData2D* data = mData2D[ pX ][ pZ ];
    if ( data->isLoaded() )
    {
        const Real h = data->DeformHeight (x, z, modificationHeight);
   
        // If we're on a page edge, we must duplicate the change on the 
        // neighbour page (if it has one...)
        if (x == 0 && pX != 0)
        {
            data = mData2D[ pX - 1 ][ pZ ];
            if ( data->isLoaded() )
            {
                data->setHeight (PagingLandScapeOptions::getSingleton().PageSize - 1, z, h);
            } 
        }
        if (x == pSize - 1 && pX < wL - 1)
        {
            data = mData2D[ pX + 1 ][ pZ ];
            if ( data->isLoaded() )
            {
                data->setHeight (0, z, h);
            }    
        }

        if (z == 0 && pZ != 0)
        {
            
            data = mData2D[ pX ][ pZ - 1 ];
            if ( data->isLoaded() )
            {
                data->setHeight (x, PagingLandScapeOptions::getSingleton().PageSize - 1, h);
            } 
        }
        if (z == pSize - 1 && pZ < hL - 1)
        {
            data = mData2D[ pX ][ pZ  + 1];
            if ( data->isLoaded() )
            {
                data->setHeight (x, 0, h);
            } 
        } 
    }
}

//-----------------------------------------------------------------------
bool PagingLandScapeData2DManager::addNewHeight( const Sphere newSphere )
{
	uint x, z;
	// Calculate where is going to be placed the new height
	getPageIndices( newSphere.getCenter(), x, z);
	// TODO: DeScale and add the sphere to all the necessary pages

	//place it there
	return mData2D[ x ][ z ]->addNewHeight(newSphere);
}

//-----------------------------------------------------------------------
bool PagingLandScapeData2DManager::removeNewHeight( const Sphere oldSphere )
{
	uint x, z;
	// Calculate where is going to be placed the new height
	getPageIndices( oldSphere.getCenter(), x, z);
	// TODO: DeScale and add the sphere to all the necessary pages

	//remove it
	return mData2D[ x ][ z ]->removeNewHeight(oldSphere);
}
//-----------------------------------------------------------------------
const Real PagingLandScapeData2DManager::getMaxHeight(const uint x, const uint z)
{ 
    PagingLandScapeData2D* data = mData2D[ x ][ z ];  
    if ( data->isLoaded() )
    {
        return mData2D[ x ][ z ]->getMaxHeight();
    }
    return mMaxHeight;
}
//-----------------------------------------------------------------------
const Real PagingLandScapeData2DManager::getMaxHeight()
{ 
    return mMaxHeight;
}
//-----------------------------------------------------------------------
const float PagingLandScapeData2DManager::getRealPageHeight (const float x, const float z, const uint pageX, const uint pageZ, const uint Lod)
{
    // scale position from world to page scale
    Real localX = x /PagingLandScapeOptions::getSingleton().scale.x;
    Real localZ = z / PagingLandScapeOptions::getSingleton().scale.z;

    // adjust x and z to be local to page
    uint pSize = PagingLandScapeOptions::getSingleton().PageSize - 1;
    localX -= (float)(pageX - PagingLandScapeOptions::getSingleton().world_width  * 0.5f) * pSize;
    localZ -= (float)(pageZ - PagingLandScapeOptions::getSingleton().world_height * 0.5f) * pSize;

    // make sure x and z do not go outside the world boundaries
    if (localX < 0)
        localX = 0;
    else if (localX > pSize) 
        localX = pSize;

    if (localZ < 0)
        localZ = 0;
    else if (localZ > pSize)

⌨️ 快捷键说明

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