llkeyframemotion.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /**
  2. * @file llkeyframemotion.h
  3. * @brief Implementation of LLKeframeMotion class.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLKEYFRAMEMOTION_H
  33. #define LL_LLKEYFRAMEMOTION_H
  34. #include "llassetstorage.h"
  35. #include "llbboxlocal.h"
  36. #include "llbvhconsts.h"
  37. #include "hbfastmap.h"
  38. #include "llhandmotion.h"
  39. #include "lljointstate.h"
  40. #include "llmotion.h"
  41. #include "llquaternion.h"
  42. #include "lluuid.h"
  43. #include "llvector3d.h"
  44. #include "llvector3.h"
  45. class LLKeyframeDataCache;
  46. class LLDataPacker;
  47. #define MIN_REQUIRED_PIXEL_AREA_KEYFRAME (40.f)
  48. #define MAX_CHAIN_LENGTH (4)
  49. constexpr S32 KEYFRAME_MOTION_VERSION = 1;
  50. constexpr S32 KEYFRAME_MOTION_SUBVERSION = 0;
  51. class LLKeyframeMotion : public LLMotion
  52. {
  53. friend class LLKeyframeDataCache;
  54. protected:
  55. LOG_CLASS(LLKeyframeMotion);
  56. public:
  57. LLKeyframeMotion(const LLUUID& id);
  58. ~LLKeyframeMotion() override;
  59. LL_INLINE LLKeyframeMotion* asKeyframeMotion() override
  60. {
  61. return this;
  62. }
  63. LL_INLINE static LLMotion* create(const LLUUID& id)
  64. {
  65. return new LLKeyframeMotion(id);
  66. }
  67. // Motions must specify whether or not they loop
  68. LL_INLINE bool getLoop() override
  69. {
  70. return mJointMotionList && mJointMotionList->mLoop;
  71. }
  72. // Motions must report their total duration
  73. LL_INLINE F32 getDuration() override
  74. {
  75. return mJointMotionList ? mJointMotionList->mDuration : 0.f;
  76. }
  77. // Motions must report their "ease in" duration
  78. LL_INLINE F32 getEaseInDuration() override
  79. {
  80. return mJointMotionList ? mJointMotionList->mEaseInDuration : 0.f;
  81. }
  82. // Motions must report their "ease out" duration.
  83. LL_INLINE F32 getEaseOutDuration() override
  84. {
  85. return mJointMotionList ? mJointMotionList->mEaseOutDuration : 0.f;
  86. }
  87. // Motions must report their priority
  88. LL_INLINE LLJoint::JointPriority getPriority() override
  89. {
  90. return mJointMotionList ? mJointMotionList->mBasePriority
  91. : LLJoint::LOW_PRIORITY;
  92. }
  93. LL_INLINE LLMotionBlendType getBlendType() override { return NORMAL_BLEND; }
  94. // Called to determine when a motion should be activated/deactivated based
  95. // on avatar pixel coverage
  96. LL_INLINE F32 getMinPixelArea() override { return MIN_REQUIRED_PIXEL_AREA_KEYFRAME; }
  97. // Run-time (post constructor) initialization, called after parameters
  98. // have been set; must return true to indicate success and be available
  99. // for activation
  100. LLMotionInitStatus onInitialize(LLCharacter* character) override;
  101. // Called when a motion is activated; must return true to indicate success,
  102. // or else it will be deactivated
  103. bool onActivate() override;
  104. // Called per time step; must return true while it is active, and must
  105. // return false when the motion is completed.
  106. bool onUpdate(F32 time, U8* joint_mask) override;
  107. // Called when a motion is deactivated
  108. void onDeactivate() override;
  109. void setStopTime(F32 time) override;
  110. LL_INLINE void setCharacter(LLCharacter* character) { mCharacter = character; }
  111. static void onLoadComplete(const LLUUID& asset_uuid, LLAssetType::EType,
  112. void* user_data, S32 status, LLExtStat);
  113. U32 getFileSize();
  114. bool serialize(LLDataPacker& dp) const;
  115. bool deserialize(LLDataPacker& dp, const LLUUID& asset_id,
  116. bool allow_invalid_joints = true);
  117. LL_INLINE bool isLoaded() { return mJointMotionList != NULL; }
  118. bool dumpToFile(const std::string& name);
  119. // setters for modifying a keyframe animation
  120. void setLoop(bool loop);
  121. LL_INLINE F32 getLoopIn()
  122. {
  123. return mJointMotionList ? mJointMotionList->mLoopInPoint : 0.f;
  124. }
  125. LL_INLINE F32 getLoopOut()
  126. {
  127. return mJointMotionList ? mJointMotionList->mLoopOutPoint : 0.f;
  128. }
  129. void setLoopIn(F32 in_point);
  130. void setLoopOut(F32 out_point);
  131. LL_INLINE void setHandPose(LLHandMotion::eHandPose pose)
  132. {
  133. if (mJointMotionList)
  134. {
  135. mJointMotionList->mHandPose = pose;
  136. }
  137. }
  138. LL_INLINE LLHandMotion::eHandPose getHandPose()
  139. {
  140. return mJointMotionList ? mJointMotionList->mHandPose
  141. : LLHandMotion::HAND_POSE_RELAXED;
  142. }
  143. void setPriority(S32 priority);
  144. void setEmote(const LLUUID& emote_id);
  145. void setEaseIn(F32 ease_in);
  146. void setEaseOut(F32 ease_in);
  147. LL_INLINE F32 getLastUpdateTime() { return mLastLoopedTime; }
  148. const LLBBoxLocal& getPelvisBBox();
  149. static void flushKeyframeCache();
  150. private:
  151. // Private helper functions to wrap some asserts
  152. LLPointer<LLJointState>& getJointState(U32 index);
  153. LLJoint* getJoint(U32 index);
  154. protected:
  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. mSourceConstraintVolId(-1),
  168. mTargetConstraintVolId(-1),
  169. mJointStateIndices(NULL)
  170. {
  171. }
  172. LL_INLINE ~JointConstraintSharedData() { delete[] mJointStateIndices; }
  173. public:
  174. S32 mSourceConstraintVolId;
  175. S32 mTargetConstraintVolId;
  176. LLVector3 mSourceConstraintOffset;
  177. LLVector3 mTargetConstraintOffset;
  178. LLVector3 mTargetConstraintDir;
  179. S32 mChainLength;
  180. S32* mJointStateIndices;
  181. F32 mEaseInStartTime;
  182. F32 mEaseInStopTime;
  183. F32 mEaseOutStartTime;
  184. F32 mEaseOutStopTime;
  185. EConstraintType mConstraintType;
  186. bool mUseTargetOffset;
  187. EConstraintTargetType mConstraintTargetType;
  188. };
  189. class JointConstraint
  190. {
  191. public:
  192. JointConstraint(JointConstraintSharedData* shared_data);
  193. public:
  194. JointConstraintSharedData* mSharedData;
  195. LLJoint* mSourceVolume;
  196. LLJoint* mTargetVolume;
  197. F32 mWeight;
  198. F32 mTotalLength;
  199. F32 mFixupDistanceRMS;
  200. F32 mJointLengths[MAX_CHAIN_LENGTH];
  201. F32 mJointLengthFractions[MAX_CHAIN_LENGTH];
  202. LLVector3 mPositions[MAX_CHAIN_LENGTH];
  203. LLVector3 mGroundNorm;
  204. LLVector3d mGroundPos;
  205. bool mActive;
  206. };
  207. void applyKeyframes(F32 time);
  208. void applyConstraints(F32 time, U8* joint_mask);
  209. void activateConstraint(JointConstraint* constraintp);
  210. void initializeConstraint(JointConstraint* constraint);
  211. void deactivateConstraint(JointConstraint *constraintp);
  212. void applyConstraint(JointConstraint* constraintp, F32 time,
  213. U8* joint_mask);
  214. bool setupPose();
  215. public:
  216. enum AssetStatus {
  217. ASSET_LOADED,
  218. ASSET_FETCHED,
  219. ASSET_NEEDS_FETCH,
  220. ASSET_FETCH_FAILED,
  221. ASSET_UNDEFINED
  222. };
  223. enum InterpolationType { IT_STEP, IT_LINEAR, IT_SPLINE };
  224. class ScaleKey
  225. {
  226. public:
  227. LL_INLINE ScaleKey() { mTime = 0.f; }
  228. LL_INLINE ScaleKey(F32 time, const LLVector3& scale)
  229. {
  230. mTime = time;
  231. mScale = scale;
  232. }
  233. public:
  234. F32 mTime;
  235. LLVector3 mScale;
  236. };
  237. class RotationKey
  238. {
  239. public:
  240. LL_INLINE RotationKey() { mTime = 0.f; }
  241. LL_INLINE RotationKey(F32 time, const LLQuaternion& rot)
  242. {
  243. mTime = time;
  244. mRotation = rot;
  245. }
  246. public:
  247. F32 mTime;
  248. LLQuaternion mRotation;
  249. };
  250. class PositionKey
  251. {
  252. public:
  253. LL_INLINE PositionKey() { mTime = 0.f; }
  254. LL_INLINE PositionKey(F32 time, const LLVector3& pos)
  255. {
  256. mTime = time;
  257. mPosition = pos;
  258. }
  259. public:
  260. F32 mTime;
  261. LLVector3 mPosition;
  262. };
  263. class ScaleCurve
  264. {
  265. protected:
  266. LOG_CLASS(LLKeyframeMotion::ScaleCurve);
  267. public:
  268. ScaleCurve();
  269. ~ScaleCurve();
  270. LLVector3 getValue(F32 time, F32 duration);
  271. LLVector3 interp(F32 u, ScaleKey& before, ScaleKey& after);
  272. public:
  273. InterpolationType mInterpolationType;
  274. S32 mNumKeys;
  275. typedef std::map<F32, ScaleKey> key_map_t;
  276. key_map_t mKeys;
  277. ScaleKey mLoopInKey;
  278. ScaleKey mLoopOutKey;
  279. };
  280. class RotationCurve
  281. {
  282. protected:
  283. LOG_CLASS(LLKeyframeMotion::RotationCurve);
  284. public:
  285. RotationCurve();
  286. ~RotationCurve();
  287. LLQuaternion getValue(F32 time, F32 duration);
  288. LLQuaternion interp(F32 u, RotationKey& before, RotationKey& after);
  289. public:
  290. InterpolationType mInterpolationType;
  291. S32 mNumKeys;
  292. typedef std::map<F32, RotationKey> key_map_t;
  293. key_map_t mKeys;
  294. RotationKey mLoopInKey;
  295. RotationKey mLoopOutKey;
  296. };
  297. class PositionCurve
  298. {
  299. protected:
  300. LOG_CLASS(LLKeyframeMotion::PositionCurve);
  301. public:
  302. PositionCurve();
  303. ~PositionCurve();
  304. LLVector3 getValue(F32 time, F32 duration);
  305. LLVector3 interp(F32 u, PositionKey& before, PositionKey& after);
  306. public:
  307. InterpolationType mInterpolationType;
  308. S32 mNumKeys;
  309. typedef std::map<F32, PositionKey> key_map_t;
  310. key_map_t mKeys;
  311. PositionKey mLoopInKey;
  312. PositionKey mLoopOutKey;
  313. };
  314. class JointMotion
  315. {
  316. public:
  317. void update(LLJointState* joint_state, F32 time, F32 duration);
  318. public:
  319. PositionCurve mPositionCurve;
  320. RotationCurve mRotationCurve;
  321. ScaleCurve mScaleCurve;
  322. U32 mJointKey;
  323. U32 mUsage;
  324. LLJoint::JointPriority mPriority;
  325. std::string mJointName;
  326. };
  327. class JointMotionList
  328. {
  329. protected:
  330. LOG_CLASS(LLKeyframeMotion::JointMotionList);
  331. public:
  332. JointMotionList();
  333. ~JointMotionList();
  334. U32 dumpDiagInfo();
  335. LL_INLINE JointMotion* getJointMotion(U32 index) const
  336. {
  337. return index < mJointMotionArray.size() ? mJointMotionArray[index]
  338. : NULL;
  339. }
  340. LL_INLINE U32 getNumJointMotions() const
  341. {
  342. return mJointMotionArray.size();
  343. }
  344. public:
  345. std::vector<JointMotion*> mJointMotionArray;
  346. F32 mDuration;
  347. F32 mLoopInPoint;
  348. F32 mLoopOutPoint;
  349. F32 mEaseInDuration;
  350. F32 mEaseOutDuration;
  351. LLJoint::JointPriority mBasePriority;
  352. LLHandMotion::eHandPose mHandPose;
  353. LLJoint::JointPriority mMaxPriority;
  354. LLBBoxLocal mPelvisBBox;
  355. typedef std::list<JointConstraintSharedData*> constraint_list_t;
  356. constraint_list_t mConstraints;
  357. LLUUID mEmoteID;
  358. // mEmoteName is a facial motion, but it's necessary to appear here so
  359. // that it's cached.
  360. // *TODO: LLKeyframeDataCache::getKeyframeData should probably return a
  361. // class containing JointMotionList and mEmoteName, see
  362. // LLKeyframeMotion::onInitialize().
  363. std::string mEmoteName;
  364. bool mLoop;
  365. };
  366. protected:
  367. JointMotionList* mJointMotionList;
  368. std::vector<LLPointer<LLJointState> > mJointStates;
  369. LLJoint* mPelvisp;
  370. LLCharacter* mCharacter;
  371. typedef std::list<JointConstraint*> constraint_list_t;
  372. constraint_list_t mConstraints;
  373. U32 mLastSkeletonSerialNum;
  374. F32 mLastUpdateTime;
  375. F32 mLastLoopedTime;
  376. AssetStatus mAssetStatus;
  377. static std::string sStaticAnimsDir;
  378. };
  379. // Purely static class
  380. class LLKeyframeDataCache
  381. {
  382. LLKeyframeDataCache() = delete;
  383. ~LLKeyframeDataCache() = delete;
  384. protected:
  385. LOG_CLASS(LLKeyframeDataCache);
  386. public:
  387. LL_INLINE static void addKeyframeData(const LLUUID& id,
  388. LLKeyframeMotion::JointMotionList* ml)
  389. {
  390. sKeyframeDataMap[id] = ml;
  391. }
  392. static LLKeyframeMotion::JointMotionList* getKeyframeData(const LLUUID& id);
  393. static void removeKeyframeData(const LLUUID& id);
  394. // Prints out diagnostic info
  395. static void dumpDiagInfo();
  396. static void clear();
  397. private:
  398. typedef fast_hmap<LLUUID, LLKeyframeMotion::JointMotionList*> data_map_t;
  399. static data_map_t sKeyframeDataMap;
  400. };
  401. #endif // LL_LLKEYFRAMEMOTION_H