PageRenderTime 117ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcharacter/llmotioncontroller.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 1127 lines | 729 code | 133 blank | 265 comment | 154 complexity | b1572596f6843de0d3ccae63f81e6219 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llmotioncontroller.cpp
  3. * @brief Implementation of LLMotionController 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 "llmemtype.h"
  31. #include "llmotioncontroller.h"
  32. #include "llkeyframemotion.h"
  33. #include "llmath.h"
  34. #include "lltimer.h"
  35. #include "llanimationstates.h"
  36. #include "llstl.h"
  37. const S32 NUM_JOINT_SIGNATURE_STRIDES = LL_CHARACTER_MAX_JOINTS / 4;
  38. const U32 MAX_MOTION_INSTANCES = 32;
  39. //-----------------------------------------------------------------------------
  40. // Constants and statics
  41. //-----------------------------------------------------------------------------
  42. LLMotionRegistry LLMotionController::sRegistry;
  43. //-----------------------------------------------------------------------------
  44. //-----------------------------------------------------------------------------
  45. // LLMotionRegistry class
  46. //-----------------------------------------------------------------------------
  47. //-----------------------------------------------------------------------------
  48. //-----------------------------------------------------------------------------
  49. // LLMotionRegistry()
  50. // Class Constructor
  51. //-----------------------------------------------------------------------------
  52. LLMotionRegistry::LLMotionRegistry()
  53. {
  54. }
  55. //-----------------------------------------------------------------------------
  56. // ~LLMotionRegistry()
  57. // Class Destructor
  58. //-----------------------------------------------------------------------------
  59. LLMotionRegistry::~LLMotionRegistry()
  60. {
  61. mMotionTable.clear();
  62. }
  63. //-----------------------------------------------------------------------------
  64. // addMotion()
  65. //-----------------------------------------------------------------------------
  66. BOOL LLMotionRegistry::registerMotion( const LLUUID& id, LLMotionConstructor constructor )
  67. {
  68. // llinfos << "Registering motion: " << name << llendl;
  69. if (!is_in_map(mMotionTable, id))
  70. {
  71. mMotionTable[id] = constructor;
  72. return TRUE;
  73. }
  74. return FALSE;
  75. }
  76. //-----------------------------------------------------------------------------
  77. // markBad()
  78. //-----------------------------------------------------------------------------
  79. void LLMotionRegistry::markBad( const LLUUID& id )
  80. {
  81. mMotionTable[id] = LLMotionConstructor(NULL);
  82. }
  83. //-----------------------------------------------------------------------------
  84. // createMotion()
  85. //-----------------------------------------------------------------------------
  86. LLMotion *LLMotionRegistry::createMotion( const LLUUID &id )
  87. {
  88. LLMotionConstructor constructor = get_if_there(mMotionTable, id, LLMotionConstructor(NULL));
  89. LLMotion* motion = NULL;
  90. if ( constructor == NULL )
  91. {
  92. // *FIX: need to replace with a better default scheme. RN
  93. motion = LLKeyframeMotion::create(id);
  94. }
  95. else
  96. {
  97. motion = constructor(id);
  98. }
  99. return motion;
  100. }
  101. //-----------------------------------------------------------------------------
  102. //-----------------------------------------------------------------------------
  103. // LLMotionController class
  104. //-----------------------------------------------------------------------------
  105. //-----------------------------------------------------------------------------
  106. //-----------------------------------------------------------------------------
  107. // LLMotionController()
  108. // Class Constructor
  109. //-----------------------------------------------------------------------------
  110. LLMotionController::LLMotionController()
  111. : mTimeFactor(1.f),
  112. mCharacter(NULL),
  113. mAnimTime(0.f),
  114. mPrevTimerElapsed(0.f),
  115. mLastTime(0.0f),
  116. mHasRunOnce(FALSE),
  117. mPaused(FALSE),
  118. mPauseTime(0.f),
  119. mTimeStep(0.f),
  120. mTimeStepCount(0),
  121. mLastInterp(0.f),
  122. mIsSelf(FALSE)
  123. {
  124. }
  125. //-----------------------------------------------------------------------------
  126. // ~LLMotionController()
  127. // Class Destructor
  128. //-----------------------------------------------------------------------------
  129. LLMotionController::~LLMotionController()
  130. {
  131. deleteAllMotions();
  132. }
  133. void LLMotionController::incMotionCounts(S32& num_motions, S32& num_loading_motions, S32& num_loaded_motions, S32& num_active_motions, S32& num_deprecated_motions)
  134. {
  135. num_motions += mAllMotions.size();
  136. num_loading_motions += mLoadingMotions.size();
  137. num_loaded_motions += mLoadedMotions.size();
  138. num_active_motions += mActiveMotions.size();
  139. num_deprecated_motions += mDeprecatedMotions.size();
  140. }
  141. //-----------------------------------------------------------------------------
  142. // deleteAllMotions()
  143. //-----------------------------------------------------------------------------
  144. void LLMotionController::deleteAllMotions()
  145. {
  146. mLoadingMotions.clear();
  147. mLoadedMotions.clear();
  148. mActiveMotions.clear();
  149. for_each(mAllMotions.begin(), mAllMotions.end(), DeletePairedPointer());
  150. mAllMotions.clear();
  151. }
  152. //-----------------------------------------------------------------------------
  153. // purgeExcessMotion()
  154. //-----------------------------------------------------------------------------
  155. void LLMotionController::purgeExcessMotions()
  156. {
  157. if (mLoadedMotions.size() > MAX_MOTION_INSTANCES)
  158. {
  159. // clean up deprecated motions
  160. for (motion_set_t::iterator deprecated_motion_it = mDeprecatedMotions.begin();
  161. deprecated_motion_it != mDeprecatedMotions.end(); )
  162. {
  163. motion_set_t::iterator cur_iter = deprecated_motion_it++;
  164. LLMotion* cur_motionp = *cur_iter;
  165. if (!isMotionActive(cur_motionp))
  166. {
  167. // Motion is deprecated so we know it's not cannonical,
  168. // we can safely remove the instance
  169. removeMotionInstance(cur_motionp); // modifies mDeprecatedMotions
  170. mDeprecatedMotions.erase(cur_iter);
  171. }
  172. }
  173. }
  174. std::set<LLUUID> motions_to_kill;
  175. if (mLoadedMotions.size() > MAX_MOTION_INSTANCES)
  176. {
  177. // too many motions active this frame, kill all blenders
  178. mPoseBlender.clearBlenders();
  179. for (motion_set_t::iterator loaded_motion_it = mLoadedMotions.begin();
  180. loaded_motion_it != mLoadedMotions.end();
  181. ++loaded_motion_it)
  182. {
  183. LLMotion* cur_motionp = *loaded_motion_it;
  184. // motion isn't playing, delete it
  185. if (!isMotionActive(cur_motionp))
  186. {
  187. motions_to_kill.insert(cur_motionp->getID());
  188. }
  189. }
  190. }
  191. // clean up all inactive, loaded motions
  192. for (std::set<LLUUID>::iterator motion_it = motions_to_kill.begin();
  193. motion_it != motions_to_kill.end();
  194. ++motion_it)
  195. {
  196. // look up the motion again by ID to get canonical instance
  197. // and kill it only if that one is inactive
  198. LLUUID motion_id = *motion_it;
  199. LLMotion* motionp = findMotion(motion_id);
  200. if (motionp && !isMotionActive(motionp))
  201. {
  202. removeMotion(motion_id);
  203. }
  204. }
  205. if (mLoadedMotions.size() > 2*MAX_MOTION_INSTANCES)
  206. {
  207. LL_WARNS_ONCE("Animation") << "> " << 2*MAX_MOTION_INSTANCES << " Loaded Motions" << llendl;
  208. }
  209. }
  210. //-----------------------------------------------------------------------------
  211. // deactivateStoppedMotions()
  212. //-----------------------------------------------------------------------------
  213. void LLMotionController::deactivateStoppedMotions()
  214. {
  215. // Since we're hidden, deactivate any stopped motions.
  216. for (motion_list_t::iterator iter = mActiveMotions.begin();
  217. iter != mActiveMotions.end(); )
  218. {
  219. motion_list_t::iterator curiter = iter++;
  220. LLMotion* motionp = *curiter;
  221. if (motionp->isStopped())
  222. {
  223. deactivateMotionInstance(motionp);
  224. }
  225. }
  226. }
  227. //-----------------------------------------------------------------------------
  228. // setTimeStep()
  229. //-----------------------------------------------------------------------------
  230. void LLMotionController::setTimeStep(F32 step)
  231. {
  232. mTimeStep = step;
  233. if (step != 0.f)
  234. {
  235. // make sure timestamps conform to new quantum
  236. for (motion_list_t::iterator iter = mActiveMotions.begin();
  237. iter != mActiveMotions.end(); ++iter)
  238. {
  239. LLMotion* motionp = *iter;
  240. F32 activation_time = motionp->mActivationTimestamp;
  241. motionp->mActivationTimestamp = (F32)(llfloor(activation_time / step)) * step;
  242. BOOL stopped = motionp->isStopped();
  243. motionp->setStopTime((F32)(llfloor(motionp->getStopTime() / step)) * step);
  244. motionp->setStopped(stopped);
  245. motionp->mSendStopTimestamp = (F32)llfloor(motionp->mSendStopTimestamp / step) * step;
  246. }
  247. }
  248. }
  249. //-----------------------------------------------------------------------------
  250. // setTimeFactor()
  251. //-----------------------------------------------------------------------------
  252. void LLMotionController::setTimeFactor(F32 time_factor)
  253. {
  254. mTimeFactor = time_factor;
  255. }
  256. //-----------------------------------------------------------------------------
  257. // setCharacter()
  258. //-----------------------------------------------------------------------------
  259. void LLMotionController::setCharacter(LLCharacter *character)
  260. {
  261. mCharacter = character;
  262. }
  263. //-----------------------------------------------------------------------------
  264. // registerMotion()
  265. //-----------------------------------------------------------------------------
  266. BOOL LLMotionController::registerMotion( const LLUUID& id, LLMotionConstructor constructor )
  267. {
  268. return sRegistry.registerMotion(id, constructor);
  269. }
  270. //-----------------------------------------------------------------------------
  271. // removeMotion()
  272. //-----------------------------------------------------------------------------
  273. void LLMotionController::removeMotion( const LLUUID& id)
  274. {
  275. LLMotion* motionp = findMotion(id);
  276. mAllMotions.erase(id);
  277. removeMotionInstance(motionp);
  278. }
  279. // removes instance of a motion from all runtime structures, but does
  280. // not erase entry by ID, as this could be a duplicate instance
  281. // use removeMotion(id) to remove all references to a given motion by id.
  282. void LLMotionController::removeMotionInstance(LLMotion* motionp)
  283. {
  284. if (motionp)
  285. {
  286. llassert(findMotion(motionp->getID()) != motionp);
  287. if (motionp->isActive())
  288. motionp->deactivate();
  289. mLoadingMotions.erase(motionp);
  290. mLoadedMotions.erase(motionp);
  291. mActiveMotions.remove(motionp);
  292. delete motionp;
  293. }
  294. }
  295. //-----------------------------------------------------------------------------
  296. // createMotion()
  297. //-----------------------------------------------------------------------------
  298. LLMotion* LLMotionController::createMotion( const LLUUID &id )
  299. {
  300. LLMemType mt(LLMemType::MTYPE_ANIMATION);
  301. // do we have an instance of this motion for this character?
  302. LLMotion *motion = findMotion(id);
  303. // if not, we need to create one
  304. if (!motion)
  305. {
  306. // look up constructor and create it
  307. motion = sRegistry.createMotion(id);
  308. if (!motion)
  309. {
  310. return NULL;
  311. }
  312. // look up name for default motions
  313. const char* motion_name = gAnimLibrary.animStateToString(id);
  314. if (motion_name)
  315. {
  316. motion->setName(motion_name);
  317. }
  318. // initialize the new instance
  319. LLMotion::LLMotionInitStatus stat = motion->onInitialize(mCharacter);
  320. switch(stat)
  321. {
  322. case LLMotion::STATUS_FAILURE:
  323. llinfos << "Motion " << id << " init failed." << llendl;
  324. sRegistry.markBad(id);
  325. delete motion;
  326. return NULL;
  327. case LLMotion::STATUS_HOLD:
  328. mLoadingMotions.insert(motion);
  329. break;
  330. case LLMotion::STATUS_SUCCESS:
  331. // add motion to our list
  332. mLoadedMotions.insert(motion);
  333. break;
  334. default:
  335. llerrs << "Invalid initialization status" << llendl;
  336. break;
  337. }
  338. mAllMotions[id] = motion;
  339. }
  340. return motion;
  341. }
  342. //-----------------------------------------------------------------------------
  343. // startMotion()
  344. //-----------------------------------------------------------------------------
  345. BOOL LLMotionController::startMotion(const LLUUID &id, F32 start_offset)
  346. {
  347. // do we have an instance of this motion for this character?
  348. LLMotion *motion = findMotion(id);
  349. // motion that is stopping will be allowed to stop but
  350. // replaced by a new instance of that motion
  351. if (motion
  352. && !mPaused
  353. && motion->canDeprecate()
  354. && motion->getFadeWeight() > 0.01f // not LOD-ed out
  355. && (motion->isBlending() || motion->getStopTime() != 0.f))
  356. {
  357. deprecateMotionInstance(motion);
  358. // force creation of new instance
  359. motion = NULL;
  360. }
  361. // create new motion instance
  362. if (!motion)
  363. {
  364. motion = createMotion(id);
  365. }
  366. if (!motion)
  367. {
  368. return FALSE;
  369. }
  370. //if the motion is already active and allows deprecation, then let it keep playing
  371. else if (motion->canDeprecate() && isMotionActive(motion))
  372. {
  373. return TRUE;
  374. }
  375. // llinfos << "Starting motion " << name << llendl;
  376. return activateMotionInstance(motion, mAnimTime - start_offset);
  377. }
  378. //-----------------------------------------------------------------------------
  379. // stopMotionLocally()
  380. //-----------------------------------------------------------------------------
  381. BOOL LLMotionController::stopMotionLocally(const LLUUID &id, BOOL stop_immediate)
  382. {
  383. // if already inactive, return false
  384. LLMotion *motion = findMotion(id);
  385. return stopMotionInstance(motion, stop_immediate);
  386. }
  387. BOOL LLMotionController::stopMotionInstance(LLMotion* motion, BOOL stop_immediate)
  388. {
  389. if (!motion)
  390. {
  391. return FALSE;
  392. }
  393. // If on active list, stop it
  394. if (isMotionActive(motion) && !motion->isStopped())
  395. {
  396. motion->setStopTime(mAnimTime);
  397. if (stop_immediate)
  398. {
  399. deactivateMotionInstance(motion);
  400. }
  401. return TRUE;
  402. }
  403. else if (isMotionLoading(motion))
  404. {
  405. motion->setStopped(TRUE);
  406. return TRUE;
  407. }
  408. return FALSE;
  409. }
  410. //-----------------------------------------------------------------------------
  411. // updateRegularMotions()
  412. //-----------------------------------------------------------------------------
  413. void LLMotionController::updateRegularMotions()
  414. {
  415. updateMotionsByType(LLMotion::NORMAL_BLEND);
  416. }
  417. //-----------------------------------------------------------------------------
  418. // updateAdditiveMotions()
  419. //-----------------------------------------------------------------------------
  420. void LLMotionController::updateAdditiveMotions()
  421. {
  422. updateMotionsByType(LLMotion::ADDITIVE_BLEND);
  423. }
  424. //-----------------------------------------------------------------------------
  425. // resetJointSignatures()
  426. //-----------------------------------------------------------------------------
  427. void LLMotionController::resetJointSignatures()
  428. {
  429. memset(&mJointSignature[0][0], 0, sizeof(U8) * LL_CHARACTER_MAX_JOINTS);
  430. memset(&mJointSignature[1][0], 0, sizeof(U8) * LL_CHARACTER_MAX_JOINTS);
  431. }
  432. //-----------------------------------------------------------------------------
  433. // updateIdleMotion()
  434. // minimal updates for active motions
  435. //-----------------------------------------------------------------------------
  436. void LLMotionController::updateIdleMotion(LLMotion* motionp)
  437. {
  438. if (motionp->isStopped() && mAnimTime > motionp->getStopTime() + motionp->getEaseOutDuration())
  439. {
  440. deactivateMotionInstance(motionp);
  441. }
  442. else if (motionp->isStopped() && mAnimTime > motionp->getStopTime())
  443. {
  444. // is this the first iteration in the ease out phase?
  445. if (mLastTime <= motionp->getStopTime())
  446. {
  447. // store residual weight for this motion
  448. motionp->mResidualWeight = motionp->getPose()->getWeight();
  449. }
  450. }
  451. else if (mAnimTime > motionp->mSendStopTimestamp)
  452. {
  453. // notify character of timed stop event on first iteration past sendstoptimestamp
  454. // this will only be called when an animation stops itself (runs out of time)
  455. if (mLastTime <= motionp->mSendStopTimestamp)
  456. {
  457. mCharacter->requestStopMotion( motionp );
  458. stopMotionInstance(motionp, FALSE);
  459. }
  460. }
  461. else if (mAnimTime >= motionp->mActivationTimestamp)
  462. {
  463. if (mLastTime < motionp->mActivationTimestamp)
  464. {
  465. motionp->mResidualWeight = motionp->getPose()->getWeight();
  466. }
  467. }
  468. }
  469. //-----------------------------------------------------------------------------
  470. // updateIdleActiveMotions()
  471. // Call this instead of updateMotionsByType for hidden avatars
  472. //-----------------------------------------------------------------------------
  473. void LLMotionController::updateIdleActiveMotions()
  474. {
  475. for (motion_list_t::iterator iter = mActiveMotions.begin();
  476. iter != mActiveMotions.end(); )
  477. {
  478. motion_list_t::iterator curiter = iter++;
  479. LLMotion* motionp = *curiter;
  480. updateIdleMotion(motionp);
  481. }
  482. }
  483. //-----------------------------------------------------------------------------
  484. // updateMotionsByType()
  485. //-----------------------------------------------------------------------------
  486. void LLMotionController::updateMotionsByType(LLMotion::LLMotionBlendType anim_type)
  487. {
  488. BOOL update_result = TRUE;
  489. U8 last_joint_signature[LL_CHARACTER_MAX_JOINTS];
  490. memset(&last_joint_signature, 0, sizeof(U8) * LL_CHARACTER_MAX_JOINTS);
  491. // iterate through active motions in chronological order
  492. for (motion_list_t::iterator iter = mActiveMotions.begin();
  493. iter != mActiveMotions.end(); )
  494. {
  495. motion_list_t::iterator curiter = iter++;
  496. LLMotion* motionp = *curiter;
  497. if (motionp->getBlendType() != anim_type)
  498. {
  499. continue;
  500. }
  501. BOOL update_motion = FALSE;
  502. if (motionp->getPose()->getWeight() < 1.f)
  503. {
  504. update_motion = TRUE;
  505. }
  506. else
  507. {
  508. // NUM_JOINT_SIGNATURE_STRIDES should be multiple of 4
  509. for (S32 i = 0; i < NUM_JOINT_SIGNATURE_STRIDES; i++)
  510. {
  511. U32 *current_signature = (U32*)&(mJointSignature[0][i * 4]);
  512. U32 test_signature = *(U32*)&(motionp->mJointSignature[0][i * 4]);
  513. if ((*current_signature | test_signature) > (*current_signature))
  514. {
  515. *current_signature |= test_signature;
  516. update_motion = TRUE;
  517. }
  518. *((U32*)&last_joint_signature[i * 4]) = *(U32*)&(mJointSignature[1][i * 4]);
  519. current_signature = (U32*)&(mJointSignature[1][i * 4]);
  520. test_signature = *(U32*)&(motionp->mJointSignature[1][i * 4]);
  521. if ((*current_signature | test_signature) > (*current_signature))
  522. {
  523. *current_signature |= test_signature;
  524. update_motion = TRUE;
  525. }
  526. }
  527. }
  528. if (!update_motion)
  529. {
  530. updateIdleMotion(motionp);
  531. continue;
  532. }
  533. LLPose *posep = motionp->getPose();
  534. // only filter by LOD after running every animation at least once (to prime the avatar state)
  535. if (mHasRunOnce && motionp->getMinPixelArea() > mCharacter->getPixelArea())
  536. {
  537. motionp->fadeOut();
  538. //should we notify the simulator that this motion should be stopped (check even if skipped by LOD logic)
  539. if (mAnimTime > motionp->mSendStopTimestamp)
  540. {
  541. // notify character of timed stop event on first iteration past sendstoptimestamp
  542. // this will only be called when an animation stops itself (runs out of time)
  543. if (mLastTime <= motionp->mSendStopTimestamp)
  544. {
  545. mCharacter->requestStopMotion( motionp );
  546. stopMotionInstance(motionp, FALSE);
  547. }
  548. }
  549. if (motionp->getFadeWeight() < 0.01f)
  550. {
  551. if (motionp->isStopped() && mAnimTime > motionp->getStopTime() + motionp->getEaseOutDuration())
  552. {
  553. posep->setWeight(0.f);
  554. deactivateMotionInstance(motionp);
  555. }
  556. continue;
  557. }
  558. }
  559. else
  560. {
  561. motionp->fadeIn();
  562. }
  563. //**********************
  564. // MOTION INACTIVE
  565. //**********************
  566. if (motionp->isStopped() && mAnimTime > motionp->getStopTime() + motionp->getEaseOutDuration())
  567. {
  568. // this motion has gone on too long, deactivate it
  569. // did we have a chance to stop it?
  570. if (mLastTime <= motionp->getStopTime())
  571. {
  572. // if not, let's stop it this time through and deactivate it the next
  573. posep->setWeight(motionp->getFadeWeight());
  574. motionp->onUpdate(motionp->getStopTime() - motionp->mActivationTimestamp, last_joint_signature);
  575. }
  576. else
  577. {
  578. posep->setWeight(0.f);
  579. deactivateMotionInstance(motionp);
  580. continue;
  581. }
  582. }
  583. //**********************
  584. // MOTION EASE OUT
  585. //**********************
  586. else if (motionp->isStopped() && mAnimTime > motionp->getStopTime())
  587. {
  588. // is this the first iteration in the ease out phase?
  589. if (mLastTime <= motionp->getStopTime())
  590. {
  591. // store residual weight for this motion
  592. motionp->mResidualWeight = motionp->getPose()->getWeight();
  593. }
  594. if (motionp->getEaseOutDuration() == 0.f)
  595. {
  596. posep->setWeight(0.f);
  597. }
  598. else
  599. {
  600. posep->setWeight(motionp->getFadeWeight() * motionp->mResidualWeight * cubic_step(1.f - ((mAnimTime - motionp->getStopTime()) / motionp->getEaseOutDuration())));
  601. }
  602. // perform motion update
  603. update_result = motionp->onUpdate(mAnimTime - motionp->mActivationTimestamp, last_joint_signature);
  604. }
  605. //**********************
  606. // MOTION ACTIVE
  607. //**********************
  608. else if (mAnimTime > motionp->mActivationTimestamp + motionp->getEaseInDuration())
  609. {
  610. posep->setWeight(motionp->getFadeWeight());
  611. //should we notify the simulator that this motion should be stopped?
  612. if (mAnimTime > motionp->mSendStopTimestamp)
  613. {
  614. // notify character of timed stop event on first iteration past sendstoptimestamp
  615. // this will only be called when an animation stops itself (runs out of time)
  616. if (mLastTime <= motionp->mSendStopTimestamp)
  617. {
  618. mCharacter->requestStopMotion( motionp );
  619. stopMotionInstance(motionp, FALSE);
  620. }
  621. }
  622. // perform motion update
  623. update_result = motionp->onUpdate(mAnimTime - motionp->mActivationTimestamp, last_joint_signature);
  624. }
  625. //**********************
  626. // MOTION EASE IN
  627. //**********************
  628. else if (mAnimTime >= motionp->mActivationTimestamp)
  629. {
  630. if (mLastTime < motionp->mActivationTimestamp)
  631. {
  632. motionp->mResidualWeight = motionp->getPose()->getWeight();
  633. }
  634. if (motionp->getEaseInDuration() == 0.f)
  635. {
  636. posep->setWeight(motionp->getFadeWeight());
  637. }
  638. else
  639. {
  640. // perform motion update
  641. posep->setWeight(motionp->getFadeWeight() * motionp->mResidualWeight + (1.f - motionp->mResidualWeight) * cubic_step((mAnimTime - motionp->mActivationTimestamp) / motionp->getEaseInDuration()));
  642. }
  643. // perform motion update
  644. update_result = motionp->onUpdate(mAnimTime - motionp->mActivationTimestamp, last_joint_signature);
  645. }
  646. else
  647. {
  648. posep->setWeight(0.f);
  649. update_result = motionp->onUpdate(0.f, last_joint_signature);
  650. }
  651. // allow motions to deactivate themselves
  652. if (!update_result)
  653. {
  654. if (!motionp->isStopped() || motionp->getStopTime() > mAnimTime)
  655. {
  656. // animation has stopped itself due to internal logic
  657. // propagate this to the network
  658. // as not all viewers are guaranteed to have access to the same logic
  659. mCharacter->requestStopMotion( motionp );
  660. stopMotionInstance(motionp, FALSE);
  661. }
  662. }
  663. // even if onupdate returns FALSE, add this motion in to the blend one last time
  664. mPoseBlender.addMotion(motionp);
  665. }
  666. }
  667. //-----------------------------------------------------------------------------
  668. // updateLoadingMotions()
  669. //-----------------------------------------------------------------------------
  670. void LLMotionController::updateLoadingMotions()
  671. {
  672. // query pending motions for completion
  673. for (motion_set_t::iterator iter = mLoadingMotions.begin();
  674. iter != mLoadingMotions.end(); )
  675. {
  676. motion_set_t::iterator curiter = iter++;
  677. LLMotion* motionp = *curiter;
  678. if( !motionp)
  679. {
  680. continue; // maybe shouldn't happen but i've seen it -MG
  681. }
  682. LLMotion::LLMotionInitStatus status = motionp->onInitialize(mCharacter);
  683. if (status == LLMotion::STATUS_SUCCESS)
  684. {
  685. mLoadingMotions.erase(curiter);
  686. // add motion to our loaded motion list
  687. mLoadedMotions.insert(motionp);
  688. // this motion should be playing
  689. if (!motionp->isStopped())
  690. {
  691. activateMotionInstance(motionp, mAnimTime);
  692. }
  693. }
  694. else if (status == LLMotion::STATUS_FAILURE)
  695. {
  696. llinfos << "Motion " << motionp->getID() << " init failed." << llendl;
  697. sRegistry.markBad(motionp->getID());
  698. mLoadingMotions.erase(curiter);
  699. motion_set_t::iterator found_it = mDeprecatedMotions.find(motionp);
  700. if (found_it != mDeprecatedMotions.end())
  701. {
  702. mDeprecatedMotions.erase(found_it);
  703. }
  704. mAllMotions.erase(motionp->getID());
  705. delete motionp;
  706. }
  707. }
  708. }
  709. //-----------------------------------------------------------------------------
  710. // call updateMotion() or updateMotionsMinimal() every frame
  711. //-----------------------------------------------------------------------------
  712. //-----------------------------------------------------------------------------
  713. // updateMotion()
  714. //-----------------------------------------------------------------------------
  715. void LLMotionController::updateMotions(bool force_update)
  716. {
  717. BOOL use_quantum = (mTimeStep != 0.f);
  718. // Always update mPrevTimerElapsed
  719. F32 cur_time = mTimer.getElapsedTimeF32();
  720. F32 delta_time = cur_time - mPrevTimerElapsed;
  721. mPrevTimerElapsed = cur_time;
  722. mLastTime = mAnimTime;
  723. // Always cap the number of loaded motions
  724. purgeExcessMotions();
  725. // Update timing info for this time step.
  726. if (!mPaused)
  727. {
  728. F32 update_time = mAnimTime + delta_time * mTimeFactor;
  729. if (use_quantum)
  730. {
  731. F32 time_interval = fmodf(update_time, mTimeStep);
  732. // always animate *ahead* of actual time
  733. S32 quantum_count = llmax(0, llfloor((update_time - time_interval) / mTimeStep)) + 1;
  734. if (quantum_count == mTimeStepCount)
  735. {
  736. // we're still in same time quantum as before, so just interpolate and exit
  737. if (!mPaused)
  738. {
  739. F32 interp = time_interval / mTimeStep;
  740. mPoseBlender.interpolate(interp - mLastInterp);
  741. mLastInterp = interp;
  742. }
  743. updateLoadingMotions();
  744. return;
  745. }
  746. // is calculating a new keyframe pose, make sure the last one gets applied
  747. mPoseBlender.interpolate(1.f);
  748. clearBlenders();
  749. mTimeStepCount = quantum_count;
  750. mAnimTime = (F32)quantum_count * mTimeStep;
  751. mLastInterp = 0.f;
  752. }
  753. else
  754. {
  755. mAnimTime = update_time;
  756. }
  757. }
  758. updateLoadingMotions();
  759. resetJointSignatures();
  760. if (mPaused && !force_update)
  761. {
  762. updateIdleActiveMotions();
  763. }
  764. else
  765. {
  766. // update additive motions
  767. updateAdditiveMotions();
  768. resetJointSignatures();
  769. // update all regular motions
  770. updateRegularMotions();
  771. if (use_quantum)
  772. {
  773. mPoseBlender.blendAndCache(TRUE);
  774. }
  775. else
  776. {
  777. mPoseBlender.blendAndApply();
  778. }
  779. }
  780. mHasRunOnce = TRUE;
  781. // llinfos << "Motion controller time " << motionTimer.getElapsedTimeF32() << llendl;
  782. }
  783. //-----------------------------------------------------------------------------
  784. // updateMotionsMinimal()
  785. // minimal update (e.g. while hidden)
  786. //-----------------------------------------------------------------------------
  787. void LLMotionController::updateMotionsMinimal()
  788. {
  789. // Always update mPrevTimerElapsed
  790. mPrevTimerElapsed = mTimer.getElapsedTimeF32();
  791. purgeExcessMotions();
  792. updateLoadingMotions();
  793. resetJointSignatures();
  794. deactivateStoppedMotions();
  795. mHasRunOnce = TRUE;
  796. }
  797. //-----------------------------------------------------------------------------
  798. // activateMotionInstance()
  799. //-----------------------------------------------------------------------------
  800. BOOL LLMotionController::activateMotionInstance(LLMotion *motion, F32 time)
  801. {
  802. // It's not clear why the getWeight() line seems to be crashing this, but
  803. // hopefully this fixes it.
  804. if (motion == NULL || motion->getPose() == NULL)
  805. {
  806. return FALSE;
  807. }
  808. if (mLoadingMotions.find(motion) != mLoadingMotions.end())
  809. {
  810. // we want to start this motion, but we can't yet, so flag it as started
  811. motion->setStopped(FALSE);
  812. // report pending animations as activated
  813. return TRUE;
  814. }
  815. motion->mResidualWeight = motion->getPose()->getWeight();
  816. // set stop time based on given duration and ease out time
  817. if (motion->getDuration() != 0.f && !motion->getLoop())
  818. {
  819. F32 ease_out_time;
  820. F32 motion_duration;
  821. // should we stop at the end of motion duration, or a bit earlier
  822. // to allow it to ease out while moving?
  823. ease_out_time = motion->getEaseOutDuration();
  824. // is the clock running when the motion is easing in?
  825. // if not (POSTURE_EASE) then we need to wait that much longer before triggering the stop
  826. motion_duration = llmax(motion->getDuration() - ease_out_time, 0.f);
  827. motion->mSendStopTimestamp = time + motion_duration;
  828. }
  829. else
  830. {
  831. motion->mSendStopTimestamp = F32_MAX;
  832. }
  833. if (motion->isActive())
  834. {
  835. mActiveMotions.remove(motion);
  836. }
  837. mActiveMotions.push_front(motion);
  838. motion->activate(time);
  839. motion->onUpdate(0.f, mJointSignature[1]);
  840. if (mAnimTime >= motion->mSendStopTimestamp)
  841. {
  842. motion->setStopTime(motion->mSendStopTimestamp);
  843. if (motion->mResidualWeight == 0.0f)
  844. {
  845. // bit of a hack; if newly activating a motion while easing out, weight should = 1
  846. motion->mResidualWeight = 1.f;
  847. }
  848. }
  849. return TRUE;
  850. }
  851. //-----------------------------------------------------------------------------
  852. // deactivateMotionInstance()
  853. //-----------------------------------------------------------------------------
  854. BOOL LLMotionController::deactivateMotionInstance(LLMotion *motion)
  855. {
  856. motion->deactivate();
  857. motion_set_t::iterator found_it = mDeprecatedMotions.find(motion);
  858. if (found_it != mDeprecatedMotions.end())
  859. {
  860. // deprecated motions need to be completely excised
  861. removeMotionInstance(motion);
  862. mDeprecatedMotions.erase(found_it);
  863. }
  864. else
  865. {
  866. // for motions that we are keeping, simply remove from active queue
  867. mActiveMotions.remove(motion);
  868. }
  869. return TRUE;
  870. }
  871. void LLMotionController::deprecateMotionInstance(LLMotion* motion)
  872. {
  873. mDeprecatedMotions.insert(motion);
  874. //fade out deprecated motion
  875. stopMotionInstance(motion, FALSE);
  876. //no longer canonical
  877. mAllMotions.erase(motion->getID());
  878. }
  879. //-----------------------------------------------------------------------------
  880. // isMotionActive()
  881. //-----------------------------------------------------------------------------
  882. bool LLMotionController::isMotionActive(LLMotion *motion)
  883. {
  884. return (motion && motion->isActive());
  885. }
  886. //-----------------------------------------------------------------------------
  887. // isMotionLoading()
  888. //-----------------------------------------------------------------------------
  889. bool LLMotionController::isMotionLoading(LLMotion* motion)
  890. {
  891. return (mLoadingMotions.find(motion) != mLoadingMotions.end());
  892. }
  893. //-----------------------------------------------------------------------------
  894. // findMotion()
  895. //-----------------------------------------------------------------------------
  896. LLMotion* LLMotionController::findMotion(const LLUUID& id) const
  897. {
  898. motion_map_t::const_iterator iter = mAllMotions.find(id);
  899. if(iter == mAllMotions.end())
  900. {
  901. return NULL;
  902. }
  903. else
  904. {
  905. return iter->second;
  906. }
  907. }
  908. //-----------------------------------------------------------------------------
  909. // dumpMotions()
  910. //-----------------------------------------------------------------------------
  911. void LLMotionController::dumpMotions()
  912. {
  913. llinfos << "=====================================" << llendl;
  914. for (motion_map_t::iterator iter = mAllMotions.begin();
  915. iter != mAllMotions.end(); iter++)
  916. {
  917. LLUUID id = iter->first;
  918. std::string state_string;
  919. LLMotion *motion = iter->second;
  920. if (mLoadingMotions.find(motion) != mLoadingMotions.end())
  921. state_string += std::string("l");
  922. if (mLoadedMotions.find(motion) != mLoadedMotions.end())
  923. state_string += std::string("L");
  924. if (std::find(mActiveMotions.begin(), mActiveMotions.end(), motion)!=mActiveMotions.end())
  925. state_string += std::string("A");
  926. if (mDeprecatedMotions.find(motion) != mDeprecatedMotions.end())
  927. state_string += std::string("D");
  928. llinfos << gAnimLibrary.animationName(id) << " " << state_string << llendl;
  929. }
  930. }
  931. //-----------------------------------------------------------------------------
  932. // deactivateAllMotions()
  933. //-----------------------------------------------------------------------------
  934. void LLMotionController::deactivateAllMotions()
  935. {
  936. for (motion_map_t::iterator iter = mAllMotions.begin();
  937. iter != mAllMotions.end(); iter++)
  938. {
  939. LLMotion* motionp = iter->second;
  940. deactivateMotionInstance(motionp);
  941. }
  942. }
  943. //-----------------------------------------------------------------------------
  944. // flushAllMotions()
  945. //-----------------------------------------------------------------------------
  946. void LLMotionController::flushAllMotions()
  947. {
  948. std::vector<std::pair<LLUUID,F32> > active_motions;
  949. active_motions.reserve(mActiveMotions.size());
  950. for (motion_list_t::iterator iter = mActiveMotions.begin();
  951. iter != mActiveMotions.end(); )
  952. {
  953. motion_list_t::iterator curiter = iter++;
  954. LLMotion* motionp = *curiter;
  955. F32 dtime = mAnimTime - motionp->mActivationTimestamp;
  956. active_motions.push_back(std::make_pair(motionp->getID(),dtime));
  957. motionp->deactivate(); // don't call deactivateMotionInstance() because we are going to reactivate it
  958. }
  959. mActiveMotions.clear();
  960. // delete all motion instances
  961. deleteAllMotions();
  962. // kill current hand pose that was previously called out by
  963. // keyframe motion
  964. mCharacter->removeAnimationData("Hand Pose");
  965. // restart motions
  966. for (std::vector<std::pair<LLUUID,F32> >::iterator iter = active_motions.begin();
  967. iter != active_motions.end(); ++iter)
  968. {
  969. startMotion(iter->first, iter->second);
  970. }
  971. }
  972. //-----------------------------------------------------------------------------
  973. // pause()
  974. //-----------------------------------------------------------------------------
  975. void LLMotionController::pauseAllMotions()
  976. {
  977. if (!mPaused)
  978. {
  979. //llinfos << "Pausing animations..." << llendl;
  980. mPaused = TRUE;
  981. }
  982. }
  983. //-----------------------------------------------------------------------------
  984. // unpause()
  985. //-----------------------------------------------------------------------------
  986. void LLMotionController::unpauseAllMotions()
  987. {
  988. if (mPaused)
  989. {
  990. //llinfos << "Unpausing animations..." << llendl;
  991. mPaused = FALSE;
  992. }
  993. }
  994. // End