| 1 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 2 | // materialSet.h | 
|---|
| 3 | // Author     : Francesco Giordana | 
|---|
| 4 | // Start Date : January 13, 2005 | 
|---|
| 5 | // Copyright  : (C) 2006 by Francesco Giordana | 
|---|
| 6 | // Email      : fra.giordana@tiscali.it | 
|---|
| 7 | //////////////////////////////////////////////////////////////////////////////// | 
|---|
| 8 |  | 
|---|
| 9 | /********************************************************************************* | 
|---|
| 10 | *                                                                                * | 
|---|
| 11 | *   This program is free software; you can redistribute it and/or modify         * | 
|---|
| 12 | *   it under the terms of the GNU Lesser General Public License as published by  * | 
|---|
| 13 | *   the Free Software Foundation; either version 2 of the License, or            * | 
|---|
| 14 | *   (at your option) any later version.                                          * | 
|---|
| 15 | *                                                                                * | 
|---|
| 16 | **********************************************************************************/ | 
|---|
| 17 |  | 
|---|
| 18 | #ifndef _MATERIALSET_H | 
|---|
| 19 | #define _MATERIALSET_H | 
|---|
| 20 |  | 
|---|
| 21 | #include "_singleton.h" | 
|---|
| 22 | #include "material.h" | 
|---|
| 23 | #include "mayaExportLayer.h" | 
|---|
| 24 |  | 
|---|
| 25 | namespace OgreMayaExporter | 
|---|
| 26 | { | 
|---|
| 27 |         class MaterialSet : public Singleton<MaterialSet> | 
|---|
| 28 |         { | 
|---|
| 29 |         public: | 
|---|
| 30 |                 //constructor | 
|---|
| 31 |                 MaterialSet(){ | 
|---|
| 32 |                         //create a default material | 
|---|
| 33 |                         m_pDefaultMat = new Material(); | 
|---|
| 34 |                         m_pDefaultMat->m_type = MT_LAMBERT; | 
|---|
| 35 |                         m_pDefaultMat->m_name = "defaultLambert"; | 
|---|
| 36 |                         m_pDefaultMat->m_ambient = MColor(0,0,0,1); | 
|---|
| 37 |                         m_pDefaultMat->m_emissive = MColor(0,0,0,1); | 
|---|
| 38 |                         m_pDefaultMat->m_diffuse = MColor(0.5,0.5,0.5,1); | 
|---|
| 39 |                 }; | 
|---|
| 40 |                 //destructor | 
|---|
| 41 |                 ~MaterialSet(){ | 
|---|
| 42 |                         clear(); | 
|---|
| 43 |                         if (m_pDefaultMat) | 
|---|
| 44 |                                 delete m_pDefaultMat; | 
|---|
| 45 |                 } | 
|---|
| 46 |                 //clear | 
|---|
| 47 |                 void clear(){ | 
|---|
| 48 |                         for (int i=0; i<m_materials.size(); i++) | 
|---|
| 49 |                                 delete m_materials[i]; | 
|---|
| 50 |                         m_materials.clear(); | 
|---|
| 51 |                 } | 
|---|
| 52 |                 //add material | 
|---|
| 53 |                 void addMaterial(Material* pMat){ | 
|---|
| 54 |                         bool found = false; | 
|---|
| 55 |                         for (int i=0; i<m_materials.size() && !found; i++) | 
|---|
| 56 |                         { | 
|---|
| 57 |                                 if (m_materials[i]->name() == pMat->name()) | 
|---|
| 58 |                                 { | 
|---|
| 59 |                                         found = true; | 
|---|
| 60 |                                         delete pMat; | 
|---|
| 61 |                                 } | 
|---|
| 62 |                         } | 
|---|
| 63 |                         if (!found) | 
|---|
| 64 |                                 m_materials.push_back(pMat); | 
|---|
| 65 |                 } | 
|---|
| 66 |                 //get material | 
|---|
| 67 |                 Material* getMaterial(const MString& name){ | 
|---|
| 68 |                         for (int i=0; i<m_materials.size(); i++) | 
|---|
| 69 |                         { | 
|---|
| 70 |                                 if (m_materials[i]->name() == name) | 
|---|
| 71 |                                         return m_materials[i]; | 
|---|
| 72 |                         } | 
|---|
| 73 |                         return NULL; | 
|---|
| 74 |                 }; | 
|---|
| 75 |                 //get default material | 
|---|
| 76 |                 Material* getDefaultMaterial() | 
|---|
| 77 |                 { | 
|---|
| 78 |                         return m_pDefaultMat; | 
|---|
| 79 |                 }; | 
|---|
| 80 |                 //get material set | 
|---|
| 81 |                 static MaterialSet& getSingleton(){ | 
|---|
| 82 |                         assert(ms_Singleton);   | 
|---|
| 83 |                         return (*ms_Singleton); | 
|---|
| 84 |                 }; | 
|---|
| 85 |                 static MaterialSet* getSingletonPtr(){ | 
|---|
| 86 |                         return ms_Singleton; | 
|---|
| 87 |                 }; | 
|---|
| 88 |                 //write materials to Ogre XML | 
|---|
| 89 |                 MStatus writeOgreScript(ParamList ¶ms){ | 
|---|
| 90 |                         MStatus stat; | 
|---|
| 91 |                         for (int i=0; i<m_materials.size(); i++) | 
|---|
| 92 |                         { | 
|---|
| 93 |                                 stat = m_materials[i]->writeOgreScript(params); | 
|---|
| 94 |                                 if (MS::kSuccess != stat) | 
|---|
| 95 |                                 { | 
|---|
| 96 |                                         MString msg = "Error writing material "; | 
|---|
| 97 |                                         msg += m_materials[i]->name(); | 
|---|
| 98 |                                         msg += ", aborting operation"; | 
|---|
| 99 |                                         MGlobal::displayInfo(msg); | 
|---|
| 100 |                                 } | 
|---|
| 101 |                         } | 
|---|
| 102 |                         return MS::kSuccess; | 
|---|
| 103 |                 }; | 
|---|
| 104 |  | 
|---|
| 105 |         protected: | 
|---|
| 106 |                 std::vector<Material*> m_materials; | 
|---|
| 107 |                 Material* m_pDefaultMat; | 
|---|
| 108 |         }; | 
|---|
| 109 |  | 
|---|
| 110 |         template<> MaterialSet* Singleton<MaterialSet>::ms_Singleton = 0; | 
|---|
| 111 |  | 
|---|
| 112 | };      //end namespace | 
|---|
| 113 |  | 
|---|
| 114 | #endif | 
|---|