4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Nathan Letwory.
22 * ***** END GPL LICENSE BLOCK *****
25 /** \file blender/collada/DocumentImporter.cpp
30 // * name imported objects
31 // * import object rotation as euler
35 #include <algorithm> // sort()
37 #include "COLLADAFWRoot.h"
38 #include "COLLADAFWStableHeaders.h"
39 #include "COLLADAFWColorOrTexture.h"
40 #include "COLLADAFWIndexList.h"
41 #include "COLLADAFWMeshPrimitiveWithFaceVertexCount.h"
42 #include "COLLADAFWPolygons.h"
43 #include "COLLADAFWSampler.h"
44 #include "COLLADAFWTypes.h"
45 #include "COLLADAFWVisualScene.h"
46 #include "COLLADAFWArrayPrimitiveType.h"
47 #include "COLLADAFWLibraryNodes.h"
48 #include "COLLADAFWCamera.h"
49 #include "COLLADAFWLight.h"
51 #include "COLLADASaxFWLLoader.h"
52 #include "COLLADASaxFWLIExtraDataCallbackHandler.h"
54 #include "BLI_listbase.h"
56 #include "BLI_string.h"
57 #include "BLI_utildefines.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"
71 #include "DNA_camera_types.h"
72 #include "DNA_lamp_types.h"
74 #include "RNA_access.h"
76 #include "MEM_guardedalloc.h"
78 #include "ExtraHandler.h"
79 #include "DocumentImporter.h"
80 #include "TransformReader.h"
82 #include "collada_internal.h"
83 #include "collada_utils.h"
87 COLLADA Importer limitations:
88 - no multiple scene import, all objects are added to active scene
91 // #define COLLADA_DEBUG
92 // creates empties for each imported bone on layer 2, for debugging
93 // #define ARMATURE_TEST
95 DocumentImporter::DocumentImporter(bContext *C, const char *filename) :
96 mImportStage(General),
99 armature_importer(&unit_converter, &mesh_importer, &anim_importer, CTX_data_scene(C)),
100 mesh_importer(&unit_converter, &armature_importer, CTX_data_scene(C)),
101 anim_importer(&unit_converter, &armature_importer, CTX_data_scene(C))
104 DocumentImporter::~DocumentImporter() {}
106 bool DocumentImporter::import()
108 /** TODO Add error handler (implement COLLADASaxFWL::IErrorHandler */
109 COLLADASaxFWL::Loader loader;
110 COLLADAFW::Root root(&loader, this);
111 ExtraHandler *ehandler = new ExtraHandler(this);
113 loader.registerExtraDataCallbackHandler(ehandler);
116 if (!root.loadDocument(mFilename))
119 /** TODO set up scene graph and such here */
121 mImportStage = Controller;
123 COLLADASaxFWL::Loader loader2;
124 COLLADAFW::Root root2(&loader2, this);
126 if (!root2.loadDocument(mFilename))
135 void DocumentImporter::cancel(const COLLADAFW::String& errorMessage)
137 // TODO: if possible show error info
139 // Should we get rid of invisible Meshes that were created so far
140 // or maybe create objects at coordinate space origin?
142 // The latter sounds better.
145 void DocumentImporter::start(){}
147 void DocumentImporter::finish()
149 if(mImportStage!=General)
152 /** TODO Break up and put into 2-pass parsing of DAE */
153 std::vector<const COLLADAFW::VisualScene*>::iterator it;
154 for (it = vscenes.begin(); it != vscenes.end(); it++) {
155 PointerRNA sceneptr, unit_settings;
156 PropertyRNA *system, *scale;
157 // TODO: create a new scene except the selected <visual_scene> - use current blender scene for it
158 Scene *sce = CTX_data_scene(mContext);
160 // for scene unit settings: system, scale_length
161 RNA_id_pointer_create(&sce->id, &sceneptr);
162 unit_settings = RNA_pointer_get(&sceneptr, "unit_settings");
163 system = RNA_struct_find_property(&unit_settings, "system");
164 scale = RNA_struct_find_property(&unit_settings, "scale_length");
166 switch(unit_converter.isMetricSystem()) {
167 case UnitConverter::Metric:
168 RNA_property_enum_set(&unit_settings, system, USER_UNIT_METRIC);
170 case UnitConverter::Imperial:
171 RNA_property_enum_set(&unit_settings, system, USER_UNIT_IMPERIAL);
174 RNA_property_enum_set(&unit_settings, system, USER_UNIT_NONE);
177 RNA_property_float_set(&unit_settings, scale, unit_converter.getLinearMeter());
179 const COLLADAFW::NodePointerArray& roots = (*it)->getRootNodes();
181 for (unsigned int i = 0; i < roots.getCount(); i++) {
182 write_node(roots[i], NULL, sce, NULL, false);
186 armature_importer.make_armatures(mContext);
189 armature_importer.fix_animation();
192 for (std::vector<const COLLADAFW::VisualScene*>::iterator it = vscenes.begin(); it != vscenes.end(); it++) {
193 const COLLADAFW::NodePointerArray& roots = (*it)->getRootNodes();
195 for (unsigned int i = 0; i < roots.getCount(); i++)
196 translate_anim_recursive(roots[i],NULL,NULL);
199 if (libnode_ob.size()) {
200 Scene *sce = CTX_data_scene(mContext);
202 fprintf(stderr, "got %d library nodes to free\n", (int)libnode_ob.size());
203 // free all library_nodes
204 std::vector<Object*>::iterator it;
205 for (it = libnode_ob.begin(); it != libnode_ob.end(); it++) {
208 Base *base = object_in_scene(ob, sce);
210 BLI_remlink(&sce->base, base);
211 free_libblock_us(&G.main->object, base->object);
212 if (sce->basact==base)
219 DAG_scene_sort(CTX_data_main(mContext), sce);
220 DAG_ids_flush_update(CTX_data_main(mContext), 0);
225 void DocumentImporter::translate_anim_recursive(COLLADAFW::Node *node, COLLADAFW::Node *par = NULL, Object *parob = NULL)
227 if (par && par->getType() == COLLADAFW::Node::JOINT) {
228 // par is root if there's no corresp. key in root_map
229 if (root_map.find(par->getUniqueId()) == root_map.end())
230 root_map[node->getUniqueId()] = par;
232 root_map[node->getUniqueId()] = root_map[par->getUniqueId()];
235 COLLADAFW::Transformation::TransformationType types[] = {
236 COLLADAFW::Transformation::ROTATE,
237 COLLADAFW::Transformation::SCALE,
238 COLLADAFW::Transformation::TRANSLATE,
239 COLLADAFW::Transformation::MATRIX
245 for (i = 0; i < 4; i++)
246 ob = anim_importer.translate_animation(node, object_map, root_map, types[i]);
248 COLLADAFW::NodePointerArray &children = node->getChildNodes();
249 for (i = 0; i < children.getCount(); i++) {
250 translate_anim_recursive(children[i], node, ob);
254 /** When this method is called, the writer must write the global document asset.
255 @return The writer should return true, if writing succeeded, false otherwise.*/
256 bool DocumentImporter::writeGlobalAsset ( const COLLADAFW::FileInfo* asset )
258 unit_converter.read_asset(asset);
263 /** When this method is called, the writer must write the scene.
264 @return The writer should return true, if writing succeeded, false otherwise.*/
265 bool DocumentImporter::writeScene ( const COLLADAFW::Scene* scene )
267 // XXX could store the scene id, but do nothing for now
270 Object* DocumentImporter::create_camera_object(COLLADAFW::InstanceCamera *camera, Scene *sce)
272 const COLLADAFW::UniqueId& cam_uid = camera->getInstanciatedObjectId();
273 if (uid_camera_map.find(cam_uid) == uid_camera_map.end()) {
274 fprintf(stderr, "Couldn't find camera by UID.\n");
277 Object *ob = add_object(sce, OB_CAMERA);
278 Camera *cam = uid_camera_map[cam_uid];
279 Camera *old_cam = (Camera*)ob->data;
282 if (old_cam->id.us == 0)
283 free_libblock(&G.main->camera, old_cam);
287 Object* DocumentImporter::create_lamp_object(COLLADAFW::InstanceLight *lamp, Scene *sce)
289 const COLLADAFW::UniqueId& lamp_uid = lamp->getInstanciatedObjectId();
290 if (uid_lamp_map.find(lamp_uid) == uid_lamp_map.end()) {
291 fprintf(stderr, "Couldn't find lamp by UID. \n");
294 Object *ob = add_object(sce, OB_LAMP);
295 Lamp *la = uid_lamp_map[lamp_uid];
296 Lamp *old_lamp = (Lamp*)ob->data;
299 if (old_lamp->id.us == 0)
300 free_libblock(&G.main->lamp, old_lamp);
304 Object* DocumentImporter::create_instance_node(Object *source_ob, COLLADAFW::Node *source_node, COLLADAFW::Node *instance_node, Scene *sce, bool is_library_node)
306 Object *obn = copy_object(source_ob);
307 obn->recalc |= OB_RECALC_OB|OB_RECALC_DATA|OB_RECALC_TIME;
308 scene_add_base(sce, obn);
311 anim_importer.read_node_transform(instance_node, obn);
312 // if we also have a source_node (always ;), take its
313 // transformation matrix and apply it to the newly instantiated
314 // object to account for node hierarchy transforms in
317 COLLADABU::Math::Matrix4 mat4 = source_node->getTransformationMatrix();
318 COLLADABU::Math::Matrix4 bmat4 = mat4.transpose(); // transpose to get blender row-major order
320 for (int i = 0; i < 4; i++) {
321 for (int j = 0; j < 4; j++) {
322 mat[i][j] = bmat4[i][j];
325 // calc new matrix and apply
326 mul_m4_m4m4(obn->obmat, mat, obn->obmat);
327 object_apply_mat4(obn, obn->obmat, 0, 0);
331 anim_importer.read_node_transform(source_node, obn);
334 DAG_scene_sort(CTX_data_main(mContext), sce);
335 DAG_ids_flush_update(CTX_data_main(mContext), 0);
337 COLLADAFW::NodePointerArray &children = source_node->getChildNodes();
338 if (children.getCount()) {
339 for (unsigned int i = 0; i < children.getCount(); i++) {
340 COLLADAFW::Node *child_node = children[i];
341 const COLLADAFW::UniqueId& child_id = child_node->getUniqueId();
342 if (object_map.find(child_id) == object_map.end())
344 COLLADAFW::InstanceNodePointerArray &inodes = child_node->getInstanceNodes();
345 Object *new_child = NULL;
346 if (inodes.getCount()) { // \todo loop through instance nodes
347 const COLLADAFW::UniqueId& id = inodes[0]->getInstanciatedObjectId();
348 new_child = create_instance_node(object_map[id], node_map[id], child_node, sce, is_library_node);
351 new_child = create_instance_node(object_map[child_id], child_node, NULL, sce, is_library_node);
353 bc_set_parent(new_child, obn, mContext, true);
356 libnode_ob.push_back(new_child);
360 // when we have an instance_node, don't return the object, because otherwise
361 // its correct location gets overwritten in write_node(). Fixes bug #26012.
362 if(instance_node) return NULL;
366 void DocumentImporter::write_node (COLLADAFW::Node *node, COLLADAFW::Node *parent_node, Scene *sce, Object *par, bool is_library_node)
369 bool is_joint = node->getType() == COLLADAFW::Node::JOINT;
372 armature_importer.add_joint(node, parent_node == NULL || parent_node->getType() != COLLADAFW::Node::JOINT, par);
375 COLLADAFW::InstanceGeometryPointerArray &geom = node->getInstanceGeometries();
376 COLLADAFW::InstanceCameraPointerArray &camera = node->getInstanceCameras();
377 COLLADAFW::InstanceLightPointerArray &lamp = node->getInstanceLights();
378 COLLADAFW::InstanceControllerPointerArray &controller = node->getInstanceControllers();
379 COLLADAFW::InstanceNodePointerArray &inst_node = node->getInstanceNodes();
383 int controller_done = 0;
386 // XXX linking object with the first <instance_geometry>, though a node may have more of them...
387 // maybe join multiple <instance_...> meshes into 1, and link object with it? not sure...
388 // <instance_geometry>
389 while (geom_done < geom.getCount()) {
390 ob = mesh_importer.create_mesh_object(node, geom[geom_done], false, uid_material_map,
391 material_texture_mapping_map);
394 while (camera_done < camera.getCount()) {
395 ob = create_camera_object(camera[camera_done], sce);
398 while (lamp_done < lamp.getCount()) {
399 ob = create_lamp_object(lamp[lamp_done], sce);
402 while (controller_done < controller.getCount()) {
403 COLLADAFW::InstanceGeometry *geom = (COLLADAFW::InstanceGeometry*)controller[controller_done];
404 ob = mesh_importer.create_mesh_object(node, geom, true, uid_material_map, material_texture_mapping_map);
407 // XXX instance_node is not supported yet
408 while (inst_done < inst_node.getCount()) {
409 const COLLADAFW::UniqueId& node_id = inst_node[inst_done]->getInstanciatedObjectId();
410 if (object_map.find(node_id) == object_map.end()) {
411 fprintf(stderr, "Cannot find node to instanciate.\n");
415 Object *source_ob = object_map[node_id];
416 COLLADAFW::Node *source_node = node_map[node_id];
418 ob = create_instance_node(source_ob, source_node, node, sce, is_library_node);
422 // if node is empty - create empty object
423 // XXX empty node may not mean it is empty object, not sure about this
424 if ( (geom_done + camera_done + lamp_done + controller_done + inst_done) < 1) {
425 ob = add_object(sce, OB_EMPTY);
428 // check if object is not NULL
431 rename_id(&ob->id, (char*)node->getOriginalId().c_str());
433 object_map[node->getUniqueId()] = ob;
434 node_map[node->getUniqueId()] = node;
437 libnode_ob.push_back(ob);
440 anim_importer.read_node_transform(node, ob); // overwrites location set earlier
443 // if par was given make this object child of the previous
445 bc_set_parent(ob, par, mContext);
448 // if node has child nodes write them
449 COLLADAFW::NodePointerArray &child_nodes = node->getChildNodes();
450 for (unsigned int i = 0; i < child_nodes.getCount(); i++) {
451 write_node(child_nodes[i], node, sce, ob, is_library_node);
455 /** When this method is called, the writer must write the entire visual scene.
456 @return The writer should return true, if writing succeeded, false otherwise.*/
457 bool DocumentImporter::writeVisualScene ( const COLLADAFW::VisualScene* visualScene )
459 if(mImportStage!=General)
462 // this method called on post process after writeGeometry, writeMaterial, etc.
464 // for each <node> in <visual_scene>:
466 // if Mesh (previously created in writeGeometry) to which <node> corresponds exists, link Object with that mesh
468 // update: since we cannot link a Mesh with Object in
469 // writeGeometry because <geometry> does not reference <node>,
470 // we link Objects with Meshes here
472 vscenes.push_back(visualScene);
477 /** When this method is called, the writer must handle all nodes contained in the
479 @return The writer should return true, if writing succeeded, false otherwise.*/
480 bool DocumentImporter::writeLibraryNodes ( const COLLADAFW::LibraryNodes* libraryNodes )
482 if(mImportStage!=General)
485 Scene *sce = CTX_data_scene(mContext);
487 const COLLADAFW::NodePointerArray& nodes = libraryNodes->getNodes();
489 for (unsigned int i = 0; i < nodes.getCount(); i++) {
490 write_node(nodes[i], NULL, sce, NULL, true);
496 /** When this method is called, the writer must write the geometry.
497 @return The writer should return true, if writing succeeded, false otherwise.*/
498 bool DocumentImporter::writeGeometry ( const COLLADAFW::Geometry* geom )
500 if(mImportStage!=General)
503 return mesh_importer.write_geometry(geom);
506 /** When this method is called, the writer must write the material.
507 @return The writer should return true, if writing succeeded, false otherwise.*/
508 bool DocumentImporter::writeMaterial( const COLLADAFW::Material* cmat )
510 if(mImportStage!=General)
513 const std::string& str_mat_id = cmat->getOriginalId();
514 Material *ma = add_material((char*)str_mat_id.c_str());
516 this->uid_effect_map[cmat->getInstantiatedEffect()] = ma;
517 this->uid_material_map[cmat->getUniqueId()] = ma;
522 // create mtex, create texture, set texture image
523 MTex* DocumentImporter::create_texture(COLLADAFW::EffectCommon *ef, COLLADAFW::Texture &ctex, Material *ma,
524 int i, TexIndexTextureArrayMap &texindex_texarray_map)
526 COLLADAFW::SamplerPointerArray& samp_array = ef->getSamplerPointerArray();
527 COLLADAFW::Sampler *sampler = samp_array[ctex.getSamplerId()];
529 const COLLADAFW::UniqueId& ima_uid = sampler->getSourceImage();
531 if (uid_image_map.find(ima_uid) == uid_image_map.end()) {
532 fprintf(stderr, "Couldn't find an image by UID.\n");
536 ma->mtex[i] = add_mtex();
537 ma->mtex[i]->texco = TEXCO_UV;
538 ma->mtex[i]->tex = add_texture("Texture");
539 ma->mtex[i]->tex->type = TEX_IMAGE;
540 ma->mtex[i]->tex->imaflag &= ~TEX_USEALPHA;
541 ma->mtex[i]->tex->ima = uid_image_map[ima_uid];
543 texindex_texarray_map[ctex.getTextureMapId()].push_back(ma->mtex[i]);
548 void DocumentImporter::write_profile_COMMON(COLLADAFW::EffectCommon *ef, Material *ma)
550 COLLADAFW::EffectCommon::ShaderType shader = ef->getShaderType();
553 if (shader == COLLADAFW::EffectCommon::SHADER_BLINN) {
554 ma->spec_shader = MA_SPEC_BLINN;
555 ma->spec = ef->getShininess().getFloatValue();
558 else if (shader == COLLADAFW::EffectCommon::SHADER_PHONG) {
559 ma->spec_shader = MA_SPEC_PHONG;
560 ma->har = ef->getShininess().getFloatValue();
563 else if (shader == COLLADAFW::EffectCommon::SHADER_LAMBERT) {
564 ma->diff_shader = MA_DIFF_LAMBERT;
568 ma->diff_shader = MA_DIFF_LAMBERT;
569 fprintf(stderr, "Current shader type is not supported, default to lambert.\n");
572 ma->ray_mirror = ef->getReflectivity().getFloatValue();
573 // index of refraction
574 ma->ang = ef->getIndexOfRefraction().getFloatValue();
577 COLLADAFW::Color col;
579 TexIndexTextureArrayMap texindex_texarray_map;
583 if (ef->getDiffuse().isColor()) {
584 col = ef->getDiffuse().getColor();
585 ma->r = col.getRed();
586 ma->g = col.getGreen();
587 ma->b = col.getBlue();
590 else if (ef->getDiffuse().isTexture()) {
591 COLLADAFW::Texture ctex = ef->getDiffuse().getTexture();
592 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
594 mtex->mapto = MAP_COL;
601 if (ef->getAmbient().isColor()) {
602 col = ef->getAmbient().getColor();
603 ma->ambr = col.getRed();
604 ma->ambg = col.getGreen();
605 ma->ambb = col.getBlue();
608 else if (ef->getAmbient().isTexture()) {
609 COLLADAFW::Texture ctex = ef->getAmbient().getTexture();
610 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
612 mtex->mapto = MAP_AMB;
618 if (ef->getSpecular().isColor()) {
619 col = ef->getSpecular().getColor();
620 ma->specr = col.getRed();
621 ma->specg = col.getGreen();
622 ma->specb = col.getBlue();
625 else if (ef->getSpecular().isTexture()) {
626 COLLADAFW::Texture ctex = ef->getSpecular().getTexture();
627 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
629 mtex->mapto = MAP_SPEC;
635 if (ef->getReflective().isColor()) {
636 col = ef->getReflective().getColor();
637 ma->mirr = col.getRed();
638 ma->mirg = col.getGreen();
639 ma->mirb = col.getBlue();
642 else if (ef->getReflective().isTexture()) {
643 COLLADAFW::Texture ctex = ef->getReflective().getTexture();
644 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
646 mtex->mapto = MAP_REF;
652 if (ef->getEmission().isColor()) {
653 // XXX there is no emission color in blender
657 else if (ef->getEmission().isTexture()) {
658 COLLADAFW::Texture ctex = ef->getEmission().getTexture();
659 mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
661 mtex->mapto = MAP_EMIT;
667 // if (ef->getOpacity().isColor()) {
668 // // XXX don't know what to do here
671 // else if (ef->getOpacity().isTexture()) {
672 // ctex = ef->getOpacity().getTexture();
673 // if (mtex != NULL) mtex->mapto &= MAP_ALPHA;
675 // mtex = create_texture(ef, ctex, ma, i, texindex_texarray_map);
676 // if (mtex != NULL) mtex->mapto = MAP_ALPHA;
679 material_texture_mapping_map[ma] = texindex_texarray_map;
682 /** When this method is called, the writer must write the effect.
683 @return The writer should return true, if writing succeeded, false otherwise.*/
685 bool DocumentImporter::writeEffect( const COLLADAFW::Effect* effect )
687 if(mImportStage!=General)
690 const COLLADAFW::UniqueId& uid = effect->getUniqueId();
691 if (uid_effect_map.find(uid) == uid_effect_map.end()) {
692 fprintf(stderr, "Couldn't find a material by UID.\n");
696 Material *ma = uid_effect_map[uid];
698 COLLADAFW::CommonEffectPointerArray common_efs = effect->getCommonEffects();
699 if (common_efs.getCount() < 1) {
700 fprintf(stderr, "Couldn't find <profile_COMMON>.\n");
703 // XXX TODO: Take all <profile_common>s
704 // Currently only first <profile_common> is supported
705 COLLADAFW::EffectCommon *ef = common_efs[0];
706 write_profile_COMMON(ef, ma);
712 /** When this method is called, the writer must write the camera.
713 @return The writer should return true, if writing succeeded, false otherwise.*/
714 bool DocumentImporter::writeCamera( const COLLADAFW::Camera* camera )
716 if(mImportStage!=General)
720 std::string cam_id, cam_name;
722 cam_id = camera->getOriginalId();
723 cam_name = camera->getName();
724 if (cam_name.size()) cam = (Camera*)add_camera((char*)cam_name.c_str());
725 else cam = (Camera*)add_camera((char*)cam_id.c_str());
728 fprintf(stderr, "Cannot create camera. \n");
731 cam->clipsta = camera->getNearClippingPlane().getValue();
732 cam->clipend = camera->getFarClippingPlane().getValue();
734 COLLADAFW::Camera::CameraType type = camera->getCameraType();
736 case COLLADAFW::Camera::ORTHOGRAPHIC:
738 cam->type = CAM_ORTHO;
741 case COLLADAFW::Camera::PERSPECTIVE:
743 cam->type = CAM_PERSP;
746 case COLLADAFW::Camera::UNDEFINED_CAMERATYPE:
748 fprintf(stderr, "Current camera type is not supported. \n");
749 cam->type = CAM_PERSP;
754 switch(camera->getDescriptionType()) {
755 case COLLADAFW::Camera::ASPECTRATIO_AND_Y:
760 double ymag = camera->getYMag().getValue();
761 double aspect = camera->getAspectRatio().getValue();
762 double xmag = aspect*ymag;
763 cam->ortho_scale = (float)xmag;
769 double yfov = camera->getYFov().getValue();
770 double aspect = camera->getAspectRatio().getValue();
771 double xfov = aspect*yfov;
772 // xfov is in degrees, cam->lens is in millimiters
773 cam->lens = angle_to_lens((float)xfov*(M_PI/180.0f));
779 /* XXX correct way to do following four is probably to get also render
780 size and determine proper settings from that somehow */
781 case COLLADAFW::Camera::ASPECTRATIO_AND_X:
782 case COLLADAFW::Camera::SINGLE_X:
783 case COLLADAFW::Camera::X_AND_Y:
787 cam->ortho_scale = (float)camera->getXMag().getValue();
792 double x = camera->getXFov().getValue();
793 // x is in degrees, cam->lens is in millimiters
794 cam->lens = angle_to_lens((float)x*(M_PI/180.0f));
800 case COLLADAFW::Camera::SINGLE_Y:
804 cam->ortho_scale = (float)camera->getYMag().getValue();
809 double yfov = camera->getYFov().getValue();
810 // yfov is in degrees, cam->lens is in millimiters
811 cam->lens = angle_to_lens((float)yfov*(M_PI/180.0f));
817 case COLLADAFW::Camera::UNDEFINED:
818 // read nothing, use blender defaults.
822 this->uid_camera_map[camera->getUniqueId()] = cam;
823 // XXX import camera options
827 /** When this method is called, the writer must write the image.
828 @return The writer should return true, if writing succeeded, false otherwise.*/
829 bool DocumentImporter::writeImage( const COLLADAFW::Image* image )
831 if(mImportStage!=General)
834 // XXX maybe it is necessary to check if the path is absolute or relative
835 const std::string& filepath = image->getImageURI().toNativePath();
836 const char *filename = (const char*)mFilename.c_str();
838 char full_path[FILE_MAX];
840 BLI_split_dirfile(filename, dir, NULL);
841 BLI_join_dirfile(full_path, sizeof(full_path), dir, filepath.c_str());
842 Image *ima = BKE_add_image_file(full_path);
844 fprintf(stderr, "Cannot create image. \n");
847 this->uid_image_map[image->getUniqueId()] = ima;
852 /** When this method is called, the writer must write the light.
853 @return The writer should return true, if writing succeeded, false otherwise.*/
854 bool DocumentImporter::writeLight( const COLLADAFW::Light* light )
856 if(mImportStage!=General)
860 std::string la_id, la_name;
862 la_id = light->getOriginalId();
863 la_name = light->getName();
864 if (la_name.size()) lamp = (Lamp*)add_lamp((char*)la_name.c_str());
865 else lamp = (Lamp*)add_lamp((char*)la_id.c_str());
868 fprintf(stderr, "Cannot create lamp. \n");
871 if (light->getColor().isValid()) {
872 COLLADAFW::Color col = light->getColor();
873 lamp->r = col.getRed();
874 lamp->g = col.getGreen();
875 lamp->b = col.getBlue();
877 float constatt = light->getConstantAttenuation().getValue();
878 float linatt = light->getLinearAttenuation().getValue();
879 float quadatt = light->getQuadraticAttenuation().getValue();
884 float e = 1.0f/constatt;
886 /* NOTE: We assume for now that inv square is used for quadratic light
887 * and inv linear for linear light. Exported blender lin/quad weighted
888 * most likely will result in wrong import. */
889 /* quadratic light */
890 if(IS_EQ(linatt, 0.0f) && quadatt > 0.0f) {
891 //quadatt = att2/(d*d*(e*2));
892 float invquadatt = 1.0f/quadatt;
893 float d2 = invquadatt / (2 * e);
897 else if(IS_EQ(quadatt, 0.0f) && linatt > 0.0f) {
898 //linatt = att1/(d*e);
899 float invlinatt = 1.0f/linatt;
902 printf("no linear nor quad light, using defaults for attenuation, import will be incorrect: Lamp %s\n", lamp->id.name);
909 COLLADAFW::Light::LightType type = light->getLightType();
911 case COLLADAFW::Light::AMBIENT_LIGHT:
913 lamp->type = LA_HEMI;
916 case COLLADAFW::Light::SPOT_LIGHT:
918 lamp->type = LA_SPOT;
919 lamp->falloff_type = LA_FALLOFF_INVSQUARE;
922 lamp->spotsize = light->getFallOffAngle().getValue();
923 lamp->spotblend = light->getFallOffExponent().getValue();
926 case COLLADAFW::Light::DIRECTIONAL_LIGHT:
928 /* our sun is very strong, so pick a smaller energy level */
931 lamp->mode |= LA_NO_SPEC;
934 case COLLADAFW::Light::POINT_LIGHT:
936 lamp->type = LA_LOCAL;
937 lamp->falloff_type = LA_FALLOFF_INVSQUARE;
942 case COLLADAFW::Light::UNDEFINED:
944 fprintf(stderr, "Current lamp type is not supported. \n");
945 lamp->type = LA_LOCAL;
950 this->uid_lamp_map[light->getUniqueId()] = lamp;
954 // this function is called only for animations that pass COLLADAFW::validate
955 bool DocumentImporter::writeAnimation( const COLLADAFW::Animation* anim )
957 if(mImportStage!=General)
961 return anim_importer.write_animation(anim);
964 // called on post-process stage after writeVisualScenes
965 bool DocumentImporter::writeAnimationList( const COLLADAFW::AnimationList* animationList )
967 if(mImportStage!=General)
971 return anim_importer.write_animation_list(animationList);
974 /** When this method is called, the writer must write the skin controller data.
975 @return The writer should return true, if writing succeeded, false otherwise.*/
976 bool DocumentImporter::writeSkinControllerData( const COLLADAFW::SkinControllerData* skin )
978 return armature_importer.write_skin_controller_data(skin);
981 // this is called on postprocess, before writeVisualScenes
982 bool DocumentImporter::writeController( const COLLADAFW::Controller* controller )
984 if(mImportStage!=General)
987 return armature_importer.write_controller(controller);
990 bool DocumentImporter::writeFormulas( const COLLADAFW::Formulas* formulas )
995 bool DocumentImporter::writeKinematicsScene( const COLLADAFW::KinematicsScene* kinematicsScene )
1000 bool DocumentImporter::addElementData( const COLLADAFW::UniqueId &uid)