/indra/llcharacter/llkeyframemotion.h

https://bitbucket.org/lindenlab/viewer-beta/ · C Header · 459 lines · 280 code · 82 blank · 97 comment · 12 complexity · 1ef14bf26f91500f9afe613e25057154 MD5 · raw file

  1. /**
  2. * @file llkeyframemotion.h
  3. * @brief Implementation of LLKeframeMotion class.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef LL_LLKEYFRAMEMOTION_H
  27. #define LL_LLKEYFRAMEMOTION_H
  28. //-----------------------------------------------------------------------------
  29. // Header files
  30. //-----------------------------------------------------------------------------
  31. #include <string>
  32. #include "llassetstorage.h"
  33. #include "llbboxlocal.h"
  34. #include "llhandmotion.h"
  35. #include "lljointstate.h"
  36. #include "llmotion.h"
  37. #include "llquaternion.h"
  38. #include "v3dmath.h"
  39. #include "v3math.h"
  40. #include "llbvhconsts.h"
  41. class LLKeyframeDataCache;
  42. class LLVFS;
  43. class LLDataPacker;
  44. #define MIN_REQUIRED_PIXEL_AREA_KEYFRAME (40.f)
  45. #define MAX_CHAIN_LENGTH (4)
  46. const S32 KEYFRAME_MOTION_VERSION = 1;
  47. const S32 KEYFRAME_MOTION_SUBVERSION = 0;
  48. //-----------------------------------------------------------------------------
  49. // class LLKeyframeMotion
  50. //-----------------------------------------------------------------------------
  51. class LLKeyframeMotion :
  52. public LLMotion
  53. {
  54. friend class LLKeyframeDataCache;
  55. public:
  56. // Constructor
  57. LLKeyframeMotion(const LLUUID &id);
  58. // Destructor
  59. virtual ~LLKeyframeMotion();
  60. private:
  61. // private helper functions to wrap some asserts
  62. LLPointer<LLJointState>& getJointState(U32 index);
  63. LLJoint* getJoint(U32 index );
  64. public:
  65. //-------------------------------------------------------------------------
  66. // functions to support MotionController and MotionRegistry
  67. //-------------------------------------------------------------------------
  68. // static constructor
  69. // all subclasses must implement such a function and register it
  70. static LLMotion *create(const LLUUID& id);
  71. public:
  72. //-------------------------------------------------------------------------
  73. // animation callbacks to be implemented by subclasses
  74. //-------------------------------------------------------------------------
  75. // motions must specify whether or not they loop
  76. virtual BOOL getLoop() {
  77. if (mJointMotionList) return mJointMotionList->mLoop;
  78. else return FALSE;
  79. }
  80. // motions must report their total duration
  81. virtual F32 getDuration() {
  82. if (mJointMotionList) return mJointMotionList->mDuration;
  83. else return 0.f;
  84. }
  85. // motions must report their "ease in" duration
  86. virtual F32 getEaseInDuration() {
  87. if (mJointMotionList) return mJointMotionList->mEaseInDuration;
  88. else return 0.f;
  89. }
  90. // motions must report their "ease out" duration.
  91. virtual F32 getEaseOutDuration() {
  92. if (mJointMotionList) return mJointMotionList->mEaseOutDuration;
  93. else return 0.f;
  94. }
  95. // motions must report their priority
  96. virtual LLJoint::JointPriority getPriority() {
  97. if (mJointMotionList) return mJointMotionList->mBasePriority;
  98. else return LLJoint::LOW_PRIORITY;
  99. }
  100. virtual LLMotionBlendType getBlendType() { return NORMAL_BLEND; }
  101. // called to determine when a motion should be activated/deactivated based on avatar pixel coverage
  102. virtual F32 getMinPixelArea() { return MIN_REQUIRED_PIXEL_AREA_KEYFRAME; }
  103. // run-time (post constructor) initialization,
  104. // called after parameters have been set
  105. // must return true to indicate success and be available for activation
  106. virtual LLMotionInitStatus onInitialize(LLCharacter *character);
  107. // called when a motion is activated
  108. // must return TRUE to indicate success, or else
  109. // it will be deactivated
  110. virtual BOOL onActivate();
  111. // called per time step
  112. // must return TRUE while it is active, and
  113. // must return FALSE when the motion is completed.
  114. virtual BOOL onUpdate(F32 time, U8* joint_mask);
  115. // called when a motion is deactivated
  116. virtual void onDeactivate();
  117. virtual void setStopTime(F32 time);
  118. static void setVFS(LLVFS* vfs) { sVFS = vfs; }
  119. static void onLoadComplete(LLVFS *vfs,
  120. const LLUUID& asset_uuid,
  121. LLAssetType::EType type,
  122. void* user_data, S32 status, LLExtStat ext_status);
  123. public:
  124. U32 getFileSize();
  125. BOOL serialize(LLDataPacker& dp) const;
  126. BOOL deserialize(LLDataPacker& dp);
  127. BOOL isLoaded() { return mJointMotionList != NULL; }
  128. // setters for modifying a keyframe animation
  129. void setLoop(BOOL loop);
  130. F32 getLoopIn() {
  131. return (mJointMotionList) ? mJointMotionList->mLoopInPoint : 0.f;
  132. }
  133. F32 getLoopOut() {
  134. return (mJointMotionList) ? mJointMotionList->mLoopOutPoint : 0.f;
  135. }
  136. void setLoopIn(F32 in_point);
  137. void setLoopOut(F32 out_point);
  138. void setHandPose(LLHandMotion::eHandPose pose) {
  139. if (mJointMotionList) mJointMotionList->mHandPose = pose;
  140. }
  141. LLHandMotion::eHandPose getHandPose() {
  142. return (mJointMotionList) ? mJointMotionList->mHandPose : LLHandMotion::HAND_POSE_RELAXED;
  143. }
  144. void setPriority(S32 priority);
  145. void setEmote(const LLUUID& emote_id);
  146. void setEaseIn(F32 ease_in);
  147. void setEaseOut(F32 ease_in);
  148. F32 getLastUpdateTime() { return mLastLoopedTime; }
  149. const LLBBoxLocal& getPelvisBBox();
  150. static void flushKeyframeCache();
  151. protected:
  152. //-------------------------------------------------------------------------
  153. // JointConstraintSharedData
  154. //-------------------------------------------------------------------------
  155. class JointConstraintSharedData
  156. {
  157. public:
  158. JointConstraintSharedData() :
  159. mChainLength(0),
  160. mEaseInStartTime(0.f),
  161. mEaseInStopTime(0.f),
  162. mEaseOutStartTime(0.f),
  163. mEaseOutStopTime(0.f),
  164. mUseTargetOffset(FALSE),
  165. mConstraintType(CONSTRAINT_TYPE_POINT),
  166. mConstraintTargetType(CONSTRAINT_TARGET_TYPE_BODY),
  167. mSourceConstraintVolume(0),
  168. mTargetConstraintVolume(0),
  169. mJointStateIndices(NULL)
  170. { };
  171. ~JointConstraintSharedData() { delete [] mJointStateIndices; }
  172. S32 mSourceConstraintVolume;
  173. LLVector3 mSourceConstraintOffset;
  174. S32 mTargetConstraintVolume;
  175. LLVector3 mTargetConstraintOffset;
  176. LLVector3 mTargetConstraintDir;
  177. S32 mChainLength;
  178. S32* mJointStateIndices;
  179. F32 mEaseInStartTime;
  180. F32 mEaseInStopTime;
  181. F32 mEaseOutStartTime;
  182. F32 mEaseOutStopTime;
  183. BOOL mUseTargetOffset;
  184. EConstraintType mConstraintType;
  185. EConstraintTargetType mConstraintTargetType;
  186. };
  187. //-----------------------------------------------------------------------------
  188. // JointConstraint()
  189. //-----------------------------------------------------------------------------
  190. class JointConstraint
  191. {
  192. public:
  193. JointConstraint(JointConstraintSharedData* shared_data);
  194. ~JointConstraint();
  195. JointConstraintSharedData* mSharedData;
  196. F32 mWeight;
  197. F32 mTotalLength;
  198. LLVector3 mPositions[MAX_CHAIN_LENGTH];
  199. F32 mJointLengths[MAX_CHAIN_LENGTH];
  200. F32 mJointLengthFractions[MAX_CHAIN_LENGTH];
  201. BOOL mActive;
  202. LLVector3d mGroundPos;
  203. LLVector3 mGroundNorm;
  204. LLJoint* mSourceVolume;
  205. LLJoint* mTargetVolume;
  206. F32 mFixupDistanceRMS;
  207. };
  208. void applyKeyframes(F32 time);
  209. void applyConstraints(F32 time, U8* joint_mask);
  210. void activateConstraint(JointConstraint* constraintp);
  211. void initializeConstraint(JointConstraint* constraint);
  212. void deactivateConstraint(JointConstraint *constraintp);
  213. void applyConstraint(JointConstraint* constraintp, F32 time, U8* joint_mask);
  214. BOOL setupPose();
  215. public:
  216. enum AssetStatus { ASSET_LOADED, ASSET_FETCHED, ASSET_NEEDS_FETCH, ASSET_FETCH_FAILED, ASSET_UNDEFINED };
  217. enum InterpolationType { IT_STEP, IT_LINEAR, IT_SPLINE };
  218. //-------------------------------------------------------------------------
  219. // ScaleKey
  220. //-------------------------------------------------------------------------
  221. class ScaleKey
  222. {
  223. public:
  224. ScaleKey() { mTime = 0.0f; }
  225. ScaleKey(F32 time, const LLVector3 &scale) { mTime = time; mScale = scale; }
  226. F32 mTime;
  227. LLVector3 mScale;
  228. };
  229. //-------------------------------------------------------------------------
  230. // RotationKey
  231. //-------------------------------------------------------------------------
  232. class RotationKey
  233. {
  234. public:
  235. RotationKey() { mTime = 0.0f; }
  236. RotationKey(F32 time, const LLQuaternion &rotation) { mTime = time; mRotation = rotation; }
  237. F32 mTime;
  238. LLQuaternion mRotation;
  239. };
  240. //-------------------------------------------------------------------------
  241. // PositionKey
  242. //-------------------------------------------------------------------------
  243. class PositionKey
  244. {
  245. public:
  246. PositionKey() { mTime = 0.0f; }
  247. PositionKey(F32 time, const LLVector3 &position) { mTime = time; mPosition = position; }
  248. F32 mTime;
  249. LLVector3 mPosition;
  250. };
  251. //-------------------------------------------------------------------------
  252. // ScaleCurve
  253. //-------------------------------------------------------------------------
  254. class ScaleCurve
  255. {
  256. public:
  257. ScaleCurve();
  258. ~ScaleCurve();
  259. LLVector3 getValue(F32 time, F32 duration);
  260. LLVector3 interp(F32 u, ScaleKey& before, ScaleKey& after);
  261. InterpolationType mInterpolationType;
  262. S32 mNumKeys;
  263. typedef std::map<F32, ScaleKey> key_map_t;
  264. key_map_t mKeys;
  265. ScaleKey mLoopInKey;
  266. ScaleKey mLoopOutKey;
  267. };
  268. //-------------------------------------------------------------------------
  269. // RotationCurve
  270. //-------------------------------------------------------------------------
  271. class RotationCurve
  272. {
  273. public:
  274. RotationCurve();
  275. ~RotationCurve();
  276. LLQuaternion getValue(F32 time, F32 duration);
  277. LLQuaternion interp(F32 u, RotationKey& before, RotationKey& after);
  278. InterpolationType mInterpolationType;
  279. S32 mNumKeys;
  280. typedef std::map<F32, RotationKey> key_map_t;
  281. key_map_t mKeys;
  282. RotationKey mLoopInKey;
  283. RotationKey mLoopOutKey;
  284. };
  285. //-------------------------------------------------------------------------
  286. // PositionCurve
  287. //-------------------------------------------------------------------------
  288. class PositionCurve
  289. {
  290. public:
  291. PositionCurve();
  292. ~PositionCurve();
  293. LLVector3 getValue(F32 time, F32 duration);
  294. LLVector3 interp(F32 u, PositionKey& before, PositionKey& after);
  295. InterpolationType mInterpolationType;
  296. S32 mNumKeys;
  297. typedef std::map<F32, PositionKey> key_map_t;
  298. key_map_t mKeys;
  299. PositionKey mLoopInKey;
  300. PositionKey mLoopOutKey;
  301. };
  302. //-------------------------------------------------------------------------
  303. // JointMotion
  304. //-------------------------------------------------------------------------
  305. class JointMotion
  306. {
  307. public:
  308. PositionCurve mPositionCurve;
  309. RotationCurve mRotationCurve;
  310. ScaleCurve mScaleCurve;
  311. std::string mJointName;
  312. U32 mUsage;
  313. LLJoint::JointPriority mPriority;
  314. void update(LLJointState* joint_state, F32 time, F32 duration);
  315. };
  316. //-------------------------------------------------------------------------
  317. // JointMotionList
  318. //-------------------------------------------------------------------------
  319. class JointMotionList
  320. {
  321. public:
  322. std::vector<JointMotion*> mJointMotionArray;
  323. F32 mDuration;
  324. BOOL mLoop;
  325. F32 mLoopInPoint;
  326. F32 mLoopOutPoint;
  327. F32 mEaseInDuration;
  328. F32 mEaseOutDuration;
  329. LLJoint::JointPriority mBasePriority;
  330. LLHandMotion::eHandPose mHandPose;
  331. LLJoint::JointPriority mMaxPriority;
  332. typedef std::list<JointConstraintSharedData*> constraint_list_t;
  333. constraint_list_t mConstraints;
  334. LLBBoxLocal mPelvisBBox;
  335. // mEmoteName is a facial motion, but it's necessary to appear here so that it's cached.
  336. // TODO: LLKeyframeDataCache::getKeyframeData should probably return a class containing
  337. // JointMotionList and mEmoteName, see LLKeyframeMotion::onInitialize.
  338. std::string mEmoteName;
  339. public:
  340. JointMotionList();
  341. ~JointMotionList();
  342. U32 dumpDiagInfo();
  343. JointMotion* getJointMotion(U32 index) const { llassert(index < mJointMotionArray.size()); return mJointMotionArray[index]; }
  344. U32 getNumJointMotions() const { return mJointMotionArray.size(); }
  345. };
  346. protected:
  347. static LLVFS* sVFS;
  348. //-------------------------------------------------------------------------
  349. // Member Data
  350. //-------------------------------------------------------------------------
  351. JointMotionList* mJointMotionList;
  352. std::vector<LLPointer<LLJointState> > mJointStates;
  353. LLJoint* mPelvisp;
  354. LLCharacter* mCharacter;
  355. typedef std::list<JointConstraint*> constraint_list_t;
  356. constraint_list_t mConstraints;
  357. U32 mLastSkeletonSerialNum;
  358. F32 mLastUpdateTime;
  359. F32 mLastLoopedTime;
  360. AssetStatus mAssetStatus;
  361. };
  362. class LLKeyframeDataCache
  363. {
  364. public:
  365. // *FIX: implement this as an actual singleton member of LLKeyframeMotion
  366. LLKeyframeDataCache(){};
  367. ~LLKeyframeDataCache();
  368. typedef std::map<LLUUID, class LLKeyframeMotion::JointMotionList*> keyframe_data_map_t;
  369. static keyframe_data_map_t sKeyframeDataMap;
  370. static void addKeyframeData(const LLUUID& id, LLKeyframeMotion::JointMotionList*);
  371. static LLKeyframeMotion::JointMotionList* getKeyframeData(const LLUUID& id);
  372. static void removeKeyframeData(const LLUUID& id);
  373. //print out diagnostic info
  374. static void dumpDiagInfo();
  375. static void clear();
  376. };
  377. #endif // LL_LLKEYFRAMEMOTION_H