Merge branch 'blender2.7'
[blender.git] / source / blender / collada / TransformWriter.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, Jan Diederich, Tod Liverseed,
19  *                 Nathan Letwory
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  */
23
24 /** \file blender/collada/TransformWriter.cpp
25  *  \ingroup collada
26  */
27
28
29 #include "BLI_math.h"
30 #include "BLI_sys_types.h"
31
32 #include "BKE_object.h"
33
34 #include "TransformWriter.h"
35
36 void TransformWriter::add_node_transform(COLLADASW::Node& node, float mat[4][4], float parent_mat[4][4], bool limit_precision)
37 {
38         float loc[3], rot[3], scale[3];
39         float local[4][4];
40
41         if (parent_mat) {
42                 float invpar[4][4];
43                 invert_m4_m4(invpar, parent_mat);
44                 mul_m4_m4m4(local, invpar, mat);
45         }
46         else {
47                 copy_m4_m4(local, mat);
48         }
49
50         double dmat[4][4];
51         UnitConverter *converter = new UnitConverter();
52         converter->mat4_to_dae_double(dmat, local);
53         delete converter;
54
55         bc_decompose(local, loc, rot, NULL, scale);
56
57         if (node.getType() == COLLADASW::Node::JOINT) {
58                 // XXX Why are joints handled differently ?
59                 node.addMatrix("transform", dmat);
60         }
61         else {
62                 add_transform(node, loc, rot, scale);
63         }
64 }
65
66 void TransformWriter::add_node_transform_ob(
67         COLLADASW::Node& node,
68         Object *ob,
69         BC_export_transformation_type transformation_type,
70         bool limit_precision)
71 {
72 #if 0
73         float rot[3], loc[3], scale[3];
74
75         if (ob->parent) {
76                 float C[4][4], tmat[4][4], imat[4][4], mat[4][4];
77
78                 // factor out scale from obmat
79
80                 copy_v3_v3(scale, ob->size);
81
82                 ob->size[0] = ob->size[1] = ob->size[2] = 1.0f;
83                 BKE_object_to_mat4(ob, C);
84                 copy_v3_v3(ob->size, scale);
85
86                 mul_m4_series(tmat, ob->parent->obmat, ob->parentinv, C);
87
88                 // calculate local mat
89
90                 invert_m4_m4(imat, ob->parent->obmat);
91                 mul_m4_m4m4(mat, imat, tmat);
92
93                 // done
94
95                 mat4_to_eul(rot, mat);
96                 copy_v3_v3(loc, mat[3]);
97         }
98         else {
99                 copy_v3_v3(loc, ob->loc);
100                 copy_v3_v3(rot, ob->rot);
101                 copy_v3_v3(scale, ob->size);
102         }
103
104         add_transform(node, loc, rot, scale);
105 #endif
106
107         /* Export the local Matrix (relative to the object parent, be it an object, bone or vertex(-tices)) */
108         float  f_obmat[4][4];
109         BKE_object_matrix_local_get(ob, f_obmat);
110
111         switch (transformation_type) {
112                 case BC_TRANSFORMATION_TYPE_MATRIX:
113                 {
114                         UnitConverter converter;
115                         double d_obmat[4][4];
116                         converter.mat4_to_dae_double(d_obmat, f_obmat);
117                         if (limit_precision)
118                                 bc_sanitize_mat(d_obmat, LIMITTED_PRECISION);
119                         node.addMatrix("transform",d_obmat);
120                         break;
121                 }
122                 case BC_TRANSFORMATION_TYPE_TRANSROTLOC:
123                 {
124                         float loc[3], rot[3], scale[3];
125                         bc_decompose(f_obmat, loc, rot, NULL, scale);
126                         if (limit_precision) {
127                                 bc_sanitize_v3(loc, LIMITTED_PRECISION);
128                                 bc_sanitize_v3(rot, LIMITTED_PRECISION);
129                                 bc_sanitize_v3(scale, LIMITTED_PRECISION);
130                         }
131                         add_transform(node, loc, rot, scale);
132                         break;
133                 }
134         }
135
136 }
137
138 void TransformWriter::add_node_transform_identity(COLLADASW::Node& node)
139 {
140         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};
141         add_transform(node, loc, rot, scale);
142 }
143
144 void TransformWriter::add_transform(COLLADASW::Node& node, float loc[3], float rot[3], float scale[3])
145 {
146 #if 0
147         node.addRotateZ("rotationZ", COLLADABU::Math::Utils::radToDegF(rot[2]));
148         node.addRotateY("rotationY", COLLADABU::Math::Utils::radToDegF(rot[1]));
149         node.addRotateX("rotationX", COLLADABU::Math::Utils::radToDegF(rot[0]));
150 #endif
151         node.addTranslate("location", loc[0], loc[1], loc[2]);
152         node.addRotateZ("rotationZ", RAD2DEGF(rot[2]));
153         node.addRotateY("rotationY", RAD2DEGF(rot[1]));
154         node.addRotateX("rotationX", RAD2DEGF(rot[0]));
155         node.addScale("scale", scale[0], scale[1], scale[2]);
156
157 }