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 
00034 namespace Ogre {
00035 
00037     enum MaterialScriptSection
00038     {
00039         MSS_NONE,
00040         MSS_MATERIAL,
00041         MSS_TECHNIQUE,
00042         MSS_PASS,
00043         MSS_TEXTUREUNIT,
00044         MSS_PROGRAM_REF,
00045         MSS_PROGRAM,
00046         MSS_TEXTURESOURCE
00047     };
00049     struct MaterialScriptProgramDefinition
00050     {
00051         String name;
00052         GpuProgramType progType;
00053         String language;
00054         String source;
00055         String syntax;
00056         bool supportsSkeletalAnimation;
00057         std::map<String, String> customParameters;
00058     };
00060     struct MaterialScriptContext 
00061     {
00062         MaterialScriptSection section;
00063         Material* material;
00064         Technique* technique;
00065         Pass* pass;
00066         TextureUnitState* textureUnit;
00067         GpuProgram* program; // used when referencing a program, not when defining it
00068         bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
00069         bool isProgramShadowReceiver; // when referencing, are we in context of shadow caster
00070         GpuProgramParametersSharedPtr programParams;
00071         MaterialScriptProgramDefinition* programDef; // this is used while defining a program
00072 
00073         int techLev,    //Keep track of what tech, pass, and state level we are in
00074             passLev,
00075             stateLev;
00076 
00077         // Error reporting state
00078         size_t lineNo;
00079         String filename;
00080     };
00082     typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
00083 
00085     class _OgreExport MaterialSerializer
00086     {
00087     protected:
00089         typedef std::map<String, ATTRIBUTE_PARSER> AttribParserList;
00090 
00091         MaterialScriptContext mScriptContext;
00092 
00096         bool parseScriptLine(String& line);
00098         bool invokeParser(String& line, AttribParserList& parsers);
00102         void finishProgramDefinition(void);
00104         AttribParserList mRootAttribParsers;
00106         AttribParserList mMaterialAttribParsers;
00108         AttribParserList mTechniqueAttribParsers;
00110         AttribParserList mPassAttribParsers;
00112         AttribParserList mTextureUnitAttribParsers;
00114         AttribParserList mProgramRefAttribParsers;
00116         AttribParserList mProgramAttribParsers;
00117 
00118         void writeMaterial(const Material *pMat);
00119         void writeTechnique(const Technique* pTech);
00120         void writePass(const Pass* pPass);
00121         void writeTextureUnit(const TextureUnitState *pTex);
00122 
00123         void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
00124         void writeSceneBlendFactor(const SceneBlendFactor sbf);
00125         void writeCompareFunction(const CompareFunction cf);
00126         void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
00127         void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
00128         void writeLayerBlendSource(const LayerBlendSource lbs);
00129         
00130         typedef std::multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect> EffectMap;
00131 
00132         void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00133         void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00134         void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00135         void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00136 
00137         String convertFiltering(FilterOptions fo);
00138     public:
00140         MaterialSerializer();
00142         virtual ~MaterialSerializer() {};
00143 
00145         void queueForExport(const Material *pMat, bool clearQueued = false, bool exportDefaults = false);
00147         void exportQueued(const String& filename);
00149         void exportMaterial(const Material *pMat, const String& filename, bool exportDefaults = false);
00151         const String &getQueuedAsString() const;
00153         void clearQueue();
00154 
00160         void parseScript(DataChunk& chunk, const String& filename = "");
00161 
00162 
00163 
00164     private:
00165         String mBuffer;
00166         bool mDefaults;
00167 
00168         void beginSection(unsigned short level)
00169         {
00170             mBuffer += "\n";
00171             for (unsigned short i = 0; i < level; ++i)
00172             {
00173                 mBuffer += "\t";
00174             }
00175             mBuffer += "{";
00176         }
00177         void endSection(unsigned short level)
00178         {
00179             mBuffer += "\n";
00180             for (unsigned short i = 0; i < level; ++i)
00181             {
00182                 mBuffer += "\t";
00183             }
00184             mBuffer += "}";
00185         }
00186 
00187         void writeAttribute(unsigned short level, const String& att)
00188         {
00189             mBuffer += "\n";
00190             for (unsigned short i = 0; i < level; ++i)
00191             {
00192                 mBuffer += "\t";
00193             }
00194             mBuffer += att;
00195         }
00196 
00197         void writeValue(const String& val)
00198         {
00199             mBuffer += (" " + val);
00200         }
00201 
00202         void writeComment(unsigned short level, const String& comment)
00203         {
00204             mBuffer += "\n";
00205             for (unsigned short i = 0; i < level; ++i)
00206             {
00207                 mBuffer += "\t";
00208             }
00209             mBuffer += "// " + comment;
00210         }
00211 
00212     };
00213 }
00214 #endif

Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:22 2004