OGRE  2.0
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 "OgreTextureUnitState.h"
33 #include "OgreGpuProgram.h"
34 #include "OgreStringVector.h"
35 #include "OgreHeaderPrefix.h"
36 
37 namespace Ogre {
38 
47  {
57  };
60  {
68  ushort supportsPoseAnimation; // number of simultaneous poses supported
71  };
74  {
87 
88  int techLev, //Keep track of what tech, pass, and state level we are in
89  passLev,
90  stateLev;
92 
94  size_t lineNo;
97  };
99  typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
100 
103  {
104  public:
105 
106  // Material serialize event.
108  {
112  MSE_POST_WRITE
113  };
114 
118  class Listener
119  {
120  public:
121  virtual ~Listener() {}
122 
131  SerializeEvent event, bool& skip, const Material* mat)
132  { (void)ser; (void)event; (void)skip; (void)mat; }
133 
142  SerializeEvent event, bool& skip, const Technique* tech)
143  { (void)ser; (void)event; (void)skip; (void)tech; }
144 
153  SerializeEvent event, bool& skip, const Pass* pass)
154  { (void)ser; (void)event; (void)skip; (void)pass; }
155 
167  SerializeEvent event, bool& skip,
168  const String& attrib,
169  const GpuProgramPtr& program,
170  const GpuProgramParametersSharedPtr& params,
171  GpuProgramParameters* defaultParams)
172  {
173  (void)ser;
174  (void)event;
175  (void)skip;
176  (void)attrib;
177  (void)program;
178  (void)params;
179  (void)defaultParams;
180  }
181 
190  SerializeEvent event, bool& skip, const TextureUnitState* textureUnit)
191  {
192  (void)ser;
193  (void)event;
194  (void)skip;
195  (void)textureUnit;
196  }
197  };
198 
199  protected:
202 
204 
208  bool parseScriptLine(String& line);
210  bool invokeParser(String& line, AttribParserList& parsers);
214  void finishProgramDefinition(void);
231 
234  typedef ListenerList::iterator ListenerListIterator;
235  typedef ListenerList::const_iterator ListenerListConstIterator;
237 
238 
239  void writeMaterial(const MaterialPtr& pMat, const String& materialName = "");
240  void writeTechnique(const Technique* pTech);
241  void writePass(const Pass* pPass);
242  void writeVertexProgramRef(const Pass* pPass);
243  void writeTessellationHullProgramRef(const Pass* pPass);
244  void writeTessellationDomainProgramRef(const Pass* pPass);
245  void writeShadowCasterVertexProgramRef(const Pass* pPass);
246  void writeShadowCasterFragmentProgramRef(const Pass* pPass);
247  void writeGeometryProgramRef(const Pass* pPass);
248  void writeFragmentProgramRef(const Pass* pPass);
249  void writeGpuProgramRef(const String& attrib, const GpuProgramPtr& program, const GpuProgramParametersSharedPtr& params);
250  void writeGpuPrograms(void);
251  void writeGPUProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
252  const unsigned short level = 4, const bool useMainBuffer = true);
253  void writeNamedGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
254  const unsigned short level = 4, const bool useMainBuffer = true);
255  void writeLowLevelGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
256  const unsigned short level = 4, const bool useMainBuffer = true);
257  void writeGpuProgramParameter(
258  const String& commandName, const String& identifier,
259  const GpuProgramParameters::AutoConstantEntry* autoEntry,
260  const GpuProgramParameters::AutoConstantEntry* defaultAutoEntry,
261  bool isFloat, bool isDouble, bool isInt, bool isUnsignedInt,
262  size_t physicalIndex, size_t physicalSize,
263  const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
264  const unsigned short level, const bool useMainBuffer);
265  void writeTextureUnit(const TextureUnitState *pTex);
266  void writeSceneBlendFactor(const SceneBlendFactor c_src, const SceneBlendFactor c_dest,
267  const SceneBlendFactor a_src, const SceneBlendFactor a_dest);
268  void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
269  void writeSceneBlendFactor(const SceneBlendFactor sbf);
270  void writeCompareFunction(const CompareFunction cf);
271  void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
272  void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
273  void writeLayerBlendSource(const LayerBlendSource lbs);
274 
276 
277  void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
278  void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
279  void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
280  void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
281 
282  String convertFiltering(FilterOptions fo);
283 
284 
288  void fireMaterialEvent(SerializeEvent event, bool& skip, const Material* mat);
289 
293  void fireTechniqueEvent(SerializeEvent event, bool& skip, const Technique* tech);
294 
298  void firePassEvent(SerializeEvent event, bool& skip, const Pass* pass);
299 
303  void fireGpuProgramRefEvent(SerializeEvent event, bool& skip,
304  const String& attrib,
305  const GpuProgramPtr& program,
306  const GpuProgramParametersSharedPtr& params,
307  GpuProgramParameters* defaultParams);
308 
309 
313  void fireTextureUnitStateEvent(SerializeEvent event, bool& skip, const TextureUnitState* textureUnit);
314 
315  public:
319  virtual ~MaterialSerializer() {}
320 
329  void queueForExport(const MaterialPtr& pMat, bool clearQueued = false,
330  bool exportDefaults = false, const String& materialName = "");
340  void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = "");
352  void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false,
353  const bool includeProgDef = false, const String& programFilename = "",
354  const String& materialName = "");
356  const String &getQueuedAsString() const;
358  void clearQueue();
359 
362  void parseScript(DataStreamPtr& stream, const String& groupName);
363 
367  void addListener(Listener* listener);
368 
372  void removeListener(Listener* listener);
373 
374  private:
378  typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator;
380  bool mDefaults;
381 
382  public:
383  void beginSection(unsigned short level, const bool useMainBuffer = true)
384  {
385  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
386  buffer += "\n";
387  for (unsigned short i = 0; i < level; ++i)
388  {
389  buffer += "\t";
390  }
391  buffer += "{";
392  }
393  void endSection(unsigned short level, const bool useMainBuffer = true)
394  {
395  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
396  buffer += "\n";
397  for (unsigned short i = 0; i < level; ++i)
398  {
399  buffer += "\t";
400  }
401  buffer += "}";
402  }
403 
404  void writeAttribute(unsigned short level, const String& att, const bool useMainBuffer = true)
405  {
406  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
407  buffer += "\n";
408  for (unsigned short i = 0; i < level; ++i)
409  {
410  buffer += "\t";
411  }
412  buffer += att;
413  }
414 
415  void writeValue(const String& val, const bool useMainBuffer = true)
416  {
417  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
418  buffer += (" " + val);
419  }
420 
421  String quoteWord(const String& val)
422  {
423  if (val.find_first_of(" \t") != String::npos)
424  return ("\"" + val + "\"");
425  else return val;
426  }
427 
428 
429  void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true)
430  {
431  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
432  buffer += "\n";
433  for (unsigned short i = 0; i < level; ++i)
434  {
435  buffer += "\t";
436  }
437  buffer += "// " + comment;
438  }
439 
440 
441 
442  };
445 }
446 
447 #include "OgreHeaderSuffix.h"
448 
449 #endif
void writeAttribute(unsigned short level, const String &att, const bool useMainBuffer=true)
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:255
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:84
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:50
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:78
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.
AttribParserList mProgramDefaultParamAttribParsers
Parsers for the program definition section of a script.
String quoteWord(const String &val)
_StringBase String
Definition: OgreCommon.h:53
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:131
map< String, String >::type AliasTextureNamePairList
Alias / Texture name pair (first = alias, second = texture name)
Definition: OgreCommon.h:583
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:74
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.