Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/include/OgreMaterialSerializer.h @ 5

Last change on this file since 5 was 5, checked in by anonymous, 17 years ago

=hoffentlich gehts jetzt

File size: 11.8 KB
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-2006 Torus Knot Software Ltd
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
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#ifndef __MaterialSerializer_H__
30#define __MaterialSerializer_H__
31
32#include "OgrePrerequisites.h"
33#include "OgreMaterial.h"
34#include "OgreBlendMode.h"
35#include "OgreTextureUnitState.h"
36#include "OgreGpuProgram.h"
37#include "OgreStringVector.h"
38
39namespace Ogre {
40
41    /** Enum to identify material sections. */
42    enum MaterialScriptSection
43    {
44        MSS_NONE,
45        MSS_MATERIAL,
46        MSS_TECHNIQUE,
47        MSS_PASS,
48        MSS_TEXTUREUNIT,
49        MSS_PROGRAM_REF,
50                MSS_PROGRAM,
51        MSS_DEFAULT_PARAMETERS,
52                MSS_TEXTURESOURCE
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                bool usesVertexTextureFetch;
66                std::vector<std::pair<String, String> > customParameters;
67        };
68    /** Struct for holding the script context while parsing. */
69    struct MaterialScriptContext
70    {
71        MaterialScriptSection section;
72                String groupName;
73        MaterialPtr material;
74        Technique* technique;
75        Pass* pass;
76        TextureUnitState* textureUnit;
77        GpuProgramPtr program; // used when referencing a program, not when defining it
78        bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
79        bool isVertexProgramShadowReceiver; // when referencing, are we in context of shadow caster
80                bool isFragmentProgramShadowReceiver; // when referencing, are we in context of shadow caster
81        GpuProgramParametersSharedPtr programParams;
82                ushort numAnimationParametrics;
83                MaterialScriptProgramDefinition* programDef; // this is used while defining a program
84
85                int techLev,    //Keep track of what tech, pass, and state level we are in
86                        passLev,
87                        stateLev;
88        StringVector defaultParamLines;
89
90                // Error reporting state
91        size_t lineNo;
92        String filename;
93        AliasTextureNamePairList textureAliases;
94    };
95    /// Function def for material attribute parser; return value determines if the next line should be {
96    typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
97
98    /** Class for serializing Materials to / from a .material script.*/
99    class _OgreExport MaterialSerializer
100    {
101    protected:
102        /// Keyword-mapped attribute parsers.
103        typedef std::map<String, ATTRIBUTE_PARSER> AttribParserList;
104
105        MaterialScriptContext mScriptContext;
106
107        /** internal method for parsing a material
108        @returns true if it expects the next line to be a {
109        */
110        bool parseScriptLine(String& line);
111        /** internal method for finding & invoking an attribute parser. */
112        bool invokeParser(String& line, AttribParserList& parsers);
113                /** Internal method for saving a program definition which has been
114                    built up.
115                */
116                void finishProgramDefinition(void);
117        /// Parsers for the root of the material script
118        AttribParserList mRootAttribParsers;
119        /// Parsers for the material section of a script
120        AttribParserList mMaterialAttribParsers;
121        /// Parsers for the technique section of a script
122        AttribParserList mTechniqueAttribParsers;
123        /// Parsers for the pass section of a script
124        AttribParserList mPassAttribParsers;
125        /// Parsers for the texture unit section of a script
126        AttribParserList mTextureUnitAttribParsers;
127        /// Parsers for the program reference section of a script
128        AttribParserList mProgramRefAttribParsers;
129        /// Parsers for the program definition section of a script
130        AttribParserList mProgramAttribParsers;
131        /// Parsers for the program definition section of a script
132        AttribParserList mProgramDefaultParamAttribParsers;
133
134        void writeMaterial(const MaterialPtr& pMat);
135        void writeTechnique(const Technique* pTech);
136        void writePass(const Pass* pPass);
137        void writeVertexProgramRef(const Pass* pPass);
138        void writeShadowCasterVertexProgramRef(const Pass* pPass);
139        void writeShadowReceiverVertexProgramRef(const Pass* pPass);
140        void writeShadowReceiverFragmentProgramRef(const Pass* pPass);
141        void writeFragmentProgramRef(const Pass* pPass);
142        void writeGpuProgramRef(const String& attrib, const GpuProgramPtr& program, const GpuProgramParametersSharedPtr& params);
143        void writeGpuPrograms(void);
144        void writeGPUProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
145            const int level = 4, const bool useMainBuffer = true);
146                void writeNamedGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
147                        const int level = 4, const bool useMainBuffer = true);
148                void writeLowLevelGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
149                        const int level = 4, const bool useMainBuffer = true);
150                void writeGpuProgramParameter(
151                        const String& commandName, const String& identifier, 
152                        const GpuProgramParameters::AutoConstantEntry* autoEntry, 
153                        const GpuProgramParameters::AutoConstantEntry* defaultAutoEntry, 
154                        bool isFloat, size_t physicalIndex, size_t physicalSize,
155                        const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
156                        const int level, const bool useMainBuffer);
157                void writeTextureUnit(const TextureUnitState *pTex);
158                void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
159                void writeSceneBlendFactor(const SceneBlendFactor sbf);
160                void writeCompareFunction(const CompareFunction cf);
161                void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
162                void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
163                void writeLayerBlendSource(const LayerBlendSource lbs);
164               
165                typedef std::multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect> EffectMap;
166
167                void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
168                void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
169                void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
170                void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
171
172        String convertFiltering(FilterOptions fo);
173    public:
174                /** default constructor*/
175                MaterialSerializer();
176                /** default destructor*/
177                virtual ~MaterialSerializer() {};
178
179                /** Queue an in-memory Material to the internal buffer for export.
180                @param pMat Material pointer
181                @param clearQueued If true, any materials already queued will be removed
182                @param exportDefaults If true, attributes which are defaulted will be
183                        included in the script exported, otherwise they will be omitted
184                */
185        void queueForExport(const MaterialPtr& pMat, bool clearQueued = false, 
186                        bool exportDefaults = false);
187        /** Exports queued material(s) to a named material script file.
188        @param filename the file name of the material script to be exported
189                @param includeProgDef If true, vertex program and fragment program
190                        definitions will be written at the top of the material script
191        @param programFilename the file name of the vertex / fragment program
192                        script to be exported. This is only used if there are program definitions
193            to be exported and includeProgDef is false
194                        when calling queueForExport.
195        */
196                void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = "");
197        /** Exports a single in-memory Material to the named material script file.
198        @param exportDefaults if true then exports all values including defaults
199        @param includeProgDef if true includes Gpu shader program definitions in the
200            export material script otherwise if false then program definitions will
201            be exported to a seperate file with name programFilename if
202            programFilename is not empty
203        @param programFilename the file name of the vertex / fragment program
204                        script to be exported. This is only used if includeProgDef is false.
205        */
206        void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false,
207            const bool includeProgDef = false, const String& programFilename = "");
208                /** Returns a string representing the parsed material(s) */
209                const String &getQueuedAsString() const;
210                /** Clears the internal buffer */
211                void clearQueue();
212
213        /** Parses a Material script file passed as a stream.
214        */
215        void parseScript(DataStreamPtr& stream, const String& groupName);
216
217
218
219        private:
220                String mBuffer;
221        String mGpuProgramBuffer;
222        typedef std::set<String> GpuProgramDefinitionContainer;
223        typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator;
224        GpuProgramDefinitionContainer mGpuProgramDefinitionContainer;
225                bool mDefaults;
226
227        void beginSection(unsigned short level, const bool useMainBuffer = true)
228                {
229            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
230                        buffer += "\n";
231            for (unsigned short i = 0; i < level; ++i)
232            {
233                buffer += "\t";
234            }
235            buffer += "{";
236                }
237                void endSection(unsigned short level, const bool useMainBuffer = true)
238                {
239            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
240                        buffer += "\n";
241            for (unsigned short i = 0; i < level; ++i)
242            {
243                buffer += "\t";
244            }
245            buffer += "}";
246                }
247
248                void writeAttribute(unsigned short level, const String& att, const bool useMainBuffer = true)
249                {
250            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
251                        buffer += "\n";
252            for (unsigned short i = 0; i < level; ++i)
253            {
254                buffer += "\t";
255            }
256            buffer += att;
257                }
258
259                void writeValue(const String& val, const bool useMainBuffer = true)
260                {
261            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
262                        buffer += (" " + val);
263                }
264
265                void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true)
266                {
267            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
268                        buffer += "\n";
269            for (unsigned short i = 0; i < level; ++i)
270            {
271                buffer += "\t";
272            }
273            buffer += "// " + comment;
274                }
275
276    };
277}
278#endif
Note: See TracBrowser for help on using the repository browser.