PageRenderTime 64ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcharacter/llkeyframemotion.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 2046 lines | 1475 code | 274 blank | 297 comment | 258 complexity | bcac424223eba63d80fae115706dd945 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llkeyframemotion.cpp
  3. * @brief Implementation of LLKeyframeMotion 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. //-----------------------------------------------------------------------------
  27. // Header Files
  28. //-----------------------------------------------------------------------------
  29. #include "linden_common.h"
  30. #include "llmath.h"
  31. #include "llanimationstates.h"
  32. #include "llassetstorage.h"
  33. #include "lldatapacker.h"
  34. #include "llcharacter.h"
  35. #include "llcriticaldamp.h"
  36. #include "lldir.h"
  37. #include "llendianswizzle.h"
  38. #include "llkeyframemotion.h"
  39. #include "llquantize.h"
  40. #include "llvfile.h"
  41. #include "m3math.h"
  42. #include "message.h"
  43. //-----------------------------------------------------------------------------
  44. // Static Definitions
  45. //-----------------------------------------------------------------------------
  46. LLVFS* LLKeyframeMotion::sVFS = NULL;
  47. LLKeyframeDataCache::keyframe_data_map_t LLKeyframeDataCache::sKeyframeDataMap;
  48. //-----------------------------------------------------------------------------
  49. // Globals
  50. //-----------------------------------------------------------------------------
  51. static F32 JOINT_LENGTH_K = 0.7f;
  52. static S32 MAX_ITERATIONS = 20;
  53. static S32 MIN_ITERATIONS = 1;
  54. static S32 MIN_ITERATION_COUNT = 2;
  55. static F32 MAX_PIXEL_AREA_CONSTRAINTS = 80000.f;
  56. static F32 MIN_PIXEL_AREA_CONSTRAINTS = 1000.f;
  57. static F32 MIN_ACCELERATION_SQUARED = 0.0005f * 0.0005f;
  58. static F32 MAX_CONSTRAINTS = 10;
  59. //-----------------------------------------------------------------------------
  60. // JointMotionList
  61. //-----------------------------------------------------------------------------
  62. LLKeyframeMotion::JointMotionList::JointMotionList()
  63. : mDuration(0.f),
  64. mLoop(FALSE),
  65. mLoopInPoint(0.f),
  66. mLoopOutPoint(0.f),
  67. mEaseInDuration(0.f),
  68. mEaseOutDuration(0.f),
  69. mBasePriority(LLJoint::LOW_PRIORITY),
  70. mHandPose(LLHandMotion::HAND_POSE_SPREAD),
  71. mMaxPriority(LLJoint::LOW_PRIORITY)
  72. {
  73. }
  74. LLKeyframeMotion::JointMotionList::~JointMotionList()
  75. {
  76. for_each(mConstraints.begin(), mConstraints.end(), DeletePointer());
  77. for_each(mJointMotionArray.begin(), mJointMotionArray.end(), DeletePointer());
  78. }
  79. U32 LLKeyframeMotion::JointMotionList::dumpDiagInfo()
  80. {
  81. S32 total_size = sizeof(JointMotionList);
  82. for (U32 i = 0; i < getNumJointMotions(); i++)
  83. {
  84. LLKeyframeMotion::JointMotion* joint_motion_p = mJointMotionArray[i];
  85. llinfos << "\tJoint " << joint_motion_p->mJointName << llendl;
  86. if (joint_motion_p->mUsage & LLJointState::SCALE)
  87. {
  88. llinfos << "\t" << joint_motion_p->mScaleCurve.mNumKeys << " scale keys at "
  89. << joint_motion_p->mScaleCurve.mNumKeys * sizeof(ScaleKey) << " bytes" << llendl;
  90. total_size += joint_motion_p->mScaleCurve.mNumKeys * sizeof(ScaleKey);
  91. }
  92. if (joint_motion_p->mUsage & LLJointState::ROT)
  93. {
  94. llinfos << "\t" << joint_motion_p->mRotationCurve.mNumKeys << " rotation keys at "
  95. << joint_motion_p->mRotationCurve.mNumKeys * sizeof(RotationKey) << " bytes" << llendl;
  96. total_size += joint_motion_p->mRotationCurve.mNumKeys * sizeof(RotationKey);
  97. }
  98. if (joint_motion_p->mUsage & LLJointState::POS)
  99. {
  100. llinfos << "\t" << joint_motion_p->mPositionCurve.mNumKeys << " position keys at "
  101. << joint_motion_p->mPositionCurve.mNumKeys * sizeof(PositionKey) << " bytes" << llendl;
  102. total_size += joint_motion_p->mPositionCurve.mNumKeys * sizeof(PositionKey);
  103. }
  104. }
  105. llinfos << "Size: " << total_size << " bytes" << llendl;
  106. return total_size;
  107. }
  108. //-----------------------------------------------------------------------------
  109. //-----------------------------------------------------------------------------
  110. // ****Curve classes
  111. //-----------------------------------------------------------------------------
  112. //-----------------------------------------------------------------------------
  113. //-----------------------------------------------------------------------------
  114. // ScaleCurve::ScaleCurve()
  115. //-----------------------------------------------------------------------------
  116. LLKeyframeMotion::ScaleCurve::ScaleCurve()
  117. {
  118. mInterpolationType = LLKeyframeMotion::IT_LINEAR;
  119. mNumKeys = 0;
  120. }
  121. //-----------------------------------------------------------------------------
  122. // ScaleCurve::~ScaleCurve()
  123. //-----------------------------------------------------------------------------
  124. LLKeyframeMotion::ScaleCurve::~ScaleCurve()
  125. {
  126. mKeys.clear();
  127. mNumKeys = 0;
  128. }
  129. //-----------------------------------------------------------------------------
  130. // getValue()
  131. //-----------------------------------------------------------------------------
  132. LLVector3 LLKeyframeMotion::ScaleCurve::getValue(F32 time, F32 duration)
  133. {
  134. LLVector3 value;
  135. if (mKeys.empty())
  136. {
  137. value.clearVec();
  138. return value;
  139. }
  140. key_map_t::iterator right = mKeys.lower_bound(time);
  141. if (right == mKeys.end())
  142. {
  143. // Past last key
  144. --right;
  145. value = right->second.mScale;
  146. }
  147. else if (right == mKeys.begin() || right->first == time)
  148. {
  149. // Before first key or exactly on a key
  150. value = right->second.mScale;
  151. }
  152. else
  153. {
  154. // Between two keys
  155. key_map_t::iterator left = right; --left;
  156. F32 index_before = left->first;
  157. F32 index_after = right->first;
  158. ScaleKey& scale_before = left->second;
  159. ScaleKey& scale_after = right->second;
  160. if (right == mKeys.end())
  161. {
  162. scale_after = mLoopInKey;
  163. index_after = duration;
  164. }
  165. F32 u = (time - index_before) / (index_after - index_before);
  166. value = interp(u, scale_before, scale_after);
  167. }
  168. return value;
  169. }
  170. //-----------------------------------------------------------------------------
  171. // interp()
  172. //-----------------------------------------------------------------------------
  173. LLVector3 LLKeyframeMotion::ScaleCurve::interp(F32 u, ScaleKey& before, ScaleKey& after)
  174. {
  175. switch (mInterpolationType)
  176. {
  177. case IT_STEP:
  178. return before.mScale;
  179. default:
  180. case IT_LINEAR:
  181. case IT_SPLINE:
  182. return lerp(before.mScale, after.mScale, u);
  183. }
  184. }
  185. //-----------------------------------------------------------------------------
  186. // RotationCurve::RotationCurve()
  187. //-----------------------------------------------------------------------------
  188. LLKeyframeMotion::RotationCurve::RotationCurve()
  189. {
  190. mInterpolationType = LLKeyframeMotion::IT_LINEAR;
  191. mNumKeys = 0;
  192. }
  193. //-----------------------------------------------------------------------------
  194. // RotationCurve::~RotationCurve()
  195. //-----------------------------------------------------------------------------
  196. LLKeyframeMotion::RotationCurve::~RotationCurve()
  197. {
  198. mKeys.clear();
  199. mNumKeys = 0;
  200. }
  201. //-----------------------------------------------------------------------------
  202. // RotationCurve::getValue()
  203. //-----------------------------------------------------------------------------
  204. LLQuaternion LLKeyframeMotion::RotationCurve::getValue(F32 time, F32 duration)
  205. {
  206. LLQuaternion value;
  207. if (mKeys.empty())
  208. {
  209. value = LLQuaternion::DEFAULT;
  210. return value;
  211. }
  212. key_map_t::iterator right = mKeys.lower_bound(time);
  213. if (right == mKeys.end())
  214. {
  215. // Past last key
  216. --right;
  217. value = right->second.mRotation;
  218. }
  219. else if (right == mKeys.begin() || right->first == time)
  220. {
  221. // Before first key or exactly on a key
  222. value = right->second.mRotation;
  223. }
  224. else
  225. {
  226. // Between two keys
  227. key_map_t::iterator left = right; --left;
  228. F32 index_before = left->first;
  229. F32 index_after = right->first;
  230. RotationKey& rot_before = left->second;
  231. RotationKey& rot_after = right->second;
  232. if (right == mKeys.end())
  233. {
  234. rot_after = mLoopInKey;
  235. index_after = duration;
  236. }
  237. F32 u = (time - index_before) / (index_after - index_before);
  238. value = interp(u, rot_before, rot_after);
  239. }
  240. return value;
  241. }
  242. //-----------------------------------------------------------------------------
  243. // interp()
  244. //-----------------------------------------------------------------------------
  245. LLQuaternion LLKeyframeMotion::RotationCurve::interp(F32 u, RotationKey& before, RotationKey& after)
  246. {
  247. switch (mInterpolationType)
  248. {
  249. case IT_STEP:
  250. return before.mRotation;
  251. default:
  252. case IT_LINEAR:
  253. case IT_SPLINE:
  254. return nlerp(u, before.mRotation, after.mRotation);
  255. }
  256. }
  257. //-----------------------------------------------------------------------------
  258. // PositionCurve::PositionCurve()
  259. //-----------------------------------------------------------------------------
  260. LLKeyframeMotion::PositionCurve::PositionCurve()
  261. {
  262. mInterpolationType = LLKeyframeMotion::IT_LINEAR;
  263. mNumKeys = 0;
  264. }
  265. //-----------------------------------------------------------------------------
  266. // PositionCurve::~PositionCurve()
  267. //-----------------------------------------------------------------------------
  268. LLKeyframeMotion::PositionCurve::~PositionCurve()
  269. {
  270. mKeys.clear();
  271. mNumKeys = 0;
  272. }
  273. //-----------------------------------------------------------------------------
  274. // PositionCurve::getValue()
  275. //-----------------------------------------------------------------------------
  276. LLVector3 LLKeyframeMotion::PositionCurve::getValue(F32 time, F32 duration)
  277. {
  278. LLVector3 value;
  279. if (mKeys.empty())
  280. {
  281. value.clearVec();
  282. return value;
  283. }
  284. key_map_t::iterator right = mKeys.lower_bound(time);
  285. if (right == mKeys.end())
  286. {
  287. // Past last key
  288. --right;
  289. value = right->second.mPosition;
  290. }
  291. else if (right == mKeys.begin() || right->first == time)
  292. {
  293. // Before first key or exactly on a key
  294. value = right->second.mPosition;
  295. }
  296. else
  297. {
  298. // Between two keys
  299. key_map_t::iterator left = right; --left;
  300. F32 index_before = left->first;
  301. F32 index_after = right->first;
  302. PositionKey& pos_before = left->second;
  303. PositionKey& pos_after = right->second;
  304. if (right == mKeys.end())
  305. {
  306. pos_after = mLoopInKey;
  307. index_after = duration;
  308. }
  309. F32 u = (time - index_before) / (index_after - index_before);
  310. value = interp(u, pos_before, pos_after);
  311. }
  312. llassert(value.isFinite());
  313. return value;
  314. }
  315. //-----------------------------------------------------------------------------
  316. // interp()
  317. //-----------------------------------------------------------------------------
  318. LLVector3 LLKeyframeMotion::PositionCurve::interp(F32 u, PositionKey& before, PositionKey& after)
  319. {
  320. switch (mInterpolationType)
  321. {
  322. case IT_STEP:
  323. return before.mPosition;
  324. default:
  325. case IT_LINEAR:
  326. case IT_SPLINE:
  327. return lerp(before.mPosition, after.mPosition, u);
  328. }
  329. }
  330. //-----------------------------------------------------------------------------
  331. //-----------------------------------------------------------------------------
  332. // JointMotion class
  333. //-----------------------------------------------------------------------------
  334. //-----------------------------------------------------------------------------
  335. //-----------------------------------------------------------------------------
  336. // JointMotion::update()
  337. //-----------------------------------------------------------------------------
  338. void LLKeyframeMotion::JointMotion::update(LLJointState* joint_state, F32 time, F32 duration)
  339. {
  340. // this value being 0 is the cause of https://jira.lindenlab.com/browse/SL-22678 but I haven't
  341. // managed to get a stack to see how it got here. Testing for 0 here will stop the crash.
  342. if ( joint_state == NULL )
  343. {
  344. return;
  345. }
  346. U32 usage = joint_state->getUsage();
  347. //-------------------------------------------------------------------------
  348. // update scale component of joint state
  349. //-------------------------------------------------------------------------
  350. if ((usage & LLJointState::SCALE) && mScaleCurve.mNumKeys)
  351. {
  352. joint_state->setScale( mScaleCurve.getValue( time, duration ) );
  353. }
  354. //-------------------------------------------------------------------------
  355. // update rotation component of joint state
  356. //-------------------------------------------------------------------------
  357. if ((usage & LLJointState::ROT) && mRotationCurve.mNumKeys)
  358. {
  359. joint_state->setRotation( mRotationCurve.getValue( time, duration ) );
  360. }
  361. //-------------------------------------------------------------------------
  362. // update position component of joint state
  363. //-------------------------------------------------------------------------
  364. if ((usage & LLJointState::POS) && mPositionCurve.mNumKeys)
  365. {
  366. joint_state->setPosition( mPositionCurve.getValue( time, duration ) );
  367. }
  368. }
  369. //-----------------------------------------------------------------------------
  370. //-----------------------------------------------------------------------------
  371. // LLKeyframeMotion class
  372. //-----------------------------------------------------------------------------
  373. //-----------------------------------------------------------------------------
  374. //-----------------------------------------------------------------------------
  375. // LLKeyframeMotion()
  376. // Class Constructor
  377. //-----------------------------------------------------------------------------
  378. LLKeyframeMotion::LLKeyframeMotion(const LLUUID &id)
  379. : LLMotion(id),
  380. mJointMotionList(NULL),
  381. mPelvisp(NULL),
  382. mLastSkeletonSerialNum(0),
  383. mLastUpdateTime(0.f),
  384. mLastLoopedTime(0.f),
  385. mAssetStatus(ASSET_UNDEFINED)
  386. {
  387. }
  388. //-----------------------------------------------------------------------------
  389. // ~LLKeyframeMotion()
  390. // Class Destructor
  391. //-----------------------------------------------------------------------------
  392. LLKeyframeMotion::~LLKeyframeMotion()
  393. {
  394. for_each(mConstraints.begin(), mConstraints.end(), DeletePointer());
  395. }
  396. //-----------------------------------------------------------------------------
  397. // create()
  398. //-----------------------------------------------------------------------------
  399. LLMotion *LLKeyframeMotion::create(const LLUUID &id)
  400. {
  401. return new LLKeyframeMotion(id);
  402. }
  403. //-----------------------------------------------------------------------------
  404. // getJointState()
  405. //-----------------------------------------------------------------------------
  406. LLPointer<LLJointState>& LLKeyframeMotion::getJointState(U32 index)
  407. {
  408. llassert_always (index < mJointStates.size());
  409. return mJointStates[index];
  410. }
  411. //-----------------------------------------------------------------------------
  412. // getJoint()
  413. //-----------------------------------------------------------------------------
  414. LLJoint* LLKeyframeMotion::getJoint(U32 index)
  415. {
  416. llassert_always (index < mJointStates.size());
  417. LLJoint* joint = mJointStates[index]->getJoint();
  418. //Commented out 06-28-11 by Aura.
  419. //llassert_always (joint);
  420. return joint;
  421. }
  422. //-----------------------------------------------------------------------------
  423. // LLKeyframeMotion::onInitialize(LLCharacter *character)
  424. //-----------------------------------------------------------------------------
  425. LLMotion::LLMotionInitStatus LLKeyframeMotion::onInitialize(LLCharacter *character)
  426. {
  427. mCharacter = character;
  428. LLUUID* character_id;
  429. // asset already loaded?
  430. switch(mAssetStatus)
  431. {
  432. case ASSET_NEEDS_FETCH:
  433. // request asset
  434. mAssetStatus = ASSET_FETCHED;
  435. character_id = new LLUUID(mCharacter->getID());
  436. gAssetStorage->getAssetData(mID,
  437. LLAssetType::AT_ANIMATION,
  438. onLoadComplete,
  439. (void *)character_id,
  440. FALSE);
  441. return STATUS_HOLD;
  442. case ASSET_FETCHED:
  443. return STATUS_HOLD;
  444. case ASSET_FETCH_FAILED:
  445. return STATUS_FAILURE;
  446. case ASSET_LOADED:
  447. return STATUS_SUCCESS;
  448. default:
  449. // we don't know what state the asset is in yet, so keep going
  450. // check keyframe cache first then static vfs then asset request
  451. break;
  452. }
  453. LLKeyframeMotion::JointMotionList* joint_motion_list = LLKeyframeDataCache::getKeyframeData(getID());
  454. if(joint_motion_list)
  455. {
  456. // motion already existed in cache, so grab it
  457. mJointMotionList = joint_motion_list;
  458. mJointStates.reserve(mJointMotionList->getNumJointMotions());
  459. // don't forget to allocate joint states
  460. // set up joint states to point to character joints
  461. for(U32 i = 0; i < mJointMotionList->getNumJointMotions(); i++)
  462. {
  463. JointMotion* joint_motion = mJointMotionList->getJointMotion(i);
  464. if (LLJoint *joint = mCharacter->getJoint(joint_motion->mJointName))
  465. {
  466. LLPointer<LLJointState> joint_state = new LLJointState;
  467. mJointStates.push_back(joint_state);
  468. joint_state->setJoint(joint);
  469. joint_state->setUsage(joint_motion->mUsage);
  470. joint_state->setPriority(joint_motion->mPriority);
  471. }
  472. else
  473. {
  474. // add dummy joint state with no associated joint
  475. mJointStates.push_back(new LLJointState);
  476. }
  477. }
  478. mAssetStatus = ASSET_LOADED;
  479. setupPose();
  480. return STATUS_SUCCESS;
  481. }
  482. //-------------------------------------------------------------------------
  483. // Load named file by concatenating the character prefix with the motion name.
  484. // Load data into a buffer to be parsed.
  485. //-------------------------------------------------------------------------
  486. U8 *anim_data;
  487. S32 anim_file_size;
  488. if (!sVFS)
  489. {
  490. llerrs << "Must call LLKeyframeMotion::setVFS() first before loading a keyframe file!" << llendl;
  491. }
  492. BOOL success = FALSE;
  493. LLVFile* anim_file = new LLVFile(sVFS, mID, LLAssetType::AT_ANIMATION);
  494. if (!anim_file || !anim_file->getSize())
  495. {
  496. delete anim_file;
  497. anim_file = NULL;
  498. // request asset over network on next call to load
  499. mAssetStatus = ASSET_NEEDS_FETCH;
  500. return STATUS_HOLD;
  501. }
  502. else
  503. {
  504. anim_file_size = anim_file->getSize();
  505. anim_data = new U8[anim_file_size];
  506. success = anim_file->read(anim_data, anim_file_size); /*Flawfinder: ignore*/
  507. delete anim_file;
  508. anim_file = NULL;
  509. }
  510. if (!success)
  511. {
  512. llwarns << "Can't open animation file " << mID << llendl;
  513. mAssetStatus = ASSET_FETCH_FAILED;
  514. return STATUS_FAILURE;
  515. }
  516. lldebugs << "Loading keyframe data for: " << getName() << ":" << getID() << " (" << anim_file_size << " bytes)" << llendl;
  517. LLDataPackerBinaryBuffer dp(anim_data, anim_file_size);
  518. if (!deserialize(dp))
  519. {
  520. llwarns << "Failed to decode asset for animation " << getName() << ":" << getID() << llendl;
  521. mAssetStatus = ASSET_FETCH_FAILED;
  522. return STATUS_FAILURE;
  523. }
  524. delete []anim_data;
  525. mAssetStatus = ASSET_LOADED;
  526. return STATUS_SUCCESS;
  527. }
  528. //-----------------------------------------------------------------------------
  529. // setupPose()
  530. //-----------------------------------------------------------------------------
  531. BOOL LLKeyframeMotion::setupPose()
  532. {
  533. // add all valid joint states to the pose
  534. for (U32 jm=0; jm<mJointMotionList->getNumJointMotions(); jm++)
  535. {
  536. LLPointer<LLJointState> joint_state = getJointState(jm);
  537. if ( joint_state->getJoint() )
  538. {
  539. addJointState( joint_state );
  540. }
  541. }
  542. // initialize joint constraints
  543. for (JointMotionList::constraint_list_t::iterator iter = mJointMotionList->mConstraints.begin();
  544. iter != mJointMotionList->mConstraints.end(); ++iter)
  545. {
  546. JointConstraintSharedData* shared_constraintp = *iter;
  547. JointConstraint* constraintp = new JointConstraint(shared_constraintp);
  548. initializeConstraint(constraintp);
  549. mConstraints.push_front(constraintp);
  550. }
  551. if (mJointMotionList->mConstraints.size())
  552. {
  553. mPelvisp = mCharacter->getJoint("mPelvis");
  554. if (!mPelvisp)
  555. {
  556. return FALSE;
  557. }
  558. }
  559. // setup loop keys
  560. setLoopIn(mJointMotionList->mLoopInPoint);
  561. setLoopOut(mJointMotionList->mLoopOutPoint);
  562. return TRUE;
  563. }
  564. //-----------------------------------------------------------------------------
  565. // LLKeyframeMotion::onActivate()
  566. //-----------------------------------------------------------------------------
  567. BOOL LLKeyframeMotion::onActivate()
  568. {
  569. // If the keyframe anim has an associated emote, trigger it.
  570. if( mJointMotionList->mEmoteName.length() > 0 )
  571. {
  572. LLUUID emote_anim_id = gAnimLibrary.stringToAnimState(mJointMotionList->mEmoteName);
  573. // don't start emote if already active to avoid recursion
  574. if (!mCharacter->isMotionActive(emote_anim_id))
  575. {
  576. mCharacter->startMotion( emote_anim_id );
  577. }
  578. }
  579. mLastLoopedTime = 0.f;
  580. return TRUE;
  581. }
  582. //-----------------------------------------------------------------------------
  583. // LLKeyframeMotion::onUpdate()
  584. //-----------------------------------------------------------------------------
  585. BOOL LLKeyframeMotion::onUpdate(F32 time, U8* joint_mask)
  586. {
  587. llassert(time >= 0.f);
  588. if (mJointMotionList->mLoop)
  589. {
  590. if (mJointMotionList->mDuration == 0.0f)
  591. {
  592. time = 0.f;
  593. mLastLoopedTime = 0.0f;
  594. }
  595. else if (mStopped)
  596. {
  597. mLastLoopedTime = llmin(mJointMotionList->mDuration, mLastLoopedTime + time - mLastUpdateTime);
  598. }
  599. else if (time > mJointMotionList->mLoopOutPoint)
  600. {
  601. if ((mJointMotionList->mLoopOutPoint - mJointMotionList->mLoopInPoint) == 0.f)
  602. {
  603. mLastLoopedTime = mJointMotionList->mLoopOutPoint;
  604. }
  605. else
  606. {
  607. mLastLoopedTime = mJointMotionList->mLoopInPoint +
  608. fmod(time - mJointMotionList->mLoopOutPoint,
  609. mJointMotionList->mLoopOutPoint - mJointMotionList->mLoopInPoint);
  610. }
  611. }
  612. else
  613. {
  614. mLastLoopedTime = time;
  615. }
  616. }
  617. else
  618. {
  619. mLastLoopedTime = time;
  620. }
  621. applyKeyframes(mLastLoopedTime);
  622. applyConstraints(mLastLoopedTime, joint_mask);
  623. mLastUpdateTime = time;
  624. return mLastLoopedTime <= mJointMotionList->mDuration;
  625. }
  626. //-----------------------------------------------------------------------------
  627. // applyKeyframes()
  628. //-----------------------------------------------------------------------------
  629. void LLKeyframeMotion::applyKeyframes(F32 time)
  630. {
  631. llassert_always (mJointMotionList->getNumJointMotions() <= mJointStates.size());
  632. for (U32 i=0; i<mJointMotionList->getNumJointMotions(); i++)
  633. {
  634. mJointMotionList->getJointMotion(i)->update(mJointStates[i],
  635. time,
  636. mJointMotionList->mDuration );
  637. }
  638. LLJoint::JointPriority* pose_priority = (LLJoint::JointPriority* )mCharacter->getAnimationData("Hand Pose Priority");
  639. if (pose_priority)
  640. {
  641. if (mJointMotionList->mMaxPriority >= *pose_priority)
  642. {
  643. mCharacter->setAnimationData("Hand Pose", &mJointMotionList->mHandPose);
  644. mCharacter->setAnimationData("Hand Pose Priority", &mJointMotionList->mMaxPriority);
  645. }
  646. }
  647. else
  648. {
  649. mCharacter->setAnimationData("Hand Pose", &mJointMotionList->mHandPose);
  650. mCharacter->setAnimationData("Hand Pose Priority", &mJointMotionList->mMaxPriority);
  651. }
  652. }
  653. //-----------------------------------------------------------------------------
  654. // applyConstraints()
  655. //-----------------------------------------------------------------------------
  656. void LLKeyframeMotion::applyConstraints(F32 time, U8* joint_mask)
  657. {
  658. //TODO: investigate replacing spring simulation with critically damped motion
  659. // re-init constraints if skeleton has changed
  660. if (mCharacter->getSkeletonSerialNum() != mLastSkeletonSerialNum)
  661. {
  662. mLastSkeletonSerialNum = mCharacter->getSkeletonSerialNum();
  663. for (constraint_list_t::iterator iter = mConstraints.begin();
  664. iter != mConstraints.end(); ++iter)
  665. {
  666. JointConstraint* constraintp = *iter;
  667. initializeConstraint(constraintp);
  668. }
  669. }
  670. // apply constraints
  671. for (constraint_list_t::iterator iter = mConstraints.begin();
  672. iter != mConstraints.end(); ++iter)
  673. {
  674. JointConstraint* constraintp = *iter;
  675. applyConstraint(constraintp, time, joint_mask);
  676. }
  677. }
  678. //-----------------------------------------------------------------------------
  679. // LLKeyframeMotion::onDeactivate()
  680. //-----------------------------------------------------------------------------
  681. void LLKeyframeMotion::onDeactivate()
  682. {
  683. for (constraint_list_t::iterator iter = mConstraints.begin();
  684. iter != mConstraints.end(); ++iter)
  685. {
  686. JointConstraint* constraintp = *iter;
  687. deactivateConstraint(constraintp);
  688. }
  689. }
  690. //-----------------------------------------------------------------------------
  691. // setStopTime()
  692. //-----------------------------------------------------------------------------
  693. // time is in seconds since character creation
  694. void LLKeyframeMotion::setStopTime(F32 time)
  695. {
  696. LLMotion::setStopTime(time);
  697. if (mJointMotionList->mLoop && mJointMotionList->mLoopOutPoint != mJointMotionList->mDuration)
  698. {
  699. F32 start_loop_time = mActivationTimestamp + mJointMotionList->mLoopInPoint;
  700. F32 loop_fraction_time;
  701. if (mJointMotionList->mLoopOutPoint == mJointMotionList->mLoopInPoint)
  702. {
  703. loop_fraction_time = 0.f;
  704. }
  705. else
  706. {
  707. loop_fraction_time = fmod(time - start_loop_time,
  708. mJointMotionList->mLoopOutPoint - mJointMotionList->mLoopInPoint);
  709. }
  710. mStopTimestamp = llmax(time,
  711. (time - loop_fraction_time) + (mJointMotionList->mDuration - mJointMotionList->mLoopInPoint) - getEaseOutDuration());
  712. }
  713. }
  714. //-----------------------------------------------------------------------------
  715. // initializeConstraint()
  716. //-----------------------------------------------------------------------------
  717. void LLKeyframeMotion::initializeConstraint(JointConstraint* constraint)
  718. {
  719. JointConstraintSharedData *shared_data = constraint->mSharedData;
  720. S32 joint_num;
  721. LLVector3 source_pos = mCharacter->getVolumePos(shared_data->mSourceConstraintVolume, shared_data->mSourceConstraintOffset);
  722. LLJoint* cur_joint = getJoint(shared_data->mJointStateIndices[0]);
  723. if ( !cur_joint )
  724. {
  725. return;
  726. }
  727. F32 source_pos_offset = dist_vec(source_pos, cur_joint->getWorldPosition());
  728. constraint->mTotalLength = constraint->mJointLengths[0] = dist_vec(cur_joint->getParent()->getWorldPosition(), source_pos);
  729. // grab joint lengths
  730. for (joint_num = 1; joint_num < shared_data->mChainLength; joint_num++)
  731. {
  732. cur_joint = getJointState(shared_data->mJointStateIndices[joint_num])->getJoint();
  733. if (!cur_joint)
  734. {
  735. return;
  736. }
  737. constraint->mJointLengths[joint_num] = dist_vec(cur_joint->getWorldPosition(), cur_joint->getParent()->getWorldPosition());
  738. constraint->mTotalLength += constraint->mJointLengths[joint_num];
  739. }
  740. // store fraction of total chain length so we know how to shear the entire chain towards the goal position
  741. for (joint_num = 1; joint_num < shared_data->mChainLength; joint_num++)
  742. {
  743. constraint->mJointLengthFractions[joint_num] = constraint->mJointLengths[joint_num] / constraint->mTotalLength;
  744. }
  745. // add last step in chain, from final joint to constraint position
  746. constraint->mTotalLength += source_pos_offset;
  747. constraint->mSourceVolume = mCharacter->findCollisionVolume(shared_data->mSourceConstraintVolume);
  748. constraint->mTargetVolume = mCharacter->findCollisionVolume(shared_data->mTargetConstraintVolume);
  749. }
  750. //-----------------------------------------------------------------------------
  751. // activateConstraint()
  752. //-----------------------------------------------------------------------------
  753. void LLKeyframeMotion::activateConstraint(JointConstraint* constraint)
  754. {
  755. JointConstraintSharedData *shared_data = constraint->mSharedData;
  756. constraint->mActive = TRUE;
  757. S32 joint_num;
  758. // grab ground position if we need to
  759. if (shared_data->mConstraintTargetType == CONSTRAINT_TARGET_TYPE_GROUND)
  760. {
  761. LLVector3 source_pos = mCharacter->getVolumePos(shared_data->mSourceConstraintVolume, shared_data->mSourceConstraintOffset);
  762. LLVector3 ground_pos_agent;
  763. mCharacter->getGround(source_pos, ground_pos_agent, constraint->mGroundNorm);
  764. constraint->mGroundPos = mCharacter->getPosGlobalFromAgent(ground_pos_agent + shared_data->mTargetConstraintOffset);
  765. }
  766. for (joint_num = 1; joint_num < shared_data->mChainLength; joint_num++)
  767. {
  768. LLJoint* cur_joint = getJoint(shared_data->mJointStateIndices[joint_num]);
  769. if ( !cur_joint )
  770. {
  771. return;
  772. }
  773. constraint->mPositions[joint_num] = (cur_joint->getWorldPosition() - mPelvisp->getWorldPosition()) * ~mPelvisp->getWorldRotation();
  774. }
  775. constraint->mWeight = 1.f;
  776. }
  777. //-----------------------------------------------------------------------------
  778. // deactivateConstraint()
  779. //-----------------------------------------------------------------------------
  780. void LLKeyframeMotion::deactivateConstraint(JointConstraint *constraintp)
  781. {
  782. if (constraintp->mSourceVolume)
  783. {
  784. constraintp->mSourceVolume->mUpdateXform = FALSE;
  785. }
  786. if (!constraintp->mSharedData->mConstraintTargetType == CONSTRAINT_TARGET_TYPE_GROUND)
  787. {
  788. if (constraintp->mTargetVolume)
  789. {
  790. constraintp->mTargetVolume->mUpdateXform = FALSE;
  791. }
  792. }
  793. constraintp->mActive = FALSE;
  794. }
  795. //-----------------------------------------------------------------------------
  796. // applyConstraint()
  797. //-----------------------------------------------------------------------------
  798. void LLKeyframeMotion::applyConstraint(JointConstraint* constraint, F32 time, U8* joint_mask)
  799. {
  800. JointConstraintSharedData *shared_data = constraint->mSharedData;
  801. if (!shared_data) return;
  802. LLVector3 positions[MAX_CHAIN_LENGTH];
  803. const F32* joint_lengths = constraint->mJointLengths;
  804. LLVector3 velocities[MAX_CHAIN_LENGTH - 1];
  805. LLQuaternion old_rots[MAX_CHAIN_LENGTH];
  806. S32 joint_num;
  807. if (time < shared_data->mEaseInStartTime)
  808. {
  809. return;
  810. }
  811. if (time > shared_data->mEaseOutStopTime)
  812. {
  813. if (constraint->mActive)
  814. {
  815. deactivateConstraint(constraint);
  816. }
  817. return;
  818. }
  819. if (!constraint->mActive || time < shared_data->mEaseInStopTime)
  820. {
  821. activateConstraint(constraint);
  822. }
  823. LLJoint* root_joint = getJoint(shared_data->mJointStateIndices[shared_data->mChainLength]);
  824. if (! root_joint)
  825. {
  826. return;
  827. }
  828. LLVector3 root_pos = root_joint->getWorldPosition();
  829. // LLQuaternion root_rot =
  830. root_joint->getParent()->getWorldRotation();
  831. // LLQuaternion inv_root_rot = ~root_rot;
  832. // LLVector3 current_source_pos = mCharacter->getVolumePos(shared_data->mSourceConstraintVolume, shared_data->mSourceConstraintOffset);
  833. //apply underlying keyframe animation to get nominal "kinematic" joint positions
  834. for (joint_num = 0; joint_num <= shared_data->mChainLength; joint_num++)
  835. {
  836. LLJoint* cur_joint = getJoint(shared_data->mJointStateIndices[joint_num]);
  837. if (!cur_joint)
  838. {
  839. return;
  840. }
  841. if (joint_mask[cur_joint->getJointNum()] >= (0xff >> (7 - getPriority())))
  842. {
  843. // skip constraint
  844. return;
  845. }
  846. old_rots[joint_num] = cur_joint->getRotation();
  847. cur_joint->setRotation(getJointState(shared_data->mJointStateIndices[joint_num])->getRotation());
  848. }
  849. LLVector3 keyframe_source_pos = mCharacter->getVolumePos(shared_data->mSourceConstraintVolume, shared_data->mSourceConstraintOffset);
  850. LLVector3 target_pos;
  851. switch(shared_data->mConstraintTargetType)
  852. {
  853. case CONSTRAINT_TARGET_TYPE_GROUND:
  854. target_pos = mCharacter->getPosAgentFromGlobal(constraint->mGroundPos);
  855. // llinfos << "Target Pos " << constraint->mGroundPos << " on " << mCharacter->findCollisionVolume(shared_data->mSourceConstraintVolume)->getName() << llendl;
  856. break;
  857. case CONSTRAINT_TARGET_TYPE_BODY:
  858. target_pos = mCharacter->getVolumePos(shared_data->mTargetConstraintVolume, shared_data->mTargetConstraintOffset);
  859. break;
  860. default:
  861. break;
  862. }
  863. LLVector3 norm;
  864. LLJoint *source_jointp = NULL;
  865. LLJoint *target_jointp = NULL;
  866. if (shared_data->mConstraintType == CONSTRAINT_TYPE_PLANE)
  867. {
  868. switch(shared_data->mConstraintTargetType)
  869. {
  870. case CONSTRAINT_TARGET_TYPE_GROUND:
  871. norm = constraint->mGroundNorm;
  872. break;
  873. case CONSTRAINT_TARGET_TYPE_BODY:
  874. target_jointp = mCharacter->findCollisionVolume(shared_data->mTargetConstraintVolume);
  875. if (target_jointp)
  876. {
  877. // *FIX: do proper normal calculation for stretched
  878. // spheres (inverse transpose)
  879. norm = target_pos - target_jointp->getWorldPosition();
  880. }
  881. if (norm.isExactlyZero())
  882. {
  883. source_jointp = mCharacter->findCollisionVolume(shared_data->mSourceConstraintVolume);
  884. norm = -1.f * shared_data->mSourceConstraintOffset;
  885. if (source_jointp)
  886. {
  887. norm = norm * source_jointp->getWorldRotation();
  888. }
  889. }
  890. norm.normVec();
  891. break;
  892. default:
  893. norm.clearVec();
  894. break;
  895. }
  896. target_pos = keyframe_source_pos + (norm * ((target_pos - keyframe_source_pos) * norm));
  897. }
  898. if (constraint->mSharedData->mChainLength != 0 &&
  899. dist_vec_squared(root_pos, target_pos) * 0.95f > constraint->mTotalLength * constraint->mTotalLength)
  900. {
  901. constraint->mWeight = lerp(constraint->mWeight, 0.f, LLCriticalDamp::getInterpolant(0.1f));
  902. }
  903. else
  904. {
  905. constraint->mWeight = lerp(constraint->mWeight, 1.f, LLCriticalDamp::getInterpolant(0.3f));
  906. }
  907. F32 weight = constraint->mWeight * ((shared_data->mEaseOutStopTime == 0.f) ? 1.f :
  908. llmin(clamp_rescale(time, shared_data->mEaseInStartTime, shared_data->mEaseInStopTime, 0.f, 1.f),
  909. clamp_rescale(time, shared_data->mEaseOutStartTime, shared_data->mEaseOutStopTime, 1.f, 0.f)));
  910. LLVector3 source_to_target = target_pos - keyframe_source_pos;
  911. S32 max_iteration_count = llround(clamp_rescale(
  912. mCharacter->getPixelArea(),
  913. MAX_PIXEL_AREA_CONSTRAINTS,
  914. MIN_PIXEL_AREA_CONSTRAINTS,
  915. (F32)MAX_ITERATIONS,
  916. (F32)MIN_ITERATIONS));
  917. if (shared_data->mChainLength)
  918. {
  919. LLJoint* end_joint = getJoint(shared_data->mJointStateIndices[0]);
  920. if (!end_joint)
  921. {
  922. return;
  923. }
  924. LLQuaternion end_rot = end_joint->getWorldRotation();
  925. // slam start and end of chain to the proper positions (rest of chain stays put)
  926. positions[0] = lerp(keyframe_source_pos, target_pos, weight);
  927. positions[shared_data->mChainLength] = root_pos;
  928. // grab keyframe-specified positions of joints
  929. for (joint_num = 1; joint_num < shared_data->mChainLength; joint_num++)
  930. {
  931. LLJoint* cur_joint = getJoint(shared_data->mJointStateIndices[joint_num]);
  932. if (!cur_joint)
  933. {
  934. return;
  935. }
  936. LLVector3 kinematic_position = cur_joint->getWorldPosition() +
  937. (source_to_target * constraint->mJointLengthFractions[joint_num]);
  938. // convert intermediate joint positions to world coordinates
  939. positions[joint_num] = ( constraint->mPositions[joint_num] * mPelvisp->getWorldRotation()) + mPelvisp->getWorldPosition();
  940. F32 time_constant = 1.f / clamp_rescale(constraint->mFixupDistanceRMS, 0.f, 0.5f, 0.2f, 8.f);
  941. // llinfos << "Interpolant " << LLCriticalDamp::getInterpolant(time_constant, FALSE) << " and fixup distance " << constraint->mFixupDistanceRMS << " on " << mCharacter->findCollisionVolume(shared_data->mSourceConstraintVolume)->getName() << llendl;
  942. positions[joint_num] = lerp(positions[joint_num], kinematic_position,
  943. LLCriticalDamp::getInterpolant(time_constant, FALSE));
  944. }
  945. S32 iteration_count;
  946. for (iteration_count = 0; iteration_count < max_iteration_count; iteration_count++)
  947. {
  948. S32 num_joints_finished = 0;
  949. for (joint_num = 1; joint_num < shared_data->mChainLength; joint_num++)
  950. {
  951. // constraint to child
  952. LLVector3 acceleration = (positions[joint_num - 1] - positions[joint_num]) *
  953. (dist_vec(positions[joint_num], positions[joint_num - 1]) - joint_lengths[joint_num - 1]) * JOINT_LENGTH_K;
  954. // constraint to parent
  955. acceleration += (positions[joint_num + 1] - positions[joint_num]) *
  956. (dist_vec(positions[joint_num + 1], positions[joint_num]) - joint_lengths[joint_num]) * JOINT_LENGTH_K;
  957. if (acceleration.magVecSquared() < MIN_ACCELERATION_SQUARED)
  958. {
  959. num_joints_finished++;
  960. }
  961. velocities[joint_num - 1] = velocities[joint_num - 1] * 0.7f;
  962. positions[joint_num] += velocities[joint_num - 1] + (acceleration * 0.5f);
  963. velocities[joint_num - 1] += acceleration;
  964. }
  965. if ((iteration_count >= MIN_ITERATION_COUNT) &&
  966. (num_joints_finished == shared_data->mChainLength - 1))
  967. {
  968. // llinfos << iteration_count << " iterations on " <<
  969. // mCharacter->findCollisionVolume(shared_data->mSourceConstraintVolume)->getName() << llendl;
  970. break;
  971. }
  972. }
  973. for (joint_num = shared_data->mChainLength; joint_num > 0; joint_num--)
  974. {
  975. LLJoint* cur_joint = getJoint(shared_data->mJointStateIndices[joint_num]);
  976. if (!cur_joint)
  977. {
  978. return;
  979. }
  980. LLJoint* child_joint = getJoint(shared_data->mJointStateIndices[joint_num - 1]);
  981. if (!child_joint)
  982. {
  983. return;
  984. }
  985. LLQuaternion parent_rot = cur_joint->getParent()->getWorldRotation();
  986. LLQuaternion cur_rot = cur_joint->getWorldRotation();
  987. LLQuaternion fixup_rot;
  988. LLVector3 target_at = positions[joint_num - 1] - positions[joint_num];
  989. LLVector3 current_at;
  990. // at bottom of chain, use point on collision volume, not joint position
  991. if (joint_num == 1)
  992. {
  993. current_at = mCharacter->getVolumePos(shared_data->mSourceConstraintVolume, shared_data->mSourceConstraintOffset) -
  994. cur_joint->getWorldPosition();
  995. }
  996. else
  997. {
  998. current_at = child_joint->getPosition() * cur_rot;
  999. }
  1000. fixup_rot.shortestArc(current_at, target_at);
  1001. LLQuaternion target_rot = cur_rot * fixup_rot;
  1002. target_rot = target_rot * ~parent_rot;
  1003. if (weight != 1.f)
  1004. {
  1005. LLQuaternion cur_rot = getJointState(shared_data->mJointStateIndices[joint_num])->getRotation();
  1006. target_rot = nlerp(weight, cur_rot, target_rot);
  1007. }
  1008. getJointState(shared_data->mJointStateIndices[joint_num])->setRotation(target_rot);
  1009. cur_joint->setRotation(target_rot);
  1010. }
  1011. LLQuaternion end_local_rot = end_rot * ~end_joint->getParent()->getWorldRotation();
  1012. if (weight == 1.f)
  1013. {
  1014. getJointState(shared_data->mJointStateIndices[0])->setRotation(end_local_rot);
  1015. }
  1016. else
  1017. {
  1018. LLQuaternion cur_rot = getJointState(shared_data->mJointStateIndices[0])->getRotation();
  1019. getJointState(shared_data->mJointStateIndices[0])->setRotation(nlerp(weight, cur_rot, end_local_rot));
  1020. }
  1021. // save simulated positions in pelvis-space and calculate total fixup distance
  1022. constraint->mFixupDistanceRMS = 0.f;
  1023. F32 delta_time = llmax(0.02f, llabs(time - mLastUpdateTime));
  1024. for (joint_num = 1; joint_num < shared_data->mChainLength; joint_num++)
  1025. {
  1026. LLVector3 new_pos = (positions[joint_num] - mPelvisp->getWorldPosition()) * ~mPelvisp->getWorldRotation();
  1027. constraint->mFixupDistanceRMS += dist_vec_squared(new_pos, constraint->mPositions[joint_num]) / delta_time;
  1028. constraint->mPositions[joint_num] = new_pos;
  1029. }
  1030. constraint->mFixupDistanceRMS *= 1.f / (constraint->mTotalLength * (F32)(shared_data->mChainLength - 1));
  1031. constraint->mFixupDistanceRMS = (F32) sqrt(constraint->mFixupDistanceRMS);
  1032. //reset old joint rots
  1033. for (joint_num = 0; joint_num <= shared_data->mChainLength; joint_num++)
  1034. {
  1035. LLJoint* cur_joint = getJoint(shared_data->mJointStateIndices[joint_num]);
  1036. if (!cur_joint)
  1037. {
  1038. return;
  1039. }
  1040. cur_joint->setRotation(old_rots[joint_num]);
  1041. }
  1042. }
  1043. // simple positional constraint (pelvis only)
  1044. else if (getJointState(shared_data->mJointStateIndices[0])->getUsage() & LLJointState::POS)
  1045. {
  1046. LLVector3 delta = source_to_target * weight;
  1047. LLPointer<LLJointState> current_joint_state = getJointState(shared_data->mJointStateIndices[0]);
  1048. LLQuaternion parent_rot = current_joint_state->getJoint()->getParent()->getWorldRotation();
  1049. delta = delta * ~parent_rot;
  1050. current_joint_state->setPosition(current_joint_state->getJoint()->getPosition() + delta);
  1051. }
  1052. }
  1053. //-----------------------------------------------------------------------------
  1054. // deserialize()
  1055. //-----------------------------------------------------------------------------
  1056. BOOL LLKeyframeMotion::deserialize(LLDataPacker& dp)
  1057. {
  1058. BOOL old_version = FALSE;
  1059. mJointMotionList = new LLKeyframeMotion::JointMotionList;
  1060. //-------------------------------------------------------------------------
  1061. // get base priority
  1062. //-------------------------------------------------------------------------
  1063. S32 temp_priority;
  1064. U16 version;
  1065. U16 sub_version;
  1066. if (!dp.unpackU16(version, "version"))
  1067. {
  1068. llwarns << "can't read version number" << llendl;
  1069. return FALSE;
  1070. }
  1071. if (!dp.unpackU16(sub_version, "sub_version"))
  1072. {
  1073. llwarns << "can't read sub version number" << llendl;
  1074. return FALSE;
  1075. }
  1076. if (version == 0 && sub_version == 1)
  1077. {
  1078. old_version = TRUE;
  1079. }
  1080. else if (version != KEYFRAME_MOTION_VERSION || sub_version != KEYFRAME_MOTION_SUBVERSION)
  1081. {
  1082. #if LL_RELEASE
  1083. llwarns << "Bad animation version " << version << "." << sub_version << llendl;
  1084. return FALSE;
  1085. #else
  1086. llerrs << "Bad animation version " << version << "." << sub_version << llendl;
  1087. #endif
  1088. }
  1089. if (!dp.unpackS32(temp_priority, "base_priority"))
  1090. {
  1091. llwarns << "can't read animation base_priority" << llendl;
  1092. return FALSE;
  1093. }
  1094. mJointMotionList->mBasePriority = (LLJoint::JointPriority) temp_priority;
  1095. if (mJointMotionList->mBasePriority >= LLJoint::ADDITIVE_PRIORITY)
  1096. {
  1097. mJointMotionList->mBasePriority = (LLJoint::JointPriority)((int)LLJoint::ADDITIVE_PRIORITY-1);
  1098. mJointMotionList->mMaxPriority = mJointMotionList->mBasePriority;
  1099. }
  1100. else if (mJointMotionList->mBasePriority < LLJoint::USE_MOTION_PRIORITY)
  1101. {
  1102. llwarns << "bad animation base_priority " << mJointMotionList->mBasePriority << llendl;
  1103. return FALSE;
  1104. }
  1105. //-------------------------------------------------------------------------
  1106. // get duration
  1107. //-------------------------------------------------------------------------
  1108. if (!dp.unpackF32(mJointMotionList->mDuration, "duration"))
  1109. {
  1110. llwarns << "can't read duration" << llendl;
  1111. return FALSE;
  1112. }
  1113. if (mJointMotionList->mDuration > MAX_ANIM_DURATION ||
  1114. !llfinite(mJointMotionList->mDuration))
  1115. {
  1116. llwarns << "invalid animation duration" << llendl;
  1117. return FALSE;
  1118. }
  1119. //-------------------------------------------------------------------------
  1120. // get emote (optional)
  1121. //-------------------------------------------------------------------------
  1122. if (!dp.unpackString(mJointMotionList->mEmoteName, "emote_name"))
  1123. {
  1124. llwarns << "can't read optional_emote_animation" << llendl;
  1125. return FALSE;
  1126. }
  1127. if(mJointMotionList->mEmoteName==mID.asString())
  1128. {
  1129. llwarns << "Malformed animation mEmoteName==mID" << llendl;
  1130. return FALSE;
  1131. }
  1132. //-------------------------------------------------------------------------
  1133. // get loop
  1134. //-------------------------------------------------------------------------
  1135. if (!dp.unpackF32(mJointMotionList->mLoopInPoint, "loop_in_point") ||
  1136. !llfinite(mJointMotionList->mLoopInPoint))
  1137. {
  1138. llwarns << "can't read loop point" << llendl;
  1139. return FALSE;
  1140. }
  1141. if (!dp.unpackF32(mJointMotionList->mLoopOutPoint, "loop_out_point") ||
  1142. !llfinite(mJointMotionList->mLoopOutPoint))
  1143. {
  1144. llwarns << "can't read loop point" << llendl;
  1145. return FALSE;
  1146. }
  1147. if (!dp.unpackS32(mJointMotionList->mLoop, "loop"))
  1148. {
  1149. llwarns << "can't read loop" << llendl;
  1150. return FALSE;
  1151. }
  1152. //-------------------------------------------------------------------------
  1153. // get easeIn and easeOut
  1154. //-------------------------------------------------------------------------
  1155. if (!dp.unpackF32(mJointMotionList->mEaseInDuration, "ease_in_duration") ||
  1156. !llfinite(mJointMotionList->mEaseInDuration))
  1157. {
  1158. llwarns << "can't read easeIn" << llendl;
  1159. return FALSE;
  1160. }
  1161. if (!dp.unpackF32(mJointMotionList->mEaseOutDuration, "ease_out_duration") ||
  1162. !llfinite(mJointMotionList->mEaseOutDuration))
  1163. {
  1164. llwarns << "can't read easeOut" << llendl;
  1165. return FALSE;
  1166. }
  1167. //-------------------------------------------------------------------------
  1168. // get hand pose
  1169. //-------------------------------------------------------------------------
  1170. U32 word;
  1171. if (!dp.unpackU32(word, "hand_pose"))
  1172. {
  1173. llwarns << "can't read hand pose" << llendl;
  1174. return FALSE;
  1175. }
  1176. if(word > LLHandMotion::NUM_HAND_POSES)
  1177. {
  1178. llwarns << "invalid LLHandMotion::eHandPose index: " << word << llendl;
  1179. return FALSE;
  1180. }
  1181. mJointMotionList->mHandPose = (LLHandMotion::eHandPose)word;
  1182. //-------------------------------------------------------------------------
  1183. // get number of joint motions
  1184. //-------------------------------------------------------------------------
  1185. U32 num_motions = 0;
  1186. if (!dp.unpackU32(num_motions, "num_joints"))
  1187. {
  1188. llwarns << "can't read number of joints" << llendl;
  1189. return FALSE;
  1190. }
  1191. if (num_motions == 0)
  1192. {
  1193. llwarns << "no joints in animation" << llendl;
  1194. return FALSE;
  1195. }
  1196. else if (num_motions > LL_CHARACTER_MAX_JOINTS)
  1197. {
  1198. llwarns << "too many joints in animation" << llendl;
  1199. return FALSE;
  1200. }
  1201. mJointMotionList->mJointMotionArray.clear();
  1202. mJointMotionList->mJointMotionArray.reserve(num_motions);
  1203. mJointStates.clear();
  1204. mJointStates.reserve(num_motions);
  1205. //-------------------------------------------------------------------------
  1206. // initialize joint motions
  1207. //-------------------------------------------------------------------------
  1208. for(U32 i=0; i<num_motions; ++i)
  1209. {
  1210. JointMotion* joint_motion = new JointMotion;
  1211. mJointMotionList->mJointMotionArray.push_back(joint_motion);
  1212. std::string joint_name;
  1213. if (!dp.unpackString(joint_name, "joint_name"))
  1214. {
  1215. llwarns << "can't read joint name" << llendl;
  1216. return FALSE;
  1217. }
  1218. if (joint_name == "mScreen" || joint_name == "mRoot")
  1219. {
  1220. llwarns << "attempted to animate special " << joint_name << " joint" << llendl;
  1221. return FALSE;
  1222. }
  1223. //---------------------------------------------------------------------
  1224. // find the corresponding joint
  1225. //---------------------------------------------------------------------
  1226. LLJoint *joint = mCharacter->getJoint( joint_name );
  1227. if (joint)
  1228. {
  1229. // llinfos << " joint: " << joint_name << llendl;
  1230. }
  1231. else
  1232. {
  1233. llwarns << "joint not found: " << joint_name << llendl;
  1234. //return FALSE;
  1235. }
  1236. joint_motion->mJointName = joint_name;
  1237. LLPointer<LLJointState> joint_state = new LLJointState;
  1238. mJointStates.push_back(joint_state);
  1239. joint_state->setJoint( joint ); // note: can accept NULL
  1240. joint_state->setUsage( 0 );
  1241. //---------------------------------------------------------------------
  1242. // get joint priority
  1243. //---------------------------------------------------------------------
  1244. S32 joint_priority;
  1245. if (!dp.unpackS32(joint_priority, "joint_priority"))
  1246. {
  1247. llwarns << "can't read joint priority." << llendl;
  1248. return FALSE;
  1249. }
  1250. if (joint_priority < LLJoint::USE_MOTION_PRIORITY)
  1251. {
  1252. llwarns << "joint priority unknown - too low." << llendl;
  1253. return FALSE;
  1254. }
  1255. joint_motion->mPriority = (LLJoint::JointPriority)joint_priority;
  1256. if (joint_priority != LLJoint::USE_MOTION_PRIORITY &&
  1257. joint_priority > mJointMotionList->mMaxPriority)
  1258. {
  1259. mJointMotionList->mMaxPriority = (LLJoint::JointPriority)joint_priority;
  1260. }
  1261. joint_state->setPriority((LLJoint::JointPriority)joint_priority);
  1262. //---------------------------------------------------------------------
  1263. // scan rotation curve header
  1264. //---------------------------------------------------------------------
  1265. if (!dp.unpackS32(joint_motion->mRotationCurve.mNumKeys, "num_rot_keys") || joint_motion->mRotationCurve.mNumKeys < 0)
  1266. {
  1267. llwarns << "can't read number of rotation keys" << llendl;
  1268. return FALSE;
  1269. }
  1270. joint_motion->mRotationCurve.mInterpolationType = IT_LINEAR;
  1271. if (joint_motion->mRotationCurve.mNumKeys != 0)
  1272. {
  1273. joint_state->setUsage(joint_state->getUsage() | LLJointState::ROT );
  1274. }
  1275. //---------------------------------------------------------------------
  1276. // scan rotation curve keys
  1277. //---------------------------------------------------------------------
  1278. RotationCurve *rCurve = &joint_motion->mRotationCurve;
  1279. for (S32 k = 0; k < joint_motion->mRotationCurve.mNumKeys; k++)
  1280. {
  1281. F32 time;
  1282. U16 time_short;
  1283. if (old_version)
  1284. {
  1285. if (!dp.unpackF32(time, "time") ||
  1286. !llfinite(time))
  1287. {
  1288. llwarns << "can't read rotation key (" << k << ")" << llendl;
  1289. return FALSE;
  1290. }
  1291. }
  1292. else
  1293. {
  1294. if (!dp.unpackU16(time_short, "time"))
  1295. {
  1296. llwarns << "can't read rotation key (" << k << ")" << llendl;
  1297. return FALSE;
  1298. }
  1299. time = U16_to_F32(time_short, 0.f, mJointMotionList->mDuration);
  1300. if (time < 0 || time > mJointMotionList->mDuration)
  1301. {
  1302. llwarns << "invalid frame time" << llendl;
  1303. return FALSE;
  1304. }
  1305. }
  1306. RotationKey rot_key;
  1307. rot_key.mTime = time;
  1308. LLVector3 rot_angles;
  1309. U16 x, y, z;
  1310. BOOL success = TRUE;
  1311. if (old_version)
  1312. {
  1313. success = dp.unpackVector3(rot_angles, "rot_angles") && rot_angles.isFinite();
  1314. LLQuaternion::Order ro = StringToOrder("ZYX");
  1315. rot_key.mRotation = mayaQ(rot_angles.mV[VX], rot_angles.mV[VY], rot_angles.mV[VZ], ro);
  1316. }
  1317. else
  1318. {
  1319. success &= dp.unpackU16(x, "rot_angle_x");
  1320. success &= dp.unpackU16(y, "rot_angle_y");
  1321. success &= dp.unpackU16(z, "rot_angle_z");
  1322. LLVector3 rot_vec;
  1323. rot_vec.mV[VX] = U16_to_F32(x, -1.f, 1.f);
  1324. rot_vec.mV[VY] = U16_to_F32(y, -1.f, 1.f);
  1325. rot_vec.mV[VZ] = U16_to_F32(z, -1.f, 1.f);
  1326. rot_key.mRotation.unpackFromVector3(rot_vec);
  1327. }
  1328. if( !(rot_key.mRotation.isFinite()) )
  1329. {
  1330. llwarns << "non-finite angle in rotation key" << llendl;
  1331. success = FALSE;
  1332. }
  1333. if (!success)
  1334. {
  1335. llwarns << "can't read rotation key (" << k << ")" << llendl;
  1336. return FALSE;
  1337. }
  1338. rCurve->mKeys[time] = rot_key;
  1339. }
  1340. //---------------------------------------------------------------------
  1341. // scan position curve header
  1342. //---------------------------------------------------------------------
  1343. if (!dp.unpackS32(joint_motion->mPositionCurve.mNumKeys, "num_pos_keys") || joint_motion->mPositionCurve.mNumKeys < 0)
  1344. {
  1345. llwarns << "can't read number of position keys" << llendl;
  1346. return FALSE;
  1347. }
  1348. joint_motion->mPositionCurve.mInterpolationType = IT_LINEAR;
  1349. if (joint_motion->mPositionCurve.mNumKeys != 0)
  1350. {
  1351. joint_state->setUsage(joint_state->getUsage() | LLJointState::POS );
  1352. }
  1353. //---------------------------------------------------------------------
  1354. // scan position curve keys
  1355. //---------------------------------------------------------------------
  1356. PositionCurve *pCurve = &joint_motion->mPositionCurve;
  1357. BOOL is_pelvis = joint_motion->mJointName == "mPelvis";
  1358. for (S32 k = 0; k < joint_motion->mPositionCurve.mNumKeys; k++)
  1359. {
  1360. U16 time_short;
  1361. PositionKey pos_key;
  1362. if (old_version)
  1363. {
  1364. if (!dp.unpackF32(pos_key.mTime, "time") ||
  1365. !llfinite(pos_key.mTime))
  1366. {
  1367. llwarns << "can't read position key (" << k << ")" << llendl;
  1368. return FALSE;
  1369. }
  1370. }
  1371. else
  1372. {
  1373. if (!dp.unpackU16(time_short, "time"))
  1374. {
  1375. llwarns << "can't read position key (" << k << ")" << llendl;
  1376. return FALSE;
  1377. }
  1378. pos_key.mTime = U16_to_F32(time_short, 0.f, mJointMotionList->mDuration);
  1379. }
  1380. BOOL success = TRUE;
  1381. if (old_version)
  1382. {
  1383. success = dp.unpackVector3(pos_key.mPosition, "pos");
  1384. }
  1385. else
  1386. {
  1387. U16 x, y, z;
  1388. success &= dp.unpackU16(x, "pos_x");
  1389. success &= dp.unpackU16(y, "pos_y");
  1390. success &= dp.unpackU16(z, "pos_z");
  1391. pos_key.mPosition.mV[VX] = U16_to_F32(x, -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  1392. pos_key.mPosition.mV[VY] = U16_to_F32(y, -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  1393. pos_key.mPosition.mV[VZ] = U16_to_F32(z, -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  1394. }
  1395. if( !(pos_key.mPosition.isFinite()) )
  1396. {
  1397. llwarns << "non-finite position in key" << llendl;
  1398. success = FALSE;
  1399. }
  1400. if (!success)
  1401. {
  1402. llwarns << "can't read position key (" << k << ")" << llendl;
  1403. return FALSE;
  1404. }
  1405. pCurve->mKeys[pos_key.mTime] = pos_key;
  1406. if (is_pelvis)
  1407. {
  1408. mJointMotionList->mPelvisBBox.addPoint(pos_key.mPosition);
  1409. }
  1410. }
  1411. joint_motion->mUsage = joint_state->getUsage();
  1412. }
  1413. //-------------------------------------------------------------------------
  1414. // get number of constraints
  1415. //-------------------------------------------------------------------------
  1416. S32 num_constraints = 0;
  1417. if (!dp.unpackS32(num_constraints, "num_constraints"))
  1418. {
  1419. llwarns << "can't read number of constraints" << llendl;
  1420. return FALSE;
  1421. }
  1422. if (num_constraints > MAX_CONSTRAINTS || num_constraints < 0)
  1423. {
  1424. llwarns << "Bad number of constraints... ignoring: " << num_constraints << llendl;
  1425. }
  1426. else
  1427. {
  1428. //-------------------------------------------------------------------------
  1429. // get constraints
  1430. //-------------------------------------------------------------------------
  1431. std::string str;
  1432. for(S32 i = 0; i < num_constraints; ++i)
  1433. {
  1434. // read in constraint data
  1435. JointConstraintSharedData* constraintp = new JointConstraintSharedData;
  1436. U8 byte = 0;
  1437. if (!dp.unpackU8(byte, "chain_length"))
  1438. {
  1439. llwarns << "can't read constraint chain length" << llendl;
  1440. delete constraintp;
  1441. return FALSE;
  1442. }
  1443. constraintp->mChainLength = (S32) byte;
  1444. if((U32)constraintp->mChainLength > mJointMotionList->getNumJointMotions())
  1445. {
  1446. llwarns << "invalid constraint chain length" << llendl;
  1447. delete constraintp;
  1448. return FALSE;
  1449. }
  1450. if (!dp.unpackU8(byte, "constraint_type"))
  1451. {
  1452. llwarns << "can't read constraint type" << llendl;
  1453. delete constraintp;
  1454. return FALSE;
  1455. }
  1456. if( byte >= NUM_CONSTRAINT_TYPES )
  1457. {
  1458. llwarns << "invalid constraint type" << llendl;
  1459. delete constraintp;
  1460. return FALSE;
  1461. }
  1462. constraintp->mConstraintType = (EConstraintType)byte;
  1463. const S32 BIN_DATA_LENGTH = 16;
  1464. U8 bin_data[BIN_DATA_LENGTH+1];
  1465. if (!dp.unpackBinaryDataFixed(bin_data, BIN_DATA_LENGTH, "source_volume"))
  1466. {
  1467. llwarns << "can't read source volume name" << llendl;
  1468. delete constraintp;
  1469. return FALSE;
  1470. }
  1471. bin_data[BIN_DATA_LENGTH] = 0; // Ensure null termination
  1472. str = (char*)bin_data;
  1473. constraintp->mSourceConstraintVolume = mCharacter->getCollisionVolumeID(str);
  1474. if (!dp.unpackVector3(constraintp->mSourceConstraintOffset, "source_offset"))
  1475. {
  1476. llwarns << "can't read constraint source offset" << llendl;
  1477. delete constraintp;
  1478. return FALSE;
  1479. }
  1480. if( !(constraintp->mSourceConstraintOffset.isFinite()) )
  1481. {
  1482. llwarns << "non-finite constraint source offset" << llendl;
  1483. delete constraintp;
  1484. return FALSE;
  1485. }
  1486. if (!dp.unpackBinaryDataFixed(bin_data, BIN_DATA_LENGTH, "target_volume"))
  1487. {
  1488. llwarns << "can't read target volume name" << llendl;
  1489. delete constraintp;
  1490. return FALSE;
  1491. }
  1492. bin_data[BIN_DATA_LENGTH] = 0; // Ensure null termination
  1493. str = (char*)bin_data;
  1494. if (str == "GROUND")
  1495. {
  1496. // constrain to ground
  1497. constraintp->mConstraintTargetType = CONSTRAINT_TARGET_TYPE_GROUND;
  1498. }
  1499. else
  1500. {
  1501. constraintp->mConstraintTargetType = CONSTRAINT_TARGET_TYPE_BODY;
  1502. constraintp->mTargetConstraintVolume = mCharacter->getCollisionVolumeID(str);
  1503. }
  1504. if (!dp.unpackVector3(constraintp->mTargetConstraintOffset, "target_offset"))
  1505. {
  1506. llwarns << "can't read constraint target offset" << llendl;
  1507. delete constraintp;
  1508. return FALSE;
  1509. }
  1510. if( !(constraintp->mTargetConstraintOffset.isFinite()) )
  1511. {
  1512. llwarns << "non-finite constraint target offset" << llendl;
  1513. delete constraintp;
  1514. return FALSE;
  1515. }
  1516. if (!dp.unpackVector3(constraintp->mTargetConstraintDir, "target_dir"))
  1517. {
  1518. llwarns << "can't read constraint target direction" << llendl;
  1519. delete constraintp;
  1520. return FALSE;
  1521. }
  1522. if( !(constraintp->mTargetConstraintDir.isFinite()) )
  1523. {
  1524. llwarns << "non-finite constraint target direction" << llendl;
  1525. delete constraintp;
  1526. return FALSE;
  1527. }
  1528. if (!constraintp->mTargetConstraintDir.isExactlyZero())
  1529. {
  1530. constraintp->mUseTargetOffset = TRUE;
  1531. // constraintp->mTargetConstraintDir *= constraintp->mSourceConstraintOffset.magVec();
  1532. }
  1533. if (!dp.unpackF32(constraintp->mEaseInStartTime, "ease_in_start") || !llfinite(constraintp->mEaseInStartTime))
  1534. {
  1535. llwarns << "can't read constraint ease in start time" << llendl;
  1536. delete constraintp;
  1537. return FALSE;
  1538. }
  1539. if (!dp.unpackF32(constraintp->mEaseInStopTime, "ease_in_stop") || !llfinite(constraintp->mEaseInStopTime))
  1540. {
  1541. llwarns << "can't read constraint ease in stop time" << llendl;
  1542. delete constraintp;
  1543. return FALSE;
  1544. }
  1545. if (!dp.unpackF32(constraintp->mEaseOutStartTime, "ease_out_start") || !llfinite(constraintp->mEaseOutStartTime))
  1546. {
  1547. llwarns << "can't read constraint ease out start time" << llendl;
  1548. delete constraintp;
  1549. return FALSE;
  1550. }
  1551. if (!dp.unpackF32(constraintp->mEaseOutStopTime, "ease_out_stop") || !llfinite(constraintp->mEaseOutStopTime))
  1552. {
  1553. llwarns << "can't read constraint ease out stop time" << llendl;
  1554. delete constraintp;
  1555. return FALSE;
  1556. }
  1557. mJointMotionList->mConstraints.push_front(constraintp);
  1558. constraintp->mJointStateIndices = new S32[constraintp->mChainLength + 1]; // note: mChainLength is size-limited - comes from a byte
  1559. LLJoint* joint = mCharacter->findCollisionVolume(constraintp->mSourceConstraintVolume);
  1560. // get joint to which this collision volume is attached
  1561. if (!joint)
  1562. {
  1563. return FALSE;
  1564. }
  1565. for (S32 i = 0; i < constraintp->mChainLength + 1; i++)
  1566. {
  1567. LLJoint* parent = joint->getParent();
  1568. if (!parent)
  1569. {
  1570. llwarns << "Joint with no parent: " << joint->getName()
  1571. << " Emote: " << mJointMotionList->mEmoteName << llendl;
  1572. return FALSE;
  1573. }
  1574. joint = parent;
  1575. constraintp->mJointStateIndices[i] = -1;
  1576. for (U32 j = 0; j < mJointMotionList->getNumJointMotions(); j++)
  1577. {
  1578. LLJoint* constraint_joint = getJoint(j);
  1579. if ( !constraint_joint )
  1580. {
  1581. llwarns << "Invalid joint " << j << llendl;
  1582. return FALSE;
  1583. }
  1584. if(constraint_joint == joint)
  1585. {
  1586. constraintp->mJointStateIndices[i] = (S32)j;
  1587. break;
  1588. }
  1589. }
  1590. if (constraintp->mJointStateIndices[i] < 0 )
  1591. {
  1592. llwarns << "No joint index for constraint " << i << llendl;
  1593. delete constraintp;
  1594. return FALSE;
  1595. }
  1596. }
  1597. }
  1598. }
  1599. // *FIX: support cleanup of old keyframe data
  1600. LLKeyframeDataCache::addKeyframeData(getID(), mJointMotionList);
  1601. mAssetStatus = ASSET_LOADED;
  1602. setupPose();
  1603. return TRUE;
  1604. }
  1605. //-----------------------------------------------------------------------------
  1606. // serialize()
  1607. //-----------------------------------------------------------------------------
  1608. BOOL LLKeyframeMotion::serialize(LLDataPacker& dp) const
  1609. {
  1610. BOOL success = TRUE;
  1611. success &= dp.packU16(KEYFRAME_MOTION_VERSION, "version");
  1612. success &= dp.packU16(KEYFRAME_MOTION_SUBVERSION, "sub_version");
  1613. success &= dp.packS32(mJointMotionList->mBasePriority, "base_priority");
  1614. success &= dp.packF32(mJointMotionList->mDuration, "duration");
  1615. success &= dp.packString(mJointMotionList->mEmoteName, "emote_name");
  1616. success &= dp.packF32(mJointMotionList->mLoopInPoint, "loop_in_point");
  1617. success &= dp.packF32(mJointMotionList->mLoopOutPoint, "loop_out_point");
  1618. success &= dp.packS32(mJointMotionList->mLoop, "loop");
  1619. success &= dp.packF32(mJointMotionList->mEaseInDuration, "ease_in_duration");
  1620. success &= dp.packF32(mJointMotionList->mEaseOutDuration, "ease_out_duration");
  1621. success &= dp.packU32(mJointMotionList->mHandPose, "hand_pose");
  1622. success &= dp.packU32(mJointMotionList->getNumJointMotions(), "num_joints");
  1623. for (U32 i = 0; i < mJointMotionList->getNumJointMotions(); i++)
  1624. {
  1625. JointMotion* joint_motionp = mJointMotionList->getJointMotion(i);
  1626. success &= dp.packString(joint_motionp->mJointName, "joint_name");
  1627. success &= dp.packS32(joint_motionp->mPriority, "joint_priority");
  1628. success &= dp.packS32(joint_motionp->mRotationCurve.mNumKeys, "num_rot_keys");
  1629. for (RotationCurve::key_map_t::iterator iter = joint_motionp->mRotationCurve.mKeys.begin();
  1630. iter != joint_motionp->mRotationCurve.mKeys.end(); ++iter)
  1631. {
  1632. RotationKey& rot_key = iter->second;
  1633. U16 time_short = F32_to_U16(rot_key.mTime, 0.f, mJointMotionList->mDuration);
  1634. success &= dp.packU16(time_short, "time");
  1635. LLVector3 rot_angles = rot_key.mRotation.packToVector3();
  1636. U16 x, y, z;
  1637. rot_angles.quantize16(-1.f, 1.f, -1.f, 1.f);
  1638. x = F32_to_U16(rot_angles.mV[VX], -1.f, 1.f);
  1639. y = F32_to_U16(rot_angles.mV[VY], -1.f, 1.f);
  1640. z = F32_to_U16(rot_angles.mV[VZ], -1.f, 1.f);
  1641. success &= dp.packU16(x, "rot_angle_x");
  1642. success &= dp.packU16(y, "rot_angle_y");
  1643. success &= dp.packU16(z, "rot_angle_z");
  1644. }
  1645. success &= dp.packS32(joint_motionp->mPositionCurve.mNumKeys, "num_pos_keys");
  1646. for (PositionCurve::key_map_t::iterator iter = joint_motionp->mPositionCurve.mKeys.begin();
  1647. iter != joint_motionp->mPositionCurve.mKeys.end(); ++iter)
  1648. {
  1649. PositionKey& pos_key = iter->second;
  1650. U16 time_short = F32_to_U16(pos_key.mTime, 0.f, mJointMotionList->mDuration);
  1651. success &= dp.packU16(time_short, "time");
  1652. U16 x, y, z;
  1653. pos_key.mPosition.quantize16(-LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET, -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  1654. x = F32_to_U16(pos_key.mPosition.mV[VX], -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  1655. y = F32_to_U16(pos_key.mPosition.mV[VY], -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  1656. z = F32_to_U16(pos_key.mPosition.mV[VZ], -LL_MAX_PELVIS_OFFSET, LL_MAX_PELVIS_OFFSET);
  1657. success &= dp.packU16(x, "pos_x");
  1658. success &= dp.packU16(y, "pos_y");
  1659. success &= dp.packU16(z, "pos_z");
  1660. }
  1661. }
  1662. success &= dp.packS32(mJointMotionList->mConstraints.size(), "num_constraints");
  1663. for (JointMotionList::constraint_list_t::const_iterator iter = mJointMotionList->mConstraints.begin();
  1664. iter != mJointMotionList->mConstraints.end(); ++iter)
  1665. {
  1666. JointConstraintSharedData* shared_constraintp = *iter;
  1667. success &= dp.packU8(shared_constraintp->mChainLength, "chain_length");
  1668. success &= dp.packU8(shared_constraintp->mConstraintType, "constraint_type");
  1669. char volume_name[16]; /* Flawfinder: ignore */
  1670. snprintf(volume_name, sizeof(volume_name), "%s", /* Flawfinder: ignore */
  1671. mCharacter->findCollisionVolume(shared_constraintp->mSourceConstraintVolume)->getName().c_str());
  1672. success &= dp.packBinaryDataFixed((U8*)volume_name, 16, "source_volume");
  1673. success &= dp.packVector3(shared_constraintp->mSourceConstraintOffset, "source_offset");
  1674. if (shared_constraintp->mConstraintTargetType == CONSTRAINT_TARGET_TYPE_GROUND)
  1675. {
  1676. snprintf(volume_name,sizeof(volume_name), "%s", "GROUND"); /* Flawfinder: ignore */
  1677. }
  1678. else
  1679. {
  1680. snprintf(volume_name, sizeof(volume_name),"%s", /* Flawfinder: ignore */
  1681. mCharacter->findCollisionVolume(shared_constraintp->mTargetConstraintVolume)->getName().c_str());
  1682. }
  1683. success &= dp.packBinaryDataFixed((U8*)volume_name, 16, "target_volume");
  1684. success &= dp.packVector3(shared_constraintp->mTargetConstraintOffset, "target_offset");
  1685. success &= dp.packVector3(shared_constraintp->mTargetConstraintDir, "target_dir");
  1686. success &= dp.packF32(shared_constraintp->mEaseInStartTime, "ease_in_start");
  1687. success &= dp.packF32(shared_constraintp->mEaseInStopTime, "ease_in_stop");
  1688. success &= dp.packF32(shared_constraintp->mEaseOutStartTime, "ease_out_start");
  1689. success &= dp.packF32(shared_constraintp->mEaseOutStopTime, "ease_out_stop");
  1690. }
  1691. return success;
  1692. }
  1693. //-----------------------------------------------------------------------------
  1694. // getFileSize()
  1695. //-----------------------------------------------------------------------------
  1696. U32 LLKeyframeMotion::getFileSize()
  1697. {
  1698. // serialize into a dummy buffer to calculate required size
  1699. LLDataPackerBinaryBuffer dp;
  1700. serialize(dp);
  1701. return dp.getCurrentSize();
  1702. }
  1703. //-----------------------------------------------------------------------------
  1704. // getPelvisBBox()
  1705. //-----------------------------------------------------------------------------
  1706. const LLBBoxLocal &LLKeyframeMotion::getPelvisBBox()
  1707. {
  1708. return mJointMotionList->mPelvisBBox;
  1709. }
  1710. //-----------------------------------------------------------------------------
  1711. // setPriority()
  1712. //-----------------------------------------------------------------------------
  1713. void LLKeyframeMotion::setPriority(S32 priority)
  1714. {
  1715. if (mJointMotionList)
  1716. {
  1717. S32 priority_delta = priority - mJointMotionList->mBasePriority;
  1718. mJointMotionList->mBasePriority = (LLJoint::JointPriority)priority;
  1719. mJointMotionList->mMaxPriority = mJointMotionList->mBasePriority;
  1720. for (U32 i = 0; i < mJointMotionList->getNumJointMotions(); i++)
  1721. {
  1722. JointMotion* joint_motion = mJointMotionList->getJointMotion(i);
  1723. joint_motion->mPriority = (LLJoint::JointPriority)llclamp(
  1724. (S32)joint_motion->mPriority + priority_delta,
  1725. (S32)LLJoint::LOW_PRIORITY,
  1726. (S32)LLJoint::HIGHEST_PRIORITY);
  1727. getJointState(i)->setPriority(joint_motion->mPriority);
  1728. }
  1729. }
  1730. }
  1731. //-----------------------------------------------------------------------------
  1732. // setEmote()
  1733. //-----------------------------------------------------------------------------
  1734. void LLKeyframeMotion::setEmote(const LLUUID& emote_id)
  1735. {
  1736. const char* emote_name = gAnimLibrary.animStateToString(emote_id);
  1737. if (emote_name)
  1738. {
  1739. mJointMotionList->mEmoteName = emote_name;
  1740. }
  1741. else
  1742. {
  1743. mJointMotionList->mEmoteName = "";
  1744. }
  1745. }
  1746. //-----------------------------------------------------------------------------
  1747. // setEaseIn()
  1748. //-----------------------------------------------------------------------------
  1749. void LLKeyframeMotion::setEaseIn(F32 ease_in)
  1750. {
  1751. if (mJointMotionList)
  1752. {
  1753. mJointMotionList->mEaseInDuration = llmax(ease_in, 0.f);
  1754. }
  1755. }
  1756. //-----------------------------------------------------------------------------
  1757. // setEaseOut()
  1758. //-----------------------------------------------------------------------------
  1759. void LLKeyframeMotion::setEaseOut(F32 ease_in)
  1760. {
  1761. if (mJointMotionList)
  1762. {
  1763. mJointMotionList->mEaseOutDuration = llmax(ease_in, 0.f);
  1764. }
  1765. }
  1766. //-----------------------------------------------------------------------------
  1767. // flushKeyframeCache()
  1768. //-----------------------------------------------------------------------------
  1769. void LLKeyframeMotion::flushKeyframeCache()
  1770. {
  1771. // TODO: Make this safe to do
  1772. // LLKeyf