Blender  V2.59
TransformReader.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: TransformReader.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, Nathan Letwory.
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00029 /* COLLADABU_ASSERT, may be able to remove later */
00030 #include "COLLADABUPlatform.h"
00031 
00032 #include "TransformReader.h"
00033 
00034 TransformReader::TransformReader(UnitConverter* conv) : unit_converter(conv) {}
00035 
00036 void TransformReader::get_node_mat(float mat[][4], COLLADAFW::Node *node, std::map<COLLADAFW::UniqueId, Animation> *animation_map, Object *ob)
00037 {
00038         float cur[4][4];
00039         float copy[4][4];
00040 
00041         unit_m4(mat);
00042         
00043         for (unsigned int i = 0; i < node->getTransformations().getCount(); i++) {
00044 
00045                 COLLADAFW::Transformation *tm = node->getTransformations()[i];
00046                 COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
00047 
00048                 switch(type) {
00049                 case COLLADAFW::Transformation::TRANSLATE:
00050                         dae_translate_to_mat4(tm, cur);
00051                         break;
00052                 case COLLADAFW::Transformation::ROTATE:
00053                         dae_rotate_to_mat4(tm, cur);
00054                         break;
00055                 case COLLADAFW::Transformation::SCALE:
00056                         dae_scale_to_mat4(tm, cur);
00057                         break;
00058                 case COLLADAFW::Transformation::MATRIX:
00059                         dae_matrix_to_mat4(tm, cur);
00060                         break;
00061                 case COLLADAFW::Transformation::LOOKAT:
00062                 case COLLADAFW::Transformation::SKEW:
00063                         fprintf(stderr, "LOOKAT and SKEW transformations are not supported yet.\n");
00064                         break;
00065                 }
00066 
00067                 copy_m4_m4(copy, mat);
00068                 mul_m4_m4m4(mat, cur, copy);
00069 
00070                 if (animation_map) {
00071                         // AnimationList that drives this Transformation
00072                         const COLLADAFW::UniqueId& anim_list_id = tm->getAnimationList();
00073                 
00074                         // store this so later we can link animation data with ob
00075                         Animation anim = {ob, node, tm};
00076                         (*animation_map)[anim_list_id] = anim;
00077                 }
00078         }
00079 }
00080 
00081 void TransformReader::dae_rotate_to_mat4(COLLADAFW::Transformation *tm, float m[][4])
00082 {
00083         COLLADAFW::Rotate *ro = (COLLADAFW::Rotate*)tm;
00084         COLLADABU::Math::Vector3& axis = ro->getRotationAxis();
00085         float angle = (float)(ro->getRotationAngle() * M_PI / 180.0f);
00086         float ax[] = {axis[0], axis[1], axis[2]};
00087         // float quat[4];
00088         // axis_angle_to_quat(quat, axis, angle);
00089         // quat_to_mat4(m, quat);
00090         axis_angle_to_mat4(m, ax, angle);
00091 }
00092 
00093 void TransformReader::dae_translate_to_mat4(COLLADAFW::Transformation *tm, float m[][4])
00094 {
00095         COLLADAFW::Translate *tra = (COLLADAFW::Translate*)tm;
00096         COLLADABU::Math::Vector3& t = tra->getTranslation();
00097 
00098         unit_m4(m);
00099 
00100         m[3][0] = (float)t[0];
00101         m[3][1] = (float)t[1];
00102         m[3][2] = (float)t[2];
00103 }
00104 
00105 void TransformReader::dae_scale_to_mat4(COLLADAFW::Transformation *tm, float m[][4])
00106 {
00107         COLLADABU::Math::Vector3& s = ((COLLADAFW::Scale*)tm)->getScale();
00108         float size[3] = {(float)s[0], (float)s[1], (float)s[2]};
00109         size_to_mat4(m, size);
00110 }
00111 
00112 void TransformReader::dae_matrix_to_mat4(COLLADAFW::Transformation *tm, float m[][4])
00113 {
00114         unit_converter->dae_matrix_to_mat4_(m, ((COLLADAFW::Matrix*)tm)->getMatrix());
00115 }
00116 
00117 void TransformReader::dae_translate_to_v3(COLLADAFW::Transformation *tm, float v[3])
00118 {
00119         dae_vector3_to_v3(((COLLADAFW::Translate*)tm)->getTranslation(), v);
00120 }
00121 
00122 void TransformReader::dae_scale_to_v3(COLLADAFW::Transformation *tm, float v[3])
00123 {
00124         dae_vector3_to_v3(((COLLADAFW::Scale*)tm)->getScale(), v);
00125 }
00126 
00127 void TransformReader::dae_vector3_to_v3(const COLLADABU::Math::Vector3 &v3, float v[3])
00128 {
00129         v[0] = v3.x;
00130         v[1] = v3.y;
00131         v[2] = v3.z;
00132 }