Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class 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 (c) 2000-2005 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         String groupName;
00066         MaterialPtr material;
00067         Technique* technique;
00068         Pass* pass;
00069         TextureUnitState* textureUnit;
00070         GpuProgramPtr program; // used when referencing a program, not when defining it
00071         bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
00072         bool isProgramShadowReceiver; // when referencing, are we in context of shadow caster
00073         GpuProgramParametersSharedPtr programParams;
00074         MaterialScriptProgramDefinition* programDef; // this is used while defining a program
00075 
00076         int techLev,    //Keep track of what tech, pass, and state level we are in
00077             passLev,
00078             stateLev;
00079         StringVector defaultParamLines;
00080 
00081         // Error reporting state
00082         size_t lineNo;
00083         String filename;
00084     };
00086     typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
00087 
00089     class _OgreExport MaterialSerializer
00090     {
00091     protected:
00093         typedef std::map<String, ATTRIBUTE_PARSER> AttribParserList;
00094 
00095         MaterialScriptContext mScriptContext;
00096 
00100         bool parseScriptLine(String& line);
00102         bool invokeParser(String& line, AttribParserList& parsers);
00106         void finishProgramDefinition(void);
00108         AttribParserList mRootAttribParsers;
00110         AttribParserList mMaterialAttribParsers;
00112         AttribParserList mTechniqueAttribParsers;
00114         AttribParserList mPassAttribParsers;
00116         AttribParserList mTextureUnitAttribParsers;
00118         AttribParserList mProgramRefAttribParsers;
00120         AttribParserList mProgramAttribParsers;
00122         AttribParserList mProgramDefaultParamAttribParsers;
00123 
00124         void writeMaterial(const MaterialPtr& pMat);
00125         void writeTechnique(const Technique* pTech);
00126         void writePass(const Pass* pPass);
00127         void writeTextureUnit(const TextureUnitState *pTex);
00128 
00129         void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
00130         void writeSceneBlendFactor(const SceneBlendFactor sbf);
00131         void writeCompareFunction(const CompareFunction cf);
00132         void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
00133         void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
00134         void writeLayerBlendSource(const LayerBlendSource lbs);
00135         
00136         typedef std::multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect> EffectMap;
00137 
00138         void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00139         void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00140         void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00141         void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00142 
00143         String convertFiltering(FilterOptions fo);
00144     public:
00146         MaterialSerializer();
00148         virtual ~MaterialSerializer() {};
00149 
00151         void queueForExport(const MaterialPtr& pMat, bool clearQueued = false, bool exportDefaults = false);
00153         void exportQueued(const String& filename);
00155         void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false);
00157         const String &getQueuedAsString() const;
00159         void clearQueue();
00160 
00163         void parseScript(DataStreamPtr& stream, const String& groupName);
00164 
00165 
00166 
00167     private:
00168         String mBuffer;
00169         bool mDefaults;
00170 
00171         void beginSection(unsigned short level)
00172         {
00173             mBuffer += "\n";
00174             for (unsigned short i = 0; i < level; ++i)
00175             {
00176                 mBuffer += "\t";
00177             }
00178             mBuffer += "{";
00179         }
00180         void endSection(unsigned short level)
00181         {
00182             mBuffer += "\n";
00183             for (unsigned short i = 0; i < level; ++i)
00184             {
00185                 mBuffer += "\t";
00186             }
00187             mBuffer += "}";
00188         }
00189 
00190         void writeAttribute(unsigned short level, const String& att)
00191         {
00192             mBuffer += "\n";
00193             for (unsigned short i = 0; i < level; ++i)
00194             {
00195                 mBuffer += "\t";
00196             }
00197             mBuffer += att;
00198         }
00199 
00200         void writeValue(const String& val)
00201         {
00202             mBuffer += (" " + val);
00203         }
00204 
00205         void writeComment(unsigned short level, const String& comment)
00206         {
00207             mBuffer += "\n";
00208             for (unsigned short i = 0; i < level; ++i)
00209             {
00210                 mBuffer += "\t";
00211             }
00212             mBuffer += "// " + comment;
00213         }
00214 
00215     };
00216 }
00217 #endif

Copyright © 2000-2005 by The OGRE Team
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Feb 12 12:59:47 2006