Merging trunk up to r39145.
[blender-staging.git] / source / blender / collada / InstanceWriter.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
21  *                 Nathan Letwory
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/collada/InstanceWriter.cpp
27  *  \ingroup collada
28  */
29
30
31 #include <string>
32 #include <sstream>
33
34 #include "COLLADASWInstanceMaterial.h"
35
36 #include "BKE_customdata.h"
37 #include "BKE_material.h"
38
39 #include "DNA_mesh_types.h"
40
41 #include "InstanceWriter.h"
42
43 #include "collada_internal.h"
44 #include "collada_utils.h"
45
46 void InstanceWriter::add_material_bindings(COLLADASW::BindMaterial& bind_material, Object *ob)
47 {
48         for(int a = 0; a < ob->totcol; a++)     {
49                 Material *ma = give_current_material(ob, a+1);
50                         
51                 COLLADASW::InstanceMaterialList& iml = bind_material.getInstanceMaterialList();
52
53                 if (ma) {
54                         std::string matid(get_material_id(ma));
55                         matid = translate_id(matid);
56                         std::ostringstream ostr;
57                         ostr << translate_id(id_name(ma)) << a+1;
58                         COLLADASW::InstanceMaterial im(ostr.str(), COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, matid));
59                         
60                         // create <bind_vertex_input> for each uv layer
61                         Mesh *me = (Mesh*)ob->data;
62                         int totlayer = CustomData_number_of_layers(&me->fdata, CD_MTFACE);
63                         
64                         for (int b = 0; b < totlayer; b++) {
65                                 char *name = bc_CustomData_get_layer_name(&me->fdata, CD_MTFACE, b);
66                                 im.push_back(COLLADASW::BindVertexInput(name, "TEXCOORD", b));
67                         }
68                         
69                         iml.push_back(im);
70                 }
71         }
72 }