# 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         bool supportsMorphAnimation;
    00060         ushort supportsPoseAnimation; // number of simultaneous poses supported
    00061         std::map<String, String> customParameters;
    00062     };
    00064     struct MaterialScriptContext
    00065     {
    00066         MaterialScriptSection section;
    00067         String groupName;
    00068         MaterialPtr material;
    00069         Technique* technique;
    00070         Pass* pass;
    00071         TextureUnitState* textureUnit;
    00072         GpuProgramPtr program; // used when referencing a program, not when defining it
    00073         bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
    00074         bool isVertexProgramShadowReceiver; // when referencing, are we in context of shadow caster
    00075         bool isFragmentProgramShadowReceiver; // when referencing, are we in context of shadow caster
    00076         GpuProgramParametersSharedPtr programParams;
    00077         ushort numAnimationParametrics;
    00078         MaterialScriptProgramDefinition* programDef; // this is used while defining a program
    00079
    00080         int techLev,    //Keep track of what tech, pass, and state level we are in
    00081             passLev,
    00082             stateLev;
    00083         StringVector defaultParamLines;
    00084
    00085         // Error reporting state
    00086         size_t lineNo;
    00087         String filename;
    00088         AliasTextureNamePairList textureAliases;
    00089     };
    00091     typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
    00092
    00094     class _OgreExport MaterialSerializer
    00095     {
    00096     protected:
    00098         typedef std::map<String, ATTRIBUTE_PARSER> AttribParserList;
    00099
    00100         MaterialScriptContext mScriptContext;
    00101
    00105         bool parseScriptLine(String& line);
    00107         bool invokeParser(String& line, AttribParserList& parsers);
    00111         void finishProgramDefinition(void);
    00113         AttribParserList mRootAttribParsers;
    00115         AttribParserList mMaterialAttribParsers;
    00117         AttribParserList mTechniqueAttribParsers;
    00119         AttribParserList mPassAttribParsers;
    00121         AttribParserList mTextureUnitAttribParsers;
    00123         AttribParserList mProgramRefAttribParsers;
    00125         AttribParserList mProgramAttribParsers;
    00127         AttribParserList mProgramDefaultParamAttribParsers;
    00128
    00129         void writeMaterial(const MaterialPtr& pMat);
    00130         void writeTechnique(const Technique* pTech);
    00131         void writePass(const Pass* pPass);
    00132         void writeVertexProgramRef(const Pass* pPass);
    00133         void writeShadowCasterVertexProgramRef(const Pass* pPass);
    00134         void writeShadowReceiverVertexProgramRef(const Pass* pPass);
    00135         void writeShadowReceiverFragmentProgramRef(const Pass* pPass);
    00136         void writeFragmentProgramRef(const Pass* pPass);
    00137         void writeGpuProgramRef(const String& attrib, const GpuProgramPtr& program, const GpuProgramParametersSharedPtr& params);
    00138         void writeGpuPrograms(void);
    00139         void writeGPUProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
    00140             const int level = 4, const bool useMainBuffer = true);
    00141         void writeTextureUnit(const TextureUnitState *pTex);
    00142
    00143         void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
    00144         void writeSceneBlendFactor(const SceneBlendFactor sbf);
    00145         void writeCompareFunction(const CompareFunction cf);
    00146         void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
    00147         void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
    00148         void writeLayerBlendSource(const LayerBlendSource lbs);
    00149
    00150         typedef std::multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect> EffectMap;
    00151
    00152         void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00153         void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00154         void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00155         void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00156
    00157         String convertFiltering(FilterOptions fo);
    00158     public:
    00160         MaterialSerializer();
    00162         virtual ~MaterialSerializer() {};
    00163
    00170         void queueForExport(const MaterialPtr& pMat, bool clearQueued = false,
    00171             bool exportDefaults = false);
    00181         void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = "");
    00191         void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false,
    00192             const bool includeProgDef = false, const String& programFilename = "");
    00194         const String &getQueuedAsString() const;
    00196         void clearQueue();
    00197
    00200         void parseScript(DataStreamPtr& stream, const String& groupName);
    00201
    00202
    00203
    00204     private:
    00205         String mBuffer;
    00206         String mGpuProgramBuffer;
    00207         typedef std::set<String> GpuProgramDefinitionContainer;
    00208         typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator;
    00209         GpuProgramDefinitionContainer mGpuProgramDefinitionContainer;
    00210         bool mDefaults;
    00211
    00212         void beginSection(unsigned short level, const bool useMainBuffer = true)
    00213         {
    00214             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00215             buffer += "\n";
    00216             for (unsigned short i = 0; i < level; ++i)
    00217             {
    00218                 buffer += "\t";
    00219             }
    00220             buffer += "{";
    00221         }
    00222         void endSection(unsigned short level, const bool useMainBuffer = true)
    00223         {
    00224             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00225             buffer += "\n";
    00226             for (unsigned short i = 0; i < level; ++i)
    00227             {
    00228                 buffer += "\t";
    00229             }
    00230             buffer += "}";
    00231         }
    00232
    00233         void writeAttribute(unsigned short level, const String& att, const bool useMainBuffer = true)
    00234         {
    00235             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00236             buffer += "\n";
    00237             for (unsigned short i = 0; i < level; ++i)
    00238             {
    00239                 buffer += "\t";
    00240             }
    00241             buffer += att;
    00242         }
    00243
    00244         void writeValue(const String& val, const bool useMainBuffer = true)
    00245         {
    00246             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00247             buffer += (" " + val);
    00248         }
    00249
    00250         void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true)
    00251         {
    00252             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00253             buffer += "\n";
    00254             for (unsigned short i = 0; i < level; ++i)
    00255             {
    00256                 buffer += "\t";
    00257             }
    00258             buffer += "// " + comment;
    00259         }
    00260
    00261     };
    00262 }
    00263 #endif

Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Sep 17 15:39:10 2006