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

📄 ogreipltexture_splatting1.cpp

📁 使用stl技术,(还没看,是听说的)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************
	OgreIPLTexture_Splatting1.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_Splatting1.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_Splatting1::IPLTexture_Splatting1( 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;
}

//-----------------------------------------------------------------------
IPLTexture_Splatting1::~IPLTexture_Splatting1( void )
{
	BaseTex = 0;
	Alpha1Tex = 0;
	Alpha2Tex = 0;
	Alpha3Tex = 0;
	Alpha4Tex = 0;
}

//-----------------------------------------------------------------------
void IPLTexture_Splatting1::setupMaterial( int pageCount, IPLVirtualPageWindow* vw )
{
	// create material name based on page count
	String baseName = "IPLSM_Splatting1." + StringConverter::toString( pageCount );
	if ( mMaterial->getName( ) == "BaseWhite" )
	{
		// Create a new texture using the template material
		mMaterial = reinterpret_cast< Material* >(MaterialManager::getSingleton( ).getByName( "IPLSM_Splatting1" ) );
		mMaterial = mMaterial->clone( baseName );
	}

	uint tileSize = 256;
	uint tileMemorySize = tileSize * tileSize * 4;

	// Create the base image
	DataChunk dc;
	dc.allocate(tileMemorySize);
	// Assign the texture to the alpha map
	BaseImage.loadRawData(dc, tileSize, tileSize, PF_A8R8G8B8);
	dc.clear();

	// Create the 1st alpha map
	dc.allocate(tileMemorySize);
	memset (dc.getPtr (), 0, tileMemorySize);
	AlphaMap1.loadRawData(dc, tileSize, tileSize, PF_A8R8G8B8);
	dc.clear();

	// Create the 2nd alpha map
	dc.allocate(tileMemorySize);
	memset (dc.getPtr (), 0, tileMemorySize);
	AlphaMap2.loadRawData(dc, tileSize, tileSize, PF_A8R8G8B8);
	dc.clear();

	// Create the 3rd alpha map
	dc.allocate(tileMemorySize);
	memset (dc.getPtr (), 0, tileMemorySize);
	AlphaMap3.loadRawData(dc, tileSize, tileSize, PF_A8R8G8B8);
	dc.clear();

	// Create the 4th alpha map
	dc.allocate(tileMemorySize);
	memset (dc.getPtr (), 0, tileMemorySize);
	AlphaMap4.loadRawData(dc, tileSize, tileSize, PF_A8R8G8B8);
	dc.clear();

	String texname = baseName + String(".Base");
	BaseTex = 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);

	uint pass_number = 1;
	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_Splatting1::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_Splatting1::_loadMaterial( void )
{
	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();
	uchar *AlphaData1 = AlphaMap1.getData();
	uchar *AlphaData2 = AlphaMap2.getData();
	uchar *AlphaData3 = AlphaMap3.getData();
	uchar *AlphaData4 = AlphaMap4.getData();

	Real alpha1, alpha2, alpha3, alpha4;
	bool bAlpha1NotUsed, bAlpha2NotUsed, bAlpha3NotUsed, bAlpha4NotUsed;
	bAlpha1NotUsed = bAlpha2NotUsed = bAlpha3NotUsed = bAlpha4NotUsed = true;
	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] = color.r * 255;
		BaseData[j*4+1] = color.g * 255;
		BaseData[j*4+2] = color.b * 255;
		BaseData[j*4+3] = ( 1.0 - ( alpha1 + alpha2 + alpha3 + alpha4 ) ) * 255 ; // Opaque

		// Generate the alpha map 1
		AlphaData1[j*4+3] = alpha1 * 255;
		if (bAlpha1NotUsed && alpha1 - 0.05f > 0)
		{
			bAlpha1NotUsed  = false;
		}

		// Generate the alpha map 2
		AlphaData2[j*4+3] = alpha2 * 255;
		if (bAlpha2NotUsed && alpha2 - 0.05f > 0)
		{
			bAlpha2NotUsed  = false;
		}

		// Generate the alpha map 3
		AlphaData3[j*4+3] = alpha3 * 255;
		if (bAlpha3NotUsed && alpha3 - 0.05f > 0)
		{
			bAlpha3NotUsed  = false;
		}

		// Generate the alpha map 4
		AlphaData4[j*4+3] = alpha4 * 255;
		if (bAlpha4NotUsed && alpha4 - 0.05f > 0 )
		{
			bAlpha4NotUsed  = false;
		}
	}
	BaseTex->loadImage( BaseImage );
	Alpha1Tex->loadImage( AlphaMap1 );
	Alpha2Tex->loadImage( AlphaMap2 );
	Alpha3Tex->loadImage( AlphaMap3 );
	Alpha4Tex->loadImage( AlphaMap4 );
}

//-----------------------------------------------------------------------
void IPLTexture_Splatting1::_unloadMaterial( void )
{
	if ( mMaterial != 0 )
	{
		mMaterial->unload();
		// We can磘 destroy de material here.
		//mMaterial -> destroy();
		//delete mMaterial;
		//mMaterial = 0;
	}
}

⌨️ 快捷键说明

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