📄 ogreipltexture_splatting2.cpp
字号:
/***************************************************************************
OgreIPLTexture_Splatting2.cpp - description
-------------------
begin : Mon Apr 16 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 "OgreIPLTexture_Splatting2.h"
#include "OgreIPLOptions.h"
#include "OgreIPLSceneManager.h"
#include <OgreStringConverter.h>
#include <OgreMaterialManager.h>
#include <OgreTextureManager.h>
#include <OgreTechnique.h>
#include <OgrePass.h>
namespace Ogre
{
IPLTexture_Splatting2::IPLTexture_Splatting2( IPLPage* p )
: IPLTexture( p )
{
BaseTex = 0;
Alpha1Tex = 0;
Alpha2Tex = 0;
Alpha3Tex = 0;
Alpha4Tex = 0;
//TODO: this is not used now, what should we do with it?
bAlpha1NotUsed = bAlpha2NotUsed = bAlpha3NotUsed = bAlpha4NotUsed = false;
Bpp = 4;//one day Ogre will support PF_A8 Texture format => 1;
pxl = PF_A8R8G8B8;//this day will come : PF_A8;
}
//-----------------------------------------------------------------------
IPLTexture_Splatting2::~IPLTexture_Splatting2( void )
{
BaseTex = 0;
Alpha1Tex = 0;
Alpha2Tex = 0;
Alpha3Tex = 0;
Alpha4Tex = 0;
}
//-----------------------------------------------------------------------
void IPLTexture_Splatting2::setupMaterial( int pageCount, IPLVirtualPageWindow* vw )
{
// create material name based on page count
String baseName = String( "IPLSM_Splatting2." ) + StringConverter::toString( pageCount );
if ( mMaterial->getName( ) == "BaseWhite" )
{
// Create a new texture using the template material
mMaterial = reinterpret_cast< Material * >(MaterialManager::getSingleton( ).getByName( "IPLSM_Splatting2" ) );
mMaterial = mMaterial->clone( baseName );
}
DataChunk dc;
// Create the base image
//TODO: En este punto no tenemos los datos de commonName dado que la pagina no esta asignada
// BaseImage.load( IPLOptions::getSingleton( ).landscape_filename + ".Color." + commonName + "." + IPLOptions::getSingleton( ).landscape_extension );
// Create the 1st alpha map
dc.allocate( 256 * 256 * Bpp );
memset( dc.getPtr( ), 0, 256 * 256 * Bpp );
AlphaMap1.loadRawData( dc, 256, 256, pxl );
dc.clear( );
// Create the 2nd alpha map
dc.allocate( 256 * 256 * 4 );
memset( dc.getPtr( ), 0, 256 * 256 * Bpp );
AlphaMap2.loadRawData( dc, 256, 256, pxl );
dc.clear( );
// Create the 3rd alpha map
dc.allocate( 256 * 256 * Bpp );
memset( dc.getPtr( ), 0, 256 * 256 * Bpp );
AlphaMap3.loadRawData( dc, 256, 256, pxl );
dc.clear();
// Create the 4th alpha map
dc.allocate( 256 * 256 * Bpp );
memset( dc.getPtr( ), 0, 256 * 256 * Bpp );
AlphaMap4.loadRawData( dc, 256, 256, pxl );
dc.clear( );
String texname = baseName + String( ".Base" );
BaseTex = TextureManager::getSingleton( ).loadImage( texname, BaseImage, TEX_TYPE_2D, 0, 1.0, 1 + 2 );
// assign this texture to the material
mMaterial->getTechnique( 0 )->getPass( 0 )->getTextureUnitState( 0 )->setTextureName( texname );
mMaterial->getTechnique( 1 )->getPass( 0 )->getTextureUnitState( 0 )->setTextureName( texname );
mMaterial->getTechnique( 1 )->getPass( 0 )->setLightingEnabled( IPLOptions::getSingleton( ).lit );
uint pass_number = 1;
//mMaterial->getTechnique( 0 )->removePass( 0 );
//pass_number--;
//bAlpha1NotUsed = bAlpha2NotUsed = bAlpha3NotUsed = bAlpha4NotUsed = false;
if ( bAlpha1NotUsed )
{
mMaterial->getTechnique( 0 )->removePass( pass_number );
}
else
{
// Create a new texture using the 1st alpha map
texname = baseName + String( ".Alpha1" );
Alpha1Tex = TextureManager::getSingleton( ).loadImage( texname, AlphaMap1, TEX_TYPE_2D, 0, 1.0, 1 + 2 );
// assing this texture to the material
mMaterial->getTechnique( 0 )->getPass( 1 )->getTextureUnitState( 0 )->setTextureName( texname );
pass_number++;
}
if ( bAlpha2NotUsed )
{
mMaterial->getTechnique( 0 )->removePass( pass_number );
}
else
{
// Create a new texture using the 2nd alpha map
texname = baseName + String( ".Alpha2" );
Alpha2Tex = TextureManager::getSingleton( ).loadImage( texname, AlphaMap2, TEX_TYPE_2D, 0, 1.0, 1 + 2 );
// assing this texture to the material
mMaterial->getTechnique( 0 )->getPass( pass_number )->getTextureUnitState( 0 )->setTextureName( texname );
pass_number++;
}
if ( bAlpha3NotUsed )
{
mMaterial->getTechnique( 0 )->removePass( pass_number );
}
else
{
// Create a new texture using the 3rd alpha map
texname = baseName + String( ".Alpha3" );
Alpha3Tex = TextureManager::getSingleton( ).loadImage( texname, AlphaMap3, TEX_TYPE_2D, 0, 1.0, 1 + 2 );
// assing this texture to the material
mMaterial->getTechnique( 0 )->getPass( pass_number )->getTextureUnitState( 0 )->setTextureName( texname );
pass_number++;
}
if ( bAlpha4NotUsed )
{
mMaterial->getTechnique( 0 )->removePass( pass_number );
}
else
{
// Create a new texture using the 4th alpha map
texname = baseName + String( ".Alpha4" );
Alpha4Tex = TextureManager::getSingleton( ).loadImage( texname, AlphaMap4, TEX_TYPE_2D, 0, 1.0, 1 + 2 );
// assing this texture to the material
mMaterial->getTechnique( 0 )->getPass( pass_number )->getTextureUnitState( 0 )->setTextureName( texname );
pass_number++;
}
// Now that we have all the resources in place, we load the material
mMaterial->load( );
mMaterial->setLightingEnabled( IPLOptions::getSingleton( ).lit );
}
//-----------------------------------------------------------------------
void IPLTexture_Splatting2::preLoad( void )
{
// Image newImage;
// for ( int z = 0; z < IPLOptions::getSingleton( ).virtual_window_height; z++ )
// {
// for ( int x = 0; x < IPLOptions::getSingleton( ).virtual_window_width; x++ )
// {
// // preload terrain base texture
// TextureManager::getSingleton( ).load( IPLOptions::getSingleton( ).mMapTextureName + "." + StringConverter::toString( z ) + "." +
// StringConverter::toString( x ) + "." + IPLOptions::getSingleton( ).mMapTextureExt, TEX_TYPE_2D );
// }
// }
}
//-----------------------------------------------------------------------
void IPLTexture_Splatting2::_loadMaterial( void )
{
ColourValue color;
Image CoverageImage;
String commonName = StringConverter::toString( mDataZ ) + String( "." ) + StringConverter::toString( mDataX );
CoverageImage.load( IPLOptions::getSingleton( ).landscape_filename + ".Coverage." + commonName + "." + IPLOptions::getSingleton( ).landscape_extension );
// This texture will be used as a base color for the terrain, it will fake the splat for distant renderables.
uchar *AlphaData1 = AlphaMap1.getData( );
uchar *AlphaData2 = AlphaMap2.getData( );
uchar *AlphaData3 = AlphaMap3.getData( );
uchar *AlphaData4 = AlphaMap4.getData( );
uchar *CoverageData = CoverageImage.getData( );
// fill the alpha map from the pre-calculated coverage maps.
uint index_pixel = 0 + ( Bpp - 1 ); // shift initial index on good channel
uint index_4pixel = 0;
for ( uint j = 0; j < 256 * 256; j++ )
{
AlphaData1[index_pixel] = CoverageData[index_4pixel];
if ( bAlpha1NotUsed && CoverageData[index_4pixel] - 0.05f > 0 )
bAlpha1NotUsed = false;
AlphaData2[index_pixel] = CoverageData[index_4pixel + 1];
if ( bAlpha2NotUsed && CoverageData[index_4pixel + 1] - 0.05f > 0 )
bAlpha2NotUsed = false;
AlphaData3[index_pixel] = CoverageData[index_4pixel + 2];
if ( bAlpha3NotUsed && CoverageData[index_4pixel + 2] - 0.05f > 0 )
bAlpha3NotUsed = false;
AlphaData4[index_pixel] = CoverageData[index_4pixel + 3];
if ( bAlpha4NotUsed && CoverageData[index_4pixel + 3] - 0.05f > 0 )
bAlpha4NotUsed = false;
index_pixel += Bpp;
index_4pixel += 4;
}
}
//-----------------------------------------------------------------------
void IPLTexture_Splatting2::_unloadMaterial( void )
{
if ( mMaterial != 0 )
{
mMaterial->unload( );
// We can磘 destroy de material here.
//mMaterial->destroy( );
//delete mMaterial;
//mMaterial = 0;
}
}
//-----------------------------------------------------------------------
void IPLTexture_Splatting2::_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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -