| 
									
										
										
										
											2011-02-23 10:52:22 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup collada | 
					
						
							| 
									
										
										
										
											2011-02-27 20:30:35 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-23 12:45:40 +01:00
										 |  |  | #include "BLI_math.h"
 | 
					
						
							| 
									
										
										
										
											2018-04-17 10:58:27 +02:00
										 |  |  | #include "BLI_sys_types.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BKE_object.h"
 | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "TransformWriter.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 22:05:09 +02:00
										 |  |  | static BC_export_transformation_type get_transformation_type(BCExportSettings &export_settings) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   bool enforce_matrix_export = export_settings.get_include_animations(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (enforce_matrix_export) ? BC_TRANSFORMATION_TYPE_MATRIX : | 
					
						
							|  |  |  |                                                   export_settings.get_object_transformation_type(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static BC_export_transformation_type get_transformation_type(Object *ob, | 
					
						
							|  |  |  |                                                              BCExportSettings &export_settings) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   bool enforce_matrix_export = ob->type == OB_ARMATURE && export_settings.get_include_animations(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (enforce_matrix_export) ? BC_TRANSFORMATION_TYPE_MATRIX : | 
					
						
							|  |  |  |                                                   export_settings.get_object_transformation_type(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-27 15:02:11 +02:00
										 |  |  | void TransformWriter::add_joint_transform(COLLADASW::Node &node, | 
					
						
							| 
									
										
										
										
											2019-05-28 16:11:49 +10:00
										 |  |  |                                           float mat[4][4], | 
					
						
							|  |  |  |                                           float parent_mat[4][4], | 
					
						
							|  |  |  |                                           BCExportSettings &export_settings, | 
					
						
							|  |  |  |                                           bool has_restmat) | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   float local[4][4]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (parent_mat) { | 
					
						
							|  |  |  |     float invpar[4][4]; | 
					
						
							|  |  |  |     invert_m4_m4(invpar, parent_mat); | 
					
						
							|  |  |  |     mul_m4_m4m4(local, invpar, mat); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     copy_m4_m4(local, mat); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 22:27:45 +02:00
										 |  |  |   if (!has_restmat && export_settings.get_apply_global_orientation()) { | 
					
						
							| 
									
										
										
										
											2019-05-23 12:17:17 +02:00
										 |  |  |     bc_apply_global_transform(local, export_settings.get_global_transform()); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   double dmat[4][4]; | 
					
						
							|  |  |  |   UnitConverter *converter = new UnitConverter(); | 
					
						
							|  |  |  |   converter->mat4_to_dae_double(dmat, local); | 
					
						
							|  |  |  |   delete converter; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 22:05:09 +02:00
										 |  |  |   if (get_transformation_type(export_settings) == BC_TRANSFORMATION_TYPE_MATRIX) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     node.addMatrix("transform", dmat); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							| 
									
										
										
										
											2019-05-27 15:34:05 +02:00
										 |  |  |     float loc[3], rot[3], scale[3]; | 
					
						
							| 
									
										
										
										
											2019-05-23 12:17:17 +02:00
										 |  |  |     bc_decompose(local, loc, rot, NULL, scale); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     add_transform(node, loc, rot, scale); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | void TransformWriter::add_node_transform_ob(COLLADASW::Node &node, | 
					
						
							|  |  |  |                                             Object *ob, | 
					
						
							| 
									
										
										
										
											2019-05-23 12:17:17 +02:00
										 |  |  |                                             BCExportSettings &export_settings) | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-23 12:17:17 +02:00
										 |  |  |   bool limit_precision = export_settings.get_limit_precision(); | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 12:17:17 +02:00
										 |  |  |   /* Export the local Matrix (relative to the object parent,
 | 
					
						
							|  |  |  |    * be it an object, bone or vertex(-tices)). */ | 
					
						
							|  |  |  |   Matrix f_obmat; | 
					
						
							|  |  |  |   BKE_object_matrix_local_get(ob, f_obmat); | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-23 12:17:17 +02:00
										 |  |  |   if (export_settings.get_apply_global_orientation()) { | 
					
						
							|  |  |  |     bc_apply_global_transform(f_obmat, export_settings.get_global_transform()); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							| 
									
										
										
										
											2019-05-23 12:17:17 +02:00
										 |  |  |     bc_add_global_transform(f_obmat, export_settings.get_global_transform()); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 22:05:09 +02:00
										 |  |  |   switch (get_transformation_type(ob, export_settings)) { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |     case BC_TRANSFORMATION_TYPE_MATRIX: { | 
					
						
							|  |  |  |       UnitConverter converter; | 
					
						
							|  |  |  |       double d_obmat[4][4]; | 
					
						
							|  |  |  |       converter.mat4_to_dae_double(d_obmat, f_obmat); | 
					
						
							| 
									
										
										
										
											2019-05-28 09:23:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-31 22:51:19 +10:00
										 |  |  |       if (limit_precision) { | 
					
						
							| 
									
										
										
										
											2019-05-28 09:23:05 +02:00
										 |  |  |         BCMatrix::sanitize(d_obmat, LIMITTED_PRECISION); | 
					
						
							| 
									
										
										
										
											2019-05-31 22:51:19 +10:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |       node.addMatrix("transform", d_obmat); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-27 18:35:34 +02:00
										 |  |  |     case BC_TRANSFORMATION_TYPE_DECOMPOSED: { | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |       float loc[3], rot[3], scale[3]; | 
					
						
							|  |  |  |       bc_decompose(f_obmat, loc, rot, NULL, scale); | 
					
						
							|  |  |  |       if (limit_precision) { | 
					
						
							|  |  |  |         bc_sanitize_v3(loc, LIMITTED_PRECISION); | 
					
						
							|  |  |  |         bc_sanitize_v3(rot, LIMITTED_PRECISION); | 
					
						
							|  |  |  |         bc_sanitize_v3(scale, LIMITTED_PRECISION); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       add_transform(node, loc, rot, scale); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 16:11:49 +10:00
										 |  |  | void TransformWriter::add_node_transform_identity(COLLADASW::Node &node, | 
					
						
							|  |  |  |                                                   BCExportSettings &export_settings) | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-27 15:34:05 +02:00
										 |  |  |   BC_export_transformation_type transformation_type = | 
					
						
							|  |  |  |       export_settings.get_object_transformation_type(); | 
					
						
							|  |  |  |   switch (transformation_type) { | 
					
						
							|  |  |  |     case BC_TRANSFORMATION_TYPE_MATRIX: { | 
					
						
							|  |  |  |       BCMatrix mat; | 
					
						
							|  |  |  |       DMatrix d_obmat; | 
					
						
							|  |  |  |       mat.get_matrix(d_obmat); | 
					
						
							|  |  |  |       node.addMatrix("transform", d_obmat); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     default: { | 
					
						
							|  |  |  |       float loc[3] = {0.0f, 0.0f, 0.0f}; | 
					
						
							|  |  |  |       float scale[3] = {1.0f, 1.0f, 1.0f}; | 
					
						
							|  |  |  |       float rot[3] = {0.0f, 0.0f, 0.0f}; | 
					
						
							|  |  |  |       add_transform(node, loc, rot, scale); | 
					
						
							|  |  |  |       break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | void TransformWriter::add_transform(COLLADASW::Node &node, | 
					
						
							|  |  |  |                                     float loc[3], | 
					
						
							|  |  |  |                                     float rot[3], | 
					
						
							|  |  |  |                                     float scale[3]) | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-29 08:57:47 +02:00
										 |  |  |   node.addScale("scale", scale[0], scale[1], scale[2]); | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  |   node.addRotateZ("rotationZ", RAD2DEGF(rot[2])); | 
					
						
							|  |  |  |   node.addRotateY("rotationY", RAD2DEGF(rot[1])); | 
					
						
							|  |  |  |   node.addRotateX("rotationX", RAD2DEGF(rot[0])); | 
					
						
							| 
									
										
										
										
											2019-05-29 08:57:47 +02:00
										 |  |  |   node.addTranslate("location", loc[0], loc[1], loc[2]); | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | } |