Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

LWInstance.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: LWInstance.cpp,v 1.1 2002/02/16 12:41:39 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/TeddyConfig.h"
00026 #if defined( TEDDY_INCLUDE_LW_SCENE )
00027 
00028 
00029 #include "Teddy/Imports/LWInstance.h"
00030 #include "Teddy/Imports/LWModel.h"
00031 #include "Teddy/Imports/LWMotion.h"
00032 #include "Teddy/Imports/LWChannelEnvelope.h"
00033 #include "Teddy/Maths/Quaternion.h"
00034 #include "Teddy/SysSupport/Messages.h"
00035 using namespace Teddy::SysSupport;
00036 using namespace Teddy::Maths;
00037 
00038 
00039 namespace Teddy   {
00040 namespace Imports {
00041 
00042 
00043 LWInstance::LWInstance(){
00044     this->motion           = NULL;
00045     this->next_bone        = 0;
00046     this->parent_object    = NULL;
00047     this->parent_object_id = 0xffffffff;
00048     this->last_po_time     = -666.666f;
00049     this->last_ro_time     = -666.666f;
00050     this->last_pi_time     = -666.666f;
00051     this->last_position    = Vector(0,0,0);
00052     this->last_rotation    = Matrix::IDENTITY;
00053     this->last_pivot       = Vector(0,0,0);
00054     this->pivot_position   = Vector(0,0,0);
00055 
00056 }
00057 
00058 
00059 void LWInstance::setParentObject( LWInstance *parent ){
00060     this->parent_object = parent;
00061 }
00062 
00063 
00064 void LWInstance::add( LWBone *bone ){
00065     bones.insert( make_pair(next_bone,bone) );
00066     next_bone++;
00067 }
00068 
00069 
00070 LWBone *LWInstance::getBone( int bone_num ){
00071     LWBone                  *bone = NULL;
00072     int_to_LWBone::iterator  b_it = bones.find( bone_num );
00073     if( b_it != bones.end() ){
00074         bone = (*b_it).second;
00075     }
00076     return bone;
00077 }
00078 
00079 
00080 void LWInstance::setModel( Model *m ){
00081     this->model = m;
00082 }
00083 
00084 
00085 Model *LWInstance::getModel(){
00086     return this->model;
00087 }
00088 
00089 
00090 void LWInstance::setModelName( const std::string &model_name ){
00091     this->model_name = model_name;
00092 }
00093 
00094 
00095 std::string &LWInstance::getModelName(){
00096     return this->model_name;
00097 }
00098 
00099 
00100 LWMotion *LWInstance::getMotion(){
00101     return this->motion;
00102 }
00103 
00104 
00105 void LWInstance::setMotion( LWMotion *motion ){
00106     this->motion = motion;
00107 }
00108 
00109 void LWInstance::setVisibility( int visibility ){
00110     this->visibility = visibility;
00111 }
00112 
00113 
00114 void LWInstance::setColor( int color ){
00115     this->color = color;
00116 }
00117 
00118 
00119 void LWInstance::setParentObjectId( unsigned long parent_object_id ){
00120     this->parent_object_id = parent_object_id;
00121 }
00122 
00123 
00124 unsigned long LWInstance::getParentObjectId(){
00125     return parent_object_id;
00126 }
00127 
00128 
00129 void LWInstance::setTime( float time ){
00130     Vector position = evalPosition( time );
00131     
00132 //  position[0] = -position[0];
00133     
00134     model->setPosition( position );
00135     model->getAttitude() = evalRotation( time );
00136 }
00137 
00138 
00139 void LWInstance::setPivotPosition( Vector pivot_position ){
00140     this->pivot_position = pivot_position;
00141 }
00142 
00143 
00144 Vector &LWInstance::evalPosition( float time ){
00145     if( time == last_po_time ){
00146         return last_position;
00147     }
00148     last_po_time = time;
00149 
00150     //  Evaluate
00151     LWChannelEnvelope *x_env = motion->getChannel( LW_CHANNEL_X );
00152     LWChannelEnvelope *y_env = motion->getChannel( LW_CHANNEL_Y );
00153     LWChannelEnvelope *z_env = motion->getChannel( LW_CHANNEL_Z );
00154 
00155     double x = ( x_env != NULL ) ? x = x_env->eval( time ) : 0.0;
00156     double y = ( y_env != NULL ) ? y = y_env->eval( time ) : 0.0;
00157     double z = ( z_env != NULL ) ? z = z_env->eval( time ) : 0.0;
00158 
00159     last_position = Vector( x, y, z );
00160 
00161     if( parent_object != NULL ){
00162         last_position  = parent_object->evalRotation( time ) * last_position;
00163         last_position += parent_object->evalPosition( time );
00164     }
00165     last_position += evalPivot( time );
00166 
00167     return last_position;
00168 }
00169 
00170 
00171 Vector &LWInstance::evalPivot( float time ){
00172     if( time == last_pi_time ){
00173         return last_pivot;
00174     }
00175     last_pi_time = time;
00176 
00177     last_pivot = evalRotation( time ) * -pivot_position;
00178     return last_pivot;
00179 }
00180 
00181 
00182 Matrix &LWInstance::evalRotation( float time ){
00183     if( time == last_ro_time ){
00184         return last_rotation;
00185     }
00186     last_ro_time = time;
00187 
00188     //  Evaluate
00189     LWChannelEnvelope *h_env = motion->getChannel( LW_CHANNEL_H );
00190     LWChannelEnvelope *p_env = motion->getChannel( LW_CHANNEL_P );
00191     LWChannelEnvelope *b_env = motion->getChannel( LW_CHANNEL_B );
00192 
00193     double h = ( h_env != NULL ) ? h = h_env->eval( time ) : 0.0;
00194     double p = ( p_env != NULL ) ? p = p_env->eval( time ) : 0.0;
00195     double b = ( b_env != NULL ) ? b = b_env->eval( time ) : 0.0;
00196 
00197 #if 0
00198     Quaternion q  = Quaternion( Vector(0.0,1.0,0.0), h );
00199     q *=            Quaternion( Vector(1.0,0.0,0.0), p );
00200     q *=            Quaternion( Vector(0.0,0.0,1.0), b );
00201     last_rotation = q;
00202 #else
00203     last_rotation.rotateYMatrix(  h );
00204     last_rotation.rotateX      (  p );
00205     last_rotation.rotateZ      (  b );
00206 #endif
00207 
00208     if( parent_object != NULL ){
00209         last_rotation = parent_object->evalRotation( time ) * last_rotation;
00210     }
00211 
00212     return last_rotation;
00213 }
00214 
00215 
00216 };  //  namespace Imports
00217 };  //  namespace Teddy
00218 
00219 
00220 #endif  //  TEDDY_INCLUDE_LW_SCENE