source: OGRE/trunk/ogrenew/OgreMain/include/OgreMaterialSerializer.h @ 692

Revision 692, 11.0 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

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    };
50        /** Struct for holding a program definition which is in progress. */
51        struct MaterialScriptProgramDefinition
52        {
53                String name;
54                GpuProgramType progType;
55        String language;
56                String source;
57                String syntax;
58        bool supportsSkeletalAnimation;
59                bool supportsMorphAnimation;
60                ushort supportsPoseAnimation; // number of simultaneous poses supported
61                std::map<String, String> customParameters;
62        };
63    /** Struct for holding the script context while parsing. */
64    struct MaterialScriptContext
65    {
66        MaterialScriptSection section;
67                String groupName;
68        MaterialPtr material;
69        Technique* technique;
70        Pass* pass;
71        TextureUnitState* textureUnit;
72        GpuProgramPtr program; // used when referencing a program, not when defining it
73        bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
74        bool isVertexProgramShadowReceiver; // when referencing, are we in context of shadow caster
75                bool isFragmentProgramShadowReceiver; // when referencing, are we in context of shadow caster
76        GpuProgramParametersSharedPtr programParams;
77                ushort numAnimationParametrics;
78                MaterialScriptProgramDefinition* programDef; // this is used while defining a program
79
80                int techLev,    //Keep track of what tech, pass, and state level we are in
81                        passLev,
82                        stateLev;
83        StringVector defaultParamLines;
84
85                // Error reporting state
86        size_t lineNo;
87        String filename;
88        AliasTextureNamePairList textureAliases;
89    };
90    /// Function def for material attribute parser; return value determines if the next line should be {
91    typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
92
93    /** Class for serializing Materials to / from a .material script.*/
94    class _OgreExport MaterialSerializer
95    {
96    protected:
97        /// Keyword-mapped attribute parsers.
98        typedef std::map<String, ATTRIBUTE_PARSER> AttribParserList;
99
100        MaterialScriptContext mScriptContext;
101
102        /** internal method for parsing a material
103        @returns true if it expects the next line to be a {
104        */
105        bool parseScriptLine(String& line);
106        /** internal method for finding & invoking an attribute parser. */
107        bool invokeParser(String& line, AttribParserList& parsers);
108                /** Internal method for saving a program definition which has been
109                    built up.
110                */
111                void finishProgramDefinition(void);
112        /// Parsers for the root of the material script
113        AttribParserList mRootAttribParsers;
114        /// Parsers for the material section of a script
115        AttribParserList mMaterialAttribParsers;
116        /// Parsers for the technique section of a script
117        AttribParserList mTechniqueAttribParsers;
118        /// Parsers for the pass section of a script
119        AttribParserList mPassAttribParsers;
120        /// Parsers for the texture unit section of a script
121        AttribParserList mTextureUnitAttribParsers;
122        /// Parsers for the program reference section of a script
123        AttribParserList mProgramRefAttribParsers;
124        /// Parsers for the program definition section of a script
125        AttribParserList mProgramAttribParsers;
126        /// Parsers for the program definition section of a script
127        AttribParserList mProgramDefaultParamAttribParsers;
128
129        void writeMaterial(const MaterialPtr& pMat);
130        void writeTechnique(const Technique* pTech);
131        void writePass(const Pass* pPass);
132        void writeVertexProgramRef(const Pass* pPass);
133        void writeShadowCasterVertexProgramRef(const Pass* pPass);
134        void writeShadowReceiverVertexProgramRef(const Pass* pPass);
135        void writeShadowReceiverFragmentProgramRef(const Pass* pPass);
136        void writeFragmentProgramRef(const Pass* pPass);
137        void writeGpuProgramRef(const String& attrib, const GpuProgramPtr& program, const GpuProgramParametersSharedPtr& params);
138        void writeGpuPrograms(void);
139        void writeGPUProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
140            const int level = 4, const bool useMainBuffer = true);
141                void writeTextureUnit(const TextureUnitState *pTex);
142
143                void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
144                void writeSceneBlendFactor(const SceneBlendFactor sbf);
145                void writeCompareFunction(const CompareFunction cf);
146                void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
147                void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
148                void writeLayerBlendSource(const LayerBlendSource lbs);
149               
150                typedef std::multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect> EffectMap;
151
152                void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
153                void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
154                void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
155                void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
156
157        String convertFiltering(FilterOptions fo);
158    public:
159                /** default constructor*/
160                MaterialSerializer();
161                /** default destructor*/
162                virtual ~MaterialSerializer() {};
163
164                /** Queue an in-memory Material to the internal buffer for export.
165                @param pMat Material pointer
166                @param clearQueued If true, any materials already queued will be removed
167                @param exportDefaults If true, attributes which are defaulted will be
168                        included in the script exported, otherwise they will be omitted
169                */
170        void queueForExport(const MaterialPtr& pMat, bool clearQueued = false,
171                        bool exportDefaults = false);
172        /** Exports queued material(s) to a named material script file.
173        @param filename the file name of the material script to be exported
174                @param includeProgDef If true, vertex program and fragment program
175                        definitions will be written at the top of the material script
176        @param programFilename the file name of the vertex / fragment program
177                        script to be exported. This is only used if there are program definitions
178            to be exported and includeProgDef is false
179                        when calling queueForExport.
180        */
181                void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = "");
182        /** Exports a single in-memory Material to the named material script file.
183        @param exportDefaults if true then exports all values including defaults
184        @param includeProgDef if true includes Gpu shader program definitions in the
185            export material script otherwise if false then program definitions will
186            be exported to a seperate file with name programFilename if
187            programFilename is not empty
188        @param programFilename the file name of the vertex / fragment program
189                        script to be exported. This is only used if includeProgDef is false.
190        */
191        void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false,
192            const bool includeProgDef = false, const String& programFilename = "");
193                /** Returns a string representing the parsed material(s) */
194                const String &getQueuedAsString() const;
195                /** Clears the internal buffer */
196                void clearQueue();
197
198        /** Parses a Material script file passed as a stream.
199        */
200        void parseScript(DataStreamPtr& stream, const String& groupName);
201
202
203
204        private:
205                String mBuffer;
206        String mGpuProgramBuffer;
207        typedef std::set<String> GpuProgramDefinitionContainer;
208        typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator;
209        GpuProgramDefinitionContainer mGpuProgramDefinitionContainer;
210                bool mDefaults;
211
212        void beginSection(unsigned short level, const bool useMainBuffer = true)
213                {
214            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
215                        buffer += "\n";
216            for (unsigned short i = 0; i < level; ++i)
217            {
218                buffer += "\t";
219            }
220            buffer += "{";
221                }
222                void endSection(unsigned short level, const bool useMainBuffer = true)
223                {
224            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
225                        buffer += "\n";
226            for (unsigned short i = 0; i < level; ++i)
227            {
228                buffer += "\t";
229            }
230            buffer += "}";
231                }
232
233                void writeAttribute(unsigned short level, const String& att, const bool useMainBuffer = true)
234                {
235            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
236                        buffer += "\n";
237            for (unsigned short i = 0; i < level; ++i)
238            {
239                buffer += "\t";
240            }
241            buffer += att;
242                }
243
244                void writeValue(const String& val, const bool useMainBuffer = true)
245                {
246            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
247                        buffer += (" " + val);
248                }
249
250                void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true)
251                {
252            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
253                        buffer += "\n";
254            for (unsigned short i = 0; i < level; ++i)
255            {
256                buffer += "\t";
257            }
258            buffer += "// " + comment;
259                }
260
261    };
262}
263#endif
Note: See TracBrowser for help on using the repository browser.