Blender V2.61 - r43446

MaterialExporter.h

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
00019  *                 Nathan Letwory
00020  *
00021  * ***** END GPL LICENSE BLOCK *****
00022  */
00023 
00028 #ifndef __MATERIALEXPORTER_H__
00029 #define __MATERIALEXPORTER_H__
00030 
00031 #include <string>
00032 #include <vector>
00033 
00034 #include "COLLADASWLibraryMaterials.h"
00035 #include "COLLADASWStreamWriter.h"
00036 
00037 #include "BKE_material.h"
00038 
00039 #include "DNA_material_types.h"
00040 #include "DNA_object_types.h"
00041 #include "DNA_scene_types.h"
00042 
00043 #include "GeometryExporter.h"
00044 #include "collada_internal.h"
00045 #include "ExportSettings.h"
00046 
00047 class MaterialsExporter: COLLADASW::LibraryMaterials
00048 {
00049 public:
00050     MaterialsExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings);
00051     void exportMaterials(Scene *sce);
00052     void operator()(Material *ma, Object *ob);
00053 
00054 private:
00055     bool hasMaterials(Scene *sce);
00056     const ExportSettings *export_settings;
00057 };
00058 
00059 // used in forEachMaterialInScene
00060 template <class Functor>
00061 class ForEachMaterialFunctor
00062 {
00063     std::vector<std::string> mMat; // contains list of material names, to avoid duplicate calling of f
00064     Functor *f;
00065 public:
00066     ForEachMaterialFunctor(Functor*f) : f(f) {}
00067     
00068     void operator ()(Object *ob)
00069     {
00070         int a;
00071         for(a = 0; a < ob->totcol; a++) {
00072 
00073             Material *ma = give_current_material(ob, a+1);
00074 
00075             if (!ma) continue;
00076 
00077             std::string translated_id = translate_id(id_name(ma));
00078             if (find(mMat.begin(), mMat.end(), translated_id) == mMat.end()) {
00079                 (*this->f)(ma, ob);
00080 
00081                 mMat.push_back(translated_id);
00082             }
00083         }
00084     }
00085 };
00086 
00087 struct MaterialFunctor {
00088     // calls f for each unique material linked to each object in sce
00089     // f should have
00090     // void operator()(Material* ma)
00091     template<class Functor>
00092     void forEachMaterialInScene(Scene *sce, Functor &f, bool export_selected)
00093     {
00094         ForEachMaterialFunctor<Functor> matfunc(&f);
00095         GeometryFunctor gf;
00096         gf.forEachMeshObjectInScene<ForEachMaterialFunctor<Functor> >(sce, matfunc, export_selected);
00097     }
00098 };
00099 
00100 
00101 #endif