OgreTerrainMaterialGenerator.h
Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004 (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2013 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 
00029 #ifndef __Ogre_TerrainMaterialGenerator_H__
00030 #define __Ogre_TerrainMaterialGenerator_H__
00031 
00032 #include "OgreTerrainPrerequisites.h"
00033 #include "OgrePixelFormat.h"
00034 #include "OgreMaterial.h"
00035 #include "OgreTexture.h"
00036 
00037 namespace Ogre
00038 {
00039     class Terrain;
00040 
00056     enum TerrainLayerSamplerSemantic
00057     {
00059         TLSS_ALBEDO = 0,
00061         TLSS_NORMAL = 1,
00063         TLSS_HEIGHT = 2,
00065         TLSS_SPECULAR = 3
00066     };
00067 
00070     struct _OgreTerrainExport TerrainLayerSamplerElement
00071     {
00073         uint8 source;
00075         TerrainLayerSamplerSemantic semantic;
00077         uint8 elementStart;
00079         uint8 elementCount;
00080 
00081         bool operator==(const TerrainLayerSamplerElement& e) const
00082         {
00083             return source == e.source &&
00084                 semantic == e.semantic &&
00085                 elementStart == e.elementStart &&
00086                 elementCount == e.elementCount;
00087         }
00088 
00089         TerrainLayerSamplerElement() : 
00090             source(0), semantic(TLSS_ALBEDO), elementStart(0), elementCount(0)
00091         {}
00092 
00093         TerrainLayerSamplerElement(uint8 src, TerrainLayerSamplerSemantic sem,
00094             uint8 elemStart, uint8 elemCount)
00095             : source(src), semantic(sem), elementStart(elemStart), elementCount(elemCount)
00096         {
00097         }
00098     };
00099     typedef vector<TerrainLayerSamplerElement>::type TerrainLayerSamplerElementList;
00100 
00103     struct _OgreTerrainExport TerrainLayerSampler
00104     {
00106         String alias;
00108         PixelFormat format;
00109 
00110         bool operator==(const TerrainLayerSampler& s) const
00111         {
00112             return alias == s.alias && format == s.format;
00113         }
00114 
00115         TerrainLayerSampler()
00116             : alias(""), format(PF_UNKNOWN)
00117         {
00118         }
00119 
00120         TerrainLayerSampler(const String& aliasName, PixelFormat fmt)
00121             : alias(aliasName), format(fmt)
00122         {
00123         }
00124     };
00125     typedef vector<TerrainLayerSampler>::type TerrainLayerSamplerList;
00126 
00131     struct _OgreTerrainExport TerrainLayerDeclaration
00132     {
00133         TerrainLayerSamplerList samplers;
00134         TerrainLayerSamplerElementList elements;
00135 
00136         bool operator==(const TerrainLayerDeclaration& dcl) const
00137         {
00138             return samplers == dcl.samplers && elements == dcl.elements;
00139         }
00140     };
00141 
00161     class _OgreTerrainExport TerrainMaterialGenerator : public TerrainAlloc
00162     {
00163     public:
00167         class _OgreTerrainExport Profile : public TerrainAlloc
00168         {
00169         protected:
00170             TerrainMaterialGenerator* mParent;
00171             String mName;
00172             String mDesc;
00173         public:
00174             Profile(TerrainMaterialGenerator* parent, const String& name, const String& desc)
00175                 : mParent(parent), mName(name), mDesc(desc) {}
00176             Profile(const Profile& prof) 
00177                 : mParent(prof.mParent), mName(prof.mName), mDesc(prof.mDesc) {}
00178             virtual ~Profile() {}
00180             TerrainMaterialGenerator* getParent() const { return mParent; }
00182             const String& getName() const { return mName; }
00184             const String& getDescription() const { return mDesc; }
00186             virtual bool isVertexCompressionSupported() const = 0;      
00188             virtual MaterialPtr generate(const Terrain* terrain) = 0;
00190             virtual MaterialPtr generateForCompositeMap(const Terrain* terrain) = 0;
00192             virtual void setLightmapEnabled(bool enabled) = 0;
00194             virtual uint8 getMaxLayers(const Terrain* terrain) const = 0;
00196             virtual void updateCompositeMap(const Terrain* terrain, const Rect& rect);
00197 
00199             virtual void updateParams(const MaterialPtr& mat, const Terrain* terrain) = 0;
00201             virtual void updateParamsForCompositeMap(const MaterialPtr& mat, const Terrain* terrain) = 0;
00202 
00204             virtual void requestOptions(Terrain* terrain) = 0;
00205 
00206         };
00207 
00208         TerrainMaterialGenerator();
00209         virtual ~TerrainMaterialGenerator();
00210 
00212         typedef vector<Profile*>::type ProfileList;
00213     
00216         virtual const ProfileList& getProfiles() const { return mProfiles; }
00217 
00219         virtual void setActiveProfile(const String& name)
00220         {
00221             if (!mActiveProfile || mActiveProfile->getName() != name)
00222             {
00223                 for (ProfileList::iterator i = mProfiles.begin(); i != mProfiles.end(); ++i)
00224                 {
00225                     if ((*i)->getName() == name)
00226                     {
00227                         setActiveProfile(*i);
00228                         break;
00229                     }
00230                 }
00231             }
00232 
00233         }
00234 
00236         virtual void setActiveProfile(Profile* p)
00237         {
00238             if (mActiveProfile != p)
00239             {
00240                 mActiveProfile = p;
00241                 _markChanged();
00242             }
00243         }
00245         Profile* getActiveProfile() const 
00246         { 
00247             // default if not chosen yet
00248             if (!mActiveProfile && !mProfiles.empty())
00249                 mActiveProfile = mProfiles[0];
00250 
00251             return mActiveProfile; 
00252         }
00253 
00255         void _markChanged() { ++mChangeCounter; }
00256 
00260         unsigned long long int getChangeCount() const { return mChangeCounter; }
00261 
00264         virtual const TerrainLayerDeclaration& getLayerDeclaration() const { return mLayerDecl; }
00271         virtual bool canGenerateUsingDeclaration(const TerrainLayerDeclaration& decl)
00272         {
00273             return decl == mLayerDecl;
00274         }
00275         
00279         virtual bool isVertexCompressionSupported() const
00280         {
00281             return getActiveProfile()->isVertexCompressionSupported();
00282         }
00283 
00286         virtual void requestOptions(Terrain* terrain)
00287         {
00288             Profile* p = getActiveProfile();
00289             if (p)
00290                 p->requestOptions(terrain);
00291 
00292         }
00295         virtual MaterialPtr generate(const Terrain* terrain)
00296         {
00297             Profile* p = getActiveProfile();
00298             if (!p)
00299                 return MaterialPtr();
00300             else
00301                 return p->generate(terrain);
00302         }
00305         virtual MaterialPtr generateForCompositeMap(const Terrain* terrain)
00306         {
00307             Profile* p = getActiveProfile();
00308             if (!p)
00309                 return MaterialPtr();
00310             else
00311                 return p->generateForCompositeMap(terrain);
00312         }
00316         virtual void setLightmapEnabled(bool enabled)
00317         {
00318             Profile* p = getActiveProfile();
00319             if (p)
00320                 return p->setLightmapEnabled(enabled);
00321         }
00325         virtual uint8 getMaxLayers(const Terrain* terrain) const
00326         {
00327             Profile* p = getActiveProfile();
00328             if (p)
00329                 return p->getMaxLayers(terrain);
00330             else
00331                 return 0;
00332         }
00333 
00340         virtual void updateCompositeMap(const Terrain* terrain, const Rect& rect)
00341         {
00342             Profile* p = getActiveProfile();
00343             if (!p)
00344                 return;
00345             else
00346                 p->updateCompositeMap(terrain, rect);
00347         }
00348 
00349 
00352         virtual void updateParams(const MaterialPtr& mat, const Terrain* terrain)
00353         {
00354             Profile* p = getActiveProfile();
00355             if (p)
00356                 p->updateParams(mat, terrain);
00357         }
00360         virtual void updateParamsForCompositeMap(const MaterialPtr& mat, const Terrain* terrain)
00361         {
00362             Profile* p = getActiveProfile();
00363             if (p)
00364                 p->updateParamsForCompositeMap(mat, terrain);
00365         }
00366 
00375         virtual void setDebugLevel(unsigned int dbg)
00376         {
00377             if (mDebugLevel != dbg)
00378             {
00379                 mDebugLevel = dbg;
00380                 _markChanged();
00381             }
00382         }
00384         virtual unsigned int getDebugLevel() const { return mDebugLevel; }
00385 
00392         virtual void _renderCompositeMap(size_t size, const Rect& rect, 
00393             const MaterialPtr& mat, const TexturePtr& destCompositeMap);
00394 
00395         Texture* _getCompositeMapRTT() { return mCompositeMapRTT; }
00396     protected:
00397 
00398         ProfileList mProfiles;
00399         mutable Profile* mActiveProfile;
00400         unsigned long long int mChangeCounter;
00401         TerrainLayerDeclaration mLayerDecl;
00402         unsigned int mDebugLevel;
00403         SceneManager* mCompositeMapSM;
00404         Camera* mCompositeMapCam;
00405         Texture* mCompositeMapRTT; // deliberately holding this by raw pointer to avoid shutdown issues
00406         ManualObject* mCompositeMapPlane;
00407         Light* mCompositeMapLight;
00408 
00409 
00410 
00411     };
00412 
00413     typedef SharedPtr<TerrainMaterialGenerator> TerrainMaterialGeneratorPtr;
00414 
00418 }
00419 #endif
00420 

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:48