OGRE  1.7
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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-2011 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 
38 namespace Ogre {
39 
48  {
58  };
61  {
69  ushort supportsPoseAnimation; // number of simultaneous poses supported
72  };
75  {
82  GpuProgramPtr program; // used when referencing a program, not when defining it
83  bool isProgramShadowCaster; // when referencing, are we in context of shadow caster
84  bool isVertexProgramShadowReceiver; // when referencing, are we in context of shadow caster
85  bool isFragmentProgramShadowReceiver; // when referencing, are we in context of shadow caster
88  MaterialScriptProgramDefinition* programDef; // this is used while defining a program
89 
90  int techLev, //Keep track of what tech, pass, and state level we are in
91  passLev,
92  stateLev;
94 
95  // Error reporting state
96  size_t lineNo;
99  };
101  typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context);
102 
105  {
106  public:
107 
108  // Material serizliae event.
110  {
114  MSE_POST_WRITE
115  };
116 
120  class Listener
121  {
122  public:
123  virtual ~Listener() {}
124 
133  SerializeEvent event, bool& skip, const Material* mat)
134  { (void)ser; (void)event; (void)skip; (void)mat; }
135 
144  SerializeEvent event, bool& skip, const Technique* tech)
145  { (void)ser; (void)event; (void)skip; (void)tech; }
146 
155  SerializeEvent event, bool& skip, const Pass* pass)
156  { (void)ser; (void)event; (void)skip; (void)pass; }
157 
169  SerializeEvent event, bool& skip,
170  const String& attrib,
171  const GpuProgramPtr& program,
172  const GpuProgramParametersSharedPtr& params,
173  GpuProgramParameters* defaultParams)
174  {
175  (void)ser;
176  (void)event;
177  (void)skip;
178  (void)attrib;
179  (void)program;
180  (void)params;
181  (void)defaultParams;
182  }
183 
192  SerializeEvent event, bool& skip, const TextureUnitState* textureUnit)
193  {
194  (void)ser;
195  (void)event;
196  (void)skip;
197  (void)textureUnit;
198  }
199  };
200 
201  protected:
204 
206 
210  bool parseScriptLine(String& line);
212  bool invokeParser(String& line, AttribParserList& parsers);
216  void finishProgramDefinition(void);
233 
236  typedef ListenerList::iterator ListenerListIterator;
237  typedef ListenerList::const_iterator ListenerListConstIterator;
239 
240 
241  void writeMaterial(const MaterialPtr& pMat, const String& materialName = "");
242  void writeTechnique(const Technique* pTech);
243  void writePass(const Pass* pPass);
244  void writeVertexProgramRef(const Pass* pPass);
245  void writeShadowCasterVertexProgramRef(const Pass* pPass);
246  void writeShadowReceiverVertexProgramRef(const Pass* pPass);
247  void writeShadowReceiverFragmentProgramRef(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, size_t physicalIndex, size_t physicalSize,
262  const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
263  const unsigned short level, const bool useMainBuffer);
264  void writeTextureUnit(const TextureUnitState *pTex);
265  void writeSceneBlendFactor(const SceneBlendFactor c_src, const SceneBlendFactor c_dest,
266  const SceneBlendFactor a_src, const SceneBlendFactor a_dest);
267  void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
268  void writeSceneBlendFactor(const SceneBlendFactor sbf);
269  void writeCompareFunction(const CompareFunction cf);
270  void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
271  void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
272  void writeLayerBlendSource(const LayerBlendSource lbs);
273 
275 
276  void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
277  void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
278  void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
279  void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
280 
281  String convertFiltering(FilterOptions fo);
282 
283 
287  void fireMaterialEvent(SerializeEvent event, bool& skip, const Material* mat);
288 
292  void fireTechniqueEvent(SerializeEvent event, bool& skip, const Technique* tech);
293 
297  void firePassEvent(SerializeEvent event, bool& skip, const Pass* pass);
298 
302  void fireGpuProgramRefEvent(SerializeEvent event, bool& skip,
303  const String& attrib,
304  const GpuProgramPtr& program,
305  const GpuProgramParametersSharedPtr& params,
306  GpuProgramParameters* defaultParams);
307 
308 
312  void fireTextureUnitStateEvent(SerializeEvent event, bool& skip, const TextureUnitState* textureUnit);
313 
314  public:
318  virtual ~MaterialSerializer() {}
319 
328  void queueForExport(const MaterialPtr& pMat, bool clearQueued = false,
329  bool exportDefaults = false, const String& materialName = "");
339  void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = "");
351  void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false,
352  const bool includeProgDef = false, const String& programFilename = "",
353  const String& materialName = "");
355  const String &getQueuedAsString() const;
357  void clearQueue();
358 
361  void parseScript(DataStreamPtr& stream, const String& groupName);
362 
366  void addListener(Listener* listener);
367 
371  void removeListener(Listener* listener);
372 
373  private:
377  typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator;
379  bool mDefaults;
380 
381  public:
382  void beginSection(unsigned short level, const bool useMainBuffer = true)
383  {
384  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
385  buffer += "\n";
386  for (unsigned short i = 0; i < level; ++i)
387  {
388  buffer += "\t";
389  }
390  buffer += "{";
391  }
392  void endSection(unsigned short level, const bool useMainBuffer = true)
393  {
394  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
395  buffer += "\n";
396  for (unsigned short i = 0; i < level; ++i)
397  {
398  buffer += "\t";
399  }
400  buffer += "}";
401  }
402 
403  void writeAttribute(unsigned short level, const String& att, const bool useMainBuffer = true)
404  {
405  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
406  buffer += "\n";
407  for (unsigned short i = 0; i < level; ++i)
408  {
409  buffer += "\t";
410  }
411  buffer += att;
412  }
413 
414  void writeValue(const String& val, const bool useMainBuffer = true)
415  {
416  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
417  buffer += (" " + val);
418  }
419 
420  void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true)
421  {
422  String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
423  buffer += "\n";
424  for (unsigned short i = 0; i < level; ++i)
425  {
426  buffer += "\t";
427  }
428  buffer += "// " + comment;
429  }
430 
431 
432 
433  };
436 }
437 #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:203
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)
ListenerList::const_iterator ListenerListConstIterator
Specialisation of SharedPtr to allow SharedPtr to be assigned to GpuProgramPtr.
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
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.
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.
_StringBase String
LayerBlendOperationEx
Expert list of valid texture blending operations, for use with TextureUnitState::setColourOperationEx...
Definition: OgreBlendMode.h:75
Specialisation of SharedPtr to allow SharedPtr to be assigned to MaterialPtr.
Definition: OgreMaterial.h:677
FilterOptions
Filtering options for textures / mipmaps.
Definition: OgreCommon.h:101
map< String, String >::type AliasTextureNamePairList
Alias / Texture name pair (first = alias, second = texture name)
Definition: OgreCommon.h:527
GpuProgramParametersSharedPtr programParams
CompareFunction
Comparison functions used for the depth/stencil buffer operations and others.
Definition: OgreCommon.h:65
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.