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

📄 ogrepaginglandscapedata2d_heightfield.cpp

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

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

#include "OgreException.h"

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

#include "OgrePagingLandScapeData2D_HeightField.h"

namespace Ogre
{

PagingLandScapeData2D_HeightField::PagingLandScapeData2D_HeightField()
: PagingLandScapeData2D()
{
    mImage = 0;
    mCoverage = 0;
    mBase = 0;
    mMaxheight = 256.0f * PagingLandScapeOptions::getSingleton().scale.y;
}

//-----------------------------------------------------------------------
PagingLandScapeData2D_HeightField::~PagingLandScapeData2D_HeightField()
{
	if ( mImage )
        delete mImage;
    if ( mCoverage )
        delete mCoverage;
    if ( mBase )
        delete mBase; 
}
//-----------------------------------------------------------------------
ColourValue PagingLandScapeData2D_HeightField::getBase (const float mX, const float mZ)
{
    if ( mBase != 0 )
    {
        uint Pos = uint (( mZ * (mBase->getWidth()) )*4 + mX*4);//4 bytes (mImage is RGBA)
        if ( mBase->getSize () > Pos )
        {
            const Real divider = 1.0f / 255.0f;
            return ColourValue((Real) mBase->getData()[ Pos + 0] * divider,
                                (Real) mBase->getData()[ Pos + 1] * divider,
                                (Real) mBase->getData()[ Pos + 2] * divider,
                                (Real) mBase->getData()[ Pos + 3] * divider);
        }
        else
        {	
            return ColourValue::White;
        }
    }
    else
    {
        return ColourValue::White;
    }
}

//-----------------------------------------------------------------------
ColourValue PagingLandScapeData2D_HeightField::getCoverage (const float mX, const float mZ)
{
    if ( mCoverage != 0 )
     {
        uint Pos = uint (( mZ * (mCoverage->getWidth()) )*4 + mX*4);//4 bytes (mImage is RGBA)
        if ( mCoverage->getSize () > Pos )
        {
            const Real divider = 1.0f / 255.0f;
            return ColourValue((Real) mCoverage->getData()[ Pos + 0] * divider,
                               (Real) mCoverage->getData()[ Pos + 1] * divider,
                               (Real) mCoverage->getData()[ Pos + 2] * divider,
                               (Real) mCoverage->getData()[ Pos + 3] * divider);
        }
        else
        {	
            return ColourValue::White;
        }
     }
    else
     {
        return ColourValue::White;
    }
}
//-----------------------------------------------------------------------
Vector3 PagingLandScapeData2D_HeightField::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_HeightField::_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;
	    
	    if (PagingLandScapeOptions::getSingleton().coverage_vertex_color)
	    { 
		mCoverage = new Image();
		mCoverage -> load( PagingLandScapeOptions::getSingleton().landscape_filename + 
				   ".Coverage." + 
				   StringConverter::toString( mZ ) + "." + 
				   StringConverter::toString( mX ) + "." + 
				   PagingLandScapeOptions::getSingleton().landscape_extension );
		
	    }
	    if (PagingLandScapeOptions::getSingleton().base_vertex_color)
	    {
		mBase = new Image();
		mBase -> load( PagingLandScapeOptions::getSingleton().landscape_filename + 
			       ".Base." + 
			       StringConverter::toString( mZ ) + "." + 
			       StringConverter::toString( mX ) + "." + 
			       PagingLandScapeOptions::getSingleton().landscape_extension );
	    }
	    
	    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_HeightField::_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";
			Except( Exception::ERR_INVALIDPARAMS, err, "PagingLandScapeData2D_HeightField::_load" );
		}

        mBpp = mImage->getNumElemBytes(mImage->getFormat ());
		if (mBpp != 1)
		{
			Except( Exception::ERR_INVALIDPARAMS, "Error: Image is not a greyscale image.(1 byte, 8 bits)", 
                "PagingLandScapeData2D_HeightField::_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_HeightField::_load" );
	}
}
//-----------------------------------------------------------------------
void PagingLandScapeData2D_HeightField::_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 arready loaded ";
		Except( Exception::ERR_INTERNAL_ERROR, err, "PagingLandScapeData2D_HeightFieldTC::_load" );
	}
}
//-----------------------------------------------------------------------
void PagingLandScapeData2D_HeightField::_unload()
{
	if ( mImage != 0 )
	{
		delete mImage;
		mImage = 0;
    }	
    if ( mCoverage != 0 )
    {
        delete mCoverage; 
        mCoverage = 0;
    }
    if ( mBase != 0 )
    {
        delete mBase;
        mBase = 0;
    }
}
} //namespace

⌨️ 快捷键说明

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