svn merge ^/trunk/blender -r46300:46330
[blender.git] / source / blender / collada / DocumentImporter.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/DocumentImporter.cpp
24  *  \ingroup collada
25  */
26
27 // TODO:
28 // * name imported objects
29 // * import object rotation as euler
30
31 #include <string>
32 #include <map>
33 #include <algorithm> // sort()
34
35 #include "COLLADAFWRoot.h"
36 #include "COLLADAFWStableHeaders.h"
37 #include "COLLADAFWColorOrTexture.h"
38 #include "COLLADAFWIndexList.h"
39 #include "COLLADAFWMeshPrimitiveWithFaceVertexCount.h"
40 #include "COLLADAFWPolygons.h"
41 #include "COLLADAFWSampler.h"
42 #include "COLLADAFWTypes.h"
43 #include "COLLADAFWVisualScene.h"
44 #include "COLLADAFWArrayPrimitiveType.h"
45 #include "COLLADAFWLibraryNodes.h"
46 #include "COLLADAFWCamera.h"
47 #include "COLLADAFWLight.h"
48
49 #include "COLLADASaxFWLLoader.h"
50 #include "COLLADASaxFWLIExtraDataCallbackHandler.h"
51
52 #include "BLI_listbase.h"
53 #include "BLI_math.h"
54 #include "BLI_string.h"
55 #include "BLI_utildefines.h"
56
57 #include "BKE_camera.h"
58 #include "BKE_main.h"
59 #include "BKE_lamp.h"
60 #include "BKE_library.h"
61 #include "BKE_texture.h"
62 #include "BKE_fcurve.h"
63 #include "BKE_depsgraph.h"
64 #include "BLI_path_util.h"
65 #include "BKE_scene.h"
66 #include "BKE_global.h"
67 #include "BKE_material.h"
68 #include "BKE_utildefines.h"
69 #include "BKE_image.h"
70
71 #include "DNA_camera_types.h"
72 #include "DNA_lamp_types.h"
73
74 #include "RNA_access.h"
75
76 #include "MEM_guardedalloc.h"
77
78 #include "ExtraHandler.h"
79 #include "ErrorHandler.h"
80 #include "DocumentImporter.h"
81 #include "TransformReader.h"
82
83 #include "collada_internal.h"
84 #include "collada_utils.h"
85
86
87 /*
88   COLLADA Importer limitations:
89   - no multiple scene import, all objects are added to active scene
90  */
91
92 // #define COLLADA_DEBUG
93 // creates empties for each imported bone on layer 2, for debugging
94 // #define ARMATURE_TEST
95
96 DocumentImporter::DocumentImporter(bContext *C, const char *filename) :
97         mImportStage(General),
98         mFilename(filename),
99         mContext(C),
100         armature_importer(&unit_converter, &mesh_importer, &anim_importer, CTX_data_scene(C)),
101         mesh_importer(&unit_converter, &armature_importer, CTX_data_scene(C)),
102         anim_importer(&unit_converter, &armature_importer, CTX_data_scene(C))
103 {}
104
105 DocumentImporter::~DocumentImporter()
106 {
107         TagsMap::iterator etit;
108         etit = uid_tags_map.begin();
109         while (etit!=uid_tags_map.end()) {
110                 delete etit->second;
111                 etit++;
112         }
113 }
114
115 bool DocumentImporter::import()
116 {
117         ErrorHandler errorHandler;
118         COLLADASaxFWL::Loader loader(&errorHandler);
119         COLLADAFW::Root root(&loader, this);
120         ExtraHandler *ehandler = new ExtraHandler(this, &(this->anim_importer));
121         
122         loader.registerExtraDataCallbackHandler(ehandler);
123
124         // deselect all to select new objects
125         BKE_scene_base_deselect_all(CTX_data_scene(mContext));
126
127         if (!root.loadDocument(mFilename)) {
128                 fprintf(stderr, "COLLADAFW::Root::loadDocument() returned false on 1st pass\n");
129                 return false;
130         }
131         
132         if (errorHandler.hasError())
133                 return false;
134         
135         /** TODO set up scene graph and such here */
136         
137         mImportStage = Controller;
138         
139         COLLADASaxFWL::Loader loader2;
140         COLLADAFW::Root root2(&loader2, this);
141         
142         if (!root2.loadDocument(mFilename)) {
143                 fprintf(stderr, "COLLADAFW::Root::loadDocument() returned false on 2nd pass\n");
144                 return false;
145         }
146         
147         
148         delete ehandler;
149
150         mesh_importer.bmeshConversion();
151
152         return true;
153 }
154
155 void DocumentImporter::cancel(const COLLADAFW::String& errorMessage)
156 {
157         // TODO: if possible show error info
158         //
159         // Should we get rid of invisible Meshes that were created so far
160         // or maybe create objects at coordinate space origin?
161         //
162         // The latter sounds better.
163 }
164
165 void DocumentImporter::start() {}
166
167 void DocumentImporter::finish()
168 {
169         if (mImportStage!=General)
170                 return;
171                 
172         /** TODO Break up and put into 2-pass parsing of DAE */
173         std::vector<const COLLADAFW::VisualScene*>::iterator it;
174         for (it = vscenes.begin(); it != vscenes.end(); it++) {
175                 PointerRNA sceneptr, unit_settings;
176                 PropertyRNA *system, *scale;
177                 // TODO: create a new scene except the selected <visual_scene> - use current blender scene for it
178                 Scene *sce = CTX_data_scene(mContext);
179                 
180                 // for scene unit settings: system, scale_length
181                 RNA_id_pointer_create(&sce->id, &sceneptr);
182                 unit_settings = RNA_pointer_get(&sceneptr, "unit_settings");
183                 system = RNA_struct_find_property(&unit_settings, "system");
184                 scale = RNA_struct_find_property(&unit_settings, "scale_length");
185                 
186                 switch (unit_converter.isMetricSystem()) {
187                         case UnitConverter::Metric:
188                                 RNA_property_enum_set(&unit_settings, system, USER_UNIT_METRIC);
189                                 break;
190                         case UnitConverter::Imperial:
191                                 RNA_property_enum_set(&unit_settings, system, USER_UNIT_IMPERIAL);
192                                 break;
193                         default:
194                                 RNA_property_enum_set(&unit_settings, system, USER_UNIT_NONE);
195                                 break;
196                 }
197                 RNA_property_float_set(&unit_settings, scale, unit_converter.getLinearMeter());
198                 
199                 const COLLADAFW::NodePointerArray& roots = (*it)->getRootNodes();
200
201                 for (unsigned int i = 0; i < roots.getCount(); i++) {
202                         write_node(roots[i], NULL, sce, NULL, false);
203                 }
204         }
205         armature_importer.set_tags_map(this->uid_tags_map);
206         armature_importer.make_armatures(mContext);
207
208 #if 0
209         armature_importer.fix_animation();
210 #endif
211
212         for (std::vector<const COLLADAFW::VisualScene*>::iterator it = vscenes.begin(); it != vscenes.end(); it++) {
213                 const COLLADAFW::NodePointerArray& roots = (*it)->getRootNodes();
214
215                 for (unsigned int i = 0; i < roots.getCount(); i++)
216                         translate_anim_recursive(roots[i], NULL, NULL);
217         }
218
219         if (libnode_ob.size()) {
220                 Scene *sce = CTX_data_scene(mContext);
221
222                 fprintf(stderr, "got %d library nodes to free\n", (int)libnode_ob.size());
223                 // free all library_nodes
224                 std::vector<Object*>::iterator it;
225                 for (it = libnode_ob.begin(); it != libnode_ob.end(); it++) {
226                         Object *ob = *it;
227
228                         Base *base = BKE_scene_base_find(sce, ob);
229                         if (base) {
230                                 BLI_remlink(&sce->base, base);
231                                 BKE_libblock_free_us(&G.main->object, base->object);
232                                 if (sce->basact==base)
233                                         sce->basact= NULL;
234                                 MEM_freeN(base);
235                         }
236                 }
237                 libnode_ob.clear();
238
239                 DAG_scene_sort(CTX_data_main(mContext), sce);
240                 DAG_ids_flush_update(CTX_data_main(mContext), 0);
241         }
242 }
243
244
245 void DocumentImporter::translate_anim_recursive(COLLADAFW::Node *node, COLLADAFW::Node *par = NULL, Object *parob = NULL)
246 {
247
248         // The split in #29246, rootmap must point at actual root when
249         // calculating bones in apply_curves_as_matrix.
250         // This has to do with inverse bind poses being world space
251         // (the sources for skinned bones' restposes) and the way
252         // non-skinning nodes have their "restpose" recursively calculated.
253         // XXX TODO: design issue, how to support unrelated joints taking
254         // part in skinning.
255         if (par) { // && par->getType() == COLLADAFW::Node::JOINT) {
256                 // par is root if there's no corresp. key in root_map
257                 if (root_map.find(par->getUniqueId()) == root_map.end())
258                         root_map[node->getUniqueId()] = par;
259                 else
260                         root_map[node->getUniqueId()] = root_map[par->getUniqueId()];
261         }
262
263         /*COLLADAFW::Transformation::TransformationType types[] = {
264                 COLLADAFW::Transformation::ROTATE,
265                 COLLADAFW::Transformation::SCALE,
266                 COLLADAFW::Transformation::TRANSLATE,
267                 COLLADAFW::Transformation::MATRIX
268         };
269
270         Object *ob;*/
271         unsigned int i;
272
273         //for (i = 0; i < 4; i++)
274                 //ob = 
275         anim_importer.translate_Animations(node, root_map, object_map, FW_object_map);
276
277         COLLADAFW::NodePointerArray &children = node->getChildNodes();
278         for (i = 0; i < children.getCount(); i++) {
279                 translate_anim_recursive(children[i], node, NULL);
280         }
281 }
282
283 /** When this method is called, the writer must write the global document asset.
284         \return The writer should return true, if writing succeeded, false otherwise.*/
285 bool DocumentImporter::writeGlobalAsset ( const COLLADAFW::FileInfo* asset ) 
286 {
287         unit_converter.read_asset(asset);
288
289         return true;
290 }
291
292 /** When this method is called, the writer must write the scene.
293         \return The writer should return true, if writing succeeded, false otherwise.*/
294 bool DocumentImporter::writeScene ( const COLLADAFW::Scene* scene ) 
295 {
296         // XXX could store the scene id, but do nothing for now
297         return true;
298 }
299 Object* DocumentImporter::create_camera_object(COLLADAFW::InstanceCamera *camera, Scene *sce)
300 {
301         const COLLADAFW::UniqueId& cam_uid = camera->getInstanciatedObjectId();
302         if (uid_camera_map.find(cam_uid) == uid_camera_map.end()) {     
303                 fprintf(stderr, "Couldn't find camera by UID.\n");
304                 return NULL;
305         }
306         Object *ob = add_object(sce, OB_CAMERA);
307         Camera *cam = uid_camera_map[cam_uid];
308         Camera *old_cam = (Camera*)ob->data;
309         ob->data = cam;
310         old_cam->id.us--;
311         if (old_cam->id.us == 0)
312                 BKE_libblock_free(&G.main->camera, old_cam);
313         return ob;
314 }
315
316 Object* DocumentImporter::create_lamp_object(COLLADAFW::InstanceLight *lamp, Scene *sce)
317 {
318         const COLLADAFW::UniqueId& lamp_uid = lamp->getInstanciatedObjectId();
319         if (uid_lamp_map.find(lamp_uid) == uid_lamp_map.end()) {        
320                 fprintf(stderr, "Couldn't find lamp by UID.\n");
321                 return NULL;
322         }
323         Object *ob = add_object(sce, OB_LAMP);
324         Lamp *la = uid_lamp_map[lamp_uid];
325         Lamp *old_lamp = (Lamp*)ob->data;
326         ob->data = la;
327         old_lamp->id.us--;
328         if (old_lamp->id.us == 0)
329                 BKE_libblock_free(&G.main->lamp, old_lamp);
330         return ob;
331 }
332
333 Object* DocumentImporter::create_instance_node(Object *source_ob, COLLADAFW::Node *source_node, COLLADAFW::Node *instance_node, Scene *sce, bool is_library_node)
334 {
335         fprintf(stderr, "create <instance_node> under node id=%s from node id=%s\n", instance_node ? instance_node->getOriginalId().c_str() : NULL, source_node ? source_node->getOriginalId().c_str() : NULL);
336
337         Object *obn = BKE_object_copy(source_ob);
338         obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
339         BKE_scene_base_add(sce, obn);
340
341         if (instance_node) {
342                 anim_importer.read_node_transform(instance_node, obn);
343                 // if we also have a source_node (always ;), take its
344                 // transformation matrix and apply it to the newly instantiated
345                 // object to account for node hierarchy transforms in
346                 // .dae
347                 if (source_node) {
348                         COLLADABU::Math::Matrix4 mat4 = source_node->getTransformationMatrix();
349                         COLLADABU::Math::Matrix4 bmat4 = mat4.transpose(); // transpose to get blender row-major order
350                         float mat[4][4];
351                         for (int i = 0; i < 4; i++) {
352                                 for (int j = 0; j < 4; j++) {
353                                         mat[i][j] = bmat4[i][j];
354                                 }
355                         }
356                         // calc new matrix and apply
357                         mult_m4_m4m4(obn->obmat, obn->obmat, mat);
358                         BKE_object_apply_mat4(obn, obn->obmat, 0, 0);
359                 }
360         }
361         else {
362                 anim_importer.read_node_transform(source_node, obn);
363         }
364
365         DAG_scene_sort(CTX_data_main(mContext), sce);
366         DAG_ids_flush_update(CTX_data_main(mContext), 0);
367
368         COLLADAFW::NodePointerArray &children = source_node->getChildNodes();
369         if (children.getCount()) {
370                 for (unsigned int i = 0; i < children.getCount(); i++) {
371                         COLLADAFW::Node *child_node = children[i];
372                         const COLLADAFW::UniqueId& child_id = child_node->getUniqueId();
373                         if (object_map.find(child_id) == object_map.end())
374                                 continue;
375                         COLLADAFW::InstanceNodePointerArray &inodes = child_node->getInstanceNodes();
376                         Object *new_child = NULL;
377                         if (inodes.getCount()) { // \todo loop through instance nodes
378                                 const COLLADAFW::UniqueId& id = inodes[0]->getInstanciatedObjectId();
379                                 fprintf(stderr, "Doing %d child nodes\n", node_map.count(id));
380                                 new_child = create_instance_node(object_map.find(id)->second, node_map[id], child_node, sce, is_library_node);
381                         }
382                         else {
383                                 new_child = create_instance_node(object_map.find(child_id)->second, child_node, NULL, sce, is_library_node);
384                         }
385                         bc_set_parent(new_child, obn, mContext, true);
386
387                         if (is_library_node)
388                                 libnode_ob.push_back(new_child);
389                 }
390         }
391
392         return obn;
393 }
394
395 void DocumentImporter::write_node (COLLADAFW::Node *node, COLLADAFW::Node *parent_node, Scene *sce, Object *par, bool is_library_node)
396 {
397         Object *ob = NULL;
398         bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
399         bool read_transform = true;
400
401         std::vector<Object*> * objects_done = new std::vector<Object *>();
402
403         if (is_joint) {
404                 if ( par ) {
405                 Object * empty = par;
406                 par = add_object(sce, OB_ARMATURE);
407                 bc_set_parent(par, empty->parent, mContext);
408                 //remove empty : todo
409                 object_map.insert(std::make_pair<COLLADAFW::UniqueId, Object *>(parent_node->getUniqueId(), par));
410                 }
411                 armature_importer.add_joint(node, parent_node == NULL || parent_node->getType() != COLLADAFW::Node::JOINT, par, sce);
412         }
413         else {
414                 COLLADAFW::InstanceGeometryPointerArray &geom = node->getInstanceGeometries();
415                 COLLADAFW::InstanceCameraPointerArray &camera = node->getInstanceCameras();
416                 COLLADAFW::InstanceLightPointerArray &lamp = node->getInstanceLights();
417                 COLLADAFW::InstanceControllerPointerArray &controller = node->getInstanceControllers();
418                 COLLADAFW::InstanceNodePointerArray &inst_node = node->getInstanceNodes();
419                 size_t geom_done = 0;
420                 size_t camera_done = 0;
421                 size_t lamp_done = 0;
422                 size_t controller_done = 0;
423                 size_t inst_done = 0;
424
425                 // XXX linking object with the first <instance_geometry>, though a node may have more of them...
426                 // maybe join multiple <instance_...> meshes into 1, and link object with it? not sure...
427                 // <instance_geometry>
428                 while (geom_done < geom.getCount()) {
429                         ob = mesh_importer.create_mesh_object(node, geom[geom_done], false, uid_material_map,
430                                                                                                   material_texture_mapping_map);
431                         objects_done->push_back(ob);
432                         ++geom_done;
433                 }
434                 while (camera_done < camera.getCount()) {
435                         ob = create_camera_object(camera[camera_done], sce);
436                         objects_done->push_back(ob);
437                         ++camera_done;
438                 }
439                 while (lamp_done < lamp.getCount()) {
440                         ob = create_lamp_object(lamp[lamp_done], sce);
441                         objects_done->push_back(ob);
442                         ++lamp_done;
443                 }
444                 while (controller_done < controller.getCount()) {
445                         COLLADAFW::InstanceGeometry *geom = (COLLADAFW::InstanceGeometry*)controller[controller_done];
446                         ob = mesh_importer.create_mesh_object(node, geom, true, uid_material_map, material_texture_mapping_map);
447                         objects_done->push_back(ob);
448                         ++controller_done;
449                 }
450                 // XXX instance_node is not supported yet
451                 while (inst_done < inst_node.getCount()) {
452                         const COLLADAFW::UniqueId& node_id = inst_node[inst_done]->getInstanciatedObjectId();
453                         if (object_map.find(node_id) == object_map.end()) {
454                                 fprintf(stderr, "Cannot find object for node referenced by <instance_node name=\"%s\">.\n", inst_node[inst_done]->getName().c_str());
455                                 ob = NULL;
456                         }
457                         else {
458                                 std::pair<std::multimap<COLLADAFW::UniqueId, Object *>::iterator, std::multimap<COLLADAFW::UniqueId, Object *>::iterator> pair_iter = object_map.equal_range(node_id);
459                                 for (std::multimap<COLLADAFW::UniqueId, Object *>::iterator it2 = pair_iter.first; it2 != pair_iter.second; it2++) {
460                                         Object *source_ob = (Object *)it2->second;
461                                         COLLADAFW::Node *source_node = node_map[node_id];
462                                         ob = create_instance_node(source_ob, source_node, node, sce, is_library_node);
463                                 }
464                         }
465                         if (ob != NULL) objects_done->push_back(ob);
466                         ++inst_done;
467
468                         read_transform = false;
469                 }
470                 // if node is empty - create empty object
471                 // XXX empty node may not mean it is empty object, not sure about this
472                 if ( (geom_done + camera_done + lamp_done + controller_done + inst_done) < 1) {
473                         ob = add_object(sce, OB_EMPTY);
474                         objects_done->push_back(ob);
475                 }
476                 
477                 // XXX: if there're multiple instances, only one is stored
478
479                 if (!ob) return;
480                 for (std::vector<Object *>::iterator it = objects_done->begin(); it != objects_done->end(); ++it) {
481                         ob = *it;
482                         std::string nodename = node->getName().size() ? node->getName() : node->getOriginalId();
483                         rename_id(&ob->id, (char*)nodename.c_str());
484                         object_map.insert(std::make_pair<COLLADAFW::UniqueId, Object *>(node->getUniqueId(), ob));
485                         node_map[node->getUniqueId()] = node;
486
487                         if (is_library_node)
488                                 libnode_ob.push_back(ob);
489                 }
490
491         }
492
493         for (std::vector<Object *>::iterator it = objects_done->begin(); it != objects_done->end(); ++it) {
494                 ob =*it;
495
496                 if (read_transform)
497                         anim_importer.read_node_transform(node, ob); // overwrites location set earlier
498
499                 if (!is_joint) {
500                         // if par was given make this object child of the previous
501                         if (par && ob)
502                                 bc_set_parent(ob, par, mContext);
503                 }
504         }
505         // if node has child nodes write them
506         COLLADAFW::NodePointerArray &child_nodes = node->getChildNodes();
507         for (unsigned int i = 0; i < child_nodes.getCount(); i++) {     
508                 write_node(child_nodes[i], node, sce, ob, is_library_node);
509         }
510 }
511
512 /** When this method is called, the writer must write the entire visual scene.
513         \return The writer should return true, if writing succeeded, false otherwise.*/
514 bool DocumentImporter::writeVisualScene ( const COLLADAFW::VisualScene* visualScene ) 
515 {
516         if (mImportStage!=General)
517                 return true;
518                 
519         // this method called on post process after writeGeometry, writeMaterial, etc.
520
521         // for each <node> in <visual_scene>:
522         // create an Object
523         // if Mesh (previously created in writeGeometry) to which <node> corresponds exists, link Object with that mesh
524
525         // update: since we cannot link a Mesh with Object in
526         // writeGeometry because <geometry> does not reference <node>,
527         // we link Objects with Meshes here
528
529         vscenes.push_back(visualScene);
530         
531         return true;
532 }
533
534 /** When this method is called, the writer must handle all nodes contained in the 
535         library nodes.
536         \return The writer should return true, if writing succeeded, false otherwise.*/
537 bool DocumentImporter::writeLibraryNodes ( const COLLADAFW::LibraryNodes* libraryNodes ) 
538 {
539         if (mImportStage!=General)
540                 return true;
541                 
542         Scene *sce = CTX_data_scene(mContext);
543
544         const COLLADAFW::NodePointerArray& nodes = libraryNodes->getNodes();
545
546         for (unsigned int i = 0; i < nodes.getCount(); i++) {
547                 write_node(nodes[i], NULL, sce, NULL, true);
548         }
549
550         return true;
551 }
552
553 /** When this method is called, the writer must write the geometry.
554         \return The writer should return true, if writing succeeded, false otherwise.*/
555 bool DocumentImporter::writeGeometry ( const COLLADAFW::Geometry* geom ) 
556 {
557         if (mImportStage!=General)
558                 return true;
559                 
560         return mesh_importer.write_geometry(geom);
561 }
562
563 /** When this method is called, the writer must write the material.
564         \return The writer should return true, if writing succeeded, false otherwise.*/
565 bool DocumentImporter::writeMaterial( const COLLADAFW::Material* cmat ) 
566 {
567         if (mImportStage!=General)
568                 return true;
569                 
570         const std::string& str_mat_id = cmat->getName().size() ? cmat->getName() : cmat->getOriginalId();
571         Material *ma = BKE_material_add((char*)str_mat_id.c_str());
572         
573         this->uid_effect_map[cmat->getInstantiatedEffect()] = ma;
574         this->uid_material_map[cmat->getUniqueId()] = ma;
575         
576         return true;
577 }
578
579 // create mtex, create texture, set texture image
580 MTex* DocumentImporter::create_texture(COLLADAFW::EffectCommon *ef, COLLADAFW::Texture &ctex, Material *ma,
581                                          int i, TexIndexTextureArrayMap &texindex_texarray_map)
582 {
583         COLLADAFW::SamplerPointerArray& samp_array = ef->getSamplerPointerArray();
584         COLLADAFW::Sampler *sampler = samp_array[ctex.getSamplerId()];
585                 
586         const COLLADAFW::UniqueId& ima_uid = sampler->getSourceImage();
587         
588         if (uid_image_map.find(ima_uid) == uid_image_map.end()) {
589                 fprintf(stderr, "Couldn't find an image by UID.\n");
590                 return NULL;
591         }
592         
593         ma->mtex[i] = add_mtex();
594         ma->mtex[i]->texco = TEXCO_UV;
595         ma->mtex[i]->tex = add_texture("Texture");
596         ma->mtex[i]->tex->type = TEX_IMAGE;
597         ma->mtex[i]->tex->imaflag &= ~TEX_USEALPHA;
598         ma->mtex[i]->tex->ima = uid_image_map[ima_uid];
599         
600         texindex_texarray_map[ctex.getTextureMapId()].push_back(ma->mtex[i]);
601         
602         return ma->mtex[i];
603 }
604
605 void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Material *ma)
606 {
607         COLLADAFW::EffectCommon::ShaderType shader = ef->getShaderType();
608         
609         // blinn
610         if (shader == COLLADAFW::EffectCommon::SHADER_BLINN) {
611                 ma->spec_shader = MA_SPEC_BLINN;
612                 ma->spec = ef->getShininess().getFloatValue();
613         }
614         // phong
615         else if (shader == COLLADAFW::EffectCommon::SHADER_PHONG) {
616                 ma->spec_shader = MA_SPEC_PHONG;
617                 ma->har = ef->getShininess().getFloatValue();
618         }
619         // lambert
620         else if (shader == COLLADAFW::EffectCommon::SHADER_LAMBERT) {
621                 ma->diff_shader = MA_DIFF_LAMBERT;
622         }
623         // default - lambert
624         else {
625                 ma->diff_shader = MA_DIFF_LAMBERT;
626                 fprintf(stderr, "Current shader type is not supported, default to lambert.\n");
627         }
628         // reflectivity
629         ma->ray_mirror = ef->getReflectivity().getFloatValue();
630         // index of refraction
631         ma->ang = ef->getIndexOfRefraction().getFloatValue();
632         
633         int i = 0;
634         COLLADAFW::Color col;
635         MTex *mtex = NULL;
636         TexIndexTextureArrayMap texindex_texarray_map;
637         
638         // DIFFUSE
639         // color
640         if (ef->getDiffuse().isColor()) {
641                 col = ef->getDiffuse().getColor();
642                 ma->r = col.getRed();
643                 ma->g = col.getGreen();
644                 ma->b = col.getBlue();
645         }
646         // texture
647         else if (ef->getDiffuse().isTexture()) {
648                 COLLADAFW::Texture ctex = ef->getDiffuse().getTexture(); 
649                 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
650                 if (mtex != NULL) {
651                         mtex->mapto = MAP_COL;
652                         ma->texact = (int)i;
653                         i++;
654                 }
655         }
656         // AMBIENT
657         // color
658         if (ef->getAmbient().isColor()) {
659                 col = ef->getAmbient().getColor();
660                 ma->ambr = col.getRed();
661                 ma->ambg = col.getGreen();
662                 ma->ambb = col.getBlue();
663         }
664         // texture
665         else if (ef->getAmbient().isTexture()) {
666                 COLLADAFW::Texture ctex = ef->getAmbient().getTexture(); 
667                 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
668                 if (mtex != NULL) {
669                         mtex->mapto = MAP_AMB; 
670                         i++;
671                 }
672         }
673         // SPECULAR
674         // color
675         if (ef->getSpecular().isColor()) {
676                 col = ef->getSpecular().getColor();
677                 ma->specr = col.getRed();
678                 ma->specg = col.getGreen();
679                 ma->specb = col.getBlue();
680         }
681         // texture
682         else if (ef->getSpecular().isTexture()) {
683                 COLLADAFW::Texture ctex = ef->getSpecular().getTexture(); 
684                 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
685                 if (mtex != NULL) {
686                         mtex->mapto = MAP_SPEC; 
687                         i++;
688                 }
689         }
690         // REFLECTIVE
691         // color
692         if (ef->getReflective().isColor()) {
693                 col = ef->getReflective().getColor();
694                 ma->mirr = col.getRed();
695                 ma->mirg = col.getGreen();
696                 ma->mirb = col.getBlue();
697         }
698         // texture
699         else if (ef->getReflective().isTexture()) {
700                 COLLADAFW::Texture ctex = ef->getReflective().getTexture(); 
701                 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
702                 if (mtex != NULL) {
703                         mtex->mapto = MAP_REF; 
704                         i++;
705                 }
706         }
707         // EMISSION
708         // color
709         if (ef->getEmission().isColor()) {
710                 // XXX there is no emission color in blender
711                 // but I am not sure
712         }
713         // texture
714         else if (ef->getEmission().isTexture()) {
715                 COLLADAFW::Texture ctex = ef->getEmission().getTexture(); 
716                 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
717                 if (mtex != NULL) {
718                         mtex->mapto = MAP_EMIT; 
719                         i++;
720                 }
721         }
722         
723         if (ef->getOpacity().isTexture()) {
724                 COLLADAFW::Texture ctex = ef->getOpacity().getTexture();
725                 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
726                 if (mtex != NULL) {
727                         mtex->mapto = MAP_ALPHA;
728                         mtex->tex->imaflag |= TEX_USEALPHA;
729                         i++;
730                         ma->spectra = ma->alpha = 0;
731                         ma->mode |= MA_ZTRANSP|MA_TRANSP;
732                 }
733         }
734         // TRANSPARENT
735         // color
736 //      if (ef->getOpacity().isColor()) {
737 //                      // XXX don't know what to do here
738 //              }
739 //              // texture
740 //              else if (ef->getOpacity().isTexture()) {
741 //                      ctex = ef->getOpacity().getTexture();
742 //                      if (mtex != NULL) mtex->mapto &= MAP_ALPHA;
743 //                      else {
744 //                              mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
745 //                              if (mtex != NULL) mtex->mapto = MAP_ALPHA;
746 //                      }
747 //              }
748         material_texture_mapping_map[ma] = texindex_texarray_map;
749 }
750
751 /** When this method is called, the writer must write the effect.
752         \return The writer should return true, if writing succeeded, false otherwise.*/
753
754 bool DocumentImporter::writeEffect( const COLLADAFW::Effect* effect ) 
755 {
756         if (mImportStage!=General)
757                 return true;
758         
759         const COLLADAFW::UniqueId& uid = effect->getUniqueId();
760         
761         if (uid_effect_map.find(uid) == uid_effect_map.end()) {
762                 fprintf(stderr, "Couldn't find a material by UID.\n");
763                 return true;
764         }
765         
766         Material *ma = uid_effect_map[uid];
767         std::map<COLLADAFW::UniqueId, Material*>::iterator  iter;
768         for (iter = uid_material_map.begin(); iter != uid_material_map.end() ; iter++ ) {
769                 if ( iter->second == ma ) {
770                         this->FW_object_map[iter->first] = effect;
771                         break;
772                 }
773         }
774         COLLADAFW::CommonEffectPointerArray common_efs = effect->getCommonEffects();
775         if (common_efs.getCount() < 1) {
776                 fprintf(stderr, "Couldn't find <profile_COMMON>.\n");
777                 return true;
778         }
779         // XXX TODO: Take all <profile_common>s
780         // Currently only first <profile_common> is supported
781         COLLADAFW::EffectCommon *ef = common_efs[0];
782         write_profile_COMMON(ef, ma);
783         this->FW_object_map[effect->getUniqueId()] = effect;
784                 
785         return true;
786 }
787
788
789 /** When this method is called, the writer must write the camera.
790         \return The writer should return true, if writing succeeded, false otherwise.*/
791 bool DocumentImporter::writeCamera( const COLLADAFW::Camera* camera ) 
792 {
793         if (mImportStage!=General)
794                 return true;
795                 
796         Camera *cam = NULL;
797         std::string cam_id, cam_name;
798         
799         cam_id = camera->getOriginalId();
800         cam_name = camera->getName();
801         if (cam_name.size()) cam = (Camera *)BKE_camera_add((char*)cam_name.c_str());
802         else cam = (Camera *)BKE_camera_add((char*)cam_id.c_str());
803         
804         if (!cam) {
805                 fprintf(stderr, "Cannot create camera.\n");
806                 return true;
807         }
808         cam->clipsta = camera->getNearClippingPlane().getValue();
809         cam->clipend = camera->getFarClippingPlane().getValue();
810         
811         COLLADAFW::Camera::CameraType type = camera->getCameraType();
812         switch (type) {
813         case COLLADAFW::Camera::ORTHOGRAPHIC:
814                 {
815                         cam->type = CAM_ORTHO;
816                 }
817                 break;
818         case COLLADAFW::Camera::PERSPECTIVE:
819                 {
820                         cam->type = CAM_PERSP;
821                 }
822                 break;
823         case COLLADAFW::Camera::UNDEFINED_CAMERATYPE:
824                 {
825                         fprintf(stderr, "Current camera type is not supported.\n");
826                         cam->type = CAM_PERSP;
827                 }
828                 break;
829         }
830         
831         switch (camera->getDescriptionType()) {
832         case COLLADAFW::Camera::ASPECTRATIO_AND_Y:
833                 {
834                         switch (cam->type) {
835                                 case CAM_ORTHO:
836                                         {
837                                                 double ymag = camera->getYMag().getValue();
838                                                 double aspect = camera->getAspectRatio().getValue();
839                                                 double xmag = aspect*ymag;
840                                                 cam->ortho_scale = (float)xmag;
841                                         }
842                                         break;
843                                 case CAM_PERSP:
844                                 default:
845                                         {
846                                                 double yfov = camera->getYFov().getValue();
847                                                 double aspect = camera->getAspectRatio().getValue();
848                                                 double xfov = aspect*yfov;
849                                                 // xfov is in degrees, cam->lens is in millimiters
850                                                 cam->lens = fov_to_focallength(DEG2RADF(xfov), cam->sensor_x);
851                                         }
852                                         break;
853                         }
854                 }
855                 break;
856         /* XXX correct way to do following four is probably to get also render
857            size and determine proper settings from that somehow */
858         case COLLADAFW::Camera::ASPECTRATIO_AND_X:
859         case COLLADAFW::Camera::SINGLE_X:
860         case COLLADAFW::Camera::X_AND_Y:
861                 {
862                         switch (cam->type) {
863                                 case CAM_ORTHO:
864                                         cam->ortho_scale = (float)camera->getXMag().getValue();
865                                         break;
866                                 case CAM_PERSP:
867                                 default:
868                                         {
869                                                 double x = camera->getXFov().getValue();
870                                                 // x is in degrees, cam->lens is in millimiters
871                                                 cam->lens = fov_to_focallength(DEG2RADF(x), cam->sensor_x);
872                                         }
873                                         break;
874                         }
875                 }
876                 break;
877         case COLLADAFW::Camera::SINGLE_Y:
878                 {
879                         switch (cam->type) {
880                                 case CAM_ORTHO:
881                                         cam->ortho_scale = (float)camera->getYMag().getValue();
882                                         break;
883                                 case CAM_PERSP:
884                                 default:
885                                         {
886                                         double yfov = camera->getYFov().getValue();
887                                         // yfov is in degrees, cam->lens is in millimiters
888                                         cam->lens = fov_to_focallength(DEG2RADF(yfov), cam->sensor_x);
889                                         }
890                                         break;
891                         }
892                 }
893                 break;
894         case COLLADAFW::Camera::UNDEFINED:
895                 // read nothing, use blender defaults.
896                 break;
897         }
898         
899         this->uid_camera_map[camera->getUniqueId()] = cam;
900         this->FW_object_map[camera->getUniqueId()] = camera;
901         // XXX import camera options
902         return true;
903 }
904
905 /** When this method is called, the writer must write the image.
906         \return The writer should return true, if writing succeeded, false otherwise.*/
907 bool DocumentImporter::writeImage( const COLLADAFW::Image* image ) 
908 {
909         if (mImportStage!=General)
910                 return true;
911                 
912         // XXX maybe it is necessary to check if the path is absolute or relative
913         const std::string& filepath = image->getImageURI().toNativePath();
914         const char *filename = (const char*)mFilename.c_str();
915         char dir[FILE_MAX];
916         char full_path[FILE_MAX];
917         
918         BLI_split_dir_part(filename, dir, sizeof(dir));
919         BLI_join_dirfile(full_path, sizeof(full_path), dir, filepath.c_str());
920         Image *ima = BKE_image_load_exists(full_path);
921         if (!ima) {
922                 fprintf(stderr, "Cannot create image.\n");
923                 return true;
924         }
925         this->uid_image_map[image->getUniqueId()] = ima;
926         
927         return true;
928 }
929
930 /** When this method is called, the writer must write the light.
931         \return The writer should return true, if writing succeeded, false otherwise.*/
932 bool DocumentImporter::writeLight( const COLLADAFW::Light* light ) 
933 {
934         if (mImportStage!=General)
935                 return true;
936
937         Lamp *lamp = NULL;
938         std::string la_id, la_name;
939
940         TagsMap::iterator etit;
941         ExtraTags *et = 0;
942         etit = uid_tags_map.find(light->getUniqueId().toAscii());
943         if (etit != uid_tags_map.end())
944                 et = etit->second;
945
946         la_id = light->getOriginalId();
947         la_name = light->getName();
948         if (la_name.size()) lamp = (Lamp*)BKE_lamp_add((char*)la_name.c_str());
949         else lamp = (Lamp*)BKE_lamp_add((char*)la_id.c_str());
950
951         if (!lamp) {
952                 fprintf(stderr, "Cannot create lamp.\n");
953                 return true;
954         }
955
956         // if we find an ExtraTags for this, use that instead.
957         if (et && et->isProfile("blender")) {
958                 et->setData("type", &(lamp->type));
959                 et->setData("flag", &(lamp->flag));
960                 et->setData("mode", &(lamp->mode));
961                 et->setData("gamma", &(lamp->k));
962                 et->setData("red", &(lamp->r));
963                 et->setData("green", &(lamp->g));
964                 et->setData("blue", &(lamp->b));
965                 et->setData("shadow_r", &(lamp->shdwr));
966                 et->setData("shadow_g", &(lamp->shdwg));
967                 et->setData("shadow_b", &(lamp->shdwb));
968                 et->setData("energy", &(lamp->energy));
969                 et->setData("dist", &(lamp->dist));
970                 et->setData("spotsize", &(lamp->spotsize));
971                 et->setData("spotblend", &(lamp->spotblend));
972                 et->setData("halo_intensity", &(lamp->haint));
973                 et->setData("att1", &(lamp->att1));
974                 et->setData("att2", &(lamp->att2));
975                 et->setData("falloff_type", &(lamp->falloff_type));
976                 et->setData("clipsta", &(lamp->clipsta));
977                 et->setData("clipend", &(lamp->clipend));
978                 et->setData("shadspotsize", &(lamp->shadspotsize));
979                 et->setData("bias", &(lamp->bias));
980                 et->setData("soft", &(lamp->soft));
981                 et->setData("compressthresh", &(lamp->compressthresh));
982                 et->setData("bufsize", &(lamp->bufsize));
983                 et->setData("samp", &(lamp->samp));
984                 et->setData("buffers", &(lamp->buffers));
985                 et->setData("filtertype", &(lamp->filtertype));
986                 et->setData("bufflag", &(lamp->bufflag));
987                 et->setData("buftype", &(lamp->buftype));
988                 et->setData("ray_samp", &(lamp->ray_samp));
989                 et->setData("ray_sampy", &(lamp->ray_sampy));
990                 et->setData("ray_sampz", &(lamp->ray_sampz));
991                 et->setData("ray_samp_type", &(lamp->ray_samp_type));
992                 et->setData("area_shape", &(lamp->area_shape));
993                 et->setData("area_size", &(lamp->area_size));
994                 et->setData("area_sizey", &(lamp->area_sizey));
995                 et->setData("area_sizez", &(lamp->area_sizez));
996                 et->setData("adapt_thresh", &(lamp->adapt_thresh));
997                 et->setData("ray_samp_method", &(lamp->ray_samp_method));
998                 et->setData("shadhalostep", &(lamp->shadhalostep));
999                 et->setData("sun_effect_type", &(lamp->shadhalostep));
1000                 et->setData("skyblendtype", &(lamp->skyblendtype));
1001                 et->setData("horizon_brightness", &(lamp->horizon_brightness));
1002                 et->setData("spread", &(lamp->spread));
1003                 et->setData("sun_brightness", &(lamp->sun_brightness));
1004                 et->setData("sun_size", &(lamp->sun_size));
1005                 et->setData("backscattered_light", &(lamp->backscattered_light));
1006                 et->setData("sun_intensity", &(lamp->sun_intensity));
1007                 et->setData("atm_turbidity", &(lamp->atm_turbidity));
1008                 et->setData("atm_extinction_factor", &(lamp->atm_extinction_factor));
1009                 et->setData("atm_distance_factor", &(lamp->atm_distance_factor));
1010                 et->setData("skyblendfac", &(lamp->skyblendfac));
1011                 et->setData("sky_exposure", &(lamp->sky_exposure));
1012                 et->setData("sky_colorspace", &(lamp->sky_colorspace));
1013         }
1014         else {
1015                 float constatt = light->getConstantAttenuation().getValue();
1016                 float linatt = light->getLinearAttenuation().getValue();
1017                 float quadatt = light->getQuadraticAttenuation().getValue();
1018                 float d = 25.0f;
1019                 float att1 = 0.0f;
1020                 float att2 = 0.0f;
1021                 float e = 1.0f;
1022
1023                 if (light->getColor().isValid()) {
1024                         COLLADAFW::Color col = light->getColor();
1025                         lamp->r = col.getRed();
1026                         lamp->g = col.getGreen();
1027                         lamp->b = col.getBlue();
1028                 }
1029
1030                 if (IS_EQ(linatt, 0.0f) && quadatt > 0.0f) {
1031                         att2 = quadatt;
1032                         d = sqrt(1.0f/quadatt);
1033                 }
1034                 // linear light
1035                 else if (IS_EQ(quadatt, 0.0f) && linatt > 0.0f) {
1036                         att1 = linatt;
1037                         d = (1.0f/linatt);
1038                 }
1039                 else if (IS_EQ(constatt, 1.0f)) {
1040                         att1 = 1.0f;
1041                 }
1042                 else {
1043                         // assuming point light (const att = 1.0);
1044                         att1 = 1.0f;
1045                 }
1046                 
1047                 d *= ( 1.0f / unit_converter.getLinearMeter());
1048
1049                 lamp->energy = e;
1050                 lamp->dist = d;
1051
1052                 COLLADAFW::Light::LightType type = light->getLightType();
1053                 switch (type) {
1054                         case COLLADAFW::Light::AMBIENT_LIGHT:
1055                                 {
1056                                         lamp->type = LA_HEMI;
1057                                 }
1058                                 break;
1059                         case COLLADAFW::Light::SPOT_LIGHT:
1060                                 {
1061                                         lamp->type = LA_SPOT;
1062                                         lamp->att1 = att1;
1063                                         lamp->att2 = att2;
1064                                         if (IS_EQ(att1, 0.0f) && att2 > 0)
1065                                                 lamp->falloff_type = LA_FALLOFF_INVSQUARE;
1066                                         if (IS_EQ(att2, 0.0f) && att1 > 0)
1067                                                 lamp->falloff_type = LA_FALLOFF_INVLINEAR;
1068                                         lamp->spotsize = light->getFallOffAngle().getValue();
1069                                         lamp->spotblend = light->getFallOffExponent().getValue();
1070                                 }
1071                                 break;
1072                         case COLLADAFW::Light::DIRECTIONAL_LIGHT:
1073                                 {
1074                                         /* our sun is very strong, so pick a smaller energy level */
1075                                         lamp->type = LA_SUN;
1076                                         lamp->mode |= LA_NO_SPEC;
1077                                 }
1078                                 break;
1079                         case COLLADAFW::Light::POINT_LIGHT:
1080                                 {
1081                                         lamp->type = LA_LOCAL;
1082                                         lamp->att1 = att1;
1083                                         lamp->att2 = att2;
1084                                         if (IS_EQ(att1, 0.0f) && att2 > 0)
1085                                                 lamp->falloff_type = LA_FALLOFF_INVSQUARE;
1086                                         if (IS_EQ(att2, 0.0f) && att1 > 0)
1087                                                 lamp->falloff_type = LA_FALLOFF_INVLINEAR;
1088                                 }
1089                                 break;
1090                         case COLLADAFW::Light::UNDEFINED:
1091                                 {
1092                                         fprintf(stderr, "Current lamp type is not supported.\n");
1093                                         lamp->type = LA_LOCAL;
1094                                 }
1095                                 break;
1096                 }
1097         }
1098
1099         this->uid_lamp_map[light->getUniqueId()] = lamp;
1100         this->FW_object_map[light->getUniqueId()] = light;
1101         return true;
1102 }
1103
1104 // this function is called only for animations that pass COLLADAFW::validate
1105 bool DocumentImporter::writeAnimation( const COLLADAFW::Animation* anim ) 
1106 {
1107         if (mImportStage!=General)
1108                 return true;
1109                 
1110         // return true;
1111         return anim_importer.write_animation(anim);
1112 }
1113
1114 // called on post-process stage after writeVisualScenes
1115 bool DocumentImporter::writeAnimationList( const COLLADAFW::AnimationList* animationList ) 
1116 {
1117         if (mImportStage!=General)
1118                 return true;
1119                 
1120         // return true;
1121         return anim_importer.write_animation_list(animationList);
1122 }
1123
1124 /** When this method is called, the writer must write the skin controller data.
1125         \return The writer should return true, if writing succeeded, false otherwise.*/
1126 bool DocumentImporter::writeSkinControllerData( const COLLADAFW::SkinControllerData* skin ) 
1127 {
1128         return armature_importer.write_skin_controller_data(skin);
1129 }
1130
1131 // this is called on postprocess, before writeVisualScenes
1132 bool DocumentImporter::writeController( const COLLADAFW::Controller* controller ) 
1133 {
1134         if (mImportStage!=General)
1135                 return true;
1136                 
1137         return armature_importer.write_controller(controller);
1138 }
1139
1140 bool DocumentImporter::writeFormulas( const COLLADAFW::Formulas* formulas )
1141 {
1142         return true;
1143 }
1144
1145 bool DocumentImporter::writeKinematicsScene( const COLLADAFW::KinematicsScene* kinematicsScene )
1146 {
1147         return true;
1148 }
1149
1150 ExtraTags* DocumentImporter::getExtraTags(const COLLADAFW::UniqueId &uid)
1151 {
1152         if (uid_tags_map.find(uid.toAscii())==uid_tags_map.end()) {
1153                 return NULL;
1154         }
1155         return uid_tags_map[uid.toAscii()];
1156 }
1157
1158 bool DocumentImporter::addExtraTags( const COLLADAFW::UniqueId &uid, ExtraTags *extra_tags)
1159 {
1160         uid_tags_map[uid.toAscii()] = extra_tags;
1161         return true;
1162 }
1163