llvoavatarpuppet.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /**
  2. * @file llvoavatarpuppet.cpp
  3. * @brief Implementation for special dummy avatar used to drive rigged meshes.
  4. *
  5. * $LicenseInfo:firstyear=2017&license=viewergpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2017, 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 "llviewerprecompiledheaders.h"
  33. #include "llvoavatarpuppet.h"
  34. #include "llanimationstates.h"
  35. #include "llagent.h"
  36. #include "llmeshrepository.h"
  37. #include "llpipeline.h"
  38. #include "llskinningutil.h"
  39. #include "llviewercontrol.h"
  40. #include "llviewerobjectlist.h"
  41. #include "llviewerregion.h"
  42. #include "llviewerwearable.h"
  43. #include "llvoavatarself.h"
  44. constexpr F32 MAX_LEGAL_OFFSET = 3.f;
  45. constexpr F32 MAX_LEGAL_SIZE = 64.f;
  46. // Static members
  47. boost::signals2::connection LLVOAvatarPuppet::sRegionChangedSlot;
  48. LLVOAvatarPuppet::signaled_anim_map_t LLVOAvatarPuppet::sSignaledAnimMap;
  49. LLVOAvatarPuppet::LLVOAvatarPuppet(const LLUUID& id, LLViewerRegion* regionp)
  50. : LLVOAvatar(id, regionp),
  51. mRootVolp(NULL),
  52. mGlobalScale(1.f),
  53. mScaleConstraintFixup(1.f),
  54. mPlaying(false),
  55. mMarkedForDeath(false),
  56. mRegionChanged(false)
  57. {
  58. mIsDummy = true;
  59. mEnableDefaultMotions = false;
  60. }
  61. //virtual
  62. void LLVOAvatarPuppet::initInstance()
  63. {
  64. LLVOAvatar::initInstance();
  65. createDrawable();
  66. updateJointLODs();
  67. updateGeometry(mDrawable);
  68. hideSkirt();
  69. }
  70. LLVOAvatarPuppet* LLVOAvatarPuppet::createAvatarPuppet(LLVOVolume* obj)
  71. {
  72. LLVOAvatarPuppet* puppet =
  73. (LLVOAvatarPuppet*)gObjectList.createObjectViewer(LL_PCODE_LEGACY_AVATAR,
  74. gAgent.getRegion(),
  75. CO_FLAG_PUPPET_AVATAR);
  76. if (puppet)
  77. {
  78. puppet->mRootVolp = obj;
  79. // Sync up position/rotation with object
  80. puppet->matchVolumeTransform();
  81. }
  82. return puppet;
  83. }
  84. //virtual
  85. U32 LLVOAvatarPuppet::getPartitionType() const
  86. {
  87. return LLViewerRegion::PARTITION_PUPPET;
  88. }
  89. void LLVOAvatarPuppet::getNewConstraintFixups(LLVector3& new_pos_fixup,
  90. F32& new_scale_fixup) const
  91. {
  92. static LLCachedControl<F32> max_offset(gSavedSettings,
  93. "AnimatedObjectsMaxLegalOffset");
  94. F32 max_legal_offset = max_offset >= 0.f ? max_offset : MAX_LEGAL_OFFSET;
  95. static LLCachedControl<F32> max_size(gSavedSettings,
  96. "AnimatedObjectsMaxLegalSize");
  97. F32 max_legal_size = max_size >= 1.f ? max_size : MAX_LEGAL_SIZE;
  98. new_pos_fixup.clear();
  99. new_scale_fixup = 1.f;
  100. if (!LLVector3::boxValidAndNonZero(getLastAnimExtents()))
  101. {
  102. return;
  103. }
  104. // The goal here is to ensure that the extent of the avatar's bounding box
  105. // does not wander too far from the official position of the corresponding
  106. // volume. We do this by tracking the distance and applying a correction to
  107. // the puppet avatar position if needed.
  108. const LLVector3* extents = getLastAnimExtents();
  109. LLVector3 unshift_extents[2];
  110. unshift_extents[0] = extents[0] - mPositionConstraintFixup;
  111. unshift_extents[1] = extents[1] - mPositionConstraintFixup;
  112. LLVector3 box_dims = extents[1] - extents[0];
  113. F32 box_size = llmax(box_dims[0], box_dims[1], box_dims[2]);
  114. if (!mRootVolp->isAttachment())
  115. {
  116. LLVector3 pos_box_offset =
  117. LLVector3::pointToBoxOffset(mRootVolp->getRenderPosition(),
  118. unshift_extents);
  119. F32 offset_dist = pos_box_offset.length();
  120. F32 target_dist = offset_dist - max_legal_offset;
  121. if (target_dist > 0.f && offset_dist > 0.f)
  122. {
  123. new_pos_fixup = (target_dist / offset_dist) * pos_box_offset;
  124. }
  125. }
  126. if (box_size / mScaleConstraintFixup > max_legal_size)
  127. {
  128. new_scale_fixup = mScaleConstraintFixup * max_legal_size / box_size;
  129. }
  130. }
  131. void LLVOAvatarPuppet::matchVolumeTransform()
  132. {
  133. if (!mRootVolp || isDead())
  134. {
  135. return;
  136. }
  137. if (mRegionChanged)
  138. {
  139. mRegionChanged = false;
  140. }
  141. else
  142. {
  143. LLVector3 new_pos_fixup;
  144. F32 new_scale_fixup;
  145. getNewConstraintFixups(new_pos_fixup, new_scale_fixup);
  146. mPositionConstraintFixup = new_pos_fixup;
  147. mScaleConstraintFixup = new_scale_fixup;
  148. }
  149. if (mRootVolp->isAttachment())
  150. {
  151. LLVOAvatar* avatarp = mRootVolp->getAvatarAncestor();
  152. if (!avatarp)
  153. {
  154. llwarns_once << "Cannot find attached avatar for puppet: "
  155. << std::hex << (uintptr_t)this << std::dec << llendl;
  156. return;
  157. }
  158. LLViewerJointAttachment* attachp =
  159. avatarp->getTargetAttachmentPoint(mRootVolp);
  160. if (attachp) // Paranoia
  161. {
  162. if (getRegion())
  163. {
  164. setPositionAgent(mRootVolp->getRenderPosition());
  165. }
  166. attachp->updateWorldPRSParent();
  167. LLVector3 joint_pos = attachp->getWorldPosition();
  168. LLQuaternion joint_rot = attachp->getWorldRotation();
  169. LLDrawable* drawblep = mRootVolp->mDrawable;
  170. LLVector3 obj_pos;
  171. LLQuaternion obj_rot;
  172. if (drawblep)
  173. {
  174. obj_pos = drawblep->getPosition();
  175. obj_rot = drawblep->getRotation();
  176. }
  177. else
  178. {
  179. obj_pos = mRootVolp->getPosition();
  180. obj_rot = mRootVolp->getRotation();
  181. }
  182. obj_pos.rotVec(joint_rot);
  183. mRoot->setWorldPosition(obj_pos + joint_pos);
  184. mRoot->setWorldRotation(obj_rot * joint_rot);
  185. setRotation(mRoot->getRotation());
  186. setGlobalScale(mScaleConstraintFixup);
  187. }
  188. return;
  189. }
  190. LLVector3 vol_pos = mRootVolp->getRenderPosition();
  191. LLQuaternion obj_rot;
  192. LLDrawable* drawable = mRootVolp->mDrawable;
  193. if (drawable)
  194. {
  195. obj_rot = drawable->getRotation();
  196. }
  197. else
  198. {
  199. obj_rot = mRootVolp->getRotation();
  200. }
  201. #if 1
  202. const LLMeshSkinInfo* skin_info = mRootVolp->getSkinInfo();
  203. if (skin_info)
  204. {
  205. LLQuaternion bind_rot;
  206. bind_rot =
  207. LLSkinningUtil::getUnscaledQuaternion(skin_info->mBindShapeMatrix);
  208. setRotation(bind_rot * obj_rot);
  209. mRoot->setWorldRotation(bind_rot * obj_rot);
  210. }
  211. else
  212. #endif
  213. {
  214. setRotation(obj_rot);
  215. mRoot->setWorldRotation(obj_rot);
  216. }
  217. if (getRegion())
  218. {
  219. setPositionAgent(vol_pos);
  220. }
  221. mRoot->setPosition(vol_pos + mPositionConstraintFixup);
  222. setGlobalScale(mScaleConstraintFixup);
  223. }
  224. void LLVOAvatarPuppet::setGlobalScale(F32 scale)
  225. {
  226. if (scale <= 0.f)
  227. {
  228. llwarns << "invalid global scale " << scale << llendl;
  229. return;
  230. }
  231. if (mGlobalScale == 0.f) // This should never happen
  232. {
  233. mGlobalScale = 1.f;
  234. }
  235. if (scale != mGlobalScale)
  236. {
  237. F32 adjust_scale = scale / mGlobalScale;
  238. LL_DEBUGS("Puppets") << "scale " << scale << " adjustment "
  239. << adjust_scale << LL_ENDL;
  240. // AXON: should we be scaling from the pelvis or the root ?
  241. recursiveScaleJoint(mPelvisp, adjust_scale);
  242. mGlobalScale = scale;
  243. }
  244. }
  245. void LLVOAvatarPuppet::recursiveScaleJoint(LLJoint* joint, F32 factor)
  246. {
  247. if (!joint) return;
  248. joint->setScale(factor * joint->getScale());
  249. for (S32 i = 0, count = joint->mChildren.size(); i < count; ++i)
  250. {
  251. LLJoint* child = joint->mChildren[i];
  252. if (child) // Paranoia
  253. {
  254. recursiveScaleJoint(child, factor);
  255. }
  256. }
  257. }
  258. // Based on LLViewerJointAttachment::setupDrawable(), without the attaching
  259. // part.
  260. void LLVOAvatarPuppet::updateVolumeGeom()
  261. {
  262. if (!mRootVolp)
  263. {
  264. return;
  265. }
  266. LLDrawable* drawable = mRootVolp->mDrawable;
  267. if (!drawable)
  268. {
  269. return;
  270. }
  271. if (drawable->isActive())
  272. {
  273. drawable->makeStatic(false);
  274. }
  275. drawable->makeActive();
  276. gPipeline.markMoved(drawable);
  277. // Face may need to change draw pool to/from POOL_HUD
  278. gPipeline.markTextured(drawable);
  279. LLViewerObject::const_child_list_t& child_list = mRootVolp->getChildren();
  280. for (LLViewerObject::child_list_t::const_iterator iter = child_list.begin(),
  281. end = child_list.end();
  282. iter != end; ++iter)
  283. {
  284. LLViewerObject* childp = *iter;
  285. if (!childp) continue;
  286. LLDrawable* drawable = childp->mDrawable;
  287. if (drawable)
  288. {
  289. // Face may need to change draw pool to/from POOL_HUD
  290. gPipeline.markTextured(drawable);
  291. gPipeline.markMoved(drawable);
  292. }
  293. }
  294. gPipeline.markRebuild(drawable);
  295. mRootVolp->markForUpdate();
  296. // Note that attachment overrides are not needed here as they have already
  297. // been applied at the time the puppet avatar was created in llvovolume.cpp
  298. matchVolumeTransform();
  299. }
  300. //virtual
  301. void LLVOAvatarPuppet::markDead()
  302. {
  303. // Normally mRootVolp has already been cleared in unlinkPuppetAvatar(),
  304. // unless there's some bulk object cleanup happening e.g. on region
  305. // destruction. In that case the puppet avatar may be killed first.
  306. if (mRootVolp)
  307. {
  308. mRootVolp->mPuppetAvatar = NULL;
  309. mRootVolp = NULL;
  310. }
  311. LLVOAvatar::markDead();
  312. }
  313. //virtual
  314. void LLVOAvatarPuppet::idleUpdate(F64 time)
  315. {
  316. if (mMarkedForDeath)
  317. {
  318. if (!isDead())
  319. {
  320. markDead();
  321. }
  322. }
  323. else
  324. {
  325. LLVOAvatar::idleUpdate(time);
  326. }
  327. }
  328. void LLVOAvatarPuppet::getAnimatedVolumes(std::vector<LLVOVolume*>& volumes)
  329. {
  330. if (!mRootVolp)
  331. {
  332. return;
  333. }
  334. volumes.push_back(mRootVolp);
  335. LLViewerObject::const_child_list_t& child_list = mRootVolp->getChildren();
  336. for (LLViewerObject::const_child_list_t::const_iterator
  337. iter = child_list.begin(), end = child_list.end();
  338. iter != end; ++iter)
  339. {
  340. LLViewerObject* childp = *iter;
  341. if (!childp) continue; // Paranoia
  342. LLVOVolume* child_volp = childp->asVolume();
  343. if (child_volp && child_volp->isAnimatedObject())
  344. {
  345. volumes.push_back(child_volp);
  346. }
  347. }
  348. }
  349. // This is called after an associated object receives an animation message.
  350. // Combine the signaled animations for all associated objects and process any
  351. // resulting state changes.
  352. void LLVOAvatarPuppet::updateAnimations()
  353. {
  354. if (!mRootVolp)
  355. {
  356. llwarns_once << "No root volume for puppet " << std::hex << this
  357. << std::dec << ". Aborted." << llendl;
  358. return;
  359. }
  360. std::vector<LLVOVolume*> volumes;
  361. getAnimatedVolumes(volumes);
  362. // Rebuild mSignaledAnimations from the associated volumes.
  363. LLVOAvatar::anim_map_t anims;
  364. for (S32 i = 0, count = volumes.size(); i < count; ++i)
  365. {
  366. LLVOVolume* volp = volumes[i];
  367. LLVOAvatar::anim_map_t& sig_anims = sSignaledAnimMap[volp->getID()];
  368. for (LLVOAvatar::anim_it_t anim_it = sig_anims.begin(),
  369. anim_end = sig_anims.end();
  370. anim_it != anim_end; ++anim_it)
  371. {
  372. LLVOAvatar::anim_it_t found_it = anims.find(anim_it->first);
  373. if (found_it != anim_end)
  374. {
  375. // Animation already present, use the larger sequence id
  376. anims[anim_it->first] = llmax(found_it->second,
  377. anim_it->second);
  378. }
  379. else
  380. {
  381. // Animation not already present, use this sequence id.
  382. anims.emplace(anim_it->first, anim_it->second);
  383. }
  384. }
  385. }
  386. if (!mPlaying)
  387. {
  388. mPlaying = true;
  389. #if 0
  390. if (!mRootVolp->isAnySelected())
  391. #endif
  392. {
  393. updateVolumeGeom();
  394. mRootVolp->recursiveMarkForUpdate();
  395. }
  396. }
  397. mSignaledAnimations.swap(anims);
  398. processAnimationStateChanges();
  399. }
  400. //virtual
  401. LLViewerObject* LLVOAvatarPuppet::lineSegmentIntersectRiggedAttachments(const LLVector4a& start,
  402. const LLVector4a& end,
  403. S32 face,
  404. bool pick_transparent,
  405. bool pick_rigged,
  406. S32* face_hit,
  407. LLVector4a* intersection,
  408. LLVector2* tex_coord,
  409. LLVector4a* normal,
  410. LLVector4a* tangent)
  411. {
  412. if (!mRootVolp || !lineSegmentBoundingBox(start, end))
  413. {
  414. return NULL;
  415. }
  416. LLVector4a local_end = end;
  417. LLVector4a local_intersection;
  418. if (mRootVolp->lineSegmentIntersect(start, local_end, face,
  419. pick_transparent, pick_rigged,
  420. face_hit, &local_intersection,
  421. tex_coord, normal, tangent))
  422. {
  423. local_end = local_intersection;
  424. if (intersection)
  425. {
  426. *intersection = local_intersection;
  427. }
  428. return (LLViewerObject*)mRootVolp;
  429. }
  430. std::vector<LLVOVolume*> volumes;
  431. getAnimatedVolumes(volumes);
  432. for (S32 i = 0, count = volumes.size(); i < count; ++i)
  433. {
  434. LLVOVolume* volp = volumes[i];
  435. if (volp != mRootVolp &&
  436. volp->lineSegmentIntersect(start, local_end, face,
  437. pick_transparent, pick_rigged,
  438. face_hit, &local_intersection,
  439. tex_coord, normal, tangent))
  440. {
  441. local_end = local_intersection;
  442. if (intersection)
  443. {
  444. *intersection = local_intersection;
  445. }
  446. return (LLViewerObject*)volp;
  447. }
  448. }
  449. return NULL;
  450. }
  451. //virtual
  452. LLVOAvatar* LLVOAvatarPuppet::getAttachedAvatar()
  453. {
  454. if (mRootVolp && mRootVolp->isAttachment())
  455. {
  456. return mRootVolp->getAvatarAncestor();
  457. }
  458. return NULL;
  459. }
  460. //virtual
  461. LLVOAvatar* LLVOAvatarPuppet::getAttachedAvatar() const
  462. {
  463. if (mRootVolp && mRootVolp->isAttachment())
  464. {
  465. return mRootVolp->getAvatarAncestor();
  466. }
  467. return NULL;
  468. }
  469. //virtual
  470. bool LLVOAvatarPuppet::shouldRenderRigged() const
  471. {
  472. if (mRootVolp && mRootVolp->isAttachment())
  473. {
  474. LLVOAvatar* avatarp = mRootVolp->getAvatarAncestor();
  475. if (avatarp)
  476. {
  477. return avatarp->shouldRenderRigged();
  478. }
  479. }
  480. return true;
  481. }
  482. //virtual
  483. bool LLVOAvatarPuppet::isImpostor()
  484. {
  485. if (mMarkedForDeath) return false;
  486. if (mRootVolp && mRootVolp->isAttachment())
  487. {
  488. // Attached animated objects should match state of their attached av.
  489. LLVOAvatar* avatarp = mRootVolp->getAvatarAncestor();
  490. if (avatarp)
  491. {
  492. return avatarp->isImpostor();
  493. }
  494. }
  495. return LLVOAvatar::isImpostor();
  496. }
  497. //virtual
  498. bool LLVOAvatarPuppet::isTooComplex() const
  499. {
  500. #if 0 // Avatar puppets "jelly-dollifying" does not work anyway...
  501. if (mMarkedForDeath) return false;
  502. if (mRootVolp && mRootVolp->isAttachment())
  503. {
  504. // NOTE: an attached animesh complexity is already accounted for as any
  505. // other avatar attachment: should it prove to be too complex, then the
  506. // avatar it is attached to gets jelly-dollified, and none of its
  507. // attachments are rendered at all, this animesh included. We therefore
  508. // do not need to check for attached animesh complexity and can always
  509. // return false here. HB
  510. return false;
  511. }
  512. // This is a standalone animesh: use complexity...
  513. return LLVOAvatar::isTooComplex();
  514. #else
  515. return false;
  516. #endif
  517. }
  518. #if 0 // *TODO ?
  519. //virtual
  520. void LLVOAvatarPuppet::updateDebugText()
  521. {
  522. }
  523. #endif
  524. //static
  525. void LLVOAvatarPuppet::onRegionChanged()
  526. {
  527. for (S32 i = 0, count = LLCharacter::sInstances.size(); i < count; ++i)
  528. {
  529. LLVOAvatarPuppet* puppet =
  530. dynamic_cast<LLVOAvatarPuppet*>(LLCharacter::sInstances[i]);
  531. if (puppet)
  532. {
  533. puppet->mRegionChanged = true;
  534. }
  535. }
  536. }