Blender  V2.59
TransformWriter.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: TransformWriter.cpp 35243 2011-02-27 20:30:35Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Jan Diederich, Tod Liverseed,
00021  *                 Nathan Letwory
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00031 #include "BKE_object.h"
00032 
00033 #include "TransformWriter.h"
00034 
00035 #include "BLI_math.h"
00036 
00037 void TransformWriter::add_node_transform(COLLADASW::Node& node, float mat[][4], float parent_mat[][4])
00038 {
00039         float loc[3], rot[3], scale[3];
00040         float local[4][4];
00041 
00042         if (parent_mat) {
00043                 float invpar[4][4];
00044                 invert_m4_m4(invpar, parent_mat);
00045                 mul_m4_m4m4(local, mat, invpar);
00046         }
00047         else {
00048                 copy_m4_m4(local, mat);
00049         }
00050 
00051         TransformBase::decompose(local, loc, rot, NULL, scale);
00052         
00053         add_transform(node, loc, rot, scale);
00054 }
00055 
00056 void TransformWriter::add_node_transform_ob(COLLADASW::Node& node, Object *ob)
00057 {
00058         float rot[3], loc[3], scale[3];
00059 
00060         if (ob->parent) {
00061                 float C[4][4], tmat[4][4], imat[4][4], mat[4][4];
00062 
00063                 // factor out scale from obmat
00064 
00065                 copy_v3_v3(scale, ob->size);
00066 
00067                 ob->size[0] = ob->size[1] = ob->size[2] = 1.0f;
00068                 object_to_mat4(ob, C);
00069                 copy_v3_v3(ob->size, scale);
00070 
00071                 mul_serie_m4(tmat, ob->parent->obmat, ob->parentinv, C, NULL, NULL, NULL, NULL, NULL);
00072 
00073                 // calculate local mat
00074 
00075                 invert_m4_m4(imat, ob->parent->obmat);
00076                 mul_m4_m4m4(mat, tmat, imat);
00077 
00078                 // done
00079 
00080                 mat4_to_eul(rot, mat);
00081                 copy_v3_v3(loc, mat[3]);
00082         }
00083         else {
00084                 copy_v3_v3(loc, ob->loc);
00085                 copy_v3_v3(rot, ob->rot);
00086                 copy_v3_v3(scale, ob->size);
00087         }
00088 
00089         add_transform(node, loc, rot, scale);
00090 }
00091 
00092 void TransformWriter::add_node_transform_identity(COLLADASW::Node& node)
00093 {
00094         float loc[] = {0.0f, 0.0f, 0.0f}, scale[] = {1.0f, 1.0f, 1.0f}, rot[] = {0.0f, 0.0f, 0.0f};
00095         add_transform(node, loc, rot, scale);
00096 }
00097 
00098 void TransformWriter::add_transform(COLLADASW::Node& node, float loc[3], float rot[3], float scale[3])
00099 {
00100         node.addTranslate("location", loc[0], loc[1], loc[2]);
00101         node.addRotateZ("rotationZ", COLLADABU::Math::Utils::radToDegF(rot[2]));
00102         node.addRotateY("rotationY", COLLADABU::Math::Utils::radToDegF(rot[1]));
00103         node.addRotateX("rotationX", COLLADABU::Math::Utils::radToDegF(rot[0]));
00104         node.addScale("scale", scale[0], scale[1], scale[2]);
00105 }