| 
									
										
										
										
											2011-02-23 10:52:22 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed, | 
					
						
							|  |  |  |  *                 Nathan Letwory | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-27 20:30:35 +00:00
										 |  |  | /** \file blender/collada/TransformWriter.cpp
 | 
					
						
							|  |  |  |  *  \ingroup collada | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | #include "BKE_object.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "TransformWriter.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_math.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TransformWriter::add_node_transform(COLLADASW::Node& node, float mat[][4], float parent_mat[][4]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	float loc[3], rot[3], scale[3]; | 
					
						
							|  |  |  | 	float local[4][4]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (parent_mat) { | 
					
						
							|  |  |  | 		float invpar[4][4]; | 
					
						
							|  |  |  | 		invert_m4_m4(invpar, parent_mat); | 
					
						
							| 
									
										
										
											
												Math lib: matrix multiplication order fix for two functions that were
inconsistent with similar functions & math notation:
mul_m4_m4m4(R, B, A) => mult_m4_m4m4(R, A, B)
mul_m3_m3m4(R, B, A) => mult_m3_m3m4(R, A, B)
For branch maintainers, it should be relatively simple to fix things manually,
it's also possible run this script after merging to do automatic replacement:
http://www.pasteall.org/27459/python
											
										 
											2011-12-16 19:53:12 +00:00
										 |  |  | 		mult_m4_m4m4(local, invpar, mat); | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		copy_m4_m4(local, mat); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-08 16:38:57 +00:00
										 |  |  | 	double dmat[4][4]; | 
					
						
							| 
									
										
										
										
											2011-08-17 18:28:01 +00:00
										 |  |  | 	UnitConverter* converter = new UnitConverter(); | 
					
						
							| 
									
										
										
										
											2012-04-29 15:47:02 +00:00
										 |  |  | 	converter->mat4_to_dae_double(dmat, local); | 
					
						
							| 
									
										
										
										
											2011-08-08 16:38:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | 	TransformBase::decompose(local, loc, rot, NULL, scale); | 
					
						
							| 
									
										
										
										
											2011-08-17 18:28:01 +00:00
										 |  |  | 	if ( node.getType() == COLLADASW::Node::JOINT) | 
					
						
							| 
									
										
										
										
											2012-04-29 15:47:02 +00:00
										 |  |  | 		node.addMatrix("transform", dmat); | 
					
						
							| 
									
										
										
										
											2011-08-17 18:28:01 +00:00
										 |  |  | 	else | 
					
						
							|  |  |  | 		add_transform(node, loc, rot, scale); | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TransformWriter::add_node_transform_ob(COLLADASW::Node& node, Object *ob) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-02-05 16:19:28 +00:00
										 |  |  | 	/*
 | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | 	float rot[3], loc[3], scale[3]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ob->parent) { | 
					
						
							|  |  |  | 		float C[4][4], tmat[4][4], imat[4][4], mat[4][4]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// factor out scale from obmat
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		copy_v3_v3(scale, ob->size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ob->size[0] = ob->size[1] = ob->size[2] = 1.0f; | 
					
						
							| 
									
										
										
										
											2012-05-05 14:03:12 +00:00
										 |  |  | 		BKE_object_to_mat4(ob, C); | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | 		copy_v3_v3(ob->size, scale); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		mul_serie_m4(tmat, ob->parent->obmat, ob->parentinv, C, NULL, NULL, NULL, NULL, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// calculate local mat
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		invert_m4_m4(imat, ob->parent->obmat); | 
					
						
							| 
									
										
										
											
												Math lib: matrix multiplication order fix for two functions that were
inconsistent with similar functions & math notation:
mul_m4_m4m4(R, B, A) => mult_m4_m4m4(R, A, B)
mul_m3_m3m4(R, B, A) => mult_m3_m3m4(R, A, B)
For branch maintainers, it should be relatively simple to fix things manually,
it's also possible run this script after merging to do automatic replacement:
http://www.pasteall.org/27459/python
											
										 
											2011-12-16 19:53:12 +00:00
										 |  |  | 		mult_m4_m4m4(mat, imat, tmat); | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// done
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		mat4_to_eul(rot, mat); | 
					
						
							|  |  |  | 		copy_v3_v3(loc, mat[3]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		copy_v3_v3(loc, ob->loc); | 
					
						
							|  |  |  | 		copy_v3_v3(rot, ob->rot); | 
					
						
							|  |  |  | 		copy_v3_v3(scale, ob->size); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	add_transform(node, loc, rot, scale); | 
					
						
							| 
									
										
										
										
											2012-02-05 16:19:28 +00:00
										 |  |  | 	*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Using parentinv should allow use of existing curves */ | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 	if (ob->parent) { | 
					
						
							| 
									
										
										
										
											2012-02-18 15:41:09 +00:00
										 |  |  | 		// If parentinv is identity don't add it.
 | 
					
						
							|  |  |  | 		bool add_parinv = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 		for (int i = 0; i < 16; ++i) { | 
					
						
							| 
									
										
										
										
											2012-02-27 10:35:39 +00:00
										 |  |  | 			float f = (i % 4 == i / 4) ? 1.0f : 0.0f; | 
					
						
							|  |  |  | 			add_parinv |= (ob->parentinv[i % 4][i / 4] != f); | 
					
						
							| 
									
										
										
										
											2012-02-18 15:41:09 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-28 06:31:57 +00:00
										 |  |  | 		if (add_parinv) { | 
					
						
							| 
									
										
										
										
											2012-02-18 15:41:09 +00:00
										 |  |  | 			double dmat[4][4]; | 
					
						
							|  |  |  | 			UnitConverter converter; | 
					
						
							|  |  |  | 			converter.mat4_to_dae_double(dmat, ob->parentinv); | 
					
						
							|  |  |  | 			node.addMatrix("parentinverse", dmat); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2012-02-05 16:19:28 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	add_transform(node, ob->loc, ob->rot, ob->size); | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TransformWriter::add_node_transform_identity(COLLADASW::Node& node) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	float loc[] = {0.0f, 0.0f, 0.0f}, scale[] = {1.0f, 1.0f, 1.0f}, rot[] = {0.0f, 0.0f, 0.0f}; | 
					
						
							|  |  |  | 	add_transform(node, loc, rot, scale); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void TransformWriter::add_transform(COLLADASW::Node& node, float loc[3], float rot[3], float scale[3]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	node.addTranslate("location", loc[0], loc[1], loc[2]); | 
					
						
							| 
									
										
										
										
											2011-06-25 07:23:23 +00:00
										 |  |  | 	/*node.addRotateZ("rotationZ", COLLADABU::Math::Utils::radToDegF(rot[2]));
 | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | 	node.addRotateY("rotationY", COLLADABU::Math::Utils::radToDegF(rot[1])); | 
					
						
							| 
									
										
										
										
											2011-06-25 07:23:23 +00:00
										 |  |  | 	node.addRotateX("rotationX", COLLADABU::Math::Utils::radToDegF(rot[0]));*/ | 
					
						
							| 
									
										
										
										
											2011-09-17 09:43:51 +00:00
										 |  |  | 	node.addRotateZ("rotationZ", RAD2DEGF(rot[2])); | 
					
						
							|  |  |  | 	node.addRotateY("rotationY", RAD2DEGF(rot[1])); | 
					
						
							|  |  |  | 	node.addRotateX("rotationX", RAD2DEGF(rot[0])); | 
					
						
							| 
									
										
										
										
											2011-06-25 07:23:23 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-06 11:02:44 +00:00
										 |  |  | 	node.addScale("scale", scale[0], scale[1], scale[2]); | 
					
						
							|  |  |  | } |