|
| 1 | +/* |
| 2 | +----------------------------------------------------------------------------- |
| 3 | +This source file is part of OGRE |
| 4 | +(Object-oriented Graphics Rendering Engine) |
| 5 | +For the latest info, see http://www.ogre3d.org/ |
| 6 | +
|
| 7 | +Copyright (c) 2000-2009 Torus Knot Software Ltd |
| 8 | +Also see acknowledgements in Readme.html |
| 9 | +
|
| 10 | +You may use this sample code for anything you like, it is not covered by the |
| 11 | +same license as the rest of the engine. |
| 12 | +----------------------------------------------------------------------------- |
| 13 | +*/ |
| 14 | + |
| 15 | +#include <OgreTerrain.h> |
| 16 | +#include <OgreTerrainGroup.h> |
| 17 | +#include <OgreConfigFile.h> |
| 18 | + |
| 19 | +namespace Ogre |
| 20 | +{ |
| 21 | +class CustomMatProfile : public Ogre::TerrainMaterialGenerator::Profile |
| 22 | +{ |
| 23 | + MaterialPtr mMaterial; |
| 24 | + bool mIsInit; |
| 25 | + bool mNormalMapRequired; |
| 26 | +public: |
| 27 | + CustomMatProfile(const String& matName) : Profile(NULL, "", ""), mIsInit(false), mNormalMapRequired(true) |
| 28 | + { |
| 29 | + auto terrainGlobals = TerrainGlobalOptions::getSingletonPtr(); |
| 30 | + mMaterial = |
| 31 | + MaterialManager::getSingleton().getByName(matName, terrainGlobals->getDefaultResourceGroup()); |
| 32 | + } |
| 33 | + |
| 34 | + bool isVertexCompressionSupported() const { return false; } |
| 35 | + |
| 36 | + void setNormalMapRequired(bool enable) { mNormalMapRequired = enable; } |
| 37 | + |
| 38 | + MaterialPtr generate(const Terrain* terrain) |
| 39 | + { |
| 40 | + if (!mIsInit && mNormalMapRequired) |
| 41 | + { |
| 42 | + // Get default pass |
| 43 | + Pass *p = mMaterial->getTechnique(0)->getPass(0); |
| 44 | + |
| 45 | + // Add terrain's global normalmap to renderpass so the fragment program can find it. |
| 46 | + p->createTextureUnitState()->_setTexturePtr(terrain->getTerrainNormalMap()); |
| 47 | + |
| 48 | + } |
| 49 | + mIsInit = true; |
| 50 | + |
| 51 | + return mMaterial; |
| 52 | + } |
| 53 | + MaterialPtr generateForCompositeMap(const Terrain* terrain) |
| 54 | + { |
| 55 | + return terrain->_getCompositeMapMaterial(); |
| 56 | + } |
| 57 | + void updateCompositeMap(const Terrain* terrain, const Rect& rect) {} |
| 58 | + void setLightmapEnabled(bool enabled) {} |
| 59 | + uint8 getMaxLayers(const Terrain* terrain) const { return 0; } |
| 60 | + |
| 61 | + void updateParams(const MaterialPtr& mat, const Terrain* terrain) {} |
| 62 | + void updateParamsForCompositeMap(const MaterialPtr& mat, const Terrain* terrain) {} |
| 63 | + void requestOptions(Terrain* terrain) |
| 64 | + { |
| 65 | + terrain->_setLightMapRequired(false); |
| 66 | + terrain->_setCompositeMapRequired(false); |
| 67 | + terrain->_setNormalMapRequired(mNormalMapRequired); |
| 68 | + } |
| 69 | +}; |
| 70 | +} // namespace Ogre |
| 71 | + |
| 72 | +inline Ogre::TerrainGroup* loadLegacyTerrain(const Ogre::String& cfgFileName, Ogre::SceneManager* sceneMgr) |
| 73 | +{ |
| 74 | + using namespace Ogre; |
| 75 | + |
| 76 | + ConfigFile cfg; |
| 77 | + cfg.loadFromResourceSystem(cfgFileName, RGN_DEFAULT); |
| 78 | + |
| 79 | + auto terrainGlobals = TerrainGlobalOptions::getSingletonPtr(); |
| 80 | + if(!terrainGlobals) |
| 81 | + terrainGlobals = new TerrainGlobalOptions(); |
| 82 | + |
| 83 | + const String& customMatName = cfg.getSetting("CustomMaterialName"); |
| 84 | + |
| 85 | + if(!customMatName.empty()) |
| 86 | + { |
| 87 | + auto profile = new CustomMatProfile(customMatName); |
| 88 | + profile->setNormalMapRequired(StringConverter::parseBool(cfg.getSetting("VertexNormals"))); |
| 89 | + terrainGlobals->getDefaultMaterialGenerator()->setActiveProfile(profile); |
| 90 | + } |
| 91 | + |
| 92 | + auto terrainGroup = new TerrainGroup(sceneMgr); |
| 93 | + terrainGroup->loadLegacyTerrain(cfgFileName); |
| 94 | + |
| 95 | + return terrainGroup; |
| 96 | +} |
0 commit comments