collada: export UV Textures as materials. Note: the reimport of the exported collada...
[blender.git] / source / blender / collada / InstanceWriter.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/InstanceWriter.cpp
25  *  \ingroup collada
26  */
27
28
29 #include <string>
30 #include <sstream>
31
32 #include "COLLADASWInstanceMaterial.h"
33
34 extern "C" {
35 #include "BKE_customdata.h"
36 #include "BKE_material.h"
37 #include "DNA_mesh_types.h"
38 }
39
40 #include "InstanceWriter.h"
41 #include "collada_internal.h"
42 #include "collada_utils.h"
43
44 void InstanceWriter::add_material_bindings(COLLADASW::BindMaterial& bind_material, Object *ob, bool active_uv_only, BC_export_texture_type export_texture_type)
45 {
46         bool all_uv_layers = !active_uv_only;
47         COLLADASW::InstanceMaterialList& iml = bind_material.getInstanceMaterialList();
48
49         if (export_texture_type == BC_TEXTURE_TYPE_UV)
50         {
51                 std::set<Image *> uv_images = bc_getUVImages(ob, all_uv_layers);
52                 std::set<Image *>::iterator uv_images_iter;
53                 for (uv_images_iter = uv_images.begin();
54                         uv_images_iter != uv_images.end();
55                         uv_images_iter++) {
56                         Image *ima = *uv_images_iter;
57                         std::string matid(id_name(ima));
58                         matid = get_material_id_from_id(matid);
59                         std::ostringstream ostr;
60                         ostr << matid;
61                         COLLADASW::InstanceMaterial im(ostr.str(), COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, matid));
62
63                         // create <bind_vertex_input> for each uv map
64                         Mesh *me = (Mesh *)ob->data;
65                         int totlayer = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
66
67                         int map_index = 0;
68                         int active_uv_index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE) - 1;
69                         for (int b = 0; b < totlayer; b++) {
70                                 if (!active_uv_only || b == active_uv_index) {
71                                         char *name = bc_CustomData_get_layer_name(&me->fdata, CD_MTFACE, b);
72                                         im.push_back(COLLADASW::BindVertexInput(name, "TEXCOORD", map_index++));
73                                 }
74                         }
75
76                         iml.push_back(im);
77                 }
78         }
79
80         else if (export_texture_type == BC_TEXTURE_TYPE_MAT) {
81                 for (int a = 0; a < ob->totcol; a++) {
82                         Material *ma = give_current_material(ob, a + 1);
83                         if (ma) {
84                                 std::string matid(get_material_id(ma));
85                                 matid = translate_id(matid);
86                                 std::ostringstream ostr;
87                                 ostr << matid;
88                                 COLLADASW::InstanceMaterial im(ostr.str(), COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, matid));
89
90                                 // create <bind_vertex_input> for each uv map
91                                 Mesh *me = (Mesh *)ob->data;
92                                 int totlayer = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
93
94                                 int map_index = 0;
95                                 int active_uv_index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE) - 1;
96                                 for (int b = 0; b < totlayer; b++) {
97                                         if (!active_uv_only || b == active_uv_index) {
98                                                 char *name = bc_CustomData_get_layer_name(&me->fdata, CD_MTFACE, b);
99                                                 im.push_back(COLLADASW::BindVertexInput(name, "TEXCOORD", map_index++));
100                                         }
101                                 }
102
103                                 iml.push_back(im);
104                         }
105                 }
106         }
107 }