Blender  V2.59
MaterialExporter.h
Go to the documentation of this file.
00001 /*
00002  * $Id: MaterialExporter.h 38770 2011-07-28 00:08:03Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
00021  *                 Nathan Letwory
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00030 #ifndef __MATERIALEXPORTER_H__
00031 #define __MATERIALEXPORTER_H__
00032 
00033 #include <string>
00034 #include <vector>
00035 
00036 #include "COLLADASWLibraryMaterials.h"
00037 #include "COLLADASWStreamWriter.h"
00038 
00039 #include "BKE_material.h"
00040 
00041 #include "DNA_material_types.h"
00042 #include "DNA_object_types.h"
00043 #include "DNA_scene_types.h"
00044 
00045 #include "GeometryExporter.h"
00046 #include "collada_internal.h"
00047 
00048 class MaterialsExporter: COLLADASW::LibraryMaterials
00049 {
00050 public:
00051         MaterialsExporter(COLLADASW::StreamWriter *sw);
00052         void exportMaterials(Scene *sce, bool export_selected);
00053         void operator()(Material *ma, Object *ob);
00054 
00055 private:
00056         bool hasMaterials(Scene *sce);
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