# 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-2006 Torus Knot Software Ltd
    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 You may alternatively use this source under the terms of a specific version of
    00025 the OGRE Unrestricted License provided you have obtained such a license from
    00026 Torus Knot Software Ltd.
    00027 -----------------------------------------------------------------------------
    00028 */
    00029 #ifndef __MaterialSerializer_H__
    00030 #define __MaterialSerializer_H__
    00031
    00032 #include "OgrePrerequisites.h"
    00033 #include "OgreMaterial.h"
    00034 #include "OgreBlendMode.h"
    00035 #include "OgreTextureUnitState.h"
    00036 #include "OgreGpuProgram.h"
    00037 #include "OgreStringVector.h"
    00038
    00039 namespace Ogre {
    00040
    00042     enum MaterialScriptSection
    00043     {
    00044         MSS_NONE,
    00045         MSS_MATERIAL,
    00046         MSS_TECHNIQUE,
    00047         MSS_PASS,
    00048         MSS_TEXTUREUNIT,
    00049         MSS_PROGRAM_REF,
    00050         MSS_PROGRAM,
    00051         MSS_DEFAULT_PARAMETERS,
    00052         MSS_TEXTURESOURCE
    00053     };
    00055     struct MaterialScriptProgramDefinition
    00056     {
    00057         String name;
    00058         GpuProgramType progType;
    00059         String language;
    00060         String source;
    00061         String syntax;
    00062         bool supportsSkeletalAnimation;
    00063         bool supportsMorphAnimation;
    00064         ushort supportsPoseAnimation; // number of simultaneous poses supported
    00065         bool usesVertexTextureFetch;
    00066         std::vector<std::pair<String, String> > customParameters;
    00067     };
    00069     struct MaterialScriptContext
    00070     {
    00071         MaterialScriptSection section;
    00072         String groupName;
    00073         MaterialPtr material;
    00074         Technique* technique;
    00075         Pass* pass;
    00076         TextureUnitState* textureUnit;
    00077         GpuProgramPtr program; // used when referencing a program, not when defining it
    00078         bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
    00079         bool isVertexProgramShadowReceiver; // when referencing, are we in context of shadow caster
    00080         bool isFragmentProgramShadowReceiver; // when referencing, are we in context of shadow caster
    00081         GpuProgramParametersSharedPtr programParams;
    00082         ushort numAnimationParametrics;
    00083         MaterialScriptProgramDefinition* programDef; // this is used while defining a program
    00084
    00085         int techLev,    //Keep track of what tech, pass, and state level we are in
    00086             passLev,
    00087             stateLev;
    00088         StringVector defaultParamLines;
    00089
    00090         // Error reporting state
    00091         size_t lineNo;
    00092         String filename;
    00093         AliasTextureNamePairList textureAliases;
    00094     };
    00096     typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
    00097
    00099     class _OgreExport MaterialSerializer : public SerializerAlloc
    00100     {
    00101     protected:
    00103         typedef std::map<String, ATTRIBUTE_PARSER> AttribParserList;
    00104
    00105         MaterialScriptContext mScriptContext;
    00106
    00110         bool parseScriptLine(String& line);
    00112         bool invokeParser(String& line, AttribParserList& parsers);
    00116         void finishProgramDefinition(void);
    00118         AttribParserList mRootAttribParsers;
    00120         AttribParserList mMaterialAttribParsers;
    00122         AttribParserList mTechniqueAttribParsers;
    00124         AttribParserList mPassAttribParsers;
    00126         AttribParserList mTextureUnitAttribParsers;
    00128         AttribParserList mProgramRefAttribParsers;
    00130         AttribParserList mProgramAttribParsers;
    00132         AttribParserList mProgramDefaultParamAttribParsers;
    00133
    00134         void writeMaterial(const MaterialPtr& pMat);
    00135         void writeTechnique(const Technique* pTech);
    00136         void writePass(const Pass* pPass);
    00137         void writeVertexProgramRef(const Pass* pPass);
    00138         void writeShadowCasterVertexProgramRef(const Pass* pPass);
    00139         void writeShadowReceiverVertexProgramRef(const Pass* pPass);
    00140         void writeShadowReceiverFragmentProgramRef(const Pass* pPass);
    00141         void writeFragmentProgramRef(const Pass* pPass);
    00142         void writeGpuProgramRef(const String& attrib, const GpuProgramPtr& program, const GpuProgramParametersSharedPtr& params);
    00143         void writeGpuPrograms(void);
    00144         void writeGPUProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
    00145             const int level = 4, const bool useMainBuffer = true);
    00146         void writeNamedGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
    00147             const int level = 4, const bool useMainBuffer = true);
    00148         void writeLowLevelGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
    00149             const int level = 4, const bool useMainBuffer = true);
    00150         void writeGpuProgramParameter(
    00151             const String& commandName, const String& identifier,
    00152             const GpuProgramParameters::AutoConstantEntry* autoEntry,
    00153             const GpuProgramParameters::AutoConstantEntry* defaultAutoEntry,
    00154             bool isFloat, size_t physicalIndex, size_t physicalSize,
    00155             const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
    00156             const int level, const bool useMainBuffer);
    00157         void writeTextureUnit(const TextureUnitState *pTex);
    00158         void writeSceneBlendFactor(const SceneBlendFactor c_src, const SceneBlendFactor c_dest,
    00159             const SceneBlendFactor a_src, const SceneBlendFactor a_dest);
    00160         void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
    00161         void writeSceneBlendFactor(const SceneBlendFactor sbf);
    00162         void writeCompareFunction(const CompareFunction cf);
    00163         void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
    00164         void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
    00165         void writeLayerBlendSource(const LayerBlendSource lbs);
    00166
    00167         typedef std::multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect> EffectMap;
    00168
    00169         void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00170         void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00171         void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00172         void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00173
    00174         String convertFiltering(FilterOptions fo);
    00175     public:
    00177         MaterialSerializer();
    00179         virtual ~MaterialSerializer() {};
    00180
    00187         void queueForExport(const MaterialPtr& pMat, bool clearQueued = false,
    00188             bool exportDefaults = false);
    00198         void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = "");
    00208         void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false,
    00209             const bool includeProgDef = false, const String& programFilename = "");
    00211         const String &getQueuedAsString() const;
    00213         void clearQueue();
    00214
    00217         void parseScript(DataStreamPtr& stream, const String& groupName);
    00218
    00219
    00220
    00221     private:
    00222         String mBuffer;
    00223         String mGpuProgramBuffer;
    00224         typedef std::set<String> GpuProgramDefinitionContainer;
    00225         typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator;
    00226         GpuProgramDefinitionContainer mGpuProgramDefinitionContainer;
    00227         bool mDefaults;
    00228
    00229         void beginSection(unsigned short level, const bool useMainBuffer = true)
    00230         {
    00231             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00232             buffer += "\n";
    00233             for (unsigned short i = 0; i < level; ++i)
    00234             {
    00235                 buffer += "\t";
    00236             }
    00237             buffer += "{";
    00238         }
    00239         void endSection(unsigned short level, const bool useMainBuffer = true)
    00240         {
    00241             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00242             buffer += "\n";
    00243             for (unsigned short i = 0; i < level; ++i)
    00244             {
    00245                 buffer += "\t";
    00246             }
    00247             buffer += "}";
    00248         }
    00249
    00250         void writeAttribute(unsigned short level, const String& att, 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 += att;
    00259         }
    00260
    00261         void writeValue(const String& val, const bool useMainBuffer = true)
    00262         {
    00263             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00264             buffer += (" " + val);
    00265         }
    00266
    00267         void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true)
    00268         {
    00269             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00270             buffer += "\n";
    00271             for (unsigned short i = 0; i < level; ++i)
    00272             {
    00273                 buffer += "\t";
    00274             }
    00275             buffer += "// " + comment;
    00276         }
    00277
    00278     };
    00279 }
    00280 #endif

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Jul 26 11:32:18 2009