source: OGRE/trunk/ogre_changes/Ogre1.2/OgreMain/include/OgreMaterialSerializer.h @ 812

Revision 812, 11.4 KB checked in by szirmay, 19 years ago (diff)
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#ifndef __MaterialSerializer_H__
26#define __MaterialSerializer_H__
27
28#include "OgrePrerequisites.h"
29#include "OgreMaterial.h"
30#include "OgreBlendMode.h"
31#include "OgreTextureUnitState.h"
32#include "OgreGpuProgram.h"
33#include "OgreStringVector.h"
34
35namespace Ogre {
36
37    /** Enum to identify material sections. */
38    enum MaterialScriptSection
39    {
40        MSS_NONE,
41        MSS_MATERIAL,
42        MSS_TECHNIQUE,
43        MSS_PASS,
44        MSS_TEXTUREUNIT,
45        MSS_PROGRAM_REF,
46                MSS_PROGRAM,
47        MSS_DEFAULT_PARAMETERS,
48                MSS_TEXTURESOURCE
49#ifdef GAMETOOLS_ILLUMINATION_MODULE
50                ,MSS_ILLUMTECHNIQUES
51                ,MSS_ILLUMTECHNIQUE_PARAMS
52#endif
53    };
54        /** Struct for holding a program definition which is in progress. */
55        struct MaterialScriptProgramDefinition
56        {
57                String name;
58                GpuProgramType progType;
59        String language;
60                String source;
61                String syntax;
62        bool supportsSkeletalAnimation;
63                bool supportsMorphAnimation;
64                ushort supportsPoseAnimation; // number of simultaneous poses supported
65                std::map<String, String> customParameters;
66        };
67    /** Struct for holding the script context while parsing. */
68    struct MaterialScriptContext
69    {
70#ifdef GAMETOOLS_ILLUMINATION_MODULE
71                class IllumTechniqueParams* illumTechniqueParams;               
72#endif
73        MaterialScriptSection section;
74                String groupName;
75        MaterialPtr material;
76        Technique* technique;
77        Pass* pass;
78        TextureUnitState* textureUnit;
79        GpuProgramPtr program; // used when referencing a program, not when defining it
80        bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
81        bool isVertexProgramShadowReceiver; // when referencing, are we in context of shadow caster
82                bool isFragmentProgramShadowReceiver; // when referencing, are we in context of shadow caster
83        GpuProgramParametersSharedPtr programParams;
84                ushort numAnimationParametrics;
85                MaterialScriptProgramDefinition* programDef; // this is used while defining a program
86
87                int techLev,    //Keep track of what tech, pass, and state level we are in
88                        passLev,
89                        stateLev;
90        StringVector defaultParamLines;
91
92                // Error reporting state
93        size_t lineNo;
94        String filename;
95        AliasTextureNamePairList textureAliases;
96    };
97    /// Function def for material attribute parser; return value determines if the next line should be {
98    typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
99
100    /** Class for serializing Materials to / from a .material script.*/
101    class _OgreExport MaterialSerializer
102    {
103    protected:
104        /// Keyword-mapped attribute parsers.
105        typedef std::map<String, ATTRIBUTE_PARSER> AttribParserList;
106
107        MaterialScriptContext mScriptContext;
108
109        /** internal method for parsing a material
110        @returns true if it expects the next line to be a {
111        */
112        bool parseScriptLine(String& line);
113        /** internal method for finding & invoking an attribute parser. */
114        bool invokeParser(String& line, AttribParserList& parsers);
115                /** Internal method for saving a program definition which has been
116                    built up.
117                */
118                void finishProgramDefinition(void);
119        /// Parsers for the root of the material script
120        AttribParserList mRootAttribParsers;
121        /// Parsers for the material section of a script
122        AttribParserList mMaterialAttribParsers;
123        /// Parsers for the technique section of a script
124        AttribParserList mTechniqueAttribParsers;
125        /// Parsers for the pass section of a script
126        AttribParserList mPassAttribParsers;
127        /// Parsers for the texture unit section of a script
128        AttribParserList mTextureUnitAttribParsers;
129        /// Parsers for the program reference section of a script
130        AttribParserList mProgramRefAttribParsers;
131        /// Parsers for the program definition section of a script
132        AttribParserList mProgramAttribParsers;
133        /// Parsers for the program definition section of a script
134        AttribParserList mProgramDefaultParamAttribParsers;
135
136#ifdef GAMETOOLS_ILLUMINATION_MODULE
137                /// Parsers for GameTools illumination Manager
138        AttribParserList mIllumTechniqueParsers;
139#endif
140
141        void writeMaterial(const MaterialPtr& pMat);
142        void writeTechnique(const Technique* pTech);
143        void writePass(const Pass* pPass);
144        void writeVertexProgramRef(const Pass* pPass);
145        void writeShadowCasterVertexProgramRef(const Pass* pPass);
146        void writeShadowReceiverVertexProgramRef(const Pass* pPass);
147        void writeShadowReceiverFragmentProgramRef(const Pass* pPass);
148        void writeFragmentProgramRef(const Pass* pPass);
149        void writeGpuProgramRef(const String& attrib, const GpuProgramPtr& program, const GpuProgramParametersSharedPtr& params);
150        void writeGpuPrograms(void);
151        void writeGPUProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
152            const int level = 4, const bool useMainBuffer = true);
153                void writeTextureUnit(const TextureUnitState *pTex);
154
155                void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
156                void writeSceneBlendFactor(const SceneBlendFactor sbf);
157                void writeCompareFunction(const CompareFunction cf);
158                void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
159                void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
160                void writeLayerBlendSource(const LayerBlendSource lbs);
161               
162                typedef std::multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect> EffectMap;
163
164                void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
165                void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
166                void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
167                void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
168
169        String convertFiltering(FilterOptions fo);
170    public:
171                /** default constructor*/
172                MaterialSerializer();
173                /** default destructor*/
174                virtual ~MaterialSerializer() {};
175
176                /** Queue an in-memory Material to the internal buffer for export.
177                @param pMat Material pointer
178                @param clearQueued If true, any materials already queued will be removed
179                @param exportDefaults If true, attributes which are defaulted will be
180                        included in the script exported, otherwise they will be omitted
181                */
182        void queueForExport(const MaterialPtr& pMat, bool clearQueued = false,
183                        bool exportDefaults = false);
184        /** Exports queued material(s) to a named material script file.
185        @param filename the file name of the material script to be exported
186                @param includeProgDef If true, vertex program and fragment program
187                        definitions will be written at the top of the material script
188        @param programFilename the file name of the vertex / fragment program
189                        script to be exported. This is only used if there are program definitions
190            to be exported and includeProgDef is false
191                        when calling queueForExport.
192        */
193                void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = "");
194        /** Exports a single in-memory Material to the named material script file.
195        @param exportDefaults if true then exports all values including defaults
196        @param includeProgDef if true includes Gpu shader program definitions in the
197            export material script otherwise if false then program definitions will
198            be exported to a seperate file with name programFilename if
199            programFilename is not empty
200        @param programFilename the file name of the vertex / fragment program
201                        script to be exported. This is only used if includeProgDef is false.
202        */
203        void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false,
204            const bool includeProgDef = false, const String& programFilename = "");
205                /** Returns a string representing the parsed material(s) */
206                const String &getQueuedAsString() const;
207                /** Clears the internal buffer */
208                void clearQueue();
209
210        /** Parses a Material script file passed as a stream.
211        */
212        void parseScript(DataStreamPtr& stream, const String& groupName);
213
214
215
216        private:
217                String mBuffer;
218        String mGpuProgramBuffer;
219        typedef std::set<String> GpuProgramDefinitionContainer;
220        typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator;
221        GpuProgramDefinitionContainer mGpuProgramDefinitionContainer;
222                bool mDefaults;
223
224        void beginSection(unsigned short level, const bool useMainBuffer = true)
225                {
226            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
227                        buffer += "\n";
228            for (unsigned short i = 0; i < level; ++i)
229            {
230                buffer += "\t";
231            }
232            buffer += "{";
233                }
234                void endSection(unsigned short level, const bool useMainBuffer = true)
235                {
236            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
237                        buffer += "\n";
238            for (unsigned short i = 0; i < level; ++i)
239            {
240                buffer += "\t";
241            }
242            buffer += "}";
243                }
244
245                void writeAttribute(unsigned short level, const String& att, const bool useMainBuffer = true)
246                {
247            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
248                        buffer += "\n";
249            for (unsigned short i = 0; i < level; ++i)
250            {
251                buffer += "\t";
252            }
253            buffer += att;
254                }
255
256                void writeValue(const String& val, const bool useMainBuffer = true)
257                {
258            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
259                        buffer += (" " + val);
260                }
261
262                void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true)
263                {
264            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
265                        buffer += "\n";
266            for (unsigned short i = 0; i < level; ++i)
267            {
268                buffer += "\t";
269            }
270            buffer += "// " + comment;
271                }
272
273    };
274}
275#endif
Note: See TracBrowser for help on using the repository browser.