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

📄 ogrepaginglandscapepagedata2d_heightfield.cpp

📁 使用stl技术,(还没看,是听说的)
💻 CPP
字号:
/***************************************************************************
  OgrePagingLandScapePageData2D_HeightField.cpp  -  description
  -------------------
  begin                : Mon Oct 13 2003
  copyright            : (C) 2003 by Jose A Milan
  email                : spoke@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 <OgreStringConverter.h>
#include <OgreException.h>

#include "OgrePagingLandScapePageData2D_HeightField.h"
#include "OgrePagingLandScapeOptions.h"

namespace Ogre
{

PagingLandScapePageData2D_HeightField::PagingLandScapePageData2D_HeightField()
: PagingLandScapePageData2D()
{
	mImage = 0;
	input_max =  3000.0f;
	input_min = 0.0f;
}

//-----------------------------------------------------------------------
PagingLandScapePageData2D_HeightField::~PagingLandScapePageData2D_HeightField()
{
	if ( mImage != 0 )
	{
		delete mImage;
	}
}

//-----------------------------------------------------------------------
void PagingLandScapePageData2D_HeightField::load( float mX, float mZ )
{
	PagingLandScapeOptions *options = PagingLandScapeOptions::getSingletonPtr();

	if ( mImage == 0 )
	{
		mImage = new Image();

		mImage -> load( options->landscape_filename + "." + StringConverter::toString( mZ ) + "." + StringConverter::toString( mX ) + "." + options->landscape_extension );

		//check to make sure it's 2^n + 1 size.
		if ( mImage -> getWidth() != mImage -> getHeight() ||	! _checkSize( mImage -> getWidth() ) )
		{
			String err = "Error: Invalid heightmap size : " +
						 StringConverter::toString( mImage -> getWidth() ) +
						 "," + StringConverter::toString( mImage -> getHeight() ) +
						 ". Should be 2^n+1, 2^n+1";
			Except( Exception::ERR_INVALIDPARAMS, err, "PagingLandScapePageData2D::load" );
		}

/*
		if ( mImage -> getFormat() != PF_L8 )
		{
			Except( Exception::ERR_INVALIDPARAMS, "Error: Image is not a grayscale image.", "PagingLandScapePageData2D::load" );
		}
*/
		mSize = options->PageSize;
		if ( mSize != mImage -> getWidth() )
		{
			Except ( Exception::ERR_INVALIDPARAMS, "Error: Declared World size <> Height Map Size.", "PagingLandScapePageData2D::load" );
		}
		mMax = mSize * mSize + 1;

		PagingLandScapePageData2D::load(mX, mZ);
	}
	else
	{
		String err = "Error: 2D Data arready loaded ";
		Except( Exception::ERR_INTERNAL_ERROR, err, "PagingLandScapePageData2D::load" );
	}
}

//-----------------------------------------------------------------------
void PagingLandScapePageData2D_HeightField::unload()
{
	if ( mImage != 0 )
	{
		delete mImage;
		mImage = 0;
	}

	PagingLandScapePageData2D::unload();
}

//-----------------------------------------------------------------------
float PagingLandScapePageData2D_HeightField::getHeight( float x, float z )
{
	int Pos = ( z * mSize ) + x;
	if ( mMax > Pos )
	{
		return (float)_decodeTC(mImage->getData()[ Pos ]);
	}
	else
	{
		return 0.0;
	}
}

//-----------------------------------------------------------------------
float PagingLandScapePageData2D_HeightField::getError( float iniX, float iniZ, float endX, float endZ )
{
	int stepX = ( endX - iniX ) / 2;
	int stepZ = ( endZ - iniZ ) / 2;
	int Height[ 9 ];
	int Interpolated[ 5 ];
	int *pInt = Height;

	for ( int k = iniZ; k <= endZ; k += stepZ )
	{
		for ( int i = iniX; i <= endX; i += stepX )
		{
			*pInt++ = getHeight( i, k );
		}
	}
	Interpolated[ 0 ] = ( Height[ 0 ] + Height[ 2 ] ) / 2;
	Interpolated[ 1 ] = ( Height[ 0 ] + Height[ 6 ] ) / 2; 
	Interpolated[ 3 ] = ( Height[ 2 ] + Height[ 8 ] ) / 2;
	Interpolated[ 4 ] = ( Height[ 6 ] + Height[ 8 ] ) / 2;
	Interpolated[ 2 ] = ( Interpolated[ 1 ] + Interpolated[ 3 ] ) / 2;
		
	if ( Interpolated[ 0 ] >= Height[ 1 ] )
	{
		Interpolated[ 0 ] = Interpolated[ 0 ] - Height[ 1 ];
	}
	else
	{
		Interpolated[ 0 ]= Height[ 1 ] - Interpolated[ 0 ];
	}

	if ( Interpolated[ 1 ] >= Height[ 3 ] )
	{
		Interpolated[ 1 ] = Interpolated[ 1 ] - Height[ 3 ];
	}
	else
	{
		Interpolated[ 1 ]= Height[ 3 ] - Interpolated[ 1 ];
	}

	if ( Interpolated[ 2 ] >= Height[ 4 ] )
	{
		Interpolated[ 2 ] = Interpolated[ 2 ] - Height[ 4 ];
	}
	else
	{
		Interpolated[ 2 ]= Height[ 4 ] - Interpolated[ 2 ];
	}

	if ( Interpolated[ 3 ] >= Height[ 5 ] )
	{
		Interpolated[ 3 ] = Interpolated[ 3 ] - Height[ 5 ];
	}
	else
	{
		Interpolated[ 3 ]= Height[ 5 ] - Interpolated[ 3 ];
	}

	if ( Interpolated[ 4 ] >= Height[ 7 ] )
	{
		Interpolated[ 4 ] = Interpolated[ 4 ] - Height[ 7 ];
	}
	else
	{
		Interpolated[ 4 ]= Height[ 7 ] - Interpolated[ 4 ];
	}

	return (float)( _Max( Interpolated[ 0 ], _Max( Interpolated[ 1 ], _Max( Interpolated[ 2 ], _Max( Interpolated[ 3 ], Interpolated[ 4 ] ) ) ) ) );
}

//-----------------------------------------------------------------------
float PagingLandScapePageData2D_HeightField::_decodeTC(int encoded)
{
	float value = (float)(encoded + 0.5f)/255;
	return value * (input_max - input_min) + input_min;
}

} //namespace

⌨️ 快捷键说明

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