OGRE  1.9
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreMaterialSerializer.h
Go to the documentation of this file.
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-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __MaterialSerializer_H__
29 #define __MaterialSerializer_H__
30 
31 #include "OgrePrerequisites.h"
32 #include "OgreMaterial.h"
33 #include "OgreBlendMode.h"
34 #include "OgreTextureUnitState.h"
35 #include "OgreGpuProgram.h"
36 #include "OgreStringVector.h"
37 #include "OgreHeaderPrefix.h"
38 
39 namespace Ogre {
40 
49  {
59  };
62  {
70  ushort supportsPoseAnimation; // number of simultaneous poses supported
73  };
76  {
91 
92  int techLev, //Keep track of what tech, pass, and state level we are in
93  passLev,
94  stateLev;
96 
98  size_t lineNo;
101  };
103  typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
104 
107  {
108  public:
109 
110  // Material serialize event.
112  {
116  MSE_POST_WRITE
117  };
118 
122  class Listener
123  {
124  public:
125  virtual ~Listener() {}
126 
135  SerializeEvent event, bool& skip, const Material* mat)
136  { (void)ser; (void)event; (void)skip; (void)mat; }
137 
146  SerializeEvent event, bool& skip, const Technique* tech)
147  { (void)ser; (void)event; (void)skip; (void)tech; }
148 
157  SerializeEvent event, bool& skip, const Pass* pass)
158  { (void)ser; (void)event; (void)skip; (void)pass; }
159 
171  SerializeEvent event, bool& skip,
172  const String& attrib,
173  const GpuProgramPtr& program,
174  const GpuProgramParametersSharedPtr& params,
175  GpuProgramParameters* defaultParams)
176  {
177  (void)ser;
178  (void)event;
179  (void)skip;
180  (void)attrib;
181  (void)program;
182  (void)params;
183  (void)defaultParams;
184  }
185 
194  SerializeEvent event, bool& skip, const TextureUnitState* textureUnit)
195  {
196  (void)ser;
197  (void)event;
198  (void)skip;
199  (void)textureUnit;
200  }
201  };
202 
203  protected:
206 
208 
212  bool parseScriptLine(String& line);
214  bool invokeParser(String& line, AttribParserList& parsers);
218  void finishProgramDefinition(void);
235 
238  typedef ListenerList::iterator ListenerListIterator;
239  typedef ListenerList::const_iterator ListenerListConstIterator;
241 
242 
243  void writeMaterial(const MaterialPtr& pMat, const String& materialName = "");
244  void writeTechnique(const Technique* pTech);
245  void writePass(const Pass* pPass);
246  void writeVertexProgramRef(const Pass* pPass);
247  void writeShadowCasterVertexProgramRef(const Pass* pPass);
248  void writeShadowCasterFragmentProgramRef(const Pass* pPass);
249  void writeShadowReceiverVertexProgramRef(const Pass* pPass);
250  void writeShadowReceiverFragmentProgramRef(const Pass* pPass);
251  void writeFragmentProgramRef(const Pass* pPass);
252  void writeGpuProgramRef(const String& attrib, const GpuProgramPtr& program, const GpuProgramParametersSharedPtr& params);
253  void writeGpuPrograms(void);
254  void writeGPUProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
255  const unsigned short level = 4, const bool useMainBuffer = true);
256  void writeNamedGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
257  const unsigned short level = 4, const bool useMainBuffer = true);
258  void writeLowLevelGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
259  const unsigned short level = 4, const bool useMainBuffer = true);
260  void writeGpuProgramParameter(
261  const String& commandName, const String& identifier,
262  const GpuProgramParameters::AutoConstantEntry* autoEntry,
263  const GpuProgramParameters::AutoConstantEntry* defaultAutoEntry,
264  bool isFloat, bool isDouble, size_t physicalIndex, size_t physicalSize,
265  const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
266  const unsigned short level, const bool useMainBuffer);
267  void writeTextureUnit(const TextureUnitState *pTex);
268  void writeSceneBlendFactor(const SceneBlendFactor c_src, const SceneBlendFactor c_dest,
269  const SceneBlendFactor a_src, const SceneBlendFactor a_dest);
270  void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
271  void writeSceneBlendFactor(const SceneBlendFactor sbf);
272  void writeCompareFunction(const CompareFunction cf);
273  void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
274  void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
275  void writeLayerBlendSource(const LayerBlendSource lbs);
276 
278 
279  void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
280  void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
281  void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
282  void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
283 
284  String convertFiltering(FilterOptions fo);
285 
286 
290  void fireMaterialEvent(SerializeEvent event, bool& skip, const Material* mat);
291 
295  void fireTechniqueEvent(SerializeEvent event, bool& skip, const Technique* tech);
296 
300  void firePassEvent(SerializeEvent event, bool& skip, const Pass* pass);
301 
305  void fireGpuProgramRefEvent(SerializeEvent event, bool& skip,
306  const String& attrib,
307  const GpuProgramPtr& program,
308  const GpuProgramParametersSharedPtr& params,
309  GpuProgramParameters* defaultParams);
310 
311 
315  void fireTextureUnitStateEvent(SerializeEvent event, bool& skip, const TextureUnitState* textureUnit);
316 
317  public:
321  virtual ~MaterialSerializer() {}
322 
331  void queueForExport(const MaterialPtr& pMat, bool clearQueued = false,
332  bool exportDefaults = false, const String& materialName = "");
342  void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = "");
354  void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false,
355  const bool includeProgDef = false, const String& programFilename = "",
356  const String& materialName = "");
358  const String &getQueuedAsString() const;
360  void clearQueue();
361 
364  void parseScript(DataStreamPtr& stream, const String& groupName);
365 
369  void addListener(Listener* listener);
370 
374  void removeListener(Listener* listener);
375 
376  private:
380  typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator;
382  bool mDefaults;
383 
384  public:
385  void beginSection(unsigned short level, const bool useMainBuffer = true)
386  {
387  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
388  buffer += "\n";
389  for (unsigned short i = 0; i < level; ++i)
390  {
391  buffer += "\t";
392  }
393  buffer += "{";
394  }
395  void endSection(unsigned short level, const bool useMainBuffer = true)
396  {
397  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
398  buffer += "\n";
399  for (unsigned short i = 0; i < level; ++i)
400  {
401  buffer += "\t";
402  }
403  buffer += "}";
404  }
405 
406  void writeAttribute(unsigned short level, const String& att, const bool useMainBuffer = true)
407  {
408  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
409  buffer += "\n";
410  for (unsigned short i = 0; i < level; ++i)
411  {
412  buffer += "\t";
413  }
414  buffer += att;
415  }
416 
417  void writeValue(const String& val, const bool useMainBuffer = true)
418  {
419  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
420  buffer += (" " + val);
421  }
422 
423  String quoteWord(const String& val)
424  {
425  if (val.find_first_of(" \t") != String::npos)
426  return ("\"" + val + "\"");
427  else return val;
428  }
429 
430 
431  void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true)
432  {
433  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
434  buffer += "\n";
435  for (unsigned short i = 0; i < level; ++i)
436  {
437  buffer += "\t";
438  }
439  buffer += "// " + comment;
440  }
441 
442 
443 
444  };
447 }
448 
449 #include "OgreHeaderSuffix.h"
450 
451 #endif
void writeAttribute(unsigned short level, const String &att, const bool useMainBuffer=true)
bool isVertexProgramShadowReceiver
When referencing, are we in context of shadow caster.
AttribParserList mMaterialAttribParsers
Parsers for the material section of a script.
virtual ~MaterialSerializer()
default destructor
MaterialScriptSection
Enum to identify material sections.
void gpuProgramRefEventRaised(MaterialSerializer *ser, SerializeEvent event, bool &skip, const String &attrib, const GpuProgramPtr &program, const GpuProgramParametersSharedPtr &params, GpuProgramParameters *defaultParams)
Called when GPU program reference section event raised.
#define _OgreExport
Definition: OgrePlatform.h:260
std::multimap< K, V, P, A > type
AliasTextureNamePairList textureAliases
Struct for holding a program definition which is in progress.
void writeValue(const String &val, const bool useMainBuffer=true)
bool isFragmentProgramShadowCaster
When referencing, are we in context of shadow caster.
ListenerList::const_iterator ListenerListConstIterator
AttribParserList mProgramRefAttribParsers
Parsers for the program reference section of a script.
Class that allows listening in on the various stages of material serialization process.
virtual void textureUnitStateEventRaised(MaterialSerializer *ser, SerializeEvent event, bool &skip, const TextureUnitState *textureUnit)
Called when texture unit state section event raised.
Class representing colour.
GpuProgramType
Enumerates the types of programs which can run on the GPU.
Internal structure defining a texture effect.
set< String >::type GpuProgramDefinitionContainer
Class encapsulates rendering properties of an object.
Definition: OgreMaterial.h:88
int techLev
This is used while defining a program.
vector< Listener * >::type ListenerList
Listeners list of this Serializer.
vector< std::pair< String, String > >::type customParameters
Class representing an approach to rendering this particular Material.
Definition: OgreTechnique.h:53
SceneBlendFactor
Blending factors for manually blending objects with the scene.
MaterialScriptProgramDefinition * programDef
MaterialScriptContext mScriptContext
Class defining a single pass of a Technique (of a Material), i.e.
Definition: OgrePass.h:80
void writeComment(unsigned short level, const String &comment, const bool useMainBuffer=true)
bool(* ATTRIBUTE_PARSER)(String &params, MaterialScriptContext &context)
Function def for material attribute parser; return value determines if the next line should be {...
Class for serializing Materials to / from a .material script.
Structure recording the use of an automatic parameter.
Struct for holding the script context while parsing.
virtual void techniqueEventRaised(MaterialSerializer *ser, SerializeEvent event, bool &skip, const Technique *tech)
Called when technique section event raised.
size_t lineNo
Error reporting state.
virtual void passEventRaised(MaterialSerializer *ser, SerializeEvent event, bool &skip, const Pass *pass)
Called when pass section event raised.
AttribParserList mProgramAttribParsers
Parsers for the program definition section of a script.
AttribParserList mTextureUnitAttribParsers
Parsers for the texture unit section of a script.
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Collects together the program parameters used for a GpuProgram.
map< String, ATTRIBUTE_PARSER >::type AttribParserList
Keyword-mapped attribute parsers.
ListenerList::iterator ListenerListIterator
vector< String >::type StringVector
GpuProgramDefinitionContainer mGpuProgramDefinitionContainer
unsigned short ushort
LayerBlendSource
List of valid sources of values for blending operations used in TextureUnitState::setColourOperation ...
AttribParserList mPassAttribParsers
Parsers for the pass section of a script.
multimap< TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect >::type EffectMap
GpuProgramDefinitionContainer::iterator GpuProgramDefIterator
void beginSection(unsigned short level, const bool useMainBuffer=true)
virtual void materialEventRaised(MaterialSerializer *ser, SerializeEvent event, bool &skip, const Material *mat)
Called when material section event raised.
AttribParserList mRootAttribParsers
Parsers for the root of the material script.
bool isFragmentProgramShadowReceiver
When referencing, are we in context of shadow caster.
AttribParserList mProgramDefaultParamAttribParsers
Parsers for the program definition section of a script.
String quoteWord(const String &val)
_StringBase String
bool isVertexProgramShadowCaster
Used when referencing a program, not when defining it.
LayerBlendOperationEx
Expert list of valid texture blending operations, for use with TextureUnitState::setColourOperationEx...
Definition: OgreBlendMode.h:75
FilterOptions
Filtering options for textures / mipmaps.
Definition: OgreCommon.h:100
map< String, String >::type AliasTextureNamePairList
Alias / Texture name pair (first = alias, second = texture name)
Definition: OgreCommon.h:553
GpuProgramParametersSharedPtr programParams
When referencing, are we in context of shadow caster.
CompareFunction
Comparison functions used for the depth/stencil buffer operations and others.
Definition: OgreCommon.h:64
Class representing the state of a single texture unit during a Pass of a Technique, of a Material.
void endSection(unsigned short level, const bool useMainBuffer=true)
AttribParserList mTechniqueAttribParsers
Parsers for the technique section of a script.