00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef ANIMATION_KEY_FRAMER
00040 #define ANIMATION_KEY_FRAMER
00041
00042
00043
00044 #include <map>
00045 #include <assert.h>
00046 #include <gmtl/Vec.h>
00047 #include <gmtl/VecOps.h>
00048 #include <gmtl/Quat.h>
00049 #include <gmtl/QuatOps.h>
00050 #include <gmtl/Matrix.h>
00051 #include <gmtl/MatrixOps.h>
00052
00053 namespace ani
00054 {
00055
00056
00057
00058
00059
00060
00061 template <class _keyType>
00062 class KeyFramer
00063 {
00064 public:
00065
00066 KeyFramer()
00067 {
00068
00069 this->clear();
00070 this->stop();
00071 }
00072
00073
00074
00075
00076 void play()
00077 {
00078 mIsPlaying = true;
00079 mReverse = false;
00080 }
00081
00082
00083
00084
00085 void rplay()
00086 {
00087 mIsPlaying = true;
00088 mReverse = true;
00089 }
00090
00091
00092 void pause()
00093 {
00094 mIsPlaying = false;
00095 }
00096
00097
00098 void rewind()
00099 {
00100 mCurrentTime = this->begintime();
00101 mFutureTime = this->begintime();
00102 }
00103
00104
00105 void fastforward()
00106 {
00107 mCurrentTime = this->length();
00108 mFutureTime = this->length();
00109 }
00110
00111
00112 void stop()
00113 {
00114 mIsPlaying = false;
00115 mCurrentTime = this->begintime();
00116 mFutureTime = this->begintime();
00117 mLoopsLeft = mLoops;
00118 }
00119
00120 void clear()
00121 {
00122 mKeys.clear();
00123 mPlayFactor = 1.0f;
00124 mLoops = 1;
00125 mCurrentTime = this->begintime();
00126 mFutureTime = this->begintime();
00127 mLoopsLeft = 1;
00128 }
00129
00130
00131
00132
00133
00134 void step( float timeOfKey )
00135 {
00136 if (mIsPlaying)
00137 {
00138 mCurrentTime = mFutureTime;
00139
00140
00141 if (mCurrentTime > this->length() || mCurrentTime < 0)
00142 {
00143
00144 if (mLoopsLeft > 0)
00145 --mLoopsLeft;
00146
00147
00148 if (mLoopsLeft != 0)
00149 {
00150 if (mReverse == false)
00151 this->rewind();
00152 else
00153 this->fastforward();
00154 }
00155
00156 else
00157 {
00158 this->stop();
00159 return;
00160 }
00161 }
00162
00163 this->jump( mCurrentTime );
00164
00165
00166
00167 if (mReverse == false)
00168 mFutureTime += timeOfKey * mPlayFactor;
00169 else
00170 mFutureTime -= timeOfKey * mPlayFactor;
00171 }
00172 }
00173
00174
00175
00176
00177
00178
00179 void setSpeed( float factor = 1.0f )
00180 {
00181 assert( factor > 0.0f && "use reverseplay() and play() to change direction (if factor < 0), use stop() to stop (if factor == 0)" );
00182 mPlayFactor = factor;
00183 }
00184
00185
00186
00187
00188
00189
00190 void setLoops( int numTimesToLoop = 1 )
00191 {
00192 mLoops = numTimesToLoop;
00193 mLoopsLeft = mLoops;
00194 }
00195
00196
00197 void addkey( const _keyType& key )
00198 {
00199 mKeys[key.time()] = key;
00200 }
00201
00202
00203 public:
00204
00205
00206 std::map<float, _keyType>& keys() { return mKeys; }
00207
00208
00209
00210 const std::map<float, _keyType>& keys() const { return mKeys; }
00211
00212
00213 float begintime() const
00214 {
00215 return (*(mKeys.begin())).first;
00216 }
00217
00218
00219 float endtime() const
00220 {
00221 return (*(mKeys.rbegin())).first;
00222 }
00223
00224
00225 float length()
00226 {
00227 if (mKeys.size() == 0)
00228 return 0.0f;
00229
00230 return this->endtime() - this->begintime();
00231 }
00232
00233 bool isPlaying() { return mIsPlaying; }
00234
00235
00236
00237
00238
00239 float speed() const
00240 {
00241 return mPlayFactor;
00242 }
00243
00244
00245
00246 int loops() { return mLoops; }
00247
00248
00249
00250 int loopsLeft() { return mLoopsLeft; }
00251
00252
00253 const _keyType& key() const { return mCurrentKey; }
00254
00255
00256 const gmtl::Quatf& rotation() const { return mCurrentKey.rotation(); }
00257
00258
00259 const gmtl::Vec3f& position() const { return mCurrentKey.position(); }
00260
00261
00262 const float& time() const
00263 {
00264
00265 return mCurrentKey.time();
00266 }
00267
00268 private:
00269
00270
00271
00272
00273
00274 public:
00275
00276
00277
00278
00279
00280
00281 void jump( float seekToTime )
00282 {
00283 if (mKeys.size() <= 0)
00284 return;
00285
00286 assert( seekToTime <= this->length() );
00287
00288
00289 std::map<float, _keyType>::iterator first, second;
00290 second = mKeys.lower_bound( seekToTime );
00291 first = second;
00292 --first;
00293 if (first == mKeys.end())
00294 {
00295 first = second;
00296 }
00297
00298
00299 float a = (*first).first, b = (*second).first;
00300 if (seekToTime < a || seekToTime > b)
00301 {
00302 seekToTime = (*first).first;
00303 }
00304
00305
00306 const _keyType& key_one = (*first).second; assert( key_one.time() == (*first).first && "not good" );
00307 const _keyType& key_two = (*second).second; assert( key_two.time() == (*second).first && "not good" );
00308 mCurrentKey.setInterpolate( seekToTime, key_one, key_two );
00309
00310
00311
00312
00313 mCurrentTime = seekToTime;
00314 }
00315
00316
00317 private:
00318
00319 std::map<float, _keyType> mKeys;
00320 bool mIsPlaying, mReverse;
00321
00322
00323 float mPlayFactor;
00324
00325
00326 int mLoops, mLoopsLeft;
00327
00328
00329 float mCurrentTime, mFutureTime;
00330
00331
00332 _keyType mCurrentKey;
00333 };
00334 };
00335
00336 #endif