llkeyframemotion.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:71k
源码类别:

游戏引擎

开发平台:

C++ Builder

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