llkeyframemotion.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_LLKEYFRAMEMOTION_H
00033 #define LL_LLKEYFRAMEMOTION_H
00034 
00035 //-----------------------------------------------------------------------------
00036 // Header files
00037 //-----------------------------------------------------------------------------
00038 
00039 #include <string>
00040 
00041 #include "llassetstorage.h"
00042 #include "llbboxlocal.h"
00043 #include "llhandmotion.h"
00044 #include "lljointstate.h"
00045 #include "llmotion.h"
00046 #include "llptrskipmap.h"
00047 #include "llquaternion.h"
00048 #include "v3dmath.h"
00049 #include "v3math.h"
00050 #include "llapr.h"
00051 
00052 class LLKeyframeDataCache;
00053 class LLVFS;
00054 class LLDataPacker;
00055 
00056 #define MIN_REQUIRED_PIXEL_AREA_KEYFRAME (40.f)
00057 #define MAX_CHAIN_LENGTH (4)
00058 
00059 const S32 KEYFRAME_MOTION_VERSION = 1;
00060 const S32 KEYFRAME_MOTION_SUBVERSION = 0;
00061 
00062 //-----------------------------------------------------------------------------
00063 // class LLKeyframeMotion
00064 //-----------------------------------------------------------------------------
00065 class LLKeyframeMotion :
00066         public LLMotion
00067 {
00068         friend class LLKeyframeDataCache;
00069 public:
00070         // Constructor
00071         LLKeyframeMotion(const LLUUID &id);
00072 
00073         // Destructor
00074         virtual ~LLKeyframeMotion();
00075 
00076 public:
00077         //-------------------------------------------------------------------------
00078         // functions to support MotionController and MotionRegistry
00079         //-------------------------------------------------------------------------
00080 
00081         // static constructor
00082         // all subclasses must implement such a function and register it
00083         static LLMotion *create(const LLUUID& id);
00084 
00085 public:
00086         //-------------------------------------------------------------------------
00087         // animation callbacks to be implemented by subclasses
00088         //-------------------------------------------------------------------------
00089 
00090         // motions must specify whether or not they loop
00091         virtual BOOL getLoop() { 
00092                 if (mJointMotionList) return mJointMotionList->mLoop; 
00093                 else return FALSE;
00094         }
00095 
00096         // motions must report their total duration
00097         virtual F32 getDuration() { 
00098                 if (mJointMotionList) return mJointMotionList->mDuration; 
00099                 else return 0.f;
00100         }
00101 
00102         // motions must report their "ease in" duration
00103         virtual F32 getEaseInDuration() { 
00104                 if (mJointMotionList) return mJointMotionList->mEaseInDuration; 
00105                 else return 0.f;
00106         }
00107 
00108         // motions must report their "ease out" duration.
00109         virtual F32 getEaseOutDuration() { 
00110                 if (mJointMotionList) return mJointMotionList->mEaseOutDuration; 
00111                 else return 0.f;
00112         }
00113 
00114         // motions must report their priority
00115         virtual LLJoint::JointPriority getPriority() { 
00116                 if (mJointMotionList) return mJointMotionList->mBasePriority; 
00117                 else return LLJoint::LOW_PRIORITY;
00118         }
00119 
00120         virtual LLMotionBlendType getBlendType() { return NORMAL_BLEND; }
00121 
00122         // called to determine when a motion should be activated/deactivated based on avatar pixel coverage
00123         virtual F32 getMinPixelArea() { return MIN_REQUIRED_PIXEL_AREA_KEYFRAME; }
00124 
00125         // run-time (post constructor) initialization,
00126         // called after parameters have been set
00127         // must return true to indicate success and be available for activation
00128         virtual LLMotionInitStatus onInitialize(LLCharacter *character);
00129 
00130         // called when a motion is activated
00131         // must return TRUE to indicate success, or else
00132         // it will be deactivated
00133         virtual BOOL onActivate();
00134 
00135         // called per time step
00136         // must return TRUE while it is active, and
00137         // must return FALSE when the motion is completed.
00138         virtual BOOL onUpdate(F32 time, U8* joint_mask);
00139 
00140         // called when a motion is deactivated
00141         virtual void onDeactivate();
00142 
00143         virtual void setStopTime(F32 time);
00144 
00145         static void setVFS(LLVFS* vfs) { sVFS = vfs; }
00146 
00147         static void onLoadComplete(LLVFS *vfs,
00148                                                            const LLUUID& asset_uuid,
00149                                                            LLAssetType::EType type,
00150                                                            void* user_data, S32 status, LLExtStat ext_status);
00151 
00152 public:
00153         U32             getFileSize();
00154         BOOL    serialize(LLDataPacker& dp) const;
00155         BOOL    deserialize(LLDataPacker& dp);
00156         void    writeCAL3D(apr_file_t* fp);
00157         BOOL    isLoaded() { return mJointMotionList != NULL; }
00158 
00159 
00160         // setters for modifying a keyframe animation
00161         void setLoop(BOOL loop);
00162 
00163         F32 getLoopIn() {
00164                 return (mJointMotionList) ? mJointMotionList->mLoopInPoint : 0.f;
00165         }
00166 
00167         F32 getLoopOut() {
00168                 return (mJointMotionList) ? mJointMotionList->mLoopOutPoint : 0.f;
00169         }
00170         
00171         void setLoopIn(F32 in_point);
00172 
00173         void setLoopOut(F32 out_point);
00174 
00175         void setHandPose(LLHandMotion::eHandPose pose) {
00176                 if (mJointMotionList) mJointMotionList->mHandPose = pose;
00177         }
00178 
00179         LLHandMotion::eHandPose getHandPose() { 
00180                 return (mJointMotionList) ? mJointMotionList->mHandPose : LLHandMotion::HAND_POSE_RELAXED;
00181         }
00182 
00183         void setPriority(S32 priority);
00184 
00185         void setEmote(const LLUUID& emote_id);
00186 
00187         void setEaseIn(F32 ease_in);
00188 
00189         void setEaseOut(F32 ease_in);
00190 
00191         F32 getLastUpdateTime() { return mLastLoopedTime; }
00192 
00193         const LLBBoxLocal& getPelvisBBox();
00194 
00195         static void flushKeyframeCache();
00196 
00197         typedef enum e_constraint_type
00198         {
00199                 TYPE_POINT,
00200                 TYPE_PLANE
00201         } EConstraintType;
00202 
00203         typedef enum e_constraint_target_type
00204         {
00205                 TYPE_BODY,
00206                 TYPE_GROUND
00207         } EConstraintTargetType;
00208 
00209 protected:
00210         //-------------------------------------------------------------------------
00211         // JointConstraintSharedData
00212         //-------------------------------------------------------------------------
00213         class JointConstraintSharedData
00214         {
00215         public:
00216                 JointConstraintSharedData() :
00217                         mChainLength(0),
00218                         mEaseInStartTime(0.f), 
00219                         mEaseInStopTime(0.f),
00220                         mEaseOutStartTime(0.f),
00221                         mEaseOutStopTime(0.f), 
00222                         mUseTargetOffset(FALSE),
00223                         mConstraintType(TYPE_POINT),
00224                         mConstraintTargetType(TYPE_BODY) {};
00225                 ~JointConstraintSharedData() { delete [] mJointStateIndices; }
00226 
00227                 S32                                             mSourceConstraintVolume;
00228                 LLVector3                               mSourceConstraintOffset;
00229                 S32                                             mTargetConstraintVolume;
00230                 LLVector3                               mTargetConstraintOffset;
00231                 LLVector3                               mTargetConstraintDir;
00232                 S32                                             mChainLength;
00233                 S32*                                    mJointStateIndices;
00234                 F32                                             mEaseInStartTime;
00235                 F32                                             mEaseInStopTime;
00236                 F32                                             mEaseOutStartTime;
00237                 F32                                             mEaseOutStopTime;
00238                 BOOL                                    mUseTargetOffset;
00239                 EConstraintType                 mConstraintType;
00240                 EConstraintTargetType   mConstraintTargetType;
00241         };
00242 
00243         //-----------------------------------------------------------------------------
00244         // JointConstraint()
00245         //-----------------------------------------------------------------------------
00246         class JointConstraint
00247         {
00248         public:
00249                 JointConstraint(JointConstraintSharedData* shared_data);
00250                 ~JointConstraint();
00251 
00252                 JointConstraintSharedData*      mSharedData;
00253                 F32                                                     mWeight;
00254                 F32                                                     mTotalLength;
00255                 LLVector3                                       mPositions[MAX_CHAIN_LENGTH];
00256                 F32                                                     mJointLengths[MAX_CHAIN_LENGTH];
00257                 F32                                                     mJointLengthFractions[MAX_CHAIN_LENGTH];
00258                 BOOL                                            mActive;
00259                 LLVector3d                                      mGroundPos;
00260                 LLVector3                                       mGroundNorm;
00261                 LLJoint*                                        mSourceVolume;
00262                 LLJoint*                                        mTargetVolume;
00263                 F32                                                     mFixupDistanceRMS;
00264         };
00265 
00266         void applyKeyframes(F32 time);
00267 
00268         void applyConstraints(F32 time, U8* joint_mask);
00269 
00270         void activateConstraint(JointConstraint* constraintp);
00271 
00272         void initializeConstraint(JointConstraint* constraint);
00273 
00274         void deactivateConstraint(JointConstraint *constraintp);
00275 
00276         void applyConstraint(JointConstraint* constraintp, F32 time, U8* joint_mask);
00277 
00278         BOOL    setupPose();
00279 
00280 public:
00281         enum AssetStatus { ASSET_LOADED, ASSET_FETCHED, ASSET_NEEDS_FETCH, ASSET_FETCH_FAILED, ASSET_UNDEFINED };
00282 
00283         enum InterpolationType { IT_STEP, IT_LINEAR, IT_SPLINE };
00284 
00285         //-------------------------------------------------------------------------
00286         // ScaleKey
00287         //-------------------------------------------------------------------------
00288         class ScaleKey
00289         {
00290         public:
00291                 ScaleKey() { mTime = 0.0f; }
00292                 ScaleKey(F32 time, const LLVector3 &scale) { mTime = time; mScale = scale; }
00293 
00294                 F32                     mTime;
00295                 LLVector3       mScale;
00296         };
00297 
00298         //-------------------------------------------------------------------------
00299         // RotationKey
00300         //-------------------------------------------------------------------------
00301         class RotationKey
00302         {
00303         public:
00304                 RotationKey() { mTime = 0.0f; }
00305                 RotationKey(F32 time, const LLQuaternion &rotation) { mTime = time; mRotation = rotation; }
00306 
00307                 F32                             mTime;
00308                 LLQuaternion    mRotation;
00309         };
00310 
00311         //-------------------------------------------------------------------------
00312         // PositionKey
00313         //-------------------------------------------------------------------------
00314         class PositionKey
00315         {
00316         public:
00317                 PositionKey() { mTime = 0.0f; }
00318                 PositionKey(F32 time, const LLVector3 &position) { mTime = time; mPosition = position; }
00319 
00320                 F32                     mTime;
00321                 LLVector3       mPosition;
00322         };
00323 
00324         //-------------------------------------------------------------------------
00325         // ScaleCurve
00326         //-------------------------------------------------------------------------
00327         class ScaleCurve
00328         {
00329         public:
00330                 ScaleCurve();
00331                 ~ScaleCurve();
00332                 LLVector3 getValue(F32 time, F32 duration);
00333                 LLVector3 interp(F32 u, ScaleKey& before, ScaleKey& after);
00334 
00335                 InterpolationType       mInterpolationType;
00336                 S32                                     mNumKeys;
00337                 LLPtrSkipMap<F32, ScaleKey*>                    mKeys;
00338                 ScaleKey                        mLoopInKey;
00339                 ScaleKey                        mLoopOutKey;
00340         };
00341 
00342         //-------------------------------------------------------------------------
00343         // RotationCurve
00344         //-------------------------------------------------------------------------
00345         class RotationCurve
00346         {
00347         public:
00348                 RotationCurve();
00349                 ~RotationCurve();
00350                 LLQuaternion getValue(F32 time, F32 duration);
00351                 LLQuaternion interp(F32 u, RotationKey& before, RotationKey& after);
00352 
00353                 InterpolationType       mInterpolationType;
00354                 S32                                     mNumKeys;
00355                 LLPtrSkipMap<F32, RotationKey*>         mKeys;
00356                 RotationKey             mLoopInKey;
00357                 RotationKey             mLoopOutKey;
00358         };
00359 
00360         //-------------------------------------------------------------------------
00361         // PositionCurve
00362         //-------------------------------------------------------------------------
00363         class PositionCurve
00364         {
00365         public:
00366                 PositionCurve();
00367                 ~PositionCurve();
00368                 LLVector3 getValue(F32 time, F32 duration);
00369                 LLVector3 interp(F32 u, PositionKey& before, PositionKey& after);
00370 
00371                 InterpolationType       mInterpolationType;
00372                 S32                                     mNumKeys;
00373                 LLPtrSkipMap<F32, PositionKey*>         mKeys;
00374                 PositionKey             mLoopInKey;
00375                 PositionKey             mLoopOutKey;
00376         };
00377 
00378         //-------------------------------------------------------------------------
00379         // JointMotion
00380         //-------------------------------------------------------------------------
00381         class JointMotion
00382         {
00383         public:
00384                 PositionCurve   mPositionCurve;
00385                 RotationCurve   mRotationCurve;
00386                 ScaleCurve              mScaleCurve;
00387                 std::string             mJointName;
00388                 U32                             mUsage;
00389                 LLJoint::JointPriority  mPriority;
00390 
00391                 void update(LLJointState *joint_state, F32 time, F32 duration);
00392         };
00393         
00394         //-------------------------------------------------------------------------
00395         // JointMotionList
00396         //-------------------------------------------------------------------------
00397         class JointMotionList
00398         {
00399         public:
00400                 U32                                             mNumJointMotions;
00401                 JointMotion*                    mJointMotionArray;
00402                 F32                                             mDuration;
00403                 BOOL                                    mLoop;
00404                 F32                                             mLoopInPoint;
00405                 F32                                             mLoopOutPoint;
00406                 F32                                             mEaseInDuration;
00407                 F32                                             mEaseOutDuration;
00408                 LLJoint::JointPriority  mBasePriority;
00409                 LLHandMotion::eHandPose mHandPose;
00410                 LLJoint::JointPriority  mMaxPriority;
00411                 typedef std::list<JointConstraintSharedData*> constraint_list_t;
00412                 constraint_list_t               mConstraints;
00413                 LLBBoxLocal                             mPelvisBBox;
00414         public:
00415                 JointMotionList();
00416                 ~JointMotionList();
00417                 U32 dumpDiagInfo();
00418         };
00419 
00420 
00421 protected:
00422         static LLVFS*                           sVFS;
00423 
00424         //-------------------------------------------------------------------------
00425         // Member Data
00426         //-------------------------------------------------------------------------
00427         JointMotionList*                                mJointMotionList;
00428         LLJointState*                                   mJointStates;
00429         LLJoint*                                                mPelvisp;
00430         LLCharacter*                                    mCharacter;
00431         std::string                                             mEmoteName;
00432         typedef std::list<JointConstraint*>     constraint_list_t;
00433         constraint_list_t                               mConstraints;
00434         U32                                                             mLastSkeletonSerialNum;
00435         F32                                                             mLastUpdateTime;
00436         F32                                                             mLastLoopedTime;
00437         AssetStatus                                             mAssetStatus;
00438 };
00439 
00440 class LLKeyframeDataCache
00441 {
00442 public:
00443         // *FIX: implement this as an actual singleton member of LLKeyframeMotion
00444         LLKeyframeDataCache(){};
00445         ~LLKeyframeDataCache();
00446 
00447         typedef std::map<LLUUID, class LLKeyframeMotion::JointMotionList*> keyframe_data_map_t; 
00448         static keyframe_data_map_t sKeyframeDataMap;
00449 
00450         static void addKeyframeData(const LLUUID& id, LLKeyframeMotion::JointMotionList*);
00451         static LLKeyframeMotion::JointMotionList* getKeyframeData(const LLUUID& id);
00452 
00453         static void removeKeyframeData(const LLUUID& id);
00454 
00455         //print out diagnostic info
00456         static void dumpDiagInfo();
00457         static void clear();
00458 };
00459 
00460 #endif // LL_LLKEYFRAMEMOTION_H
00461 
00462 

Generated on Thu Jul 1 06:08:48 2010 for Second Life Viewer by  doxygen 1.4.7