Blender V2.61 - r43446

TransformReader.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Chingiz Dyussenov, Arystanbek Dyussenov, Nathan Letwory.
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00027 /* COLLADABU_ASSERT, may be able to remove later */
00028 #include "COLLADABUPlatform.h"
00029 
00030 #include "TransformReader.h"
00031 
00032 TransformReader::TransformReader(UnitConverter* conv) : unit_converter(conv) {}
00033 
00034 void TransformReader::get_node_mat(float mat[][4], COLLADAFW::Node *node, std::map<COLLADAFW::UniqueId, Animation> *animation_map, Object *ob)
00035 {
00036     float cur[4][4];
00037     float copy[4][4];
00038 
00039     unit_m4(mat);
00040     
00041     for (unsigned int i = 0; i < node->getTransformations().getCount(); i++) {
00042 
00043         COLLADAFW::Transformation *tm = node->getTransformations()[i];
00044         COLLADAFW::Transformation::TransformationType type = tm->getTransformationType();
00045 
00046             switch(type) {
00047             case COLLADAFW::Transformation::TRANSLATE:
00048                 dae_translate_to_mat4(tm, cur);
00049                 break;
00050             case COLLADAFW::Transformation::ROTATE:
00051                 dae_rotate_to_mat4(tm, cur);
00052                 break;
00053             case COLLADAFW::Transformation::SCALE:
00054                 dae_scale_to_mat4(tm, cur);
00055                 break;
00056             case COLLADAFW::Transformation::MATRIX:
00057                 dae_matrix_to_mat4(tm, cur);
00058                 break;
00059             case COLLADAFW::Transformation::LOOKAT:
00060             case COLLADAFW::Transformation::SKEW:
00061                 fprintf(stderr, "LOOKAT and SKEW transformations are not supported yet.\n");
00062                 break;
00063             }
00064 
00065         copy_m4_m4(copy, mat);
00066         mult_m4_m4m4(mat, copy, cur);
00067 
00068         if (animation_map) {
00069             // AnimationList that drives this Transformation
00070             const COLLADAFW::UniqueId& anim_list_id = tm->getAnimationList();
00071         
00072             // store this so later we can link animation data with ob
00073             Animation anim = {ob, node, tm};
00074             (*animation_map)[anim_list_id] = anim;
00075         }
00076     }
00077 }
00078 
00079 void TransformReader::dae_rotate_to_mat4(COLLADAFW::Transformation *tm, float m[][4])
00080 {
00081     COLLADAFW::Rotate *ro = (COLLADAFW::Rotate*)tm;
00082     COLLADABU::Math::Vector3& axis = ro->getRotationAxis();
00083     const float angle = (float)DEG2RAD(ro->getRotationAngle());
00084     const float ax[] = {axis[0], axis[1], axis[2]};
00085     // float quat[4];
00086     // axis_angle_to_quat(quat, axis, angle);
00087     // quat_to_mat4(m, quat);
00088     axis_angle_to_mat4(m, ax, angle);
00089 }
00090 
00091 void TransformReader::dae_translate_to_mat4(COLLADAFW::Transformation *tm, float m[][4])
00092 {
00093     COLLADAFW::Translate *tra = (COLLADAFW::Translate*)tm;
00094     COLLADABU::Math::Vector3& t = tra->getTranslation();
00095 
00096     unit_m4(m);
00097 
00098     m[3][0] = (float)t[0];
00099     m[3][1] = (float)t[1];
00100     m[3][2] = (float)t[2];
00101 }
00102 
00103 void TransformReader::dae_scale_to_mat4(COLLADAFW::Transformation *tm, float m[][4])
00104 {
00105     COLLADABU::Math::Vector3& s = ((COLLADAFW::Scale*)tm)->getScale();
00106     float size[3] = {(float)s[0], (float)s[1], (float)s[2]};
00107     size_to_mat4(m, size);
00108 }
00109 
00110 void TransformReader::dae_matrix_to_mat4(COLLADAFW::Transformation *tm, float m[][4])
00111 {
00112     unit_converter->dae_matrix_to_mat4_(m, ((COLLADAFW::Matrix*)tm)->getMatrix());
00113 }
00114 
00115 void TransformReader::dae_translate_to_v3(COLLADAFW::Transformation *tm, float v[3])
00116 {
00117     dae_vector3_to_v3(((COLLADAFW::Translate*)tm)->getTranslation(), v);
00118 }
00119 
00120 void TransformReader::dae_scale_to_v3(COLLADAFW::Transformation *tm, float v[3])
00121 {
00122     dae_vector3_to_v3(((COLLADAFW::Scale*)tm)->getScale(), v);
00123 }
00124 
00125 void TransformReader::dae_vector3_to_v3(const COLLADABU::Math::Vector3 &v3, float v[3])
00126 {
00127     v[0] = v3.x;
00128     v[1] = v3.y;
00129     v[2] = v3.z;
00130 }