Blender  V2.93
InstanceWriter.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <sstream>
22 #include <string>
23 
24 #include "COLLADASWInstanceMaterial.h"
25 
26 #include "BKE_customdata.h"
27 #include "BKE_material.h"
28 
29 #include "DNA_mesh_types.h"
30 
31 #include "InstanceWriter.h"
32 #include "collada_internal.h"
33 #include "collada_utils.h"
34 
35 void InstanceWriter::add_material_bindings(COLLADASW::BindMaterial &bind_material,
36  Object *ob,
37  bool active_uv_only)
38 {
39  for (int a = 0; a < ob->totcol; a++) {
40  Material *ma = BKE_object_material_get(ob, a + 1);
41 
42  COLLADASW::InstanceMaterialList &iml = bind_material.getInstanceMaterialList();
43 
44  if (ma) {
45  std::string matid(get_material_id(ma));
46  matid = translate_id(matid);
47  std::ostringstream ostr;
48  ostr << matid;
49  COLLADASW::InstanceMaterial im(ostr.str(),
50  COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, matid));
51 
52  // create <bind_vertex_input> for each uv map
53  Mesh *me = (Mesh *)ob->data;
54 
55  int num_layers = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
56 
57  int map_index = 0;
58  int active_uv_index = CustomData_get_active_layer_index(&me->ldata, CD_MLOOPUV);
59  for (int b = 0; b < num_layers; b++) {
60  if (!active_uv_only || b == active_uv_index) {
61  char *name = bc_CustomData_get_layer_name(&me->ldata, CD_MLOOPUV, b);
62  im.push_back(COLLADASW::BindVertexInput(name, "TEXCOORD", map_index++));
63  }
64  }
65 
66  iml.push_back(im);
67  }
68  }
69 }
std::string EMPTY_STRING
CustomData interface, see also DNA_customdata_types.h.
int CustomData_get_active_layer_index(const struct CustomData *data, int type)
int CustomData_number_of_layers(const struct CustomData *data, int type)
General operations, lookup, etc. for materials.
struct Material * BKE_object_material_get(struct Object *ob, short act)
Definition: material.c:697
@ CD_MLOOPUV
void add_material_bindings(COLLADASW::BindMaterial &bind_material, Object *ob, bool active_uv_only)
std::string translate_id(const char *idString)
std::string get_material_id(Material *mat)
char * bc_CustomData_get_layer_name(const CustomData *data, int type, int n)
static unsigned a[3]
Definition: RandGen.cpp:92
struct CustomData pdata ldata
void * data