Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreMaterialSerializer.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 © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 #ifndef __MaterialSerializer_H__
00026 #define __MaterialSerializer_H__
00027 
00028 #include "OgrePrerequisites.h"
00029 #include "OgreMaterial.h"
00030 #include "OgreBlendMode.h"
00031 #include "OgreTextureUnitState.h"
00032 #include "OgreGpuProgram.h"
00033 #include "OgreStringVector.h"
00034 
00035 namespace Ogre {
00036 
00038     enum MaterialScriptSection
00039     {
00040         MSS_NONE,
00041         MSS_MATERIAL,
00042         MSS_TECHNIQUE,
00043         MSS_PASS,
00044         MSS_TEXTUREUNIT,
00045         MSS_PROGRAM_REF,
00046         MSS_PROGRAM,
00047         MSS_DEFAULT_PARAMETERS,
00048         MSS_TEXTURESOURCE
00049     };
00051     struct MaterialScriptProgramDefinition
00052     {
00053         String name;
00054         GpuProgramType progType;
00055         String language;
00056         String source;
00057         String syntax;
00058         bool supportsSkeletalAnimation;
00059         std::map<String, String> customParameters;
00060     };
00062     struct MaterialScriptContext 
00063     {
00064         MaterialScriptSection section;
00065         Material* material;
00066         Technique* technique;
00067         Pass* pass;
00068         TextureUnitState* textureUnit;
00069         GpuProgram* program; // used when referencing a program, not when defining it
00070         bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
00071         bool isProgramShadowReceiver; // when referencing, are we in context of shadow caster
00072         GpuProgramParametersSharedPtr programParams;
00073         MaterialScriptProgramDefinition* programDef; // this is used while defining a program
00074 
00075         int techLev,    //Keep track of what tech, pass, and state level we are in
00076             passLev,
00077             stateLev;
00078         StringVector defaultParamLines;
00079 
00080         // Error reporting state
00081         size_t lineNo;
00082         String filename;
00083     };
00085     typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
00086 
00088     class _OgreExport MaterialSerializer
00089     {
00090     protected:
00092         typedef std::map<String, ATTRIBUTE_PARSER> AttribParserList;
00093 
00094         MaterialScriptContext mScriptContext;
00095 
00099         bool parseScriptLine(String& line);
00101         bool invokeParser(String& line, AttribParserList& parsers);
00105         void finishProgramDefinition(void);
00107         AttribParserList mRootAttribParsers;
00109         AttribParserList mMaterialAttribParsers;
00111         AttribParserList mTechniqueAttribParsers;
00113         AttribParserList mPassAttribParsers;
00115         AttribParserList mTextureUnitAttribParsers;
00117         AttribParserList mProgramRefAttribParsers;
00119         AttribParserList mProgramAttribParsers;
00121         AttribParserList mProgramDefaultParamAttribParsers;
00122 
00123         void writeMaterial(const Material *pMat);
00124         void writeTechnique(const Technique* pTech);
00125         void writePass(const Pass* pPass);
00126         void writeTextureUnit(const TextureUnitState *pTex);
00127 
00128         void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
00129         void writeSceneBlendFactor(const SceneBlendFactor sbf);
00130         void writeCompareFunction(const CompareFunction cf);
00131         void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
00132         void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
00133         void writeLayerBlendSource(const LayerBlendSource lbs);
00134         
00135         typedef std::multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect> EffectMap;
00136 
00137         void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00138         void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00139         void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00140         void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00141 
00142         String convertFiltering(FilterOptions fo);
00143     public:
00145         MaterialSerializer();
00147         virtual ~MaterialSerializer() {};
00148 
00150         void queueForExport(const Material *pMat, bool clearQueued = false, bool exportDefaults = false);
00152         void exportQueued(const String& filename);
00154         void exportMaterial(const Material *pMat, const String& filename, bool exportDefaults = false);
00156         const String &getQueuedAsString() const;
00158         void clearQueue();
00159 
00165         void parseScript(DataChunk& chunk, const String& filename = "");
00166 
00167 
00168 
00169     private:
00170         String mBuffer;
00171         bool mDefaults;
00172 
00173         void beginSection(unsigned short level)
00174         {
00175             mBuffer += "\n";
00176             for (unsigned short i = 0; i < level; ++i)
00177             {
00178                 mBuffer += "\t";
00179             }
00180             mBuffer += "{";
00181         }
00182         void endSection(unsigned short level)
00183         {
00184             mBuffer += "\n";
00185             for (unsigned short i = 0; i < level; ++i)
00186             {
00187                 mBuffer += "\t";
00188             }
00189             mBuffer += "}";
00190         }
00191 
00192         void writeAttribute(unsigned short level, const String& att)
00193         {
00194             mBuffer += "\n";
00195             for (unsigned short i = 0; i < level; ++i)
00196             {
00197                 mBuffer += "\t";
00198             }
00199             mBuffer += att;
00200         }
00201 
00202         void writeValue(const String& val)
00203         {
00204             mBuffer += (" " + val);
00205         }
00206 
00207         void writeComment(unsigned short level, const String& comment)
00208         {
00209             mBuffer += "\n";
00210             for (unsigned short i = 0; i < level; ++i)
00211             {
00212                 mBuffer += "\t";
00213             }
00214             mBuffer += "// " + comment;
00215         }
00216 
00217     };
00218 }
00219 #endif

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:32 2004