collada: export UV Textures as materials. Note: the reimport of the exported collada...
[blender.git] / source / blender / collada / MaterialExporter.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
19  *                 Nathan Letwory
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/collada/MaterialExporter.cpp
25  *  \ingroup collada
26  */
27
28
29
30 #include "MaterialExporter.h"
31 #include "COLLADABUUtils.h"
32 #include "collada_internal.h"
33
34 MaterialsExporter::MaterialsExporter(COLLADASW::StreamWriter *sw, const ExportSettings *export_settings) : COLLADASW::LibraryMaterials(sw), export_settings(export_settings)
35 {
36         /* pass */
37 }
38
39 void MaterialsExporter::exportMaterials(Scene *sce)
40 {
41         if (this->export_settings->export_texture_type == BC_TEXTURE_TYPE_MAT)
42         {
43                 if (hasMaterials(sce)) {
44                         openLibrary();
45
46                         MaterialFunctor mf;
47                         mf.forEachMaterialInExportSet<MaterialsExporter>(sce, *this, this->export_settings->export_set);
48
49                         closeLibrary();
50                 }
51         }
52
53         else if (this->export_settings->export_texture_type == BC_TEXTURE_TYPE_UV)
54         {
55                 std::set<Image *> uv_images = bc_getUVImages(sce, !this->export_settings->active_uv_only);
56                 if (uv_images.size() > 0) {
57                         openLibrary();
58                         std::set<Image *>::iterator uv_images_iter;
59                         for (uv_images_iter = uv_images.begin();
60                                 uv_images_iter != uv_images.end();
61                                 uv_images_iter++) {
62
63                                 Image *ima = *uv_images_iter;
64                                 std::string matid(id_name(ima));
65
66                                 openMaterial(get_material_id_from_id(matid), translate_id(matid));
67                                 std::string efid = translate_id(matid) + "-effect";
68                                 addInstanceEffect(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, efid));
69                                 closeMaterial();
70                         }
71                         closeLibrary();
72                 }
73         }
74
75 }
76
77 bool MaterialsExporter::hasMaterials(Scene *sce)
78 {
79         LinkNode *node;
80         for (node=this->export_settings->export_set; node; node = node->next) {
81                 Object *ob = (Object *)node->link;
82                 int a;
83                 for (a = 0; a < ob->totcol; a++) {
84                         Material *ma = give_current_material(ob, a + 1);
85
86                         // no material, but check all of the slots
87                         if (!ma) continue;
88
89                         return true;
90                 }
91         }
92         return false;
93 }
94
95 void MaterialsExporter::operator()(Material *ma, Object *ob)
96 {
97         std::string name(id_name(ma));
98
99         openMaterial(get_material_id(ma), translate_id(name));
100
101         std::string efid = translate_id(name) + "-effect";
102         addInstanceEffect(COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, efid));
103
104         closeMaterial();
105 }