fix warning message when imported Collada nodes have < 3 edges.
[blender.git] / source / blender / collada / MeshImporter.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, Nathan Letwory.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file blender/collada/MeshImporter.cpp
24  *  \ingroup collada
25  */
26
27
28 #include <algorithm>
29
30 #if !defined(WIN32) || defined(FREE_WINDOWS)
31 #include <iostream>
32 #endif
33
34 /* COLLADABU_ASSERT, may be able to remove later */
35 #include "COLLADABUPlatform.h"
36
37 #include "COLLADAFWMeshPrimitive.h"
38 #include "COLLADAFWMeshVertexData.h"
39 #include "COLLADAFWPolygons.h"
40
41 extern "C" {
42         #include "BKE_blender.h"
43         #include "BKE_customdata.h"
44         #include "BKE_displist.h"
45         #include "BKE_global.h"
46         #include "BKE_library.h"
47         #include "BKE_main.h"
48         #include "BKE_material.h"
49         #include "BKE_mesh.h"
50         #include "BKE_object.h"
51
52         #include "BLI_listbase.h"
53         #include "BLI_math.h"
54         #include "BLI_string.h"
55         #include "BLI_edgehash.h"
56
57         #include "MEM_guardedalloc.h"
58 }
59
60 #include "ArmatureImporter.h"
61 #include "MeshImporter.h"
62 #include "collada_utils.h"
63
64 // get node name, or fall back to original id if not present (name is optional)
65 template<class T>
66 static const std::string bc_get_dae_name(T *node)
67 {
68         return node->getName().size() ? node->getName(): node->getOriginalId();
69 }
70
71 static const char *bc_primTypeToStr(COLLADAFW::MeshPrimitive::PrimitiveType type)
72 {
73         switch (type) {
74                 case COLLADAFW::MeshPrimitive::LINES:
75                         return "LINES";
76                 case COLLADAFW::MeshPrimitive::LINE_STRIPS:
77                         return "LINESTRIPS";
78                 case COLLADAFW::MeshPrimitive::POLYGONS:
79                         return "POLYGONS";
80                 case COLLADAFW::MeshPrimitive::POLYLIST:
81                         return "POLYLIST";
82                 case COLLADAFW::MeshPrimitive::TRIANGLES:
83                         return "TRIANGLES";
84                 case COLLADAFW::MeshPrimitive::TRIANGLE_FANS:
85                         return "TRIANGLE_FANS";
86                 case COLLADAFW::MeshPrimitive::TRIANGLE_STRIPS:
87                         return "TRIANGLE_FANS";
88                 case COLLADAFW::MeshPrimitive::POINTS:
89                         return "POINTS";
90                 case COLLADAFW::MeshPrimitive::UNDEFINED_PRIMITIVE_TYPE:
91                         return "UNDEFINED_PRIMITIVE_TYPE";
92         }
93         return "UNKNOWN";
94 }
95
96 static const char *bc_geomTypeToStr(COLLADAFW::Geometry::GeometryType type)
97 {
98         switch (type) {
99                 case COLLADAFW::Geometry::GEO_TYPE_MESH:
100                         return "MESH";
101                 case COLLADAFW::Geometry::GEO_TYPE_SPLINE:
102                         return "SPLINE";
103                 case COLLADAFW::Geometry::GEO_TYPE_CONVEX_MESH:
104                         return "CONVEX_MESH";
105                 case COLLADAFW::Geometry::GEO_TYPE_UNKNOWN:
106                 default:
107                         return "UNKNOWN";
108         }
109 }
110
111
112 UVDataWrapper::UVDataWrapper(COLLADAFW::MeshVertexData& vdata) : mVData(&vdata)
113 {
114 }
115
116 #ifdef COLLADA_DEBUG
117 void WVDataWrapper::print()
118 {
119         fprintf(stderr, "UVs:\n");
120         switch (mVData->getType()) {
121                 case COLLADAFW::MeshVertexData::DATA_TYPE_FLOAT:
122                 {
123                         COLLADAFW::ArrayPrimitiveType<float> *values = mVData->getFloatValues();
124                         if (values->getCount()) {
125                                 for (int i = 0; i < values->getCount(); i += 2) {
126                                         fprintf(stderr, "%.1f, %.1f\n", (*values)[i], (*values)[i + 1]);
127                                 }
128                         }
129                 }
130                 break;
131                 case COLLADAFW::MeshVertexData::DATA_TYPE_DOUBLE:
132                 {
133                         COLLADAFW::ArrayPrimitiveType<double> *values = mVData->getDoubleValues();
134                         if (values->getCount()) {
135                                 for (int i = 0; i < values->getCount(); i += 2) {
136                                         fprintf(stderr, "%.1f, %.1f\n", (float)(*values)[i], (float)(*values)[i + 1]);
137                                 }
138                         }
139                 }
140                 break;
141         }
142         fprintf(stderr, "\n");
143 }
144 #endif
145
146 void UVDataWrapper::getUV(int uv_index, float *uv)
147 {
148         int stride = mVData->getStride(0);
149         if (stride == 0) stride = 2;
150
151         switch (mVData->getType()) {
152                 case COLLADAFW::MeshVertexData::DATA_TYPE_FLOAT:
153                 {
154                         COLLADAFW::ArrayPrimitiveType<float> *values = mVData->getFloatValues();
155                         if (values->empty()) return;
156                         uv[0] = (*values)[uv_index * stride];
157                         uv[1] = (*values)[uv_index * stride + 1];
158                         
159                 }
160                 break;
161                 case COLLADAFW::MeshVertexData::DATA_TYPE_DOUBLE:
162                 {
163                         COLLADAFW::ArrayPrimitiveType<double> *values = mVData->getDoubleValues();
164                         if (values->empty()) return;
165                         uv[0] = (float)(*values)[uv_index * stride];
166                         uv[1] = (float)(*values)[uv_index * stride + 1];
167                         
168                 }
169                 break;
170                 case COLLADAFW::MeshVertexData::DATA_TYPE_UNKNOWN:
171                 default:
172                         fprintf(stderr, "MeshImporter.getUV(): unknown data type\n");
173         }
174 }
175
176 void MeshImporter::set_face_indices(MFace *mface, unsigned int *indices, bool quad)
177 {
178         mface->v1 = indices[0];
179         mface->v2 = indices[1];
180         mface->v3 = indices[2];
181         if (quad) mface->v4 = indices[3];
182         else mface->v4 = 0;
183 #ifdef COLLADA_DEBUG
184         // fprintf(stderr, "%u, %u, %u\n", indices[0], indices[1], indices[2]);
185 #endif
186 }
187
188 // not used anymore, test_index_face from blenkernel is better
189 #if 0
190 // change face indices order so that v4 is not 0
191 void MeshImporter::rotate_face_indices(MFace *mface)
192 {
193         mface->v4 = mface->v1;
194         mface->v1 = mface->v2;
195         mface->v2 = mface->v3;
196         mface->v3 = 0;
197 }
198 #endif
199
200 void MeshImporter::set_face_uv(MTFace *mtface, UVDataWrapper &uvs,
201                                COLLADAFW::IndexList& index_list, unsigned int *tris_indices)
202 {
203         // per face vertex indices, this means for quad we have 4 indices, not 8
204         COLLADAFW::UIntValuesArray& indices = index_list.getIndices();
205
206         uvs.getUV(indices[tris_indices[0]], mtface->uv[0]);
207         uvs.getUV(indices[tris_indices[1]], mtface->uv[1]);
208         uvs.getUV(indices[tris_indices[2]], mtface->uv[2]);
209 }
210
211 void MeshImporter::set_face_uv(MTFace *mtface, UVDataWrapper &uvs,
212                                COLLADAFW::IndexList& index_list, int index, bool quad)
213 {
214         // per face vertex indices, this means for quad we have 4 indices, not 8
215         COLLADAFW::UIntValuesArray& indices = index_list.getIndices();
216
217         uvs.getUV(indices[index + 0], mtface->uv[0]);
218         uvs.getUV(indices[index + 1], mtface->uv[1]);
219         uvs.getUV(indices[index + 2], mtface->uv[2]);
220
221         if (quad) uvs.getUV(indices[index + 3], mtface->uv[3]);
222
223 #ifdef COLLADA_DEBUG
224         if (quad) {
225                 fprintf(stderr, "face uv:\n"
226                         "((%d, %d, %d, %d))\n"
227                         "((%.1f, %.1f), (%.1f, %.1f), (%.1f, %.1f), (%.1f, %.1f))\n",
228
229                         indices[index + 0],
230                         indices[index + 1],
231                         indices[index + 2],
232                         indices[index + 3],
233
234                         mtface->uv[0][0], mtface->uv[0][1],
235                         mtface->uv[1][0], mtface->uv[1][1],
236                         mtface->uv[2][0], mtface->uv[2][1],
237                         mtface->uv[3][0], mtface->uv[3][1]);
238         }
239         else {
240                 fprintf(stderr, "face uv:\n"
241                         "((%d, %d, %d))\n"
242                         "((%.1f, %.1f), (%.1f, %.1f), (%.1f, %.1f))\n",
243
244                         indices[index + 0],
245                         indices[index + 1],
246                         indices[index + 2],
247
248                         mtface->uv[0][0], mtface->uv[0][1],
249                         mtface->uv[1][0], mtface->uv[1][1],
250                         mtface->uv[2][0], mtface->uv[2][1]);
251         }
252 #endif
253 }
254
255 #ifdef COLLADA_DEBUG
256 void MeshImporter::print_index_list(COLLADAFW::IndexList& index_list)
257 {
258         fprintf(stderr, "Index list for \"%s\":\n", index_list.getName().c_str());
259         for (int i = 0; i < index_list.getIndicesCount(); i += 2) {
260                 fprintf(stderr, "%u, %u\n", index_list.getIndex(i), index_list.getIndex(i + 1));
261         }
262         fprintf(stderr, "\n");
263 }
264 #endif
265
266 bool MeshImporter::is_nice_mesh(COLLADAFW::Mesh *mesh)  // checks if mesh has supported primitive types: lines, polylist, triangles, triangle_fans
267 {
268         COLLADAFW::MeshPrimitiveArray& prim_arr = mesh->getMeshPrimitives();
269
270         const std::string &name = bc_get_dae_name(mesh);
271         
272         for (unsigned i = 0; i < prim_arr.getCount(); i++) {
273                 
274                 COLLADAFW::MeshPrimitive *mp = prim_arr[i];
275                 COLLADAFW::MeshPrimitive::PrimitiveType type = mp->getPrimitiveType();
276
277                 const char *type_str = bc_primTypeToStr(type);
278                 
279                 // OpenCollada passes POLYGONS type for <polylist>
280                 if (type == COLLADAFW::MeshPrimitive::POLYLIST || type == COLLADAFW::MeshPrimitive::POLYGONS) {
281
282                         COLLADAFW::Polygons *mpvc = (COLLADAFW::Polygons *)mp;
283                         COLLADAFW::Polygons::VertexCountArray& vca = mpvc->getGroupedVerticesVertexCountArray();
284                         
285                         for (unsigned int j = 0; j < vca.getCount(); j++) {
286                                 int count = vca[j];
287                                 if (count < 3) {
288                                         fprintf(stderr, "Primitive %s in %s has at least one face with vertex count < 3\n",
289                                                 type_str, name.c_str());
290                                         return false;
291                                 }
292                         }
293                                 
294                 }
295
296                 else if (type == COLLADAFW::MeshPrimitive::LINES) {
297                         // TODO: Add Checker for line syntax here
298                 }
299
300                 else if (type != COLLADAFW::MeshPrimitive::TRIANGLES && type != COLLADAFW::MeshPrimitive::TRIANGLE_FANS) {
301                         fprintf(stderr, "Primitive type %s is not supported.\n", type_str);
302                         return false;
303                 }
304         }
305         
306         if (mesh->getPositions().empty()) {
307                 fprintf(stderr, "Mesh %s has no vertices.\n", name.c_str());
308                 return false;
309         }
310
311         return true;
312 }
313
314 void MeshImporter::read_vertices(COLLADAFW::Mesh *mesh, Mesh *me)
315 {
316         // vertices
317         COLLADAFW::MeshVertexData& pos = mesh->getPositions();
318         int stride = pos.getStride(0);
319         if (stride == 0) stride = 3;
320         
321         me->totvert = mesh->getPositions().getFloatValues()->getCount() / stride;
322         me->mvert = (MVert *)CustomData_add_layer(&me->vdata, CD_MVERT, CD_CALLOC, NULL, me->totvert);
323
324         MVert *mvert;
325         int i;
326
327         for (i = 0, mvert = me->mvert; i < me->totvert; i++, mvert++) {
328                 get_vector(mvert->co, pos, i, stride);
329         }
330 }
331
332 int MeshImporter::triangulate_poly(unsigned int *indices, int totvert, MVert *verts, std::vector<unsigned int>& tri)
333 {
334         ListBase dispbase;
335         DispList *dl;
336         float *vert;
337         int i = 0;
338         
339         dispbase.first = dispbase.last = NULL;
340         
341         dl = (DispList *)MEM_callocN(sizeof(DispList), "poly disp");
342         dl->nr = totvert;
343         dl->type = DL_POLY;
344         dl->parts = 1;
345         dl->verts = vert = (float *)MEM_callocN(totvert * 3 * sizeof(float), "poly verts");
346         dl->index = (int *)MEM_callocN(sizeof(int) * 3 * totvert, "dl index");
347
348         BLI_addtail(&dispbase, dl);
349         
350         for (i = 0; i < totvert; i++) {
351                 copy_v3_v3(vert, verts[indices[i]].co);
352                 vert += 3;
353         }
354         
355         BKE_displist_fill(&dispbase, &dispbase, 0);
356
357         int tottri = 0;
358         dl = (DispList *)dispbase.first;
359
360         if (dl->type == DL_INDEX3) {
361                 tottri = dl->parts;
362
363                 int *index = dl->index;
364                 for (i = 0; i < tottri; i++) {
365                         int t[3] = {*index, *(index + 1), *(index + 2)};
366
367                         std::sort(t, t + 3);
368
369                         tri.push_back(t[0]);
370                         tri.push_back(t[1]);
371                         tri.push_back(t[2]);
372
373                         index += 3;
374                 }
375         }
376
377         BKE_displist_free(&dispbase);
378
379         return tottri;
380 }
381
382 int MeshImporter::count_new_tris(COLLADAFW::Mesh *mesh, Mesh *me)
383 {
384         COLLADAFW::MeshPrimitiveArray& prim_arr = mesh->getMeshPrimitives();
385         unsigned int i;
386         int tottri = 0;
387         
388         for (i = 0; i < prim_arr.getCount(); i++) {
389                 
390                 COLLADAFW::MeshPrimitive *mp = prim_arr[i];
391                 int type = mp->getPrimitiveType();
392                 size_t prim_totface = mp->getFaceCount();
393                 unsigned int *indices = mp->getPositionIndices().getData();
394                 
395                 if (type == COLLADAFW::MeshPrimitive::POLYLIST ||
396                     type == COLLADAFW::MeshPrimitive::POLYGONS)
397                 {
398                         COLLADAFW::Polygons *mpvc = (COLLADAFW::Polygons *)mp;
399                         COLLADAFW::Polygons::VertexCountArray& vcounta = mpvc->getGroupedVerticesVertexCountArray();
400                         
401                         for (unsigned int j = 0; j < prim_totface; j++) {
402                                 int vcount = vcounta[j];
403                                 
404                                 if (vcount > 4) {
405                                         std::vector<unsigned int> tri;
406                                         
407                                         // tottri += triangulate_poly(indices, vcount, me->mvert, tri) - 1; // XXX why - 1?!
408                                         tottri += triangulate_poly(indices, vcount, me->mvert, tri);
409                                 }
410
411                                 indices += vcount;
412                         }
413                 }
414         }
415         return tottri;
416 }
417
418 // =====================================================================
419 // condition 1: The Primitive has normals
420 // condition 2: The number of normals equals the number of faces.
421 // return true if both conditions apply.
422 // return false otherwise.
423 // =====================================================================
424 bool MeshImporter::primitive_has_useable_normals(COLLADAFW::MeshPrimitive *mp) {
425
426         bool has_useable_normals = false;
427
428         int normals_count = mp->getNormalIndices().getCount();
429         if (normals_count > 0) {
430                 int index_count   = mp->getPositionIndices().getCount();
431                 if (index_count == normals_count) 
432                         has_useable_normals = true;
433                 else {
434                         fprintf(stderr,
435                                 "Warning: Number of normals %d is different from the number of vertices %d, skipping normals\n",
436                                 normals_count, index_count);
437                 }
438         }
439
440         return has_useable_normals;
441
442 }
443
444 // =====================================================================
445 // Assume that only TRIANGLES, TRIANGLE_FANS, POLYLIST and POLYGONS
446 // have faces. (to be verified)
447 // =====================================================================
448 bool MeshImporter::primitive_has_faces(COLLADAFW::MeshPrimitive *mp) {
449
450         bool has_faces = false;
451         int type = mp->getPrimitiveType();
452         switch (type) {
453                 case COLLADAFW::MeshPrimitive::TRIANGLES:
454                 case COLLADAFW::MeshPrimitive::TRIANGLE_FANS:
455                 case COLLADAFW::MeshPrimitive::POLYLIST:
456                 case COLLADAFW::MeshPrimitive::POLYGONS: {
457                         has_faces = true;
458                         break;
459                 }
460                 default: {
461                         has_faces = false; 
462                         break;
463                 }
464         }
465         return has_faces;
466 }
467
468 // =================================================================
469 // Return the number of faces by summing up
470 // the facecounts of the parts.
471 // hint: This is done because mesh->getFacesCount() does
472 // count loose edges as extra faces, which is not what we want here.
473 // =================================================================
474 void MeshImporter::allocate_face_data(COLLADAFW::Mesh *mesh, Mesh *me, int new_tris)
475 {
476         COLLADAFW::MeshPrimitiveArray& prim_arr = mesh->getMeshPrimitives();
477         int total_facecount = 0;
478
479         // collect edge_count and face_count from all parts
480         for (int i = 0; i < prim_arr.getCount(); i++) {
481                 COLLADAFW::MeshPrimitive *mp = prim_arr[i];
482                 int type = mp->getPrimitiveType();
483                 switch (type) {
484                         case COLLADAFW::MeshPrimitive::TRIANGLES:
485                         case COLLADAFW::MeshPrimitive::TRIANGLE_FANS:
486                         case COLLADAFW::MeshPrimitive::POLYLIST:
487                         case COLLADAFW::MeshPrimitive::POLYGONS: {
488                                 size_t prim_totface = mp->getFaceCount();
489                                 total_facecount += prim_totface;
490                                 break;
491                         }
492                         default: break;
493                 }
494         }
495
496         // allocate space for faces
497         if (total_facecount > 0) {
498                 me->totface = total_facecount + new_tris;
499                 me->mface   = (MFace *)CustomData_add_layer(&me->fdata, CD_MFACE, CD_CALLOC, NULL, me->totface);
500         }
501 }
502
503 unsigned int MeshImporter::get_loose_edge_count(COLLADAFW::Mesh *mesh) {
504         COLLADAFW::MeshPrimitiveArray& prim_arr = mesh->getMeshPrimitives();
505         int loose_edge_count = 0;
506
507         // collect edge_count and face_count from all parts
508         for (int i = 0; i < prim_arr.getCount(); i++) {
509                 COLLADAFW::MeshPrimitive *mp = prim_arr[i];
510                 int type = mp->getPrimitiveType();
511                 switch (type) {
512                         case COLLADAFW::MeshPrimitive::LINES: {
513                                 size_t prim_totface = mp->getFaceCount();
514                                 loose_edge_count += prim_totface;
515                                 break;
516                         }
517                         default: break;
518                 }
519         }
520         return loose_edge_count;
521 }
522
523 // =================================================================
524 // This functin is copied from source/blender/editors/mesh/mesh_data.c
525 //
526 // TODO: (As discussed with sergey-) :
527 // Maybe move this function to blenderkernel/intern/mesh.c 
528 // and add definition to BKE_mesh.c
529 // =================================================================
530 void MeshImporter::mesh_add_edges(Mesh *mesh, int len)
531 {
532         CustomData edata;
533         MEdge *medge;
534         int i, totedge;
535
536         if (len == 0)
537                 return;
538
539         totedge = mesh->totedge + len;
540
541         /* update customdata  */
542         CustomData_copy(&mesh->edata, &edata, CD_MASK_MESH, CD_DEFAULT, totedge);
543         CustomData_copy_data(&mesh->edata, &edata, 0, 0, mesh->totedge);
544
545         if (!CustomData_has_layer(&edata, CD_MEDGE))
546                 CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, totedge);
547
548         CustomData_free(&mesh->edata, mesh->totedge);
549         mesh->edata = edata;
550         mesh_update_customdata_pointers(mesh, FALSE); /* new edges don't change tessellation */
551
552         /* set default flags */
553         medge = &mesh->medge[mesh->totedge];
554         for (i = 0; i < len; i++, medge++)
555                 medge->flag = ME_EDGEDRAW | ME_EDGERENDER | SELECT;
556
557         mesh->totedge = totedge;
558 }
559
560 // =================================================================
561 // Read all loose edges.
562 // Important: This function assumes that all edges from existing 
563 // faces have allready been generated and added to me->medge
564 // So this function MUST be called after read_faces() (see below)
565 // =================================================================
566 void MeshImporter::read_lines(COLLADAFW::Mesh *mesh, Mesh *me)
567 {
568         unsigned int loose_edge_count = get_loose_edge_count(mesh);
569         if (loose_edge_count > 0) {
570
571                 unsigned int face_edge_count  = me->totedge;
572                 /* unsigned int total_edge_count = loose_edge_count + face_edge_count; */ /* UNUSED */
573                 
574                 mesh_add_edges(me, loose_edge_count);
575                 MEdge *med = me->medge + face_edge_count;
576
577                 COLLADAFW::MeshPrimitiveArray& prim_arr = mesh->getMeshPrimitives();
578
579                 for (int i = 0; i < prim_arr.getCount(); i++) {
580                         
581                         COLLADAFW::MeshPrimitive *mp = prim_arr[i];
582
583                         int type = mp->getPrimitiveType();
584                         if (type == COLLADAFW::MeshPrimitive::LINES) {
585                                 unsigned int edge_count  = mp->getFaceCount();
586                                 unsigned int *indices    = mp->getPositionIndices().getData();
587                                 
588                                 for (int i = 0; i < edge_count; i++, med++) {
589                                         med->bweight = 0;
590                                         med->crease  = 0;
591                                         med->flag   |= ME_LOOSEEDGE;
592                                         med->v1      = indices[2 * i];
593                                         med->v2      = indices[2 * i + 1];
594                                 }
595                         }
596                 }
597
598         }
599 }
600
601
602 // =======================================================================
603 // Read all faces from TRIANGLES, TRIANGLE_FANS, POLYLIST, POLYGON
604 // Important: This function MUST be called before read_lines() 
605 // Otherwise we will loose all edges from faces (see read_lines() above)
606 //
607 // TODO: import uv set names
608 // ========================================================================
609 void MeshImporter::read_faces(COLLADAFW::Mesh *mesh, Mesh *me, int new_tris) //TODO:: Refactor. Possibly replace by iterators
610 {
611         unsigned int i;
612         
613         allocate_face_data(mesh, me, new_tris);
614         
615         // allocate UV Maps
616         unsigned int totuvset = mesh->getUVCoords().getInputInfosArray().getCount();
617
618         for (i = 0; i < totuvset; i++) {
619                 if (mesh->getUVCoords().getLength(i) == 0) {
620                         totuvset = 0;
621                         break;
622                 }
623         }
624
625         for (i = 0; i < totuvset; i++) {
626                 COLLADAFW::MeshVertexData::InputInfos *info = mesh->getUVCoords().getInputInfosArray()[i];
627                 CustomData_add_layer_named(&me->fdata, CD_MTFACE, CD_CALLOC, NULL, me->totface, info->mName.c_str());
628                 //this->set_layername_map[i] = CustomData_get_layer_name(&me->fdata, CD_MTFACE, i);
629         }
630
631         // activate the first uv map
632         if (totuvset) me->mtface = (MTFace *)CustomData_get_layer_n(&me->fdata, CD_MTFACE, 0);
633
634         UVDataWrapper uvs(mesh->getUVCoords());
635
636 #ifdef COLLADA_DEBUG
637         // uvs.print();
638 #endif
639
640         MFace *mface = me->mface;
641
642         MaterialIdPrimitiveArrayMap mat_prim_map;
643
644         int face_index = 0;
645
646         COLLADAFW::MeshPrimitiveArray& prim_arr = mesh->getMeshPrimitives();
647
648         COLLADAFW::MeshVertexData& nor = mesh->getNormals();
649
650         for (i = 0; i < prim_arr.getCount(); i++) {
651                 
652                 COLLADAFW::MeshPrimitive *mp = prim_arr[i];
653
654                 // faces
655                 size_t prim_totface = mp->getFaceCount();
656                 unsigned int *indices = mp->getPositionIndices().getData();
657                 unsigned int *nind    = mp->getNormalIndices().getData();
658
659                 bool mp_has_normals = primitive_has_useable_normals(mp);
660                 bool mp_has_faces   = primitive_has_faces(mp);
661
662                 int type = mp->getPrimitiveType();
663                 int index = 0;
664                 
665                 // since we cannot set mface->mat_nr here, we store a portion of me->mface in Primitive
666                 Primitive prim = {mface, 0};
667                 COLLADAFW::IndexListArray& index_list_array = mp->getUVCoordIndicesArray();
668
669 #ifdef COLLADA_DEBUG
670                 /*
671                    fprintf(stderr, "Primitive %d:\n", i);
672                    for (unsigned int j = 0; j < totuvset; j++) {
673                     print_index_list(*index_list_array[j]);
674                    }
675                  */
676 #endif
677                 
678                 if (type == COLLADAFW::MeshPrimitive::TRIANGLES) {
679                         for (unsigned int j = 0; j < prim_totface; j++) {
680                                 
681                                 set_face_indices(mface, indices, false);
682                                 indices += 3;
683
684 #if 0
685                                 for (unsigned int k = 0; k < totuvset; k++) {
686                                         if (!index_list_array.empty() && index_list_array[k]) {
687                                                 // get mtface by face index and uv set index
688                                                 MTFace *mtface = (MTFace *)CustomData_get_layer_n(&me->fdata, CD_MTFACE, k);
689                                                 set_face_uv(&mtface[face_index], uvs, k, *index_list_array[k], index, false);
690                                         }
691                                 }
692 #else
693                                 for (unsigned int k = 0; k < index_list_array.getCount(); k++) {
694                                         // get mtface by face index and uv set index
695                                         MTFace *mtface = (MTFace *)CustomData_get_layer_n(&me->fdata, CD_MTFACE, k);
696                                         set_face_uv(&mtface[face_index], uvs, *index_list_array[k], index, false);
697                                 }
698 #endif
699
700                                 test_index_face(mface, &me->fdata, face_index, 3);
701
702                                 if (mp_has_normals) {
703                                         if (!flat_face(nind, nor, 3))
704                                                 mface->flag |= ME_SMOOTH;
705
706                                         nind += 3;
707                                 }
708                                 
709                                 index += 3;
710                                 mface++;
711                                 face_index++;
712                                 prim.totface++;
713                         }
714                 }
715
716                 // If MeshPrimitive is TRIANGLE_FANS we split it into triangles
717                 // The first trifan vertex will be the first vertex in every triangle
718                 if (type == COLLADAFW::MeshPrimitive::TRIANGLE_FANS) {
719                         unsigned grouped_vertex_count = mp->getGroupedVertexElementsCount();
720                         for (unsigned int group_index = 0; group_index < grouped_vertex_count; group_index++) {
721                                 unsigned int first_vertex = indices[0]; // Store first trifan vertex
722                                 unsigned int first_normal = nind[0]; // Store first trifan vertex normal
723                                 unsigned int vertex_count = mp->getGroupedVerticesVertexCount(group_index);
724
725                                 for (unsigned int vertex_index = 0; vertex_index < vertex_count - 2; vertex_index++) {
726                                         // For each triangle store indeces of its 3 vertices
727                                         unsigned int triangle_vertex_indices[3] = {first_vertex, indices[1], indices[2]};
728                                         set_face_indices(mface, triangle_vertex_indices, false);
729                                         test_index_face(mface, &me->fdata, face_index, 3);
730
731                                         if (mp_has_normals) {  // vertex normals, same inplementation as for the triangles
732                                                 // the same for vertces normals
733                                                 unsigned int vertex_normal_indices[3] = {first_normal, nind[1], nind[2]};
734                                                 if (!flat_face(vertex_normal_indices, nor, 3))
735                                                         mface->flag |= ME_SMOOTH;
736                                                 nind++;
737                                         }
738                                 
739                                         mface++;  // same inplementation as for the triangles
740                                         indices++;
741                                         face_index++;
742                                         prim.totface++;
743                                 }
744
745                                 // Moving cursor  to the next triangle fan.
746                                 if (mp_has_normals)
747                                         nind += 2;
748
749                                 indices +=  2;
750                         }
751                 }
752                 else if (type == COLLADAFW::MeshPrimitive::POLYLIST || type == COLLADAFW::MeshPrimitive::POLYGONS) {
753                         COLLADAFW::Polygons *mpvc = (COLLADAFW::Polygons *)mp;
754                         COLLADAFW::Polygons::VertexCountArray& vcounta = mpvc->getGroupedVerticesVertexCountArray();
755
756                         for (unsigned int j = 0; j < prim_totface; j++) {
757                                 
758                                 // face
759                                 int vcount = vcounta[j];
760                                 if (vcount == 3 || vcount == 4) {
761                                         
762                                         set_face_indices(mface, indices, vcount == 4);
763                                         
764                                         // set mtface for each uv set
765                                         // it is assumed that all primitives have equal number of UV sets
766
767 #if 0
768                                         for (unsigned int k = 0; k < totuvset; k++) {
769                                                 if (!index_list_array.empty() && index_list_array[k]) {
770                                                         // get mtface by face index and uv set index
771                                                         MTFace *mtface = (MTFace *)CustomData_get_layer_n(&me->fdata, CD_MTFACE, k);
772                                                         set_face_uv(&mtface[face_index], uvs, k, *index_list_array[k], index, mface->v4 != 0);
773                                                 }
774                                         }
775 #else
776                                         for (unsigned int k = 0; k < index_list_array.getCount(); k++) {
777                                                 // get mtface by face index and uv set index
778                                                 MTFace *mtface = (MTFace *)CustomData_get_layer_n(&me->fdata, CD_MTFACE, k);
779                                                 set_face_uv(&mtface[face_index], uvs, *index_list_array[k], index, vcount == 4);
780                                         }
781 #endif
782
783                                         test_index_face(mface, &me->fdata, face_index, vcount);
784
785                                         if (mp_has_normals) {
786                                                 if (!flat_face(nind, nor, vcount))
787                                                         mface->flag |= ME_SMOOTH;
788
789                                                 nind += vcount;
790                                         }
791                                         
792                                         mface++;
793                                         face_index++;
794                                         prim.totface++;
795                                         
796                                 }
797                                 else {
798                                         std::vector<unsigned int> tri;
799                                         
800                                         triangulate_poly(indices, vcount, me->mvert, tri);
801
802                                         for (unsigned int k = 0; k < tri.size() / 3; k++) {
803                                                 int v = k * 3;
804                                                 unsigned int uv_indices[3] = {
805                                                         index + tri[v],
806                                                         index + tri[v + 1],
807                                                         index + tri[v + 2]
808                                                 };
809                                                 unsigned int tri_indices[3] = {
810                                                         indices[tri[v]],
811                                                         indices[tri[v + 1]],
812                                                         indices[tri[v + 2]]
813                                                 };
814
815                                                 set_face_indices(mface, tri_indices, false);
816                                                 
817 #if 0
818                                                 for (unsigned int l = 0; l < totuvset; l++) {
819                                                         if (!index_list_array.empty() && index_list_array[l]) {
820                                                                 // get mtface by face index and uv set index
821                                                                 MTFace *mtface = (MTFace *)CustomData_get_layer_n(&me->fdata, CD_MTFACE, l);
822                                                                 set_face_uv(&mtface[face_index], uvs, l, *index_list_array[l], uv_indices);
823                                                         }
824                                                 }
825 #else
826                                                 for (unsigned int l = 0; l < index_list_array.getCount(); l++) {
827                                                         int uvset_index = index_list_array[l]->getSetIndex();
828
829                                                         // get mtface by face index and uv set index
830                                                         MTFace *mtface = (MTFace *)CustomData_get_layer_n(&me->fdata, CD_MTFACE, uvset_index);
831                                                         set_face_uv(&mtface[face_index], uvs, *index_list_array[l], uv_indices);
832                                                 }
833 #endif
834
835
836                                                 test_index_face(mface, &me->fdata, face_index, 3);
837
838                                                 if (mp_has_normals) {
839                                                         unsigned int ntri[3] = {nind[tri[v]], nind[tri[v + 1]], nind[tri[v + 2]]};
840
841                                                         if (!flat_face(ntri, nor, 3))
842                                                                 mface->flag |= ME_SMOOTH;
843                                                 }
844                                                 
845                                                 mface++;
846                                                 face_index++;
847                                                 prim.totface++;
848                                         }
849
850                                         if (mp_has_normals)
851                                                 nind += vcount;
852                                 }
853
854                                 index += vcount;
855                                 indices += vcount;
856                         }
857                 }
858                 else if (type == COLLADAFW::MeshPrimitive::LINES) {
859                         continue; // read the lines later after all the rest is done
860                 }
861
862                 if (mp_has_faces)
863                         mat_prim_map[mp->getMaterialId()].push_back(prim);
864         }
865
866         geom_uid_mat_mapping_map[mesh->getUniqueId()] = mat_prim_map;
867 }
868
869 void MeshImporter::get_vector(float v[3], COLLADAFW::MeshVertexData& arr, int i, int stride)
870 {
871         i *= stride;
872         
873         switch (arr.getType()) {
874                 case COLLADAFW::MeshVertexData::DATA_TYPE_FLOAT:
875                 {
876                         COLLADAFW::ArrayPrimitiveType<float> *values = arr.getFloatValues();
877                         if (values->empty()) return;
878
879                         v[0] = (*values)[i++];
880                         v[1] = (*values)[i++];
881                         v[2] = (*values)[i];
882
883                 }
884                 break;
885                 case COLLADAFW::MeshVertexData::DATA_TYPE_DOUBLE:
886                 {
887                         COLLADAFW::ArrayPrimitiveType<double> *values = arr.getDoubleValues();
888                         if (values->empty()) return;
889
890                         v[0] = (float)(*values)[i++];
891                         v[1] = (float)(*values)[i++];
892                         v[2] = (float)(*values)[i];
893                 }
894                 break;
895                 default:
896                         break;
897         }
898 }
899
900 bool MeshImporter::flat_face(unsigned int *nind, COLLADAFW::MeshVertexData& nor, int count)
901 {
902         float a[3], b[3];
903
904         get_vector(a, nor, *nind, 3);
905         normalize_v3(a);
906
907         nind++;
908
909         for (int i = 1; i < count; i++, nind++) {
910                 get_vector(b, nor, *nind, 3);
911                 normalize_v3(b);
912
913                 float dp = dot_v3v3(a, b);
914
915                 if (dp < 0.99999f || dp > 1.00001f)
916                         return false;
917         }
918
919         return true;
920 }
921
922 MeshImporter::MeshImporter(UnitConverter *unitconv, ArmatureImporter *arm, Scene *sce) : unitconverter(unitconv), scene(sce), armature_importer(arm) {
923 }
924
925 void MeshImporter::bmeshConversion()
926 {
927         for (std::map<COLLADAFW::UniqueId, Mesh *>::iterator m = uid_mesh_map.begin();
928              m != uid_mesh_map.end(); ++m)
929         {
930                 if ((*m).second) {
931                         Mesh *me = (*m).second;
932                         BKE_mesh_convert_mfaces_to_mpolys(me);
933                         BKE_mesh_tessface_clear(me);
934
935                         BKE_mesh_calc_normals_mapping(me->mvert, me->totvert, me->mloop, me->mpoly, me->totloop, me->totpoly, NULL, NULL, 0, NULL, NULL);
936                 }
937         }
938 }
939
940
941 Object *MeshImporter::get_object_by_geom_uid(const COLLADAFW::UniqueId& geom_uid)
942 {
943         if (uid_object_map.find(geom_uid) != uid_object_map.end())
944                 return uid_object_map[geom_uid];
945         return NULL;
946 }
947
948 Mesh *MeshImporter::get_mesh_by_geom_uid(const COLLADAFW::UniqueId& mesh_uid)
949 {
950         if (uid_mesh_map.find(mesh_uid) != uid_mesh_map.end())
951                 return uid_mesh_map[mesh_uid];
952         return NULL;
953 }
954
955 MTex *MeshImporter::assign_textures_to_uvlayer(COLLADAFW::TextureCoordinateBinding &ctexture,
956                                                Mesh *me, TexIndexTextureArrayMap& texindex_texarray_map,
957                                                MTex *color_texture)
958 {
959         const COLLADAFW::TextureMapId texture_index = ctexture.getTextureMapId();
960         size_t setindex = ctexture.getSetIndex();
961         std::string uvname = ctexture.getSemantic();
962         
963         if (setindex == -1) return NULL;
964         
965         const CustomData *data = &me->fdata;
966         int layer_index = CustomData_get_layer_index(data, CD_MTFACE);
967
968         if (layer_index == -1) return NULL;
969
970         CustomDataLayer *cdl = &data->layers[layer_index + setindex];
971         
972         /* set uvname to bind_vertex_input semantic */
973         BLI_strncpy(cdl->name, uvname.c_str(), sizeof(cdl->name));
974
975         if (texindex_texarray_map.find(texture_index) == texindex_texarray_map.end()) {
976                 
977                 fprintf(stderr, "Cannot find texture array by texture index.\n");
978                 return color_texture;
979         }
980         
981         std::vector<MTex *> textures = texindex_texarray_map[texture_index];
982         
983         std::vector<MTex *>::iterator it;
984         
985         for (it = textures.begin(); it != textures.end(); it++) {
986                 
987                 MTex *texture = *it;
988                 
989                 if (texture) {
990                         BLI_strncpy(texture->uvname, uvname.c_str(), sizeof(texture->uvname));
991                         if (texture->mapto == MAP_COL) color_texture = texture;
992                 }
993         }
994         return color_texture;
995 }
996
997 /**
998  * this function checks if both objects have the same
999  * materials assigned to Object (in the same order)
1000  * returns true if condition matches, otherwise false;
1001  **/
1002 static bool bc_has_same_material_configuration(Object *ob1, Object *ob2)
1003 {
1004         if (ob1->totcol != ob2->totcol) return false; // not same number of materials
1005         if (ob1->totcol == 0) return false; // no material at all
1006         
1007         for (int index=0; index < ob1->totcol; index++) {
1008                 if (ob1->matbits[index] != ob2->matbits[index]) return false; // shouldn't happen
1009                 if (ob1->matbits[index] == 0) return false; // shouldn't happen
1010                 if (ob1->mat[index] != ob2->mat[index]) return false; // different material assignment
1011         }
1012         return true;
1013 }
1014
1015
1016 /**
1017  *
1018  * Caution here: This code assumes tha all materials are assigned to Object
1019  * and no material is assigned to Data.
1020  * That is true right after the objects have been imported.
1021  *
1022  **/
1023 static void bc_copy_materials_to_data(Object *ob, Mesh *me)
1024 {
1025         for (int index = 0; index < ob->totcol; index++) {
1026                 ob->matbits[index] = 0;
1027                 me->mat[index] = ob->mat[index];
1028         }
1029 }
1030
1031 /**
1032  *
1033  * Remove all references to materials from the object
1034  *
1035  **/
1036 static void bc_remove_materials_from_object(Object *ob, Mesh *me)
1037 {
1038         for (int index = 0; index < ob->totcol; index++) {
1039                 ob->matbits[index] = 0;
1040                 ob->mat[index] = NULL;
1041         }
1042 }
1043
1044 /**
1045  * Returns the list of Users of the given Mesh object.
1046  * Note: This function uses the object user flag to control
1047  * which objects have already been processed.
1048  **/
1049 std::vector<Object *> MeshImporter::get_all_users_of(Mesh *reference_mesh)
1050 {
1051         std::vector<Object *> mesh_users;
1052         for (std::vector<Object *>::iterator it = imported_objects.begin();
1053              it != imported_objects.end(); ++it)
1054         {
1055                 Object *ob = (*it);
1056                 if (bc_is_marked(ob)) {
1057                         bc_remove_mark(ob);
1058                         Mesh *me = (Mesh *) ob->data;
1059                         if (me == reference_mesh)
1060                                 mesh_users.push_back(ob);
1061                 }
1062         }
1063         return mesh_users;
1064 }
1065
1066 /**
1067  *
1068  * During import all materials have been assigned to Object.
1069  * Now we iterate over the imported objects and optimize
1070  * the assignements as follows:
1071  *
1072  * for each imported geometry:
1073  *     if number of users is 1:
1074  *         get the user (object)
1075  *         move the materials from Object to Data
1076  *     else:
1077  *         determine which materials are assigned to the first user
1078  *         check if all other users have the same materials in the same order
1079  *         if the check is positive:
1080  *             Add the materials of the first user to the geometry
1081  *             adjust all other users accordingly.
1082  *
1083  **/
1084 void MeshImporter::optimize_material_assignements()
1085 {
1086         for (std::vector<Object *>::iterator it = imported_objects.begin();
1087              it != imported_objects.end(); ++it)
1088         {
1089                 Object *ob = (*it);
1090                 Mesh *me = (Mesh *) ob->data;
1091                 if (me->id.us==1) {
1092                         bc_copy_materials_to_data(ob, me);
1093                         bc_remove_materials_from_object(ob, me);
1094                         bc_remove_mark(ob);
1095                 }
1096                 else if (me->id.us > 1)
1097                 {
1098                         bool can_move = true;
1099                         std::vector<Object *> mesh_users = get_all_users_of(me);
1100                         if (mesh_users.size() > 1)
1101                         {
1102                                 Object *ref_ob = mesh_users[0];
1103                                 for (int index = 1; index < mesh_users.size(); index++) {
1104                                         if (!bc_has_same_material_configuration(ref_ob, mesh_users[index])) {
1105                                                 can_move = false;
1106                                                 break;
1107                                         }
1108                                 }
1109                                 if (can_move) {
1110                                         bc_copy_materials_to_data(ref_ob, me);
1111                                         for (int index = 0; index < mesh_users.size(); index++) {
1112                                                 Object *object = mesh_users[index];
1113                                                 bc_remove_materials_from_object(object, me);
1114                                                 bc_remove_mark(object);
1115                                         }
1116                                 }
1117                         }
1118                 }
1119         }
1120 }
1121
1122 /**
1123  * We do not know in advance which objects will share geometries.
1124  * And we do not know either if the objects which share geometries
1125  * come along with different materials. So we first create the objects
1126  * and assign the materials to Object, then in a later cleanup we decide
1127  * which materials shall be moved to the created geometries. Also see
1128  * optimize_material_assignements() above.
1129  */
1130 MTFace *MeshImporter::assign_material_to_geom(COLLADAFW::MaterialBinding cmaterial,
1131                                               std::map<COLLADAFW::UniqueId, Material *>& uid_material_map,
1132                                               Object *ob, const COLLADAFW::UniqueId *geom_uid,
1133                                               MTex **color_texture, char *layername, MTFace *texture_face,
1134                                               std::map<Material *, TexIndexTextureArrayMap>& material_texture_mapping_map, short mat_index)
1135 {
1136         Mesh *me = (Mesh *)ob->data;
1137         const COLLADAFW::UniqueId& ma_uid = cmaterial.getReferencedMaterial();
1138         
1139         // do we know this material?
1140         if (uid_material_map.find(ma_uid) == uid_material_map.end()) {
1141                 
1142                 fprintf(stderr, "Cannot find material by UID.\n");
1143                 return NULL;
1144         }
1145
1146         // first time we get geom_uid, ma_uid pair. Save for later check.
1147         materials_mapped_to_geom.insert(std::pair<COLLADAFW::UniqueId, COLLADAFW::UniqueId>(*geom_uid, ma_uid));
1148         
1149         Material *ma = uid_material_map[ma_uid];
1150
1151         // Attention! This temporaly assigns material to object on purpose!
1152         // See note above.
1153         ob->actcol=0;
1154         assign_material(ob, ma, mat_index + 1, BKE_MAT_ASSIGN_OBJECT); 
1155         
1156         COLLADAFW::TextureCoordinateBindingArray& tex_array = 
1157             cmaterial.getTextureCoordinateBindingArray();
1158         TexIndexTextureArrayMap texindex_texarray_map = material_texture_mapping_map[ma];
1159         unsigned int i;
1160         // loop through <bind_vertex_inputs>
1161         for (i = 0; i < tex_array.getCount(); i++) {
1162                 
1163                 *color_texture = assign_textures_to_uvlayer(tex_array[i], me, texindex_texarray_map,
1164                                                             *color_texture);
1165         }
1166         
1167         // set texture face
1168         if (*color_texture &&
1169             strlen((*color_texture)->uvname) &&
1170             strcmp(layername, (*color_texture)->uvname) != 0) {
1171                 texture_face = (MTFace *)CustomData_get_layer_named(&me->fdata, CD_MTFACE,
1172                                                                     (*color_texture)->uvname);
1173                 strcpy(layername, (*color_texture)->uvname);
1174         }
1175         
1176         MaterialIdPrimitiveArrayMap& mat_prim_map = geom_uid_mat_mapping_map[*geom_uid];
1177         COLLADAFW::MaterialId mat_id = cmaterial.getMaterialId();
1178         
1179         // assign material indices to mesh faces
1180         if (mat_prim_map.find(mat_id) != mat_prim_map.end()) {
1181                 
1182                 std::vector<Primitive>& prims = mat_prim_map[mat_id];
1183                 
1184                 std::vector<Primitive>::iterator it;
1185                 
1186                 for (it = prims.begin(); it != prims.end(); it++) {
1187                         Primitive& prim = *it;
1188                         MFace *mface = prim.mface;
1189
1190                         for (i = 0; i < prim.totface; i++, mface++) {
1191                                 mface->mat_nr = mat_index;
1192                                 // bind texture images to faces
1193                                 if (texture_face && (*color_texture)) {
1194                                         texture_face->tpage = (Image *)(*color_texture)->tex->ima;
1195                                         texture_face++;
1196                                 }
1197                         }
1198                 }
1199         }
1200         
1201         return texture_face;
1202 }
1203
1204 Object *MeshImporter::create_mesh_object(COLLADAFW::Node *node, COLLADAFW::InstanceGeometry *geom,
1205                                          bool isController,
1206                                          std::map<COLLADAFW::UniqueId, Material *>& uid_material_map,
1207                                          std::map<Material *, TexIndexTextureArrayMap>& material_texture_mapping_map)
1208 {
1209         const COLLADAFW::UniqueId *geom_uid = &geom->getInstanciatedObjectId();
1210         
1211         // check if node instanciates controller or geometry
1212         if (isController) {
1213                 
1214                 geom_uid = armature_importer->get_geometry_uid(*geom_uid);
1215                 
1216                 if (!geom_uid) {
1217                         fprintf(stderr, "Couldn't find a mesh UID by controller's UID.\n");
1218                         return NULL;
1219                 }
1220         }
1221         else {
1222                 
1223                 if (uid_mesh_map.find(*geom_uid) == uid_mesh_map.end()) {
1224                         // this could happen if a mesh was not created
1225                         // (e.g. if it contains unsupported geometry)
1226                         fprintf(stderr, "Couldn't find a mesh by UID.\n");
1227                         return NULL;
1228                 }
1229         }
1230         if (!uid_mesh_map[*geom_uid]) return NULL;
1231         
1232         // name Object
1233         const std::string& id = node->getName().size() ? node->getName() : node->getOriginalId();
1234         const char *name = (id.length()) ? id.c_str() : NULL;
1235         
1236         // add object
1237         Object *ob = bc_add_object(scene, OB_MESH, name);
1238         bc_set_mark(ob); // used later for material assignement optimization
1239
1240
1241         // store object pointer for ArmatureImporter
1242         uid_object_map[*geom_uid] = ob;
1243         imported_objects.push_back(ob);
1244         
1245         // replace ob->data freeing the old one
1246         Mesh *old_mesh = (Mesh *)ob->data;
1247
1248         set_mesh(ob, uid_mesh_map[*geom_uid]);
1249         
1250         if (old_mesh->id.us == 0) BKE_libblock_free(&G.main->mesh, old_mesh);
1251         
1252         char layername[100];
1253         layername[0] = '\0';
1254         MTFace *texture_face = NULL;
1255         MTex *color_texture = NULL;
1256         
1257         COLLADAFW::MaterialBindingArray& mat_array =
1258             geom->getMaterialBindings();
1259         
1260         // loop through geom's materials
1261         for (unsigned int i = 0; i < mat_array.getCount(); i++) {
1262                 
1263                 if (mat_array[i].getReferencedMaterial().isValid()) {
1264                         texture_face = assign_material_to_geom(mat_array[i], uid_material_map, ob, geom_uid,
1265                                                                &color_texture, layername, texture_face,
1266                                                                material_texture_mapping_map, i);
1267                 }
1268                 else {
1269                         fprintf(stderr, "invalid referenced material for %s\n", mat_array[i].getName().c_str());
1270                 }
1271         }
1272
1273         return ob;
1274 }
1275
1276 // create a mesh storing a pointer in a map so it can be retrieved later by geometry UID
1277 bool MeshImporter::write_geometry(const COLLADAFW::Geometry *geom)
1278 {
1279         // TODO: import also uvs, normals
1280         // XXX what to do with normal indices?
1281         // XXX num_normals may be != num verts, then what to do?
1282
1283         // check geometry->getType() first
1284         if (geom->getType() != COLLADAFW::Geometry::GEO_TYPE_MESH) {
1285                 // TODO: report warning
1286                 fprintf(stderr, "Mesh type %s is not supported\n", bc_geomTypeToStr(geom->getType()));
1287                 return true;
1288         }
1289         
1290         COLLADAFW::Mesh *mesh = (COLLADAFW::Mesh *)geom;
1291         
1292         if (!is_nice_mesh(mesh)) {
1293                 fprintf(stderr, "Ignoring mesh %s\n", bc_get_dae_name(mesh).c_str());
1294                 return true;
1295         }
1296         
1297         const std::string& str_geom_id = mesh->getName().size() ? mesh->getName() : mesh->getOriginalId();
1298         Mesh *me = BKE_mesh_add(G.main, (char *)str_geom_id.c_str());
1299         me->id.us--; // is already 1 here, but will be set later in set_mesh
1300
1301         // store the Mesh pointer to link it later with an Object
1302         this->uid_mesh_map[mesh->getUniqueId()] = me;
1303         
1304         int new_tris = 0;
1305         
1306         read_vertices(mesh, me);
1307
1308         new_tris = count_new_tris(mesh, me);
1309         
1310         read_faces(mesh, me, new_tris);
1311
1312         BKE_mesh_make_edges(me, 0);
1313
1314         // read_lines() must be called after the face edges have been generated.
1315         // Oterwise the loose edges will be silently deleted again.
1316         read_lines(mesh, me);
1317
1318         return true;
1319 }