be45d5295e5d9a4c6daf392aa74f4d27eae4698b
[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)
45 {
46         for (int a = 0; a < ob->totcol; a++) {
47                 Material *ma = give_current_material(ob, a + 1);
48                         
49                 COLLADASW::InstanceMaterialList& iml = bind_material.getInstanceMaterialList();
50
51                 if (ma) {
52                         std::string matid(get_material_id(ma));
53                         matid = translate_id(matid);
54                         std::ostringstream ostr;
55                         ostr << matid;
56                         COLLADASW::InstanceMaterial im(ostr.str(), COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, matid));
57                         
58                         // create <bind_vertex_input> for each uv map
59                         Mesh *me = (Mesh *)ob->data;
60                         int totlayer = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
61                         
62                         int map_index = 0;
63                         int active_uv_index = CustomData_get_active_layer_index(&me->fdata, CD_MTFACE) -1;
64                         for (int b = 0; b < totlayer; b++) {
65                                 if (!active_uv_only || b == active_uv_index) {
66                                         char *name = bc_CustomData_get_layer_name(&me->fdata, CD_MTFACE, map_index);
67                                         im.push_back(COLLADASW::BindVertexInput(name, "TEXCOORD", map_index++));
68                                 }
69                         }
70                         
71                         iml.push_back(im);
72                 }
73         }
74 }