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-2013 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 #include "OgreHeaderPrefix.h"
00038 
00039 namespace Ogre {
00040 
00048     enum MaterialScriptSection
00049     {
00050         MSS_NONE,
00051         MSS_MATERIAL,
00052         MSS_TECHNIQUE,
00053         MSS_PASS,
00054         MSS_TEXTUREUNIT,
00055         MSS_PROGRAM_REF,
00056         MSS_PROGRAM,
00057         MSS_DEFAULT_PARAMETERS,
00058         MSS_TEXTURESOURCE
00059     };
00061     struct MaterialScriptProgramDefinition
00062     {
00063         String name;
00064         GpuProgramType progType;
00065         String language;
00066         String source;
00067         String syntax;
00068         bool supportsSkeletalAnimation;
00069         bool supportsMorphAnimation;
00070         ushort supportsPoseAnimation; // number of simultaneous poses supported
00071         bool usesVertexTextureFetch;
00072         vector<std::pair<String, String> >::type customParameters;
00073     };
00075     struct MaterialScriptContext 
00076     {
00077         MaterialScriptSection section;
00078         String groupName;
00079         MaterialPtr material;
00080         Technique* technique;
00081         Pass* pass;
00082         TextureUnitState* textureUnit;
00083         GpuProgramPtr program; 
00084         bool isVertexProgramShadowCaster; 
00085         bool isFragmentProgramShadowCaster; 
00086         bool isVertexProgramShadowReceiver; 
00087         bool isFragmentProgramShadowReceiver; 
00088         GpuProgramParametersSharedPtr programParams;
00089         ushort numAnimationParametrics;
00090         MaterialScriptProgramDefinition* programDef; 
00091 
00092         int techLev,    //Keep track of what tech, pass, and state level we are in
00093             passLev,
00094             stateLev;
00095         StringVector defaultParamLines;
00096 
00098         size_t lineNo;
00099         String filename;
00100         AliasTextureNamePairList textureAliases;
00101     };
00103     typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
00104 
00106     class _OgreExport MaterialSerializer : public SerializerAlloc
00107     {   
00108     public:
00109 
00110         // Material serialize event.
00111         enum SerializeEvent
00112         {
00113             MSE_PRE_WRITE,
00114             MSE_WRITE_BEGIN,
00115             MSE_WRITE_END,
00116             MSE_POST_WRITE
00117         };
00118 
00122         class Listener
00123         {
00124         public:
00125             virtual ~Listener() {}
00126             
00134             virtual void materialEventRaised(MaterialSerializer* ser, 
00135                 SerializeEvent event, bool& skip, const Material* mat)
00136                         { (void)ser; (void)event; (void)skip; (void)mat; }
00137             
00145             virtual void techniqueEventRaised(MaterialSerializer* ser, 
00146                 SerializeEvent event, bool& skip, const Technique* tech)
00147                         { (void)ser; (void)event; (void)skip; (void)tech; }
00148         
00156             virtual void passEventRaised(MaterialSerializer* ser, 
00157                 SerializeEvent event, bool& skip, const Pass* pass)
00158                         { (void)ser; (void)event; (void)skip; (void)pass; }
00159 
00170             void gpuProgramRefEventRaised(MaterialSerializer* ser, 
00171                 SerializeEvent event, bool& skip,
00172                 const String& attrib, 
00173                 const GpuProgramPtr& program, 
00174                 const GpuProgramParametersSharedPtr& params,
00175                 GpuProgramParameters* defaultParams)
00176                         {
00177                             (void)ser;
00178                             (void)event;
00179                             (void)skip;
00180                             (void)attrib;
00181                             (void)program;
00182                             (void)params;
00183                             (void)defaultParams;
00184                         }
00185 
00193             virtual void textureUnitStateEventRaised(MaterialSerializer* ser, 
00194                 SerializeEvent event, bool& skip, const TextureUnitState* textureUnit)
00195                         {
00196                             (void)ser;
00197                             (void)event;
00198                             (void)skip;
00199                             (void)textureUnit;
00200                         }           
00201         };
00202 
00203     protected:
00205         typedef map<String, ATTRIBUTE_PARSER>::type AttribParserList;
00206 
00207         MaterialScriptContext mScriptContext;
00208 
00212         bool parseScriptLine(String& line);
00214         bool invokeParser(String& line, AttribParserList& parsers);
00218         void finishProgramDefinition(void);
00220         AttribParserList mRootAttribParsers;
00222         AttribParserList mMaterialAttribParsers;
00224         AttribParserList mTechniqueAttribParsers;
00226         AttribParserList mPassAttribParsers;
00228         AttribParserList mTextureUnitAttribParsers;
00230         AttribParserList mProgramRefAttribParsers;
00232         AttribParserList mProgramAttribParsers;
00234         AttribParserList mProgramDefaultParamAttribParsers;
00235 
00237         typedef vector<Listener*>::type         ListenerList;
00238         typedef ListenerList::iterator          ListenerListIterator;
00239         typedef ListenerList::const_iterator    ListenerListConstIterator;
00240         ListenerList mListeners;
00241 
00242 
00243         void writeMaterial(const MaterialPtr& pMat, const String& materialName = "");
00244         void writeTechnique(const Technique* pTech);
00245         void writePass(const Pass* pPass);
00246         void writeVertexProgramRef(const Pass* pPass);
00247         void writeShadowCasterVertexProgramRef(const Pass* pPass);
00248         void writeShadowCasterFragmentProgramRef(const Pass* pPass);
00249         void writeShadowReceiverVertexProgramRef(const Pass* pPass);
00250         void writeShadowReceiverFragmentProgramRef(const Pass* pPass);
00251         void writeFragmentProgramRef(const Pass* pPass);
00252         void writeGpuProgramRef(const String& attrib, const GpuProgramPtr& program, const GpuProgramParametersSharedPtr& params);
00253         void writeGpuPrograms(void);
00254         void writeGPUProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
00255             const unsigned short level = 4, const bool useMainBuffer = true);
00256         void writeNamedGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
00257             const unsigned short level = 4, const bool useMainBuffer = true);
00258         void writeLowLevelGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
00259             const unsigned short level = 4, const bool useMainBuffer = true);
00260         void writeGpuProgramParameter(
00261             const String& commandName, const String& identifier, 
00262             const GpuProgramParameters::AutoConstantEntry* autoEntry, 
00263             const GpuProgramParameters::AutoConstantEntry* defaultAutoEntry, 
00264             bool isFloat, bool isDouble, size_t physicalIndex, size_t physicalSize,
00265             const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
00266             const unsigned short level, const bool useMainBuffer);
00267         void writeTextureUnit(const TextureUnitState *pTex);
00268         void writeSceneBlendFactor(const SceneBlendFactor c_src, const SceneBlendFactor c_dest, 
00269             const SceneBlendFactor a_src, const SceneBlendFactor a_dest);
00270         void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
00271         void writeSceneBlendFactor(const SceneBlendFactor sbf);
00272         void writeCompareFunction(const CompareFunction cf);
00273         void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
00274         void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
00275         void writeLayerBlendSource(const LayerBlendSource lbs);
00276         
00277         typedef multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect>::type EffectMap;
00278 
00279         void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00280         void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00281         void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00282         void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
00283 
00284         String convertFiltering(FilterOptions fo);
00285 
00286         
00290         void fireMaterialEvent(SerializeEvent event, bool& skip, const Material* mat);
00291 
00295         void fireTechniqueEvent(SerializeEvent event, bool& skip, const Technique* tech);
00296         
00300         void firePassEvent(SerializeEvent event, bool& skip, const Pass* pass);
00301         
00305         void fireGpuProgramRefEvent(SerializeEvent event, bool& skip,
00306             const String& attrib, 
00307             const GpuProgramPtr& program, 
00308             const GpuProgramParametersSharedPtr& params,
00309             GpuProgramParameters* defaultParams);
00310     
00311 
00315         void fireTextureUnitStateEvent(SerializeEvent event, bool& skip, const TextureUnitState* textureUnit);
00316         
00317    public:      
00319         MaterialSerializer();
00321         virtual ~MaterialSerializer() {}
00322 
00331         void queueForExport(const MaterialPtr& pMat, bool clearQueued = false, 
00332             bool exportDefaults = false, const String& materialName = "");
00342         void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = "");
00354         void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false,
00355             const bool includeProgDef = false, const String& programFilename = "", 
00356             const String& materialName = "");
00358         const String &getQueuedAsString() const;
00360         void clearQueue();
00361 
00364         void parseScript(DataStreamPtr& stream, const String& groupName);
00365 
00369         void addListener(Listener* listener);
00370 
00374         void removeListener(Listener* listener);
00375 
00376     private:
00377         String mBuffer;
00378         String mGpuProgramBuffer;
00379         typedef set<String>::type GpuProgramDefinitionContainer;
00380         typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator;
00381         GpuProgramDefinitionContainer mGpuProgramDefinitionContainer;
00382         bool mDefaults;
00383         
00384     public:
00385         void beginSection(unsigned short level, const bool useMainBuffer = true)
00386         {
00387             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
00388             buffer += "\n";
00389             for (unsigned short i = 0; i < level; ++i)
00390             {
00391                 buffer += "\t";
00392             }
00393             buffer += "{";
00394         }
00395         void endSection(unsigned short level, const bool useMainBuffer = true)
00396         {
00397             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
00398             buffer += "\n";
00399             for (unsigned short i = 0; i < level; ++i)
00400             {
00401                 buffer += "\t";
00402             }
00403             buffer += "}";
00404         }
00405 
00406         void writeAttribute(unsigned short level, const String& att, const bool useMainBuffer = true)
00407         {
00408             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
00409             buffer += "\n";
00410             for (unsigned short i = 0; i < level; ++i)
00411             {
00412                 buffer += "\t";
00413             }
00414             buffer += att;
00415         }
00416 
00417         void writeValue(const String& val, const bool useMainBuffer = true)
00418         {
00419             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
00420             buffer += (" " + val);
00421         }
00422 
00423         String quoteWord(const String& val)
00424         {
00425             if (val.find_first_of(" \t") != String::npos)
00426                 return ("\"" + val + "\"");
00427             else return val;
00428         }
00429 
00430 
00431         void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true)
00432         {
00433             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
00434             buffer += "\n";
00435             for (unsigned short i = 0; i < level; ++i)
00436             {
00437                 buffer += "\t";
00438             }
00439             buffer += "// " + comment;
00440         }
00441 
00442 
00443 
00444     };
00447 }
00448 
00449 #include "OgreHeaderSuffix.h"
00450 
00451 #endif

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:44