00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://www.ogre3d.org/ 00006 00007 Copyright (c) 2000-2012 Torus Knot Software Ltd 00008 00009 Permission is hereby granted, free of charge, to any person obtaining a copy 00010 of this software and associated documentation files (the "Software"), to deal 00011 in the Software without restriction, including without limitation the rights 00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00013 copies of the Software, and to permit persons to whom the Software is 00014 furnished to do so, subject to the following conditions: 00015 00016 The above copyright notice and this permission notice shall be included in 00017 all copies or substantial portions of the Software. 00018 00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00025 THE SOFTWARE. 00026 ----------------------------------------------------------------------------- 00027 */ 00028 #ifndef __MaterialSerializer_H__ 00029 #define __MaterialSerializer_H__ 00030 00031 #include "OgrePrerequisites.h" 00032 #include "OgreMaterial.h" 00033 #include "OgreBlendMode.h" 00034 #include "OgreTextureUnitState.h" 00035 #include "OgreGpuProgram.h" 00036 #include "OgreStringVector.h" 00037 00038 namespace Ogre { 00039 00047 enum MaterialScriptSection 00048 { 00049 MSS_NONE, 00050 MSS_MATERIAL, 00051 MSS_TECHNIQUE, 00052 MSS_PASS, 00053 MSS_TEXTUREUNIT, 00054 MSS_PROGRAM_REF, 00055 MSS_PROGRAM, 00056 MSS_DEFAULT_PARAMETERS, 00057 MSS_TEXTURESOURCE 00058 }; 00060 struct MaterialScriptProgramDefinition 00061 { 00062 String name; 00063 GpuProgramType progType; 00064 String language; 00065 String source; 00066 String syntax; 00067 bool supportsSkeletalAnimation; 00068 bool supportsMorphAnimation; 00069 ushort supportsPoseAnimation; // number of simultaneous poses supported 00070 bool usesVertexTextureFetch; 00071 vector<std::pair<String, String> >::type customParameters; 00072 }; 00074 struct MaterialScriptContext 00075 { 00076 MaterialScriptSection section; 00077 String groupName; 00078 MaterialPtr material; 00079 Technique* technique; 00080 Pass* pass; 00081 TextureUnitState* textureUnit; 00082 GpuProgramPtr program; // used when referencing a program, not when defining it 00083 bool isVertexProgramShadowCaster; // when referencing, are we in context of shadow caster 00084 bool isFragmentProgramShadowCaster; // when referencing, are we in context of shadow caster 00085 bool isVertexProgramShadowReceiver; // when referencing, are we in context of shadow caster 00086 bool isFragmentProgramShadowReceiver; // when referencing, are we in context of shadow caster 00087 GpuProgramParametersSharedPtr programParams; 00088 ushort numAnimationParametrics; 00089 MaterialScriptProgramDefinition* programDef; // this is used while defining a program 00090 00091 int techLev, //Keep track of what tech, pass, and state level we are in 00092 passLev, 00093 stateLev; 00094 StringVector defaultParamLines; 00095 00096 // Error reporting state 00097 size_t lineNo; 00098 String filename; 00099 AliasTextureNamePairList textureAliases; 00100 }; 00102 typedef bool (*ATTRIBUTE_PARSER)(String& params, MaterialScriptContext& context); 00103 00105 class _OgreExport MaterialSerializer : public SerializerAlloc 00106 { 00107 public: 00108 00109 // Material serizliae event. 00110 enum SerializeEvent 00111 { 00112 MSE_PRE_WRITE, 00113 MSE_WRITE_BEGIN, 00114 MSE_WRITE_END, 00115 MSE_POST_WRITE 00116 }; 00117 00121 class Listener 00122 { 00123 public: 00124 virtual ~Listener() {} 00125 00133 virtual void materialEventRaised(MaterialSerializer* ser, 00134 SerializeEvent event, bool& skip, const Material* mat) 00135 { (void)ser; (void)event; (void)skip; (void)mat; } 00136 00144 virtual void techniqueEventRaised(MaterialSerializer* ser, 00145 SerializeEvent event, bool& skip, const Technique* tech) 00146 { (void)ser; (void)event; (void)skip; (void)tech; } 00147 00155 virtual void passEventRaised(MaterialSerializer* ser, 00156 SerializeEvent event, bool& skip, const Pass* pass) 00157 { (void)ser; (void)event; (void)skip; (void)pass; } 00158 00169 void gpuProgramRefEventRaised(MaterialSerializer* ser, 00170 SerializeEvent event, bool& skip, 00171 const String& attrib, 00172 const GpuProgramPtr& program, 00173 const GpuProgramParametersSharedPtr& params, 00174 GpuProgramParameters* defaultParams) 00175 { 00176 (void)ser; 00177 (void)event; 00178 (void)skip; 00179 (void)attrib; 00180 (void)program; 00181 (void)params; 00182 (void)defaultParams; 00183 } 00184 00192 virtual void textureUnitStateEventRaised(MaterialSerializer* ser, 00193 SerializeEvent event, bool& skip, const TextureUnitState* textureUnit) 00194 { 00195 (void)ser; 00196 (void)event; 00197 (void)skip; 00198 (void)textureUnit; 00199 } 00200 }; 00201 00202 protected: 00204 typedef map<String, ATTRIBUTE_PARSER>::type AttribParserList; 00205 00206 MaterialScriptContext mScriptContext; 00207 00211 bool parseScriptLine(String& line); 00213 bool invokeParser(String& line, AttribParserList& parsers); 00217 void finishProgramDefinition(void); 00219 AttribParserList mRootAttribParsers; 00221 AttribParserList mMaterialAttribParsers; 00223 AttribParserList mTechniqueAttribParsers; 00225 AttribParserList mPassAttribParsers; 00227 AttribParserList mTextureUnitAttribParsers; 00229 AttribParserList mProgramRefAttribParsers; 00231 AttribParserList mProgramAttribParsers; 00233 AttribParserList mProgramDefaultParamAttribParsers; 00234 00236 typedef vector<Listener*>::type ListenerList; 00237 typedef ListenerList::iterator ListenerListIterator; 00238 typedef ListenerList::const_iterator ListenerListConstIterator; 00239 ListenerList mListeners; 00240 00241 00242 void writeMaterial(const MaterialPtr& pMat, const String& materialName = ""); 00243 void writeTechnique(const Technique* pTech); 00244 void writePass(const Pass* pPass); 00245 void writeVertexProgramRef(const Pass* pPass); 00246 void writeShadowCasterVertexProgramRef(const Pass* pPass); 00247 void writeShadowCasterFragmentProgramRef(const Pass* pPass); 00248 void writeShadowReceiverVertexProgramRef(const Pass* pPass); 00249 void writeShadowReceiverFragmentProgramRef(const Pass* pPass); 00250 void writeFragmentProgramRef(const Pass* pPass); 00251 void writeGpuProgramRef(const String& attrib, const GpuProgramPtr& program, const GpuProgramParametersSharedPtr& params); 00252 void writeGpuPrograms(void); 00253 void writeGPUProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams, 00254 const unsigned short level = 4, const bool useMainBuffer = true); 00255 void writeNamedGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams, 00256 const unsigned short level = 4, const bool useMainBuffer = true); 00257 void writeLowLevelGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams, 00258 const unsigned short level = 4, const bool useMainBuffer = true); 00259 void writeGpuProgramParameter( 00260 const String& commandName, const String& identifier, 00261 const GpuProgramParameters::AutoConstantEntry* autoEntry, 00262 const GpuProgramParameters::AutoConstantEntry* defaultAutoEntry, 00263 bool isFloat, size_t physicalIndex, size_t physicalSize, 00264 const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams, 00265 const unsigned short level, const bool useMainBuffer); 00266 void writeTextureUnit(const TextureUnitState *pTex); 00267 void writeSceneBlendFactor(const SceneBlendFactor c_src, const SceneBlendFactor c_dest, 00268 const SceneBlendFactor a_src, const SceneBlendFactor a_dest); 00269 void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest); 00270 void writeSceneBlendFactor(const SceneBlendFactor sbf); 00271 void writeCompareFunction(const CompareFunction cf); 00272 void writeColourValue(const ColourValue &colour, bool writeAlpha = false); 00273 void writeLayerBlendOperationEx(const LayerBlendOperationEx op); 00274 void writeLayerBlendSource(const LayerBlendSource lbs); 00275 00276 typedef multimap<TextureUnitState::TextureEffectType, TextureUnitState::TextureEffect>::type EffectMap; 00277 00278 void writeRotationEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex); 00279 void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex); 00280 void writeScrollEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex); 00281 void writeEnvironmentMapEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex); 00282 00283 String convertFiltering(FilterOptions fo); 00284 00285 00289 void fireMaterialEvent(SerializeEvent event, bool& skip, const Material* mat); 00290 00294 void fireTechniqueEvent(SerializeEvent event, bool& skip, const Technique* tech); 00295 00299 void firePassEvent(SerializeEvent event, bool& skip, const Pass* pass); 00300 00304 void fireGpuProgramRefEvent(SerializeEvent event, bool& skip, 00305 const String& attrib, 00306 const GpuProgramPtr& program, 00307 const GpuProgramParametersSharedPtr& params, 00308 GpuProgramParameters* defaultParams); 00309 00310 00314 void fireTextureUnitStateEvent(SerializeEvent event, bool& skip, const TextureUnitState* textureUnit); 00315 00316 public: 00318 MaterialSerializer(); 00320 virtual ~MaterialSerializer() {} 00321 00330 void queueForExport(const MaterialPtr& pMat, bool clearQueued = false, 00331 bool exportDefaults = false, const String& materialName = ""); 00341 void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = ""); 00353 void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false, 00354 const bool includeProgDef = false, const String& programFilename = "", 00355 const String& materialName = ""); 00357 const String &getQueuedAsString() const; 00359 void clearQueue(); 00360 00363 void parseScript(DataStreamPtr& stream, const String& groupName); 00364 00368 void addListener(Listener* listener); 00369 00373 void removeListener(Listener* listener); 00374 00375 private: 00376 String mBuffer; 00377 String mGpuProgramBuffer; 00378 typedef set<String>::type GpuProgramDefinitionContainer; 00379 typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator; 00380 GpuProgramDefinitionContainer mGpuProgramDefinitionContainer; 00381 bool mDefaults; 00382 00383 public: 00384 void beginSection(unsigned short level, const bool useMainBuffer = true) 00385 { 00386 String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer); 00387 buffer += "\n"; 00388 for (unsigned short i = 0; i < level; ++i) 00389 { 00390 buffer += "\t"; 00391 } 00392 buffer += "{"; 00393 } 00394 void endSection(unsigned short level, const bool useMainBuffer = true) 00395 { 00396 String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer); 00397 buffer += "\n"; 00398 for (unsigned short i = 0; i < level; ++i) 00399 { 00400 buffer += "\t"; 00401 } 00402 buffer += "}"; 00403 } 00404 00405 void writeAttribute(unsigned short level, const String& att, const bool useMainBuffer = true) 00406 { 00407 String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer); 00408 buffer += "\n"; 00409 for (unsigned short i = 0; i < level; ++i) 00410 { 00411 buffer += "\t"; 00412 } 00413 buffer += att; 00414 } 00415 00416 void writeValue(const String& val, const bool useMainBuffer = true) 00417 { 00418 String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer); 00419 buffer += (" " + val); 00420 } 00421 00422 String quoteWord(const String& val) 00423 { 00424 if (val.find_first_of(" \t") != String::npos) 00425 return ("\"" + val + "\""); 00426 else return val; 00427 } 00428 00429 00430 void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true) 00431 { 00432 String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer); 00433 buffer += "\n"; 00434 for (unsigned short i = 0; i < level; ++i) 00435 { 00436 buffer += "\t"; 00437 } 00438 buffer += "// " + comment; 00439 } 00440 00441 00442 00443 }; 00446 } 00447 #endif
Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Fri May 25 23:36:24 2012