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

📄 ogrepaginglandscapetexture_splatting.cpp

📁 使用stl技术,(还没看,是听说的)
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************
OgrePagingLandScapeTexture_Splatting.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 "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_Splatting.h"
#include "OgrePagingLandScapeData2DManager.h"


namespace Ogre
{

PagingLandScapeTexture_Splatting::PagingLandScapeTexture_Splatting() : PagingLandScapeTexture()
{
}

//-----------------------------------------------------------------------
PagingLandScapeTexture_Splatting::~PagingLandScapeTexture_Splatting()
{
}

//-----------------------------------------------------------------------
void PagingLandScapeTexture_Splatting::_loadMaterial()
{
	if ( mMaterial == 0 )
	{
		mMaterial = reinterpret_cast<Material *>(MaterialManager::getSingleton().getByName("SplattingMaterial"));

		// Create a new texture using the base image
		String commonName = StringConverter::toString(mDataZ) + String(".") + StringConverter::toString(mDataX);

		String matname = String("SplattingMaterial.") + commonName;
		mMaterial = mMaterial->clone(matname);

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

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

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

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

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

		// Create the 4th alpha map
        dc.allocate(tileMemorySize);
        memset (dc.getPtr (), 0, tileMemorySize);
		Image AlphaMap4;
		AlphaMap4.loadRawData(dc, tileSize, tileSize, 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();
		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] = uchar (color.r * 255.0f);
			BaseData[j*4+1] = uchar (color.g * 255.0f);
			BaseData[j*4+2] = uchar (color.b * 255.0f);
			BaseData[j*4+3] = uchar (( 1.0 - ( alpha1 + alpha2 + alpha3 + alpha4 ) ) * 255); // Opaque

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

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

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

			// Generate the alpha map 4
			AlphaData4[j*4+3] = uchar (alpha4 * 255.0f);
            if (bAlpha4NotUsed && alpha4 - 0.05f > 0 )
                bAlpha4NotUsed  = false;
		}

		String texname = String("SplattingBase.") + commonName;
		Texture *tex = 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 = String("SplattingAlpha1.") + commonName;
            tex = 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 = String("SplattingAlpha2.") + commonName;
            tex = 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 = String("SplattingAlpha3.") + commonName;
            tex = 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 = String("SplattingAlpha4.") + commonName;
            tex = 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( PagingLandScapeOptions::getSingleton().lit );
	}
}

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

//-----------------------------------------------------------------------
void PagingLandScapeTexture_Splatting::_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.0f;
	alpha2 = 0.0f;
	alpha3 = 0.0f;

⌨️ 快捷键说明

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