lljoint.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /**
  2. * @file lljoint.cpp
  3. * @brief Implementation of LLJoint 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 "lljoint.h"
  34. #include "llmath.h"
  35. S32 LLJoint::sNumUpdates = 0;
  36. S32 LLJoint::sNumTouches = 0;
  37. std::vector<std::string> LLJoint::sJointNamesList;
  38. joint_alias_map_t LLJoint::sAvatarJointAliasMap;
  39. template <class T>
  40. LL_INLINE bool attachment_map_iter_compare_key(const T& a, const T& b)
  41. {
  42. return a.first < b.first;
  43. }
  44. bool LLVector3OverrideMap::findActiveOverride(LLUUID& mesh_id,
  45. LLVector3& pos) const
  46. {
  47. pos.setZero();
  48. mesh_id.setNull();
  49. bool found = false;
  50. map_t::const_iterator it =
  51. std::max_element(mMap.begin(), mMap.end(),
  52. attachment_map_iter_compare_key<map_t::value_type>);
  53. if (it != mMap.end())
  54. {
  55. found = true;
  56. pos = it->second;
  57. mesh_id = it->first;
  58. }
  59. return found;
  60. }
  61. void LLVector3OverrideMap::showJointVector3Overrides(std::ostringstream& os) const
  62. {
  63. map_t::const_iterator max_it =
  64. std::max_element(mMap.begin(), mMap.end(),
  65. attachment_map_iter_compare_key<map_t::value_type>);
  66. for (map_t::const_iterator it = mMap.begin(), end = mMap.end();
  67. it != end; ++it)
  68. {
  69. const LLVector3& pos = it->second;
  70. os << " " << "[" << it->first <<": " << pos << "]"
  71. << (it == max_it ? "*" : "");
  72. }
  73. }
  74. //static
  75. U32 LLJoint::getKey(const std::string& name, bool add_if_unknown)
  76. {
  77. if (sJointNamesList.empty())
  78. {
  79. // These are the "well known" joints (used in the code). If you change
  80. // this list, be sure to change the corresponding constants in
  81. // lljoint.h !
  82. sJointNamesList.reserve(LL_CHARACTER_MAX_ANIMATED_JOINTS + 1);
  83. sJointNamesList.emplace_back("unnamed");
  84. sJointNamesList.emplace_back("mScreen");
  85. sJointNamesList.emplace_back("mRoot");
  86. sJointNamesList.emplace_back("mPelvis");
  87. sJointNamesList.emplace_back("mTorso");
  88. sJointNamesList.emplace_back("mChest");
  89. sJointNamesList.emplace_back("mNeck");
  90. sJointNamesList.emplace_back("mHead");
  91. sJointNamesList.emplace_back("mSkull");
  92. sJointNamesList.emplace_back("mHipLeft");
  93. sJointNamesList.emplace_back("mHipRight");
  94. sJointNamesList.emplace_back("mKneeLeft");
  95. sJointNamesList.emplace_back("mKneeRight");
  96. sJointNamesList.emplace_back("mAnkleLeft");
  97. sJointNamesList.emplace_back("mAnkleRight");
  98. sJointNamesList.emplace_back("mFootLeft");
  99. sJointNamesList.emplace_back("mFootRight");
  100. sJointNamesList.emplace_back("mWristLeft");
  101. sJointNamesList.emplace_back("mWristRight");
  102. sJointNamesList.emplace_back("mEyeLeft");
  103. sJointNamesList.emplace_back("mEyeRight");
  104. sJointNamesList.emplace_back("mElbowLeft");
  105. sJointNamesList.emplace_back("mShoulderLeft");
  106. sJointNamesList.emplace_back("mFaceEyeAltLeft");
  107. sJointNamesList.emplace_back("mFaceEyeAltRight");
  108. }
  109. for (U32 i = 0, count = sJointNamesList.size(); i < count; ++i)
  110. {
  111. if (sJointNamesList[i] == name)
  112. {
  113. return i;
  114. }
  115. }
  116. U32 ret = 0;
  117. if (add_if_unknown)
  118. {
  119. ret = sJointNamesList.size();
  120. sJointNamesList.emplace_back(name);
  121. }
  122. return ret;
  123. }
  124. //static
  125. U32 LLJoint::getAliasedJointKey(const std::string& name)
  126. {
  127. U32 key = getKey(name, false);
  128. if (key != 0)
  129. {
  130. return key;
  131. }
  132. joint_alias_map_t::const_iterator it = sAvatarJointAliasMap.find(name);
  133. if (it == sAvatarJointAliasMap.end())
  134. {
  135. return 0;
  136. }
  137. return getKey(it->second, false);
  138. }
  139. //static
  140. const std::string& LLJoint::getName(U32 key)
  141. {
  142. return (size_t)key < sJointNamesList.size() ? sJointNamesList[key]
  143. : sJointNamesList[0];
  144. }
  145. void LLJoint::init()
  146. {
  147. mKey = 0;
  148. mParent = NULL;
  149. mXform.setScaleChildOffset(true);
  150. mXform.setScale(LLVector3(1.0f, 1.0f, 1.0f));
  151. mDirtyFlags = MATRIX_DIRTY | ROTATION_DIRTY | POSITION_DIRTY;
  152. mUpdateXform = true;
  153. mSupport = SUPPORT_BASE;
  154. mEnd = LLVector3(0.f, 0.f, 0.f);
  155. }
  156. LLJoint::LLJoint()
  157. : mJointNum(-1),
  158. mIsBone(false)
  159. {
  160. init();
  161. touch();
  162. }
  163. LLJoint::LLJoint(const std::string& name, LLJoint* parent)
  164. : mJointNum(-2),
  165. mIsBone(false)
  166. {
  167. init();
  168. mUpdateXform = false;
  169. setName(name);
  170. if (parent)
  171. {
  172. parent->addChild(this);
  173. }
  174. touch();
  175. }
  176. LLJoint::~LLJoint()
  177. {
  178. if (mParent)
  179. {
  180. mParent->removeChild(this);
  181. }
  182. removeAllChildren();
  183. }
  184. void LLJoint::setup(const std::string& name, LLJoint* parent)
  185. {
  186. setName(name);
  187. if (parent)
  188. {
  189. parent->addChild(this);
  190. }
  191. }
  192. void LLJoint::setSupport(const std::string& support_name)
  193. {
  194. if (support_name == "extended")
  195. {
  196. setSupport(SUPPORT_EXTENDED);
  197. }
  198. else if (support_name == "base")
  199. {
  200. setSupport(SUPPORT_BASE);
  201. }
  202. else
  203. {
  204. llwarns_once << "Unknown support base name: " << support_name
  205. << ". Using default support base." << llendl;
  206. setSupport(SUPPORT_BASE);
  207. }
  208. }
  209. void LLJoint::touch(U32 flags)
  210. {
  211. if ((flags | mDirtyFlags) != mDirtyFlags)
  212. {
  213. ++sNumTouches;
  214. mDirtyFlags |= flags;
  215. U32 child_flags = flags;
  216. if (flags & ROTATION_DIRTY)
  217. {
  218. child_flags |= POSITION_DIRTY;
  219. }
  220. for (S32 i = 0, count = mChildren.size(); i < count; ++i)
  221. {
  222. LLJoint* joint = mChildren[i];
  223. if (joint) // Paranoia
  224. {
  225. joint->touch(child_flags);
  226. }
  227. }
  228. }
  229. }
  230. void LLJoint::setJointNum(S32 joint_num)
  231. {
  232. mJointNum = joint_num;
  233. if (mJointNum + 2 >= (S32)LL_CHARACTER_MAX_ANIMATED_JOINTS)
  234. {
  235. llwarns << "Joint number " << joint_num << " + 2 is too large for "
  236. << LL_CHARACTER_MAX_ANIMATED_JOINTS << llendl;
  237. llassert(false);
  238. }
  239. }
  240. LLJoint* LLJoint::getRoot()
  241. {
  242. LLJoint* parent = getParent();
  243. return parent ? parent->getRoot() : this;
  244. }
  245. LLJoint* LLJoint::findJoint(U32 key)
  246. {
  247. if (key == mKey)
  248. {
  249. return this;
  250. }
  251. for (S32 i = 0, count = mChildren.size(); i < count; ++i)
  252. {
  253. LLJoint* joint = mChildren[i];
  254. if (joint) // Paranoia
  255. {
  256. LLJoint* found = joint->findJoint(key);
  257. if (found)
  258. {
  259. return found;
  260. }
  261. }
  262. }
  263. return NULL;
  264. }
  265. void LLJoint::addChild(LLJoint* joint)
  266. {
  267. if (!joint)
  268. {
  269. llwarns << "Tried to add a NULL joint (ignored) !" << llendl;
  270. return;
  271. }
  272. if (joint->mParent)
  273. {
  274. joint->mParent->removeChild(joint);
  275. }
  276. mChildren.push_back(joint);
  277. joint->mXform.setParent(&mXform);
  278. joint->mParent = this;
  279. joint->touch();
  280. }
  281. void LLJoint::removeChild(LLJoint* joint)
  282. {
  283. if (!joint)
  284. {
  285. llwarns << "Tried to remove a NULL joint (ignored) !" << llendl;
  286. return;
  287. }
  288. for (S32 i = 0, count = mChildren.size(); i < count; ++i)
  289. {
  290. if (joint == mChildren[i])
  291. {
  292. if (i < count - 1)
  293. {
  294. mChildren[i] = mChildren[count - 1];
  295. }
  296. mChildren.pop_back();
  297. joint->mXform.setParent(NULL);
  298. joint->mParent = NULL;
  299. joint->touch();
  300. return;
  301. }
  302. }
  303. }
  304. void LLJoint::removeAllChildren()
  305. {
  306. for (S32 i = 0, count = mChildren.size(); i < count; ++i)
  307. {
  308. LLJoint* joint = mChildren[i];
  309. if (joint) // Paranoia
  310. {
  311. joint->mXform.setParent(NULL);
  312. joint->mParent = NULL;
  313. joint->touch();
  314. }
  315. }
  316. mChildren.clear();
  317. }
  318. void LLJoint::setPosition(const LLVector3& requested_pos, bool do_override)
  319. {
  320. LLVector3 pos(requested_pos);
  321. if (do_override)
  322. {
  323. LLVector3 active_override;
  324. LLUUID mesh_id;
  325. if (mAttachmentPosOverrides.findActiveOverride(mesh_id,
  326. active_override))
  327. {
  328. pos = active_override;
  329. LL_DEBUGS("Avatar") << "Joint: " << getName()
  330. << " - Requested pos: " << requested_pos
  331. << " overridden by attachment to: " << pos
  332. << LL_ENDL;
  333. }
  334. }
  335. if (pos != mXform.getPosition())
  336. {
  337. mXform.setPosition(pos);
  338. touch(MATRIX_DIRTY | POSITION_DIRTY);
  339. }
  340. }
  341. void showJointPosOverrides(const LLJoint& joint, const std::string& note,
  342. const std::string& av_info)
  343. {
  344. std::ostringstream os;
  345. os << joint.mPosBeforeOverrides;
  346. joint.mAttachmentPosOverrides.showJointVector3Overrides(os);
  347. LL_DEBUGS("Avatar") << "Avatar: " << av_info
  348. << " - Joint: " << joint.getName()
  349. << " " << note << " " << os.str() << LL_ENDL;
  350. }
  351. void showJointScaleOverrides(const LLJoint& joint, const std::string& note,
  352. const std::string& av_info)
  353. {
  354. std::ostringstream os;
  355. os << joint.mScaleBeforeOverrides;
  356. joint.mAttachmentScaleOverrides.showJointVector3Overrides(os);
  357. LL_DEBUGS("Avatar") << "Avatar: " << av_info
  358. << " - Joint: " << joint.getName()
  359. << " " << note << " " << os.str() << LL_ENDL;
  360. }
  361. constexpr F32 MAX_SQUARED_OFFSET = LL_JOINT_TRESHOLD_POS_OFFSET *
  362. LL_JOINT_TRESHOLD_POS_OFFSET;
  363. bool LLJoint::aboveJointPosThreshold(const LLVector3& pos) const
  364. {
  365. LLVector3 diff = pos - mDefaultPosition;
  366. return diff.lengthSquared() > MAX_SQUARED_OFFSET;
  367. }
  368. bool LLJoint::aboveJointScaleThreshold(const LLVector3& scale) const
  369. {
  370. LLVector3 diff = scale - mDefaultScale;
  371. return diff.lengthSquared() > MAX_SQUARED_OFFSET;
  372. }
  373. void LLJoint::addAttachmentPosOverride(const LLVector3& pos,
  374. const LLUUID& mesh_id,
  375. const std::string& av_info,
  376. bool* active_override_changed)
  377. {
  378. if (active_override_changed)
  379. {
  380. *active_override_changed = false;
  381. }
  382. if (mesh_id.isNull())
  383. {
  384. return;
  385. }
  386. LLUUID id;
  387. LLVector3 before_pos;
  388. bool active_override_before = hasAttachmentPosOverride(before_pos, id);
  389. if (!mAttachmentPosOverrides.count())
  390. {
  391. LL_DEBUGS("Avatar") << "Avatar: " << av_info << " - Joint: "
  392. << getName() << " - Saving mPosBeforeOverrides: "
  393. << getPosition() << LL_ENDL;
  394. mPosBeforeOverrides = getPosition();
  395. }
  396. mAttachmentPosOverrides.add(mesh_id, pos);
  397. LLVector3 after_pos;
  398. hasAttachmentPosOverride(after_pos, id);
  399. if (!active_override_before || after_pos != before_pos)
  400. {
  401. if (active_override_changed)
  402. {
  403. *active_override_changed = true;
  404. }
  405. LL_DEBUGS("Avatar") << "Avatar: " << av_info << " - Joint: "
  406. << getName() << " - Position for mesh '"
  407. << mesh_id << "': " << pos << LL_ENDL;
  408. updatePos(av_info);
  409. }
  410. }
  411. void LLJoint::removeAttachmentPosOverride(const LLUUID& mesh_id,
  412. const std::string& av_info,
  413. bool* active_override_changed)
  414. {
  415. if (active_override_changed)
  416. {
  417. *active_override_changed = false;
  418. }
  419. if (mesh_id.isNull())
  420. {
  421. return;
  422. }
  423. LLUUID id;
  424. LLVector3 before_pos;
  425. bool active_override_before = hasAttachmentPosOverride(before_pos, id);
  426. if (mAttachmentPosOverrides.remove(mesh_id))
  427. {
  428. LLVector3 after_pos;
  429. hasAttachmentPosOverride(after_pos, id);
  430. if (!active_override_before || after_pos != before_pos)
  431. {
  432. if (active_override_changed)
  433. {
  434. *active_override_changed = true;
  435. }
  436. bool show = false;
  437. LL_DEBUGS("Avatar") << "Avatar: " << av_info;
  438. show = true;
  439. LL_CONT << " - Joint: " << getName()
  440. << " - Removing pos override for mesh: " << mesh_id
  441. << LL_ENDL;
  442. if (show)
  443. {
  444. showJointPosOverrides(*this, "remove", av_info);
  445. }
  446. updatePos(av_info);
  447. }
  448. }
  449. }
  450. void LLJoint::clearAttachmentPosOverrides()
  451. {
  452. if (mAttachmentPosOverrides.count())
  453. {
  454. mAttachmentPosOverrides.clear();
  455. setPosition(mPosBeforeOverrides);
  456. }
  457. }
  458. S32 LLJoint::getAllAttachmentPosOverrides(std::set<LLVector3>& overrides) const
  459. {
  460. for (LLVector3OverrideMap::map_t::const_iterator
  461. it = mAttachmentPosOverrides.getMap().begin(),
  462. end = mAttachmentPosOverrides.getMap().end();
  463. it != end; ++it)
  464. {
  465. overrides.emplace(it->second);
  466. }
  467. return mAttachmentPosOverrides.count();
  468. }
  469. S32 LLJoint::getAllAttachmentScaleOverrides(std::set<LLVector3>& overrides) const
  470. {
  471. for (LLVector3OverrideMap::map_t::const_iterator
  472. it = mAttachmentScaleOverrides.getMap().begin(),
  473. end = mAttachmentScaleOverrides.getMap().end();
  474. it != end; ++it)
  475. {
  476. overrides.emplace(it->second);
  477. }
  478. return mAttachmentScaleOverrides.count();
  479. }
  480. void LLJoint::updatePos(const std::string& av_info)
  481. {
  482. LLVector3 pos, found_pos;
  483. LLUUID mesh_id;
  484. if (mAttachmentPosOverrides.findActiveOverride(mesh_id, found_pos))
  485. {
  486. LL_DEBUGS("Avatar") << "Avatar: " << av_info
  487. << " - Joint: " << getName()
  488. << " - Winner of "
  489. << mAttachmentPosOverrides.count()
  490. << " is mesh " << mesh_id
  491. << " - Position = " << found_pos << LL_ENDL;
  492. setPosition(found_pos);
  493. }
  494. else
  495. {
  496. LL_DEBUGS("Avatar") << "Avatar: " << av_info
  497. << " - Joint: " << getName()
  498. << " - Winner is mPosBeforeOverrides = "
  499. << mPosBeforeOverrides << LL_ENDL;
  500. setPosition(mPosBeforeOverrides);
  501. }
  502. }
  503. void LLJoint::updateScale(const std::string& av_info)
  504. {
  505. LLVector3 scale, found_scale;
  506. LLUUID mesh_id;
  507. if (mAttachmentScaleOverrides.findActiveOverride(mesh_id, found_scale))
  508. {
  509. LL_DEBUGS("Avatar") << "Avatar: " << av_info
  510. << " - Joint: " << getName()
  511. << " - Winner of "
  512. << mAttachmentScaleOverrides.count()
  513. << " is mesh " << mesh_id
  514. << " - Position = " << found_scale << LL_ENDL;
  515. setScale(found_scale);
  516. }
  517. else
  518. {
  519. LL_DEBUGS("Avatar") << "Avatar: " << av_info
  520. << " - Joint: " << getName()
  521. << " - Winner is mScaleBeforeOverrides = "
  522. << mPosBeforeOverrides << LL_ENDL;
  523. setScale(mScaleBeforeOverrides);
  524. }
  525. }
  526. void LLJoint::addAttachmentScaleOverride(const LLVector3& scale,
  527. const LLUUID& mesh_id,
  528. const std::string& av_info)
  529. {
  530. if (mesh_id.isNull())
  531. {
  532. return;
  533. }
  534. if (!mAttachmentScaleOverrides.count())
  535. {
  536. LL_DEBUGS("Avatar") << "Avatar: " << av_info << " - Joint: "
  537. << getName() << " - Saving mScaleBeforeOverrides: "
  538. << getScale() << LL_ENDL;
  539. mScaleBeforeOverrides = getScale();
  540. }
  541. mAttachmentScaleOverrides.add(mesh_id, scale);
  542. LL_DEBUGS("Avatar") << "Avatar: " << av_info << " - Joint: "
  543. << getName() << " - Scale for mesh '"
  544. << mesh_id << "': " << scale << LL_ENDL;
  545. updateScale(av_info);
  546. }
  547. void LLJoint::removeAttachmentScaleOverride(const LLUUID& mesh_id,
  548. const std::string& av_info)
  549. {
  550. if (mesh_id.isNull())
  551. {
  552. return;
  553. }
  554. if (mAttachmentScaleOverrides.remove(mesh_id))
  555. {
  556. bool show = false;
  557. LL_DEBUGS("Avatar") << "Avatar: " << av_info;
  558. show = true;
  559. LL_CONT << " - Joint: " << getName()
  560. << " - Removing scale override for mesh: " << mesh_id
  561. << LL_ENDL;
  562. if (show)
  563. {
  564. showJointScaleOverrides(*this, "remove", av_info);
  565. }
  566. updateScale(av_info);
  567. }
  568. }
  569. void LLJoint::clearAttachmentScaleOverrides()
  570. {
  571. if (mAttachmentScaleOverrides.count())
  572. {
  573. mAttachmentScaleOverrides.clear();
  574. setScale(mScaleBeforeOverrides);
  575. }
  576. }
  577. const LLVector3& LLJoint::getWorldPosition()
  578. {
  579. updateWorldPRSParent();
  580. return mXform.getWorldPosition();
  581. }
  582. const LLVector3& LLJoint::getLastWorldPosition() const
  583. {
  584. return mXform.getWorldPosition();
  585. }
  586. void LLJoint::setWorldPosition(const LLVector3& pos)
  587. {
  588. if (mParent == NULL)
  589. {
  590. setPosition(pos);
  591. return;
  592. }
  593. LLMatrix4 temp_matrix = getWorldMatrix();
  594. temp_matrix.mMatrix[VW][VX] = pos.mV[VX];
  595. temp_matrix.mMatrix[VW][VY] = pos.mV[VY];
  596. temp_matrix.mMatrix[VW][VZ] = pos.mV[VZ];
  597. LLMatrix4 parent_matrix = mParent->getWorldMatrix();
  598. temp_matrix *= parent_matrix.invert();
  599. LLVector3 local_pos(temp_matrix.mMatrix[VW][VX],
  600. temp_matrix.mMatrix[VW][VY],
  601. temp_matrix.mMatrix[VW][VZ]);
  602. setPosition(local_pos);
  603. }
  604. const LLQuaternion& LLJoint::getRotation() const
  605. {
  606. return mXform.getRotation();
  607. }
  608. void LLJoint::setRotation(const LLQuaternion& rot)
  609. {
  610. if (rot.isFinite())
  611. {
  612. #if 0
  613. if (mXform.getRotation() != rot)
  614. #endif
  615. {
  616. mXform.setRotation(rot);
  617. touch(MATRIX_DIRTY | ROTATION_DIRTY);
  618. }
  619. }
  620. }
  621. const LLQuaternion& LLJoint::getWorldRotation()
  622. {
  623. updateWorldPRSParent();
  624. return mXform.getWorldRotation();
  625. }
  626. const LLQuaternion& LLJoint::getLastWorldRotation()
  627. {
  628. return mXform.getWorldRotation();
  629. }
  630. void LLJoint::setWorldRotation(const LLQuaternion& rot)
  631. {
  632. if (mParent == NULL)
  633. {
  634. setRotation(rot);
  635. return;
  636. }
  637. LLMatrix4 temp_mat(rot);
  638. LLMatrix4 parent_matrix = mParent->getWorldMatrix();
  639. parent_matrix.mMatrix[VW][VX] = 0;
  640. parent_matrix.mMatrix[VW][VY] = 0;
  641. parent_matrix.mMatrix[VW][VZ] = 0;
  642. temp_mat *= parent_matrix.invert();
  643. setRotation(LLQuaternion(temp_mat));
  644. }
  645. const LLVector3& LLJoint::getScale() const
  646. {
  647. return mXform.getScale();
  648. }
  649. void LLJoint::setScale(const LLVector3& scale, bool apply_attachment_overrides)
  650. {
  651. if (apply_attachment_overrides)
  652. {
  653. LLUUID mesh_id;
  654. LLVector3 active_override;
  655. if (mAttachmentScaleOverrides.findActiveOverride(mesh_id,
  656. active_override))
  657. {
  658. if (scale != active_override)
  659. {
  660. LL_DEBUGS("Avatar") << "Joint: " << getName() << " - Mesh Id: "
  661. << mesh_id << " - Requested scale: "
  662. << scale << " overriden by attachment to: "
  663. << active_override << LL_ENDL;
  664. }
  665. mXform.setScale(active_override);
  666. touch();
  667. return;
  668. }
  669. }
  670. mXform.setScale(scale);
  671. touch();
  672. }
  673. const LLMatrix4& LLJoint::getWorldMatrix()
  674. {
  675. updateWorldMatrixParent();
  676. return mXform.getWorldMatrix();
  677. }
  678. void LLJoint::updateWorldMatrixParent()
  679. {
  680. if (mDirtyFlags & MATRIX_DIRTY)
  681. {
  682. LLJoint* parent = getParent();
  683. if (parent)
  684. {
  685. parent->updateWorldMatrixParent();
  686. }
  687. updateWorldMatrix();
  688. }
  689. }
  690. void LLJoint::updateWorldPRSParent()
  691. {
  692. if (mDirtyFlags & (ROTATION_DIRTY | POSITION_DIRTY))
  693. {
  694. LLJoint* parent = getParent();
  695. if (parent)
  696. {
  697. parent->updateWorldPRSParent();
  698. }
  699. mXform.update();
  700. mDirtyFlags &= ~(ROTATION_DIRTY | POSITION_DIRTY);
  701. }
  702. }
  703. void LLJoint::updateWorldMatrixChildren()
  704. {
  705. if (!mUpdateXform) return;
  706. if (mDirtyFlags & MATRIX_DIRTY)
  707. {
  708. updateWorldMatrix();
  709. }
  710. for (S32 i = 0, count = mChildren.size(); i < count; ++i)
  711. {
  712. LLJoint* joint = mChildren[i];
  713. if (joint) // Paranoia
  714. {
  715. joint->updateWorldMatrixChildren();
  716. }
  717. }
  718. }
  719. void LLJoint::updateWorldMatrix()
  720. {
  721. if (mDirtyFlags & MATRIX_DIRTY)
  722. {
  723. ++sNumUpdates;
  724. mXform.updateMatrix(false);
  725. mDirtyFlags = 0x0;
  726. }
  727. }