OGRE: OgreMaterialSerializer.h Source File - OGRE Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | 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-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
    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 sbf_src, const SceneBlendFactor sbf_dest);
    00159         void writeSceneBlendFactor(const SceneBlendFactor sbf);
    00160         void writeCompareFunction(const CompareFunction cf);
    00161         void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
    00162         void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
    00163         void writeLayerBlendSource(const LayerBlendSource lbs);
    00164
    00165         typedef std::multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect> EffectMap;
    00166
    00167         void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00168         void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00169         void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00170         void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
    00171
    00172         String convertFiltering(FilterOptions fo);
    00173     public:
    00175         MaterialSerializer();
    00177         virtual ~MaterialSerializer() {};
    00178
    00185         void queueForExport(const MaterialPtr& pMat, bool clearQueued = false,
    00186             bool exportDefaults = false);
    00196         void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = "");
    00206         void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false,
    00207             const bool includeProgDef = false, const String& programFilename = "");
    00209         const String &getQueuedAsString() const;
    00211         void clearQueue();
    00212
    00215         void parseScript(DataStreamPtr& stream, const String& groupName);
    00216
    00217
    00218
    00219     private:
    00220         String mBuffer;
    00221         String mGpuProgramBuffer;
    00222         typedef std::set<String> GpuProgramDefinitionContainer;
    00223         typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator;
    00224         GpuProgramDefinitionContainer mGpuProgramDefinitionContainer;
    00225         bool mDefaults;
    00226
    00227         void beginSection(unsigned short level, const bool useMainBuffer = true)
    00228         {
    00229             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00230             buffer += "\n";
    00231             for (unsigned short i = 0; i < level; ++i)
    00232             {
    00233                 buffer += "\t";
    00234             }
    00235             buffer += "{";
    00236         }
    00237         void endSection(unsigned short level, const bool useMainBuffer = true)
    00238         {
    00239             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00240             buffer += "\n";
    00241             for (unsigned short i = 0; i < level; ++i)
    00242             {
    00243                 buffer += "\t";
    00244             }
    00245             buffer += "}";
    00246         }
    00247
    00248         void writeAttribute(unsigned short level, const String& att, const bool useMainBuffer = true)
    00249         {
    00250             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00251             buffer += "\n";
    00252             for (unsigned short i = 0; i < level; ++i)
    00253             {
    00254                 buffer += "\t";
    00255             }
    00256             buffer += att;
    00257         }
    00258
    00259         void writeValue(const String& val, const bool useMainBuffer = true)
    00260         {
    00261             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00262             buffer += (" " + val);
    00263         }
    00264
    00265         void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true)
    00266         {
    00267             String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
    00268             buffer += "\n";
    00269             for (unsigned short i = 0; i < level; ++i)
    00270             {
    00271                 buffer += "\t";
    00272             }
    00273             buffer += "// " + comment;
    00274         }
    00275
    00276     };
    00277 }
    00278 #endif

Copyright © 2000-2005 by The OGRE Team
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Thu Mar 6 09:46:25 2008