00001 /*===========================================================================*\ 00002 * * 00003 * OpenFlipper * 00004 * Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen * 00005 * www.openflipper.org * 00006 * * 00007 *---------------------------------------------------------------------------* 00008 * This file is part of OpenFlipper. * 00009 * * 00010 * OpenFlipper is free software: you can redistribute it and/or modify * 00011 * it under the terms of the GNU Lesser General Public License as * 00012 * published by the Free Software Foundation, either version 3 of * 00013 * the License, or (at your option) any later version with the * 00014 * following exceptions: * 00015 * * 00016 * If other files instantiate templates or use macros * 00017 * or inline functions from this file, or you compile this file and * 00018 * link it with other files to produce an executable, this file does * 00019 * not by itself cause the resulting executable to be covered by the * 00020 * GNU Lesser General Public License. This exception does not however * 00021 * invalidate any other reasons why the executable file might be * 00022 * covered by the GNU Lesser General Public License. * 00023 * * 00024 * OpenFlipper is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU Lesser General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU LesserGeneral Public * 00030 * License along with OpenFlipper. If not, * 00031 * see <http://www.gnu.org/licenses/>. * 00032 * * 00033 \*===========================================================================*/ 00034 00035 /*===========================================================================*\ 00036 * * 00037 * $Revision: 6743 $ * 00038 * $Author: moebius $ * 00039 * $Date: 2009-08-05 11:03:10 +0200 (Mi, 05. Aug 2009) $ * 00040 * * 00041 \*===========================================================================*/ 00042 00043 00044 00045 00046 //============================================================================= 00047 // 00048 // CLASS TransformNode - IMPLEMENTATION 00049 // 00050 //============================================================================= 00051 00052 00053 //== INCLUDES ================================================================= 00054 00055 #include "TransformNode.hh" 00056 00057 00058 //== IMPLEMENTATION ========================================================== 00059 00060 00061 namespace ACG { 00062 namespace SceneGraph { 00063 00064 00065 //---------------------------------------------------------------------------- 00066 00067 00068 TransformNode:: 00069 TransformNode(BaseNode* _parent, const std::string& _name) 00070 : BaseNode(_parent,_name), 00071 center_(0.0, 0.0, 0.0), 00072 applyTransformation_(true) 00073 { 00074 loadIdentity(); 00075 } 00076 00077 00078 //---------------------------------------------------------------------------- 00079 00080 00081 void 00082 TransformNode:: 00083 loadIdentity() 00084 { 00085 matrix_.identity(); 00086 inverse_matrix_.identity(); 00087 00088 rotation_matrix_.identity(); 00089 inverse_rotation_matrix_.identity(); 00090 quaternion_.identity(); 00091 00092 scale_matrix_.identity(); 00093 inverse_scale_matrix_.identity(); 00094 00095 translation_ = Vec3d(0.0, 0.0, 0.0); 00096 } 00097 00098 00099 //---------------------------------------------------------------------------- 00100 00101 00102 void 00103 TransformNode:: 00104 setIdentity() 00105 { 00106 Vec3d v3 = matrix_.transform_point(center()); 00107 set_center(v3); 00108 loadIdentity(); 00109 } 00110 00111 00112 //---------------------------------------------------------------------------- 00113 00114 00115 void 00116 TransformNode:: 00117 translate(const Vec3d& _v) 00118 { 00119 translation_ += _v; 00120 updateMatrix(); 00121 } 00122 00123 00124 //---------------------------------------------------------------------------- 00125 00126 00127 void 00128 TransformNode:: 00129 rotate(double _angle, const Vec3d& _axis) 00130 { 00131 quaternion_ *= Quaterniond(_axis, _angle/180.0*M_PI); 00132 quaternion_.normalize(); 00133 rotation_matrix_ = quaternion_.rotation_matrix(); 00134 inverse_rotation_matrix_ = rotation_matrix_; 00135 inverse_rotation_matrix_.transpose(); 00136 updateMatrix(); 00137 } 00138 00139 00140 //---------------------------------------------------------------------------- 00141 00142 00143 void 00144 TransformNode:: 00145 scale(const Vec3d& _s) 00146 { 00147 scale_matrix_.scale(_s); 00148 inverse_scale_matrix_ = scale_matrix_; 00149 inverse_scale_matrix_.invert(); 00150 updateMatrix(); 00151 } 00152 00153 //---------------------------------------------------------------------------- 00154 00155 00156 void 00157 TransformNode:: 00158 scale(const GLMatrixd& _m) 00159 { 00160 scale_matrix_ *= _m; 00161 inverse_scale_matrix_ = scale_matrix_; 00162 inverse_scale_matrix_.invert(); 00163 updateMatrix(); 00164 } 00165 00166 00167 //---------------------------------------------------------------------------- 00168 00169 00170 void 00171 TransformNode:: 00172 updateMatrix() 00173 { 00174 // build matrix 00175 matrix_.identity(); 00176 matrix_.translate(translation_); 00177 matrix_.translate(center_); 00178 matrix_ *= rotation_matrix_; 00179 matrix_ *= scale_matrix_; 00180 matrix_.translate(-center_); 00181 00182 00183 // build inverse matrix 00184 inverse_matrix_ = matrix_; 00185 inverse_matrix_.invert(); 00186 } 00187 00188 00189 //---------------------------------------------------------------------------- 00190 00191 00192 void 00193 TransformNode:: 00194 enter(GLState& _state, unsigned int /* _drawmode */ ) 00195 { 00196 _state.push_modelview_matrix(); 00197 00198 if ( applyTransformation_ ) 00199 _state.mult_matrix(matrix_, inverse_matrix_); 00200 } 00201 00202 00203 //---------------------------------------------------------------------------- 00204 00205 00206 void 00207 TransformNode:: 00208 leave(GLState& _state, unsigned int /* _drawmode */ ) 00209 { 00210 _state.pop_modelview_matrix(); 00211 } 00212 00213 00214 //============================================================================= 00215 } // namespace SceneGraph 00216 } // namespace ACG 00217 //=============================================================================