llpose.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /**
  2. * @file llpose.cpp
  3. * @brief Implementation of LLPose class.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llpose.h"
  34. #include "llmotion.h"
  35. #include "llmath.h"
  36. #include "llstl.h"
  37. //-----------------------------------------------------------------------------
  38. // LLPose class
  39. //-----------------------------------------------------------------------------
  40. LLPose::LLPose()
  41. : mWeight(0.f)
  42. {
  43. }
  44. LLJointState* LLPose::getFirstJointState()
  45. {
  46. mListIter = mJointMap.begin();
  47. if (mListIter == mJointMap.end())
  48. {
  49. return NULL;
  50. }
  51. return mListIter->second;
  52. }
  53. LLJointState* LLPose::getNextJointState()
  54. {
  55. if (++mListIter == mJointMap.end())
  56. {
  57. return NULL;
  58. }
  59. return mListIter->second;
  60. }
  61. bool LLPose::addJointState(const LLPointer<LLJointState>& jstate)
  62. {
  63. U32 joint_key = jstate->getJoint()->getKey();
  64. if (!mJointMap.count(joint_key))
  65. {
  66. mJointMap[joint_key] = jstate;
  67. }
  68. return true;
  69. }
  70. bool LLPose::removeJointState(const LLPointer<LLJointState>& jstate)
  71. {
  72. mJointMap.erase(jstate->getJoint()->getKey());
  73. return true;
  74. }
  75. bool LLPose::removeAllJointStates()
  76. {
  77. mJointMap.clear();
  78. return true;
  79. }
  80. LLJointState* LLPose::findJointState(LLJoint* joint)
  81. {
  82. joint_map_iterator iter = mJointMap.find(joint->getKey());
  83. if (iter == mJointMap.end())
  84. {
  85. return NULL;
  86. }
  87. return iter->second;
  88. }
  89. LLJointState* LLPose::findJointState(U32 key)
  90. {
  91. joint_map_iterator iter = mJointMap.find(key);
  92. if (iter == mJointMap.end())
  93. {
  94. return NULL;
  95. }
  96. return iter->second;
  97. }
  98. void LLPose::setWeight(F32 weight)
  99. {
  100. joint_map_iterator iter;
  101. for (iter = mJointMap.begin(); iter != mJointMap.end(); ++iter)
  102. {
  103. LLJointState* js = iter->second;
  104. if (js)
  105. {
  106. js->setWeight(weight);
  107. }
  108. }
  109. mWeight = weight;
  110. }
  111. //-----------------------------------------------------------------------------
  112. // LLJointStateBlender class
  113. //-----------------------------------------------------------------------------
  114. LLJointStateBlender::LLJointStateBlender()
  115. {
  116. for (S32 i = 0; i < JSB_NUM_JOINT_STATES; ++i)
  117. {
  118. mJointStates[i] = NULL;
  119. mPriorities[i] = S32_MIN;
  120. mAdditiveBlends[i] = false;
  121. }
  122. }
  123. bool LLJointStateBlender::addJointState(const LLPointer<LLJointState>& joint_state,
  124. S32 priority, bool additive_blend)
  125. {
  126. llassert(joint_state);
  127. if (!joint_state->getJoint())
  128. {
  129. // This joint state doesn't point to an actual joint, so we do not care
  130. // about applying it
  131. return false;
  132. }
  133. for (S32 i = 0; i < JSB_NUM_JOINT_STATES; ++i)
  134. {
  135. if (mJointStates[i].isNull())
  136. {
  137. mJointStates[i] = joint_state;
  138. mPriorities[i] = priority;
  139. mAdditiveBlends[i] = additive_blend;
  140. return true;
  141. }
  142. if (priority > mPriorities[i])
  143. {
  144. // We are at a higher priority than the current joint state in this
  145. // slot so shift everyone over previous joint states (newer
  146. // motions) with same priority and source motion should stay in
  147. // place
  148. for (S32 j = JSB_NUM_JOINT_STATES - 1; j > i; --j)
  149. {
  150. mJointStates[j] = mJointStates[j - 1];
  151. mPriorities[j] = mPriorities[j - 1];
  152. mAdditiveBlends[j] = mAdditiveBlends[j - 1];
  153. }
  154. // now store ourselves in this slot
  155. mJointStates[i] = joint_state;
  156. mPriorities[i] = priority;
  157. mAdditiveBlends[i] = additive_blend;
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. void LLJointStateBlender::blendJointStates(bool apply_now)
  164. {
  165. // We need at least one joint to blend; if there is one, it will be in slot
  166. // zero according to insertion logic. Instead of resetting joint state to
  167. // default, just leave it unchanged from last frame.
  168. if (mJointStates[0].isNull())
  169. {
  170. return;
  171. }
  172. LLJoint* target_joint = apply_now ? mJointStates[0]->getJoint()
  173. : &mJointCache;
  174. constexpr S32 POS_WEIGHT = 0;
  175. constexpr S32 ROT_WEIGHT = 1;
  176. constexpr S32 SCALE_WEIGHT = 2;
  177. F32 sum_weights[3];
  178. U32 sum_usage = 0;
  179. LLVector3 blended_pos = target_joint->getPosition();
  180. LLQuaternion blended_rot = target_joint->getRotation();
  181. LLVector3 blended_scale = target_joint->getScale();
  182. LLVector3 added_pos, added_scale;
  183. LLQuaternion added_rot;
  184. sum_weights[POS_WEIGHT] = 0.f;
  185. sum_weights[ROT_WEIGHT] = 0.f;
  186. sum_weights[SCALE_WEIGHT] = 0.f;
  187. for (S32 joint_state_index = 0;
  188. joint_state_index < JSB_NUM_JOINT_STATES &&
  189. mJointStates[joint_state_index].notNull();
  190. ++joint_state_index)
  191. {
  192. LLJointState* jsp = mJointStates[joint_state_index];
  193. U32 current_usage = jsp->getUsage();
  194. F32 current_weight = jsp->getWeight();
  195. if (current_weight == 0.f)
  196. {
  197. continue;
  198. }
  199. if (mAdditiveBlends[joint_state_index])
  200. {
  201. if (current_usage & LLJointState::POS)
  202. {
  203. F32 new_weight_sum = llmin(1.f,
  204. current_weight +
  205. sum_weights[POS_WEIGHT]);
  206. // Add in pos for this jointstate modulated by weight
  207. added_pos += jsp->getPosition() *
  208. (new_weight_sum - sum_weights[POS_WEIGHT]);
  209. }
  210. if (current_usage & LLJointState::SCALE)
  211. {
  212. F32 new_weight_sum = llmin(1.f,
  213. current_weight +
  214. sum_weights[SCALE_WEIGHT]);
  215. // Add in scale for this jointstate modulated by weight
  216. added_scale += jsp->getScale() *
  217. (new_weight_sum - sum_weights[SCALE_WEIGHT]);
  218. }
  219. if (current_usage & LLJointState::ROT)
  220. {
  221. F32 new_weight_sum = llmin(1.f,
  222. current_weight +
  223. sum_weights[ROT_WEIGHT]);
  224. // Add in rotation for this jointstate modulated by weight
  225. added_rot = nlerp(new_weight_sum - sum_weights[ROT_WEIGHT],
  226. added_rot, jsp->getRotation()) * added_rot;
  227. }
  228. }
  229. else // Blend two jointstates together
  230. {
  231. // Blend position
  232. if (current_usage & LLJointState::POS)
  233. {
  234. if (sum_usage & LLJointState::POS)
  235. {
  236. F32 new_weight_sum = llmin(1.f,
  237. current_weight +
  238. sum_weights[POS_WEIGHT]);
  239. // Blend positions from both
  240. blended_pos = lerp(jsp->getPosition(), blended_pos,
  241. sum_weights[POS_WEIGHT] /
  242. new_weight_sum);
  243. sum_weights[POS_WEIGHT] = new_weight_sum;
  244. }
  245. else
  246. {
  247. // Copy position from current
  248. blended_pos = jsp->getPosition();
  249. sum_weights[POS_WEIGHT] = current_weight;
  250. }
  251. }
  252. // Now do scale
  253. if (current_usage & LLJointState::SCALE)
  254. {
  255. if (sum_usage & LLJointState::SCALE)
  256. {
  257. F32 new_weight_sum = llmin(1.f,
  258. current_weight +
  259. sum_weights[SCALE_WEIGHT]);
  260. // blend scales from both
  261. blended_scale = lerp(jsp->getScale(), blended_scale,
  262. sum_weights[SCALE_WEIGHT] /
  263. new_weight_sum);
  264. sum_weights[SCALE_WEIGHT] = new_weight_sum;
  265. }
  266. else
  267. {
  268. // Copy scale from current
  269. blended_scale = jsp->getScale();
  270. sum_weights[SCALE_WEIGHT] = current_weight;
  271. }
  272. }
  273. // Rotation
  274. if (current_usage & LLJointState::ROT)
  275. {
  276. if (sum_usage & LLJointState::ROT)
  277. {
  278. F32 new_weight_sum = llmin(1.f,
  279. current_weight +
  280. sum_weights[ROT_WEIGHT]);
  281. // Blend rotations from both
  282. blended_rot = nlerp(sum_weights[ROT_WEIGHT] /
  283. new_weight_sum, jsp->getRotation(),
  284. blended_rot);
  285. sum_weights[ROT_WEIGHT] = new_weight_sum;
  286. }
  287. else
  288. {
  289. // Copy rotation from current
  290. blended_rot = jsp->getRotation();
  291. sum_weights[ROT_WEIGHT] = current_weight;
  292. }
  293. }
  294. // Update resulting usage mask
  295. sum_usage = sum_usage | current_usage;
  296. }
  297. }
  298. if (!added_scale.isFinite())
  299. {
  300. added_scale.clear();
  301. }
  302. if (!blended_scale.isFinite())
  303. {
  304. blended_scale.set(1, 1, 1);
  305. }
  306. // Apply transforms
  307. target_joint->setPosition(blended_pos + added_pos);
  308. target_joint->setScale(blended_scale + added_scale);
  309. target_joint->setRotation(added_rot * blended_rot);
  310. if (apply_now)
  311. {
  312. // Now clear joint states
  313. for (S32 i = 0; i < JSB_NUM_JOINT_STATES; ++i)
  314. {
  315. mJointStates[i] = NULL;
  316. }
  317. }
  318. }
  319. void LLJointStateBlender::interpolate(F32 u)
  320. {
  321. // Only interpolate if we have a joint state
  322. if (!mJointStates[0])
  323. {
  324. return;
  325. }
  326. LLJoint* target_joint = mJointStates[0]->getJoint();
  327. if (!target_joint)
  328. {
  329. return;
  330. }
  331. target_joint->setPosition(lerp(target_joint->getPosition(),
  332. mJointCache.getPosition(), u));
  333. target_joint->setScale(lerp(target_joint->getScale(),
  334. mJointCache.getScale(), u));
  335. target_joint->setRotation(nlerp(u, target_joint->getRotation(),
  336. mJointCache.getRotation()));
  337. }
  338. void LLJointStateBlender::clear()
  339. {
  340. for (S32 i = 0; i < JSB_NUM_JOINT_STATES; ++i)
  341. {
  342. mJointStates[i] = NULL;
  343. }
  344. }
  345. void LLJointStateBlender::resetCachedJoint()
  346. {
  347. if (mJointStates[0])
  348. {
  349. LLJoint* source_joint = mJointStates[0]->getJoint();
  350. if (source_joint)
  351. {
  352. mJointCache.setPosition(source_joint->getPosition());
  353. mJointCache.setScale(source_joint->getScale());
  354. mJointCache.setRotation(source_joint->getRotation());
  355. }
  356. }
  357. }
  358. //-----------------------------------------------------------------------------
  359. // LLPoseBlender class
  360. //-----------------------------------------------------------------------------
  361. LLPoseBlender::LLPoseBlender()
  362. : mNextPoseSlot(0)
  363. {
  364. }
  365. LLPoseBlender::~LLPoseBlender()
  366. {
  367. for_each(mJointStateBlenderPool.begin(), mJointStateBlenderPool.end(),
  368. DeletePairedPointer());
  369. mJointStateBlenderPool.clear();
  370. }
  371. bool LLPoseBlender::addMotion(LLMotion* motion)
  372. {
  373. if (!motion) return false;
  374. LLPose* pose = motion->getPose();
  375. if (!pose) return false;
  376. for (LLJointState* jsp = pose->getFirstJointState(); jsp;
  377. jsp = pose->getNextJointState())
  378. {
  379. LLJoint* jointp = jsp->getJoint();
  380. blender_map_t::iterator it = mJointStateBlenderPool.find(jointp);
  381. LLJointStateBlender* joint_blender;
  382. if (it == mJointStateBlenderPool.end())
  383. {
  384. // This is the first time we are animating this joint, so create a
  385. // new jointblender and add it to our pool
  386. joint_blender = new LLJointStateBlender();
  387. mJointStateBlenderPool[jointp] = joint_blender;
  388. }
  389. else
  390. {
  391. joint_blender = it->second;
  392. }
  393. bool additive = motion->getBlendType() == LLMotion::ADDITIVE_BLEND;
  394. if (jsp->getPriority() == LLJoint::USE_MOTION_PRIORITY)
  395. {
  396. joint_blender->addJointState(jsp, motion->getPriority(), additive);
  397. }
  398. else
  399. {
  400. joint_blender->addJointState(jsp, jsp->getPriority(), additive);
  401. }
  402. // Add it to our list of active blenders
  403. if (std::find(mActiveBlenders.begin(), mActiveBlenders.end(),
  404. joint_blender) == mActiveBlenders.end())
  405. {
  406. mActiveBlenders.push_front(joint_blender);
  407. }
  408. }
  409. return true;
  410. }
  411. void LLPoseBlender::blendAndApply()
  412. {
  413. for (blender_list_t::iterator iter = mActiveBlenders.begin();
  414. iter != mActiveBlenders.end(); )
  415. {
  416. LLJointStateBlender* jsbp = *iter++;
  417. if (jsbp)
  418. {
  419. jsbp->blendJointStates();
  420. }
  421. }
  422. // We are done now so there are no more active blenders for this frame
  423. mActiveBlenders.clear();
  424. }
  425. void LLPoseBlender::blendAndCache(bool reset_cached_joints)
  426. {
  427. for (blender_list_t::iterator iter = mActiveBlenders.begin();
  428. iter != mActiveBlenders.end(); ++iter)
  429. {
  430. LLJointStateBlender* jsbp = *iter;
  431. if (!jsbp) continue;
  432. if (reset_cached_joints)
  433. {
  434. jsbp->resetCachedJoint();
  435. }
  436. jsbp->blendJointStates(false);
  437. }
  438. }
  439. void LLPoseBlender::interpolate(F32 u)
  440. {
  441. for (blender_list_t::iterator iter = mActiveBlenders.begin();
  442. iter != mActiveBlenders.end(); ++iter)
  443. {
  444. LLJointStateBlender* jsbp = *iter;
  445. if (jsbp)
  446. {
  447. jsbp->interpolate(u);
  448. }
  449. }
  450. }
  451. void LLPoseBlender::clearBlenders()
  452. {
  453. for (blender_list_t::iterator iter = mActiveBlenders.begin();
  454. iter != mActiveBlenders.end(); ++iter)
  455. {
  456. LLJointStateBlender* jsbp = *iter;
  457. if (jsbp)
  458. {
  459. jsbp->clear();
  460. }
  461. }
  462. mActiveBlenders.clear();
  463. }