📄 ogrepaginglandscapetexture_basetexture.cpp
字号:
/***************************************************************************
OgrePagingLandScapeTexture_BaseTexture.cpp - description
-------------------
begin : Mon Apr 26 2004
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 "OgreStringConverter.h"
#include "OgreMaterialManager.h"
#include "OgreTextureManager.h"
#include "OgreTechnique.h"
#include "OgrePass.h"
#include "OgrePagingLandScapeOptions.h"
#include "OgrePagingLandScapeTexture.h"
#include "OgrePagingLandScapeTexture_BaseTexture.h"
#include "OgrePagingLandScapeData2DManager.h"
namespace Ogre
{
PagingLandScapeTexture_BaseTexture::PagingLandScapeTexture_BaseTexture() : PagingLandScapeTexture()
{
}
//-----------------------------------------------------------------------
PagingLandScapeTexture_BaseTexture::~PagingLandScapeTexture_BaseTexture()
{
}
//-----------------------------------------------------------------------
void PagingLandScapeTexture_BaseTexture::_loadMaterial()
{
if ( mMaterial == 0 )
{
mMaterial = reinterpret_cast<Material *>(MaterialManager::getSingleton().getByName("BaseMaterial"));
// Create a new texture using the base image
String commonName = StringConverter::toString(mDataX) + String(".") + StringConverter::toString(mDataZ);
String matname = String("SplattingMaterial.") + commonName;
mMaterial = mMaterial->clone(matname);
// Create the base image
DataChunk dc;
dc.allocate(256 * 256 * 4);
// Asign the texture to the alpha map
Image BaseImage;
BaseImage.loadRawData(dc, 256, 256, PF_A8R8G8B8);
dc.clear();
ColourValue color;
// This texture will be used as a base color for the terrain, it will fake the splat for distant renderables.
uchar *BaseData = BaseImage.getData();
Real alpha1, alpha2, alpha3, alpha4;
for (int j = 0; j < 256 * 256; j++)
{
// Generate the base texture
_BuildPoint( color, j, alpha1, alpha2, alpha3, alpha4 );
// R G B A is the format to add the colours
BaseData[j*4+0] = uchar (color.r * 255);
BaseData[j*4+1] = uchar (color.g * 255);
BaseData[j*4+2] = uchar (color.b * 255);
BaseData[j*4+3] = uchar (( 1.0 - ( alpha1 + alpha2 + alpha3 + alpha4 ) ) * 255 ); // Opaque
}
String texname = String("SplattingBase.") + commonName;
TextureManager::getSingleton().loadImage(texname, BaseImage, TEX_TYPE_2D, 0, 1.0, 1 + 2);
// assing this texture to the material
mMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setTextureName(texname);
mMaterial->getTechnique(1)->getPass(0)->getTextureUnitState(0)->setTextureName(texname);
// Now that we have all the resources in place, we load the material
mMaterial->load();
mMaterial->setLightingEnabled( PagingLandScapeOptions::getSingleton().lit );
}
}
//-----------------------------------------------------------------------
void PagingLandScapeTexture_BaseTexture::_unloadMaterial()
{
if ( mMaterial != 0 )
{
mMaterial->unload();
// We can磘 destroy de material here.
//mMaterial -> destroy();
//delete mMaterial;
//mMaterial = 0;
}
}
//-----------------------------------------------------------------------
void PagingLandScapeTexture_BaseTexture::_BuildPoint(ColourValue& out, const int j, Real& alpha1, Real& alpha2, Real& alpha3, Real& alpha4)
{
// Init the colour
out = ColourValue(0,0,0,0);
alpha1 = 0;
alpha2 = 0;
alpha3 = 0;
alpha4 = 0;
// Calculate the current Point position
int x = j % 256;
int z = j / 256;
// Ask for the current height value and the 8 sourrounding values
Real height[9];
height[0] = PagingLandScapeData2DManager::getSingleton().getHeightAtPage( mDataX, mDataZ, x - 1, z - 1 ); // Top-Left
height[1] = PagingLandScapeData2DManager::getSingleton().getHeightAtPage( mDataX, mDataZ, x, z - 1 ); // Top-Center
height[2] = PagingLandScapeData2DManager::getSingleton().getHeightAtPage( mDataX, mDataZ, x + 1, z - 1 ); // Top-Right
height[3] = PagingLandScapeData2DManager::getSingleton().getHeightAtPage( mDataX, mDataZ, x - 1, z ); // Left
height[4] = PagingLandScapeData2DManager::getSingleton().getHeightAtPage( mDataX, mDataZ, x, z ); // Current Point
height[5] = PagingLandScapeData2DManager::getSingleton().getHeightAtPage( mDataX, mDataZ, x + 1, z ); // Right
height[6] = PagingLandScapeData2DManager::getSingleton().getHeightAtPage( mDataX, mDataZ, x - 1, z + 1 ); // Botton-Left
height[7] = PagingLandScapeData2DManager::getSingleton().getHeightAtPage( mDataX, mDataZ, x, z + 1 ); // Botton-Center
height[8] = PagingLandScapeData2DManager::getSingleton().getHeightAtPage( mDataX, mDataZ, x + 1, z + 1 ); // Botton-Right
// Weight( pt1 , pt2 ) = 1 - DistanceSquared(pt1,pt2) / (1.75)^2
//The slopy test
Real sloppy[8];
Real dx = PagingLandScapeOptions::getSingleton().scale.x;
Real dz = PagingLandScapeOptions::getSingleton().scale.z;
sloppy[0] = Math::Abs ( height[0] - height[4] ) / ( dx + dz );
sloppy[1] = Math::Abs ( height[1] - height[4] ) / ( dz );
sloppy[2] = Math::Abs ( height[2] - height[4] ) / ( dx + dz );
sloppy[3] = Math::Abs ( height[3] - height[4] ) / ( dx );
sloppy[4] = Math::Abs ( height[5] - height[4] ) / ( dx );
sloppy[5] = Math::Abs ( height[6] - height[4] ) / ( dx + dz );
sloppy[6] = Math::Abs ( height[7] - height[4] ) / ( dz );
sloppy[7] = Math::Abs ( height[8] - height[4] ) / ( dx + dz );
//snow = 0
//sand = 1
//grass = 2
//rock = 3
for ( int i = 0; i < 7; i++ )
{
if ( height[4] < PagingLandScapeOptions::getSingleton().matHeight[0] )
{
if ( sloppy[i] < 0.20 )
{
// grass-grass
_InterpolateColour( out, 1.0, 2, 2 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 1.0, 2, 2 );
}
if ( sloppy[i] >= 0.15 && sloppy[i] < 0.40 )
{
// sand-grass
_InterpolateColour( out, 0.25, 1, 2 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 0.25, 1, 2 );
}
if ( sloppy[i] >= 0.30 && sloppy[i] < 0.65 )
{
// sand-sand
_InterpolateColour( out, 1.0, 1, 1 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 1.0, 1, 1 );
}
if ( sloppy[i] >= 0.55 && sloppy[i] < 0.75 )
{
// sand-rock
_InterpolateColour( out, 0.75, 1, 3 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 0.75, 1, 3 );
}
if ( sloppy[i] >= 0.70 )
{
// rock-rock
_InterpolateColour( out, 1.0, 3, 3 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 1.0, 3, 3 );
}
}
else if ( height[4] >= PagingLandScapeOptions::getSingleton().matHeight[0] && height[4] < PagingLandScapeOptions::getSingleton().matHeight[1] )
{
if ( sloppy[i] < 0.15 )
{
// grass-snow
_InterpolateColour( out, 0.25, 2, 0 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 0.25, 2, 0 );
}
if ( sloppy[i] >= 0.10 && sloppy[i] < 0.45 )
{
// snow-sand
_InterpolateColour( out, 0.65, 0, 1 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 0.65, 0, 1 );
}
if ( sloppy[i] >= 0.25 && sloppy[i] < 0.65 )
{
// snow-rock
_InterpolateColour( out, 0.5, 0, 3 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 0.5, 0, 3 );
}
if ( sloppy[i] >= 0.50 && sloppy[i] < 0.75 )
{
// snow-rock
_InterpolateColour( out, 0.75, 0, 3 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 0.75, 0, 3 );
}
if ( sloppy[i] >= 0.70 )
{
// rock-rock
_InterpolateColour( out, 1.0, 3, 3 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 1.0, 3, 3 );
}
}
else
{
if ( sloppy[i] < 0.15 )
{
// snow-snow
_InterpolateColour( out, 1.0, 0, 0 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 1.0, 0, 0 );
}
if ( sloppy[i] >= 0.10 && sloppy[i] < 0.45 )
{
// snow-sand
_InterpolateColour( out, 0.35, 0, 1 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 0.35, 0, 1 );
}
if ( sloppy[i] >= 0.25 && sloppy[i] < 0.65 )
{
// snow-rock
_InterpolateColour( out, 0.5, 0, 3 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 0.5, 0, 3 );
}
if ( sloppy[i] >= 0.50 && sloppy[i] < 0.75 )
{
// snow-rock
_InterpolateColour( out, 0.75, 0, 3 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 0.75, 0, 3 );
}
if ( sloppy[i] >= 0.70 )
{
// rock-rock
_InterpolateColour( out, 1.0, 3, 3 );
_InterpolateAlpha( alpha1, alpha2, alpha3, alpha4, 1.0, 3, 3 );
}
}
}
}
//-----------------------------------------------------------------------
void PagingLandScapeTexture_BaseTexture::_InterpolateColour(ColourValue& out, const Real percentaje, const int index1, const int index2)
{
Real tmp = 1 - percentaje;
out.r = ( out.r + ( percentaje * PagingLandScapeOptions::getSingleton().matColor[index1].r + tmp * PagingLandScapeOptions::getSingleton().matColor[index2].r ) ) / 2;
out.g = ( out.g + ( percentaje * PagingLandScapeOptions::getSingleton().matColor[index1].g + tmp * PagingLandScapeOptions::getSingleton().matColor[index2].g ) ) / 2;
out.b = ( out.b + ( percentaje * PagingLandScapeOptions::getSingleton().matColor[index1].b + tmp * PagingLandScapeOptions::getSingleton().matColor[index2].b ) ) / 2;
}
//-----------------------------------------------------------------------
void PagingLandScapeTexture_BaseTexture::_InterpolateAlpha(Real& alpha1, Real& alpha2, Real& alpha3, Real& alpha4, const Real percentaje, const int index1, const int index2)
{
if ( index1 == index2 )
{
if ( index1 == 0 )
{
alpha1 += 1.0;
}
else if ( index1 == 1 )
{
alpha2 += 1.0;
}
else if ( index1 == 2 )
{
alpha3 += 1.0;
}
else
{
alpha4 += 1.0;
}
}
else
{
if ( index2 == 0 )
{
alpha1 += percentaje;
}
else if ( index2 == 1 )
{
alpha2 += percentaje;
}
else if ( index2 == 2 )
{
alpha3 += percentaje;
}
else
{
alpha4 += percentaje;
}
if ( index1 == 0 )
{
alpha1 += 1.0 - percentaje;
}
else if ( index1 == 1 )
{
alpha2 += 1.0 - percentaje;
}
else if ( index1 == 2 )
{
alpha3 += 1.0 - percentaje;
}
else
{
alpha4 += 1.0 - percentaje;
}
}
//snow = 0
//sand = 1
//grass = 2
//rock = 3
alpha1 *= ( 4.0 / 5.0 );
alpha2 *= ( 3.0 / 5.0 );
alpha3 *= ( 4.0 / 5.0 );
alpha4 *= ( 2.0 / 5.0 );
// Normalize
Real total = ( alpha1 + alpha2 + alpha3 + alpha4 ) * 2.0;
alpha1 = alpha1 / total;
alpha2 = alpha2 / total;
alpha3 = alpha3 / total;
alpha4 = alpha4 / total;
}
} //namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -