1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://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 |
|
---|
35 | namespace 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 | std::map<String, String> customParameters;
|
---|
60 | };
|
---|
61 | /** Struct for holding the script context while parsing. */
|
---|
62 | struct MaterialScriptContext
|
---|
63 | {
|
---|
64 | MaterialScriptSection section;
|
---|
65 | String groupName;
|
---|
66 | MaterialPtr material;
|
---|
67 | Technique* technique;
|
---|
68 | Pass* pass;
|
---|
69 | TextureUnitState* textureUnit;
|
---|
70 | GpuProgramPtr program; // used when referencing a program, not when defining it
|
---|
71 | bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
|
---|
72 | bool isProgramShadowReceiver; // when referencing, are we in context of shadow caster
|
---|
73 | GpuProgramParametersSharedPtr programParams;
|
---|
74 | MaterialScriptProgramDefinition* programDef; // this is used while defining a program
|
---|
75 |
|
---|
76 | int techLev, //Keep track of what tech, pass, and state level we are in
|
---|
77 | passLev,
|
---|
78 | stateLev;
|
---|
79 | StringVector defaultParamLines;
|
---|
80 |
|
---|
81 | // Error reporting state
|
---|
82 | size_t lineNo;
|
---|
83 | String filename;
|
---|
84 | };
|
---|
85 | /// Function def for material attribute parser; return value determines if the next line should be {
|
---|
86 | typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
|
---|
87 |
|
---|
88 | /** Class for serializing Materials to / from a .material script.*/
|
---|
89 | class _OgreExport MaterialSerializer
|
---|
90 | {
|
---|
91 | protected:
|
---|
92 | /// Keyword-mapped attribute parsers.
|
---|
93 | typedef std::map<String, ATTRIBUTE_PARSER> AttribParserList;
|
---|
94 |
|
---|
95 | MaterialScriptContext mScriptContext;
|
---|
96 |
|
---|
97 | /** internal method for parsing a material
|
---|
98 | @returns true if it expects the next line to be a {
|
---|
99 | */
|
---|
100 | bool parseScriptLine(String& line);
|
---|
101 | /** internal method for finding & invoking an attribute parser. */
|
---|
102 | bool invokeParser(String& line, AttribParserList& parsers);
|
---|
103 | /** Internal method for saving a program definition which has been
|
---|
104 | built up.
|
---|
105 | */
|
---|
106 | void finishProgramDefinition(void);
|
---|
107 | /// Parsers for the root of the material script
|
---|
108 | AttribParserList mRootAttribParsers;
|
---|
109 | /// Parsers for the material section of a script
|
---|
110 | AttribParserList mMaterialAttribParsers;
|
---|
111 | /// Parsers for the technique section of a script
|
---|
112 | AttribParserList mTechniqueAttribParsers;
|
---|
113 | /// Parsers for the pass section of a script
|
---|
114 | AttribParserList mPassAttribParsers;
|
---|
115 | /// Parsers for the texture unit section of a script
|
---|
116 | AttribParserList mTextureUnitAttribParsers;
|
---|
117 | /// Parsers for the program reference section of a script
|
---|
118 | AttribParserList mProgramRefAttribParsers;
|
---|
119 | /// Parsers for the program definition section of a script
|
---|
120 | AttribParserList mProgramAttribParsers;
|
---|
121 | /// Parsers for the program definition section of a script
|
---|
122 | AttribParserList mProgramDefaultParamAttribParsers;
|
---|
123 |
|
---|
124 | void writeMaterial(const MaterialPtr& pMat);
|
---|
125 | void writeTechnique(const Technique* pTech);
|
---|
126 | void writePass(const Pass* pPass);
|
---|
127 | void writeTextureUnit(const TextureUnitState *pTex);
|
---|
128 |
|
---|
129 | void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
|
---|
130 | void writeSceneBlendFactor(const SceneBlendFactor sbf);
|
---|
131 | void writeCompareFunction(const CompareFunction cf);
|
---|
132 | void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
|
---|
133 | void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
|
---|
134 | void writeLayerBlendSource(const LayerBlendSource lbs);
|
---|
135 |
|
---|
136 | typedef std::multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect> EffectMap;
|
---|
137 |
|
---|
138 | void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
|
---|
139 | void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
|
---|
140 | void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
|
---|
141 | void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
|
---|
142 |
|
---|
143 | String convertFiltering(FilterOptions fo);
|
---|
144 | public:
|
---|
145 | /** default constructor*/
|
---|
146 | MaterialSerializer();
|
---|
147 | /** default destructor*/
|
---|
148 | virtual ~MaterialSerializer() {};
|
---|
149 |
|
---|
150 | /** Queue an in-memory Material to the internal buffer for export.*/
|
---|
151 | void queueForExport(const MaterialPtr& pMat, bool clearQueued = false, bool exportDefaults = false);
|
---|
152 | /** Exports queued material(s) to a named material script file. */
|
---|
153 | void exportQueued(const String& filename);
|
---|
154 | /** Exports an in-memory Material to the named material script file. */
|
---|
155 | void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false);
|
---|
156 | /** Returns a string representing the parsed material(s) */
|
---|
157 | const String &getQueuedAsString() const;
|
---|
158 | /** Clears the internal buffer */
|
---|
159 | void clearQueue();
|
---|
160 |
|
---|
161 | /** Parses a Material script file passed as a stream.
|
---|
162 | */
|
---|
163 | void parseScript(DataStreamPtr& stream, const String& groupName);
|
---|
164 |
|
---|
165 |
|
---|
166 |
|
---|
167 | private:
|
---|
168 | String mBuffer;
|
---|
169 | bool mDefaults;
|
---|
170 |
|
---|
171 | void beginSection(unsigned short level)
|
---|
172 | {
|
---|
173 | mBuffer += "\n";
|
---|
174 | for (unsigned short i = 0; i < level; ++i)
|
---|
175 | {
|
---|
176 | mBuffer += "\t";
|
---|
177 | }
|
---|
178 | mBuffer += "{";
|
---|
179 | }
|
---|
180 | void endSection(unsigned short level)
|
---|
181 | {
|
---|
182 | mBuffer += "\n";
|
---|
183 | for (unsigned short i = 0; i < level; ++i)
|
---|
184 | {
|
---|
185 | mBuffer += "\t";
|
---|
186 | }
|
---|
187 | mBuffer += "}";
|
---|
188 | }
|
---|
189 |
|
---|
190 | void writeAttribute(unsigned short level, const String& att)
|
---|
191 | {
|
---|
192 | mBuffer += "\n";
|
---|
193 | for (unsigned short i = 0; i < level; ++i)
|
---|
194 | {
|
---|
195 | mBuffer += "\t";
|
---|
196 | }
|
---|
197 | mBuffer += att;
|
---|
198 | }
|
---|
199 |
|
---|
200 | void writeValue(const String& val)
|
---|
201 | {
|
---|
202 | mBuffer += (" " + val);
|
---|
203 | }
|
---|
204 |
|
---|
205 | void writeComment(unsigned short level, const String& comment)
|
---|
206 | {
|
---|
207 | mBuffer += "\n";
|
---|
208 | for (unsigned short i = 0; i < level; ++i)
|
---|
209 | {
|
---|
210 | mBuffer += "\t";
|
---|
211 | }
|
---|
212 | mBuffer += "// " + comment;
|
---|
213 | }
|
---|
214 |
|
---|
215 | };
|
---|
216 | }
|
---|
217 | #endif
|
---|