# 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-2009 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 #ifndef __MaterialSerializer_H__
    00029 #define __MaterialSerializer_H__
    00030
    00031 #include "OgrePrerequisites.h"
    00032 #include "OgreMaterial.h"
    00033 #include "OgreBlendMode.h"
    00034 #include "OgreTextureUnitState.h"
    00035 #include "OgreGpuProgram.h"
    00036 #include "OgreStringVector.h"
    00037
    00038 namespace Ogre {
    00039
    00047     enum MaterialScriptSection
    00048     {
    00049         MSS_NONE,
    00050         MSS_MATERIAL,
    00051         MSS_TECHNIQUE,
    00052         MSS_PASS,
    00053         MSS_TEXTUREUNIT,
    00054         MSS_PROGRAM_REF,
    00055         MSS_PROGRAM,
    00056         MSS_DEFAULT_PARAMETERS,
    00057         MSS_TEXTURESOURCE
    00058     };
    00060     struct MaterialScriptProgramDefinition
    00061     {
    00062         String name;
    00063         GpuProgramType progType;
    00064         String language;
    00065         String source;
    00066         String syntax;
    00067         bool supportsSkeletalAnimation;
    00068         bool supportsMorphAnimation;
    00069         ushort supportsPoseAnimation; // number of simultaneous poses supported
    00070         bool usesVertexTextureFetch;
    00071         vector<std::pair<String, String> >::type customParameters;
    00072     };
    00074     struct MaterialScriptContext
    00075     {
    00076         MaterialScriptSection section;
    00077         String groupName;
    00078         MaterialPtr material;
    00079         Technique* technique;
    00080         Pass* pass;
    00081         TextureUnitState* textureUnit;
    00082         GpuProgramPtr program; // used when referencing a program, not when defining it
    00083         bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
    00084         bool isVertexProgramShadowReceiver; // when referencing, are we in context of shadow caster
    00085         bool isFragmentProgramShadowReceiver; // when referencing, are we in context of shadow caster
    00086         GpuProgramParametersSharedPtr programParams;
    00087         ushort numAnimationParametrics;
    00088         MaterialScriptProgramDefinition* programDef; // this is used while defining a program
    00089
    00090         int techLev,    //Keep track of what tech, pass, and state level we are in
    00091             passLev,
    00092             stateLev;
    00093         StringVector defaultParamLines;
    00094
    00095         // Error reporting state
    00096         size_t lineNo;
    00097         String filename;
    00098         AliasTextureNamePairList textureAliases;
    00099     };
    00101     typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
    00102
    00104     class _OgreExport MaterialSerializer : public SerializerAlloc
    00105     {
    00106     public:
    00107
    00108         // Material serizliae event.
    00109         enum SerializeEvent
    00110         {
    00111             MSE_PRE_WRITE,
    00112             MSE_WRITE_BEGIN,
    00113             MSE_WRITE_END,
    00114             MSE_POST_WRITE
    00115         };
    00116
    00120         class Listener
    00121         {
    00122         public:
    00123             virtual ~Listener() {}
    00124
    00132             virtual void materialEventRaised(MaterialSerializer* ser,
    00133                 SerializeEvent event, bool& skip, const Material* mat)
    00134                         { (void)ser; (void)event; (void)skip; (void)mat; }
    00135
    00143             virtual void techniqueEventRaised(MaterialSerializer* ser,
    00144                 SerializeEvent event, bool& skip, const Technique* tech)
    00145                         { (void)ser; (void)event; (void)skip; (void)tech; }
    00146
    00154             virtual void passEventRaised(MaterialSerializer* ser,
    00155                 SerializeEvent event, bool& skip, const Pass* pass)
    00156                         { (void)ser; (void)event; (void)skip; (void)pass; }
    00157
    00168             void gpuProgramRefEventRaised(MaterialSerializer* ser,
    00169                 SerializeEvent event, bool& skip,
    00170                 const String& attrib,
    00171                 const GpuProgramPtr& program,
    00172                 const GpuProgramParametersSharedPtr& params,
    00173                 GpuProgramParameters* defaultParams)
    00174                         {
    00175                             (void)ser;
    00176                             (void)event;
    00177                             (void)skip;
    00178                             (void)attrib;
    00179                             (void)program;
    00180                             (void)params;
    00181                             (void)defaultParams;
    00182                         }
    00183
    00191             virtual void textureUnitStateEventRaised(MaterialSerializer* ser,
    00192                 SerializeEvent event, bool& skip, const TextureUnitState* textureUnit)
    00193                         {
    00194                             (void)ser;
    00195                             (void)event;
    00196                             (void)skip;
    00197                             (void)textureUnit;
    00198                         }
    00199         };
    00200
    00201     protected:
    00203         typedef map<String, ATTRIBUTE_PARSER>::type AttribParserList;
    00204
    00205         MaterialScriptContext mScriptContext;
    00206
    00210         bool parseScriptLine(String& line);
    00212         bool invokeParser(String& line, AttribParserList& parsers);
    00216         void finishProgramDefinition(void);
    00218         AttribParserList mRootAttribParsers;
    00220         AttribParserList mMaterialAttribParsers;
    00222         AttribParserList mTechniqueAttribParsers;
    00224         AttribParserList mPassAttribParsers;
    00226         AttribParserList mTextureUnitAttribParsers;
    00228         AttribParserList mProgramRefAttribParsers;
    00230         AttribParserList mProgramAttribParsers;
    00232         AttribParserList mProgramDefaultParamAttribParsers;
    00233
    00235         typedef vector<Listener*>::type         ListenerList;
    00236         typedef ListenerList::iterator          ListenerListIterator;
    00237         typedef ListenerList::const_iterator    ListenerListConstIterator;
    00238         ListenerList mListeners;
    00239
    00240
    00241         void writeMaterial(const MaterialPtr& pMat, const String& materialName = "");
    00242         void writeTechnique(const Technique* pTech);
    00243         void writePass(const Pass* pPass);
    00244         void writeVertexProgramRef(const Pass* pPass);
    00245         void writeShadowCasterVertexProgramRef(const Pass* pPass);
    00246         void writeShadowReceiverVertexProgramRef(const Pass* pPass);
    00247         void writeShadowReceiverFragmentProgramRef(const Pass* pPass);
    00248         void writeFragmentProgramRef(const Pass* pPass);
    00249         void writeGpuProgramRef(const String& attrib, const GpuProgramPtr& program, const GpuProgramParametersSharedPtr& params);
    00250         void writeGpuPrograms(void);
    00251         void writeGPUProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
    00252             const unsigned short level = 4, const bool useMainBuffer = true);
    00253         void writeNamedGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
    00254             const unsigned short level = 4, const bool useMainBuffer = true);
    00255         void writeLowLevelGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
    00256             const unsigned short level = 4, const bool useMainBuffer = true);
    00257         void writeGpuProgramParameter(
    00258             const String& commandName, const String& identifier,
    00259             const GpuProgramParameters::AutoConstantEntry* autoEntry,
    00260             const GpuProgramParameters::AutoConstantEntry* defaultAutoEntry,
    00261             bool isFloat, size_t physicalIndex, size_t physicalSize,
    00262             const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
    00263             const unsigned short level, const bool useMainBuffer);
    00264         void writeTextureUnit(const TextureUnitState *pTex);
    00265         void writeSceneBlendFactor(const SceneBlendFactor c_src, const SceneBlendFactor c_dest,
    00266             const SceneBlendFactor a_src, const SceneBlendFactor a_dest);
    00267         void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
    00268         void writeSceneBlendFactor(const SceneBlendFactor sbf);
    00269         void writeCompareFunction(const CompareFunction cf);
    00270         void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
    00271         void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
    00272         void writeLayerBlendSource(const LayerBlendSource lbs);
    00273
    00274         typedef multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect>::type EffectMap;
    00275
    00276         void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00277         void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00278         void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00279         void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00280
    00281         String convertFiltering(FilterOptions fo);
    00282
    00283
    00287         void fireMaterialEvent(SerializeEvent event, bool& skip, const Material* mat);
    00288
    00292         void fireTechniqueEvent(SerializeEvent event, bool& skip, const Technique* tech);
    00293
    00297         void firePassEvent(SerializeEvent event, bool& skip, const Pass* pass);
    00298
    00302         void fireGpuProgramRefEvent(SerializeEvent event, bool& skip,
    00303             const String& attrib,
    00304             const GpuProgramPtr& program,
    00305             const GpuProgramParametersSharedPtr& params,
    00306             GpuProgramParameters* defaultParams);
    00307
    00308
    00312         void fireTextureUnitStateEvent(SerializeEvent event, bool& skip, const TextureUnitState* textureUnit);
    00313
    00314    public:
    00316         MaterialSerializer();
    00318         virtual ~MaterialSerializer() {}
    00319
    00328         void queueForExport(const MaterialPtr& pMat, bool clearQueued = false,
    00329             bool exportDefaults = false, const String& materialName = "");
    00339         void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = "");
    00351         void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false,
    00352             const bool includeProgDef = false, const String& programFilename = "",
    00353             const String& materialName = "");
    00355         const String &getQueuedAsString() const;
    00357         void clearQueue();
    00358
    00361         void parseScript(DataStreamPtr& stream, const String& groupName);
    00362
    00366         void addListener(Listener* listener);
    00367
    00371         void removeListener(Listener* listener);
    00372
    00373     private:
    00374         String mBuffer;
    00375         String mGpuProgramBuffer;
    00376         typedef set<String>::type GpuProgramDefinitionContainer;
    00377         typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator;
    00378         GpuProgramDefinitionContainer mGpuProgramDefinitionContainer;
    00379         bool mDefaults;
    00380
    00381     public:
    00382         void beginSection(unsigned short level, const bool useMainBuffer = true)
    00383         {
    00384             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00385             buffer += "\n";
    00386             for (unsigned short i = 0; i < level; ++i)
    00387             {
    00388                 buffer += "\t";
    00389             }
    00390             buffer += "{";
    00391         }
    00392         void endSection(unsigned short level, const bool useMainBuffer = true)
    00393         {
    00394             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00395             buffer += "\n";
    00396             for (unsigned short i = 0; i < level; ++i)
    00397             {
    00398                 buffer += "\t";
    00399             }
    00400             buffer += "}";
    00401         }
    00402
    00403         void writeAttribute(unsigned short level, const String& att, const bool useMainBuffer = true)
    00404         {
    00405             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00406             buffer += "\n";
    00407             for (unsigned short i = 0; i < level; ++i)
    00408             {
    00409                 buffer += "\t";
    00410             }
    00411             buffer += att;
    00412         }
    00413
    00414         void writeValue(const String& val, const bool useMainBuffer = true)
    00415         {
    00416             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00417             buffer += (" " + val);
    00418         }
    00419
    00420         void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true)
    00421         {
    00422             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00423             buffer += "\n";
    00424             for (unsigned short i = 0; i < level; ++i)
    00425             {
    00426                 buffer += "\t";
    00427             }
    00428             buffer += "// " + comment;
    00429         }
    00430
    00431
    00432
    00433     };
    00436 }
    00437 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun May 8 2011 20:28:08