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-2013 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
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);
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);
251  void writeFragmentProgramRef(const Pass* pPass);
252  void writeGpuProgramRef(const String& attrib, const GpuProgramPtr& program, const GpuProgramParametersSharedPtr& params);
253  void writeGpuPrograms(void);
255  const unsigned short level = 4, const bool useMainBuffer = true);
257  const unsigned short level = 4, const bool useMainBuffer = true);
259  const unsigned short level = 4, const bool useMainBuffer = true);
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);
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);
273  void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
276 
278 
283 
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
#define _OgreExport
Definition: OgrePlatform.h:257
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Class representing colour.
Structure recording the use of an automatic parameter.
Collects together the program parameters used for a GpuProgram.
Class that allows listening in on the various stages of material serialization process.
virtual void materialEventRaised(MaterialSerializer *ser, SerializeEvent event, bool &skip, const Material *mat)
Called when material section event raised.
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.
virtual void passEventRaised(MaterialSerializer *ser, SerializeEvent event, bool &skip, const Pass *pass)
Called when pass section event raised.
virtual void textureUnitStateEventRaised(MaterialSerializer *ser, SerializeEvent event, bool &skip, const TextureUnitState *textureUnit)
Called when texture unit state section event raised.
virtual void techniqueEventRaised(MaterialSerializer *ser, SerializeEvent event, bool &skip, const Technique *tech)
Called when technique section event raised.
Class for serializing Materials to / from a .material script.
ListenerList::iterator ListenerListIterator
void writeScrollEffect(const TextureUnitState::TextureEffect &effect, const TextureUnitState *pTex)
void writeShadowCasterVertexProgramRef(const Pass *pPass)
ListenerList::const_iterator ListenerListConstIterator
void parseScript(DataStreamPtr &stream, const String &groupName)
Parses a Material script file passed as a stream.
void removeListener(Listener *listener)
Remove a listener from this Serializer.
void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest)
void writeLayerBlendSource(const LayerBlendSource lbs)
MaterialSerializer()
default constructor
void fireMaterialEvent(SerializeEvent event, bool &skip, const Material *mat)
Internal methods that invokes registered listeners callback.
void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect &effect, const TextureUnitState *pTex)
void fireTechniqueEvent(SerializeEvent event, bool &skip, const Technique *tech)
Internal methods that invokes registered listeners callback.
map< String, ATTRIBUTE_PARSER >::type AttribParserList
Keyword-mapped attribute parsers.
void writeSceneBlendFactor(const SceneBlendFactor c_src, const SceneBlendFactor c_dest, const SceneBlendFactor a_src, const SceneBlendFactor a_dest)
vector< Listener * >::type ListenerList
Listeners list of this Serializer.
void writeShadowCasterFragmentProgramRef(const Pass *pPass)
void writeVertexProgramRef(const Pass *pPass)
String convertFiltering(FilterOptions fo)
void finishProgramDefinition(void)
Internal method for saving a program definition which has been built up.
void writePass(const Pass *pPass)
void writeTextureUnit(const TextureUnitState *pTex)
void writeAttribute(unsigned short level, const String &att, const bool useMainBuffer=true)
void writeNamedGpuProgramParameters(const GpuProgramParametersSharedPtr &params, GpuProgramParameters *defaultParams, const unsigned short level=4, const bool useMainBuffer=true)
void writeTechnique(const Technique *pTech)
void writeColourValue(const ColourValue &colour, bool writeAlpha=false)
multimap< TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect >::type EffectMap
AttribParserList mMaterialAttribParsers
Parsers for the material section of a script.
bool invokeParser(String &line, AttribParserList &parsers)
internal method for finding & invoking an attribute parser.
void writeRotationEffect(const TextureUnitState::TextureEffect &effect, const TextureUnitState *pTex)
AttribParserList mPassAttribParsers
Parsers for the pass section of a script.
AttribParserList mProgramDefaultParamAttribParsers
Parsers for the program definition section of a script.
void queueForExport(const MaterialPtr &pMat, bool clearQueued=false, bool exportDefaults=false, const String &materialName="")
Queue an in-memory Material to the internal buffer for export.
AttribParserList mProgramAttribParsers
Parsers for the program definition section of a script.
void writeLayerBlendOperationEx(const LayerBlendOperationEx op)
AttribParserList mRootAttribParsers
Parsers for the root of the material script.
set< String >::type GpuProgramDefinitionContainer
AttribParserList mTextureUnitAttribParsers
Parsers for the texture unit section of a script.
void beginSection(unsigned short level, const bool useMainBuffer=true)
void exportMaterial(const MaterialPtr &pMat, const String &filename, bool exportDefaults=false, const bool includeProgDef=false, const String &programFilename="", const String &materialName="")
Exports a single in-memory Material to the named material script file.
void writeGpuProgramRef(const String &attrib, const GpuProgramPtr &program, const GpuProgramParametersSharedPtr &params)
void firePassEvent(SerializeEvent event, bool &skip, const Pass *pass)
Internal methods that invokes registered listeners callback.
GpuProgramDefinitionContainer::iterator GpuProgramDefIterator
void writeMaterial(const MaterialPtr &pMat, const String &materialName="")
String quoteWord(const String &val)
AttribParserList mTechniqueAttribParsers
Parsers for the technique section of a script.
void exportQueued(const String &filename, const bool includeProgDef=false, const String &programFilename="")
Exports queued material(s) to a named material script file.
void writeShadowReceiverVertexProgramRef(const Pass *pPass)
void fireGpuProgramRefEvent(SerializeEvent event, bool &skip, const String &attrib, const GpuProgramPtr &program, const GpuProgramParametersSharedPtr &params, GpuProgramParameters *defaultParams)
Internal methods that invokes registered listeners callback.
void writeFragmentProgramRef(const Pass *pPass)
void writeComment(unsigned short level, const String &comment, const bool useMainBuffer=true)
void writeTransformEffect(const TextureUnitState::TextureEffect &effect, const TextureUnitState *pTex)
void writeCompareFunction(const CompareFunction cf)
void clearQueue()
Clears the internal buffer.
MaterialScriptContext mScriptContext
void fireTextureUnitStateEvent(SerializeEvent event, bool &skip, const TextureUnitState *textureUnit)
Internal methods that invokes registered listeners callback.
AttribParserList mProgramRefAttribParsers
Parsers for the program reference section of a script.
void writeGPUProgramParameters(const GpuProgramParametersSharedPtr &params, GpuProgramParameters *defaultParams, const unsigned short level=4, const bool useMainBuffer=true)
void writeValue(const String &val, const bool useMainBuffer=true)
virtual ~MaterialSerializer()
default destructor
void writeSceneBlendFactor(const SceneBlendFactor sbf)
void addListener(Listener *listener)
Register a listener to this Serializer.
void endSection(unsigned short level, const bool useMainBuffer=true)
bool parseScriptLine(String &line)
internal method for parsing a material
void writeLowLevelGpuProgramParameters(const GpuProgramParametersSharedPtr &params, GpuProgramParameters *defaultParams, const unsigned short level=4, const bool useMainBuffer=true)
void writeGpuProgramParameter(const String &commandName, const String &identifier, const GpuProgramParameters::AutoConstantEntry *autoEntry, const GpuProgramParameters::AutoConstantEntry *defaultAutoEntry, bool isFloat, bool isDouble, size_t physicalIndex, size_t physicalSize, const GpuProgramParametersSharedPtr &params, GpuProgramParameters *defaultParams, const unsigned short level, const bool useMainBuffer)
const String & getQueuedAsString() const
Returns a string representing the parsed material(s)
GpuProgramDefinitionContainer mGpuProgramDefinitionContainer
void writeShadowReceiverFragmentProgramRef(const Pass *pPass)
Class encapsulates rendering properties of an object.
Definition: OgreMaterial.h:89
Class defining a single pass of a Technique (of a Material), i.e.
Definition: OgrePass.h:81
Class representing an approach to rendering this particular Material.
Definition: OgreTechnique.h:54
Class representing the state of a single texture unit during a Pass of a Technique,...
CompareFunction
Comparison functions used for the depth/stencil buffer operations and others.
Definition: OgreCommon.h:65
FilterOptions
Filtering options for textures / mipmaps.
Definition: OgreCommon.h:101
vector< String >::type StringVector
map< String, String >::type AliasTextureNamePairList
Alias / Texture name pair (first = alias, second = texture name)
Definition: OgreCommon.h:553
bool(* ATTRIBUTE_PARSER)(String &params, MaterialScriptContext &context)
Function def for material attribute parser; return value determines if the next line should be {.
LayerBlendOperationEx
Expert list of valid texture blending operations, for use with TextureUnitState::setColourOperationEx...
Definition: OgreBlendMode.h:75
SceneBlendFactor
Blending factors for manually blending objects with the scene.
MaterialScriptSection
Enum to identify material sections.
LayerBlendSource
List of valid sources of values for blending operations used in TextureUnitState::setColourOperation ...
GpuProgramType
Enumerates the types of programs which can run on the GPU.
_StringBase String
unsigned short ushort
Struct for holding the script context while parsing.
int techLev
This is used while defining a program.
bool isFragmentProgramShadowReceiver
When referencing, are we in context of shadow caster.
MaterialScriptProgramDefinition * programDef
AliasTextureNamePairList textureAliases
bool isFragmentProgramShadowCaster
When referencing, are we in context of shadow caster.
GpuProgramParametersSharedPtr programParams
When referencing, are we in context of shadow caster.
size_t lineNo
Error reporting state.
bool isVertexProgramShadowReceiver
When referencing, are we in context of shadow caster.
bool isVertexProgramShadowCaster
Used when referencing a program, not when defining it.
Struct for holding a program definition which is in progress.
vector< std::pair< String, String > >::type customParameters
Internal structure defining a texture effect.
std::multimap< K, V, P, A > type

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.