2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
21 * ***** END GPL LICENSE BLOCK *****
24 /** \file blender/collada/TransformWriter.cpp
29 #include "BKE_object.h"
31 #include "TransformWriter.h"
35 void TransformWriter::add_node_transform(COLLADASW::Node& node, float mat[4][4], float parent_mat[4][4])
37 float loc[3], rot[3], scale[3];
42 invert_m4_m4(invpar, parent_mat);
43 mul_m4_m4m4(local, invpar, mat);
46 copy_m4_m4(local, mat);
50 UnitConverter *converter = new UnitConverter();
51 converter->mat4_to_dae_double(dmat, local);
53 TransformBase::decompose(local, loc, rot, NULL, scale);
55 if (node.getType() == COLLADASW::Node::JOINT) {
56 // XXX Why are joints handled differently ?
57 node.addMatrix("transform", dmat);
60 add_transform(node, loc, rot, scale);
64 void TransformWriter::add_node_transform_ob(COLLADASW::Node& node, Object *ob, BC_export_transformation_type transformation_type)
67 float rot[3], loc[3], scale[3];
70 float C[4][4], tmat[4][4], imat[4][4], mat[4][4];
72 // factor out scale from obmat
74 copy_v3_v3(scale, ob->size);
76 ob->size[0] = ob->size[1] = ob->size[2] = 1.0f;
77 BKE_object_to_mat4(ob, C);
78 copy_v3_v3(ob->size, scale);
80 mul_m4_series(tmat, ob->parent->obmat, ob->parentinv, C);
82 // calculate local mat
84 invert_m4_m4(imat, ob->parent->obmat);
85 mul_m4_m4m4(mat, imat, tmat);
89 mat4_to_eul(rot, mat);
90 copy_v3_v3(loc, mat[3]);
93 copy_v3_v3(loc, ob->loc);
94 copy_v3_v3(rot, ob->rot);
95 copy_v3_v3(scale, ob->size);
98 add_transform(node, loc, rot, scale);
101 UnitConverter converter;
102 double d_obmat[4][4];
105 /* Export the local Matrix (relative to the object parent) */
106 BKE_object_matrix_local_get(ob, f_obmat);
107 converter.mat4_to_dae_double(d_obmat, f_obmat);
109 switch (transformation_type) {
110 case BC_TRANSFORMATION_TYPE_MATRIX:
112 node.addMatrix("transform",d_obmat);
115 case BC_TRANSFORMATION_TYPE_BOTH:
117 node.addMatrix("transform",d_obmat);
120 case BC_TRANSFORMATION_TYPE_TRANSROTLOC:
122 float loc[3], rot[3], scale[3];
123 TransformBase::decompose(f_obmat, loc, rot, NULL, scale);
124 add_transform(node, loc, rot, scale);
131 void TransformWriter::add_node_transform_identity(COLLADASW::Node& node)
133 float loc[3] = {0.0f, 0.0f, 0.0f}, scale[3] = {1.0f, 1.0f, 1.0f}, rot[3] = {0.0f, 0.0f, 0.0f};
134 add_transform(node, loc, rot, scale);
137 void TransformWriter::add_transform(COLLADASW::Node& node, float loc[3], float rot[3], float scale[3])
140 node.addRotateZ("rotationZ", COLLADABU::Math::Utils::radToDegF(rot[2]));
141 node.addRotateY("rotationY", COLLADABU::Math::Utils::radToDegF(rot[1]));
142 node.addRotateX("rotationX", COLLADABU::Math::Utils::radToDegF(rot[0]));
144 node.addTranslate("location", loc[0], loc[1], loc[2]);
145 node.addRotateZ("rotationZ", RAD2DEGF(rot[2]));
146 node.addRotateY("rotationY", RAD2DEGF(rot[1]));
147 node.addRotateX("rotationX", RAD2DEGF(rot[0]));
148 node.addScale("scale", scale[0], scale[1], scale[2]);