📄 maputil.cpp
字号:
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
/**
@file
MapUtil.cpp
@brief
Utility that split big map in tiles, pre-calculate splatting by
pre-calculating coverage map and pre-calculate normals.
*/
#include "MapUtil.h"
#include "OgrePagingLandScapeData2D.h"
#include "OgrePagingLandScapeData2D_HeightField.h"
#include "OgrePagingLandScapeData2D_HeightFieldTC.h"
#include "OgrePagingLandScapeData2D_Spline.h"
#include <iostream>
#include <fstream>
#include <vector>
#include "OgreCommon.h"
#include "OgreVector3.h"
#include "OgreILImageCodec.h"
#include "OgreILCodecs.h"
#include "OgreDynLibManager.h"
#include "OgreDynLib.h"
#include "OgreZipArchiveFactory.h"
#include "MapNormaler.h"
#include "MapSplatter.h"
#include "MapLighter.h"
#include "MapVis.h"
#include "MapHorizon.h"
#include "MapHorizon2.h"
#include "MapBlender.h"
#include "MapBlurrer.h"
#include "MapSplitter.h"
#include "MapInfinitizer.h"
#include "MapMergeModulater.h"
typedef void (*DLL_START_PLUGIN)(void);
typedef void (*DLL_STOP_PLUGIN)(void);
namespace Ogre
{
template<> MapUtil* Singleton<MapUtil>::ms_Singleton = 0;
//-----------------------------------------------------------------------
MapUtil* MapUtil::getSingletonPtr(void)
{
return ms_Singleton;
}
//-----------------------------------------------------------------------
MapUtil& MapUtil::getSingleton(void)
{
assert( ms_Singleton ); return ( *ms_Singleton );
}
//-----------------------------------------------------------------------
MapUtil::MapUtil()
{
mRoot = new Root("plugins.cfg", "ogre.cfg", "MapSplitter.log");
// Load resource paths from config file
ConfigFile cf;
cf.load("resources.cfg");
// Go through all settings in the file
ConfigFile::SettingsIterator i = cf.getSettingsIterator();
String typeName, archName;
while (i.hasMoreElements())
{
typeName = i.peekNextKey();
archName = i.getNext();
ResourceManager::addCommonArchiveEx( archName, typeName );
}
ArchiveManager::getSingleton().addCommonSearchPath("./");
mPagingLandScapeOptions = new PagingLandScapeOptions();
mPagingLandScapeOptions->load("maptool.cfg");
mFileName = PagingLandScapeOptions::getSingleton().landscape_filename +
"." +
PagingLandScapeOptions::getSingleton().landscape_extension;
mHeightMap = new Image ();
load (mFileName, mHeightMap);
mSize = (uint) mHeightMap->getSize ();
mHeight = (uint) mHeightMap->getHeight ();
mWidth = (uint) mHeightMap->getWidth ();
mSun = PagingLandScapeOptions::getSingleton().Sun;
mAmb = PagingLandScapeOptions::getSingleton().Amb;
mDiff = PagingLandScapeOptions::getSingleton().Diff;
mBlur = PagingLandScapeOptions::getSingleton().Blur;
}
//-----------------------------------------------------------------------
MapUtil::~MapUtil()
{
delete mHeightMap;
mHeightdata->unload();
delete mHeightdata;
delete mPagingLandScapeOptions;
delete mRoot;
}
//-----------------------------------------------------------------------
const Real MapUtil::getMaxTheoHeight ()
{
return maxHeight;
}
//-----------------------------------------------------------------------
const Real MapUtil::getHeight (const float x, const float z)
{
return mHeightdata->getHeight( x, z );
}
//-----------------------------------------------------------------------
uchar *MapUtil::getHeightMapData ()
{
return mHeightMap->getData ();
}
//-----------------------------------------------------------------------
void MapUtil::load(String filename, Image *ImageMap)
{
ImageMap->load (filename);
}
//-----------------------------------------------------------------------
const Vector3 MapUtil::getNormalAt(const float x, const float z)
{
return NormalMapper.getNormalAt (uint (x), uint (z));
}
//-----------------------------------------------------------------------
void MapUtil::process()
{
MapSplatter SplatMapper;
MapLighter LightMapper;
MapBlender Blender;
MapSplitter Splitter;
MapInfinitizer Infinitizer;
MapMergeModulater MergeModulater;
String file_name;
String pure_file_name;
String ext_name;
Splitter.init ();
if (PagingLandScapeOptions::getSingleton().InfiniteMap)
{
Infinitizer.Infinitize (mHeightMap,
uint (mHeightMap->getHeight () / 10),
0.0f,
0);
}
Real Blur = PagingLandScapeOptions::getSingleton().HeightMapBlurFactor;
if (Blur > 0.0f)
{
MapBlurrer Blurrer;
// blur All map...
Blurrer.gaussianBlur (mHeightMap, int (Blur));
}
// Get codec according to filename extension
size_t pos = mFileName.find_last_of(".");
pure_file_name = mFileName.substr(0, pos + 1);
ext_name = mFileName.substr(pos + 1);
if (ext_name.length() == 0)
ext_name = PagingLandScapeOptions::getSingleton().landscape_extension;
//
// Split Heightmap in 2^n + 1 part
//
if (PagingLandScapeOptions::getSingleton().HeightMap)
Splitter.saveAndSpaceAndSplitMap (mFileName, mHeightMap);
//
// Load Heightmap data according to specialized loader
//
if ( PagingLandScapeOptions::getSingleton().data2DFormat == "HeightField" )
{
mHeightdata = new PagingLandScapeData2D_HeightField();
}
else if ( PagingLandScapeOptions::getSingleton().data2DFormat == "HeightFieldTC" )
{
mHeightdata = new PagingLandScapeData2D_HeightFieldTC();
}
else if ( PagingLandScapeOptions::getSingleton().data2DFormat == "SplineField" )
{
// needs to store spline value ???
// why spline values can't come from image instead of Rand ?
//mHeightdata = new PagingLandScapeData2D_Spline();
Except( Exception::ERR_INVALIDPARAMS,
"SplineField not supported!",
"MapUtil::MapUtil" );
}
else
{
Except( Exception::ERR_INVALIDPARAMS,
"PageData2D not supplied!",
"MapUtil::MapUtil" );
}
maxHeight = mHeightdata->getMaxHeight ();// get theoric max height
mHeightdata->load (mHeightMap);
mMax = uint ( mHeightdata->getMaxHeight ());
//
// Split Normal Map in 2^n + 1 part
//
if (PagingLandScapeOptions::getSingleton().NormalMap)
{
Splitter.saveAndSpaceAndSplitMap (pure_file_name + String("N.") + ext_name,
NormalMapper.getNormalMap ());
}
//
// Split Normal Map + HeightMap in alpha channel in 2^n + 1 part
//
if (PagingLandScapeOptions::getSingleton().HeightNormalMap)
{
// does modify NormalMap.
mNormalMap = MergeModulater.MergeGrayAndRgbInRgba (mHeightMap,
NormalMapper.getNormalMap ());
Splitter.saveAndSpaceAndSplitMap (pure_file_name + String("HN.") + ext_name,
mNormalMap);
delete mNormalMap;
}
//
// If we does calculate Light We need Normals.
//
if (PagingLandScapeOptions::getSingleton().LightMap ||
PagingLandScapeOptions::getSingleton().ShadowMap ||
PagingLandScapeOptions::getSingleton().LitColorMapGenerate ||
PagingLandScapeOptions::getSingleton().LitColorMapSplit ||
PagingLandScapeOptions::getSingleton().LitBaseMap)
{
mNormalMap = NormalMapper.getNormalMap ();
mNormalMapBpp = mNormalMap->getNumElemBytes (mNormalMap->getFormat ());
mNormalMapData = mNormalMap->getData();
mNormalMapDataSize = (uint) mNormalMap->getSize();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -