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

📄 ogrepaginglandscapedata2d_heightfieldtc.cpp

📁 使用stl技术,(还没看,是听说的)
💻 CPP
字号:
/***************************************************************************
  OgrePagingLandScapeData2D_HeightFieldTC.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 "OgreVector3.h"
#include "OgreColourValue.h"

#include "OgreImage.h"
#include "OgreStringConverter.h"

#include "OgreException.h"

#include "OgrePagingLandScapeData2D.h"
#include "OgrePagingLandScapeOptions.h"

#include "OgrePagingLandScapeData2D_HeightFieldTC.h"


namespace Ogre
{

PagingLandScapeData2D_HeightFieldTC::PagingLandScapeData2D_HeightFieldTC()
: PagingLandScapeData2D()
{
	mImage = 0;
	input_max =  3000.0f;
    input_min = 0.0f;
    mMaxheight = _decodeTC (256) * PagingLandScapeOptions::getSingleton().scale.y;
}

//-----------------------------------------------------------------------
PagingLandScapeData2D_HeightFieldTC::~PagingLandScapeData2D_HeightFieldTC()
{
	if ( mImage )
		delete mImage;
}
//-----------------------------------------------------------------------
ColourValue PagingLandScapeData2D_HeightFieldTC::getBase (const float mX, const float mZ)
{
    return ColourValue::White;
}

//-----------------------------------------------------------------------
ColourValue PagingLandScapeData2D_HeightFieldTC::getCoverage (const float mX, const float mZ)
{
    return ColourValue::White;
}
//-----------------------------------------------------------------------
Vector3 PagingLandScapeData2D_HeightFieldTC::getNormalAt (const float x, const float z)
{
    if ( mImage != 0 )
    {
        uint Pos = uint (( z * mSize )*mBpp + x*mBpp);//4 bytes (mImage is RGBA)

        if ( mMax > Pos )
        {
            const float normalscale = 1.0f / 127.0f;
            return Vector3 (((float)(mImage->getData()[Pos + 0]) - 128.0f) * normalscale, 
                            ((float)(mImage->getData()[Pos + 0]) - 128.0f) * normalscale,
                            ((float)(mImage->getData()[Pos + 0]) - 128.0f) * normalscale);
        }
        else
        {
            return Vector3::UNIT_Y;
        }	
    }
    else
    {
        return Vector3::UNIT_Y;
    }	

}
//-----------------------------------------------------------------------
void PagingLandScapeData2D_HeightFieldTC::_load( const float mX, const float mZ )
{
    if ( mImage == 0 )
     {
        mImage = new Image();

        mImage -> load( PagingLandScapeOptions::getSingleton().landscape_filename + ".HN." + StringConverter::toString( mZ ) + "." + 
            StringConverter::toString( mX ) + "." + PagingLandScapeOptions::getSingleton().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, "PagingLandScapeData2D_HeightField::_load" );
        }

        mBpp = mImage->getNumElemBytes(mImage->getFormat ());
        if ( mBpp != 4 )
        {
            Except( Exception::ERR_INVALIDPARAMS, "Error: Image is not a RGBA image.(4 bytes, 32 bits)", 
                "PagingLandScapeData2D_HeightField::_load" );
        }

        mSize = PagingLandScapeOptions::getSingleton().PageSize;
        if ( mSize != mImage->getWidth() )
        {
            Except ( Exception::ERR_INVALIDPARAMS, "Error: Declared World size <> Height Map Size.", "PagingLandScapeData2D_HeightField::_load" );
        }
        mMax = mSize * mImage->getHeight() * mBpp + 1;

        mMaxArrayPos = mSize * mImage->getHeight();
        mHeightData = new Real[mMaxArrayPos];
        uint j = 0;
        Real scale = PagingLandScapeOptions::getSingleton().scale.y;
        mMaxheight = 0.0f;
        for (uint i = 0; i < mMax - 1;  i += mBpp )
        {  
            Real h =  (Real) (mImage->getData()[ i + (mBpp - 1)]) * scale;
            mMaxheight = std::max ( h, mMaxheight);
            mHeightData[j++] = h;
        }
    }
    else
    {
        String err = "Error: 2D Data already loaded ";
        Except( Exception::ERR_INTERNAL_ERROR, err, "PagingLandScapeData2D_HeightField::_load" );
    }
}
//-----------------------------------------------------------------------
void PagingLandScapeData2D_HeightFieldTC::_load( )
{
	if ( mImage == 0 )
	{
		mImage = new Image();

		mImage -> load( PagingLandScapeOptions::getSingleton().landscape_filename + 
            "." + PagingLandScapeOptions::getSingleton().landscape_extension );

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

        mBpp = mImage->getNumElemBytes(mImage->getFormat ());
        if (mBpp != 1)
		{
			Except( Exception::ERR_INVALIDPARAMS, "Error: Image is not a grayscale image.(1 byte, 8 bits)", 
                "PagingLandScapeData2D_HeightFieldTC::_load" );
		}

		mSize = mImage->getWidth();
        mMax = mSize * mImage->getHeight() * mBpp + 1;

        mMaxArrayPos = mSize * mImage->getHeight();
        mHeightData = new Real[mMaxArrayPos];
        uint j = 0;
        Real scale = PagingLandScapeOptions::getSingleton().scale.y;
        mMaxheight = 0.0f;
        for (uint i = 0; i < mMax - 1;  i += mBpp )
        {  
            Real h =  (Real) (mImage->getData()[ i + (mBpp - 1)]) * scale;
            mMaxheight = std::max ( h, mMaxheight);
            mHeightData[j++] = h;
        }
	}
	else
	{
		String err = "Error: 2D Data already loaded ";
		Except( Exception::ERR_INTERNAL_ERROR, err, "PagingLandScapeData2D_HeightFieldTC::_load" );
	}
}
//-----------------------------------------------------------------------
void PagingLandScapeData2D_HeightFieldTC::_load(Image *newHeightmap )
{
	if ( mImage == 0 )
	{
		mImage = new Image (*newHeightmap);

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

        mBpp = mImage->getNumElemBytes(mImage->getFormat ());
        if (mBpp != 1)
		{
			Except( Exception::ERR_INVALIDPARAMS, "Error: Image is not a grayscale image.(1 byte, 8 bits)", 
                "PagingLandScapeData2D_HeightFieldTC::_load" );
		}

		mSize = mImage->getWidth();
        mMax = mSize * mImage->getHeight() * mBpp + 1;

        mMaxArrayPos = mSize * mImage->getHeight();
        mHeightData = new Real[mMaxArrayPos];
        uint j = 0;
        Real scale = PagingLandScapeOptions::getSingleton().scale.y;
        mMaxheight = 0.0f;
        for (uint i = 0; i < mMax - 1;  i += mBpp )
        {  
            Real h =  (Real) (mImage->getData()[ i + (mBpp - 1)]) * scale;
            mMaxheight = std::max ( h, mMaxheight);
            mHeightData[j++] = h;
        }
	}
	else
	{
		String err = "Error: 2D Data already loaded ";
		Except( Exception::ERR_INTERNAL_ERROR, err, "PagingLandScapeData2D_HeightFieldTC::_load" );
	}
}

//-----------------------------------------------------------------------
void PagingLandScapeData2D_HeightFieldTC::_unload()
{
	if ( mImage != 0 )
	{
		delete mImage;
		mImage = 0;
	}
}

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

} //namespace

⌨️ 快捷键说明

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