lldrawable.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /**
  2. * @file lldrawable.cpp
  3. * @brief LLDrawable class implementation
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-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 "llviewerprecompiledheaders.h"
  33. #include "lldrawable.h"
  34. #include "imageids.h"
  35. #include "llfasttimer.h"
  36. #include "llmatrix4a.h"
  37. #include "llvolume.h"
  38. #include "llagent.h"
  39. #include "llface.h"
  40. #include "llpipeline.h"
  41. #include "llsky.h"
  42. #include "llsurfacepatch.h"
  43. #include "llspatialpartition.h"
  44. #include "llviewercamera.h"
  45. #include "llviewercontrol.h"
  46. #include "llviewerobjectlist.h"
  47. #include "llviewerregion.h"
  48. #include "llviewerwindow.h"
  49. #include "llvoavatar.h"
  50. #include "llvoavatarpuppet.h"
  51. #include "llvocache.h"
  52. #include "llvovolume.h"
  53. #include "llworld.h"
  54. constexpr F32 MIN_INTERPOLATE_DISTANCE_SQUARED = 0.001f * 0.001f;
  55. constexpr F32 MAX_INTERPOLATE_DISTANCE_SQUARED = 10.f * 10.f;
  56. constexpr F32 OBJECT_DAMPING_TIME_CONSTANT = 0.06f;
  57. //////////////////////////////
  58. //
  59. // Drawable code
  60. //
  61. //
  62. // static
  63. U32 LLDrawable::sNumZombieDrawables = 0;
  64. F32 LLDrawable::sCurPixelAngle = 0;
  65. #if LL_DEBUG
  66. std::vector<LLPointer<LLDrawable> > LLDrawable::sDeadList;
  67. #endif
  68. // static
  69. void LLDrawable::incrementVisible()
  70. {
  71. LLViewerOctreeEntryData::incrementVisible();
  72. sCurPixelAngle = (F32)gViewerWindowp->getWindowDisplayHeight() / gViewerCamera.getView();
  73. }
  74. LLDrawable::LLDrawable(LLViewerObject* vobj, bool new_entry)
  75. : LLViewerOctreeEntryData(LLViewerOctreeEntry::LLDRAWABLE),
  76. mVObjp(vobj)
  77. {
  78. init(new_entry);
  79. }
  80. void LLDrawable::init(bool new_entry)
  81. {
  82. // mXform
  83. mParent = NULL;
  84. mRenderType = 0;
  85. mCurrentScale = LLVector3(1.f, 1.f, 1.f);
  86. mDistanceWRTCamera = 0.f;
  87. mState = 0;
  88. // mFaces
  89. mRadius = 0.f;
  90. mGeneration = -1;
  91. mSpatialBridge = NULL;
  92. LLViewerOctreeEntry* entry = NULL;
  93. LLVOCacheEntry* vo_entry = NULL;
  94. LLViewerRegion* region = getRegion();
  95. if (!new_entry && mVObjp && region)
  96. {
  97. vo_entry = region->getCacheEntryForOctree(mVObjp->getLocalID());
  98. if (vo_entry)
  99. {
  100. entry = vo_entry->getEntry();
  101. }
  102. }
  103. setOctreeEntry(entry);
  104. if (vo_entry)
  105. {
  106. if (!entry)
  107. {
  108. vo_entry->setOctreeEntry(mEntry);
  109. }
  110. if (region)
  111. {
  112. region->addActiveCacheEntry(vo_entry);
  113. if (vo_entry->getNumOfChildren() > 0)
  114. {
  115. // to load all children.
  116. region->addVisibleChildCacheEntry(vo_entry, NULL);
  117. }
  118. }
  119. llassert(!vo_entry->getGroup()); // not in the object cache octree.
  120. }
  121. llassert(!vo_entry || vo_entry->getEntry() == mEntry);
  122. // invisible for the current frame and the last frame.
  123. initVisible(sCurVisible - 2);
  124. }
  125. //virtual
  126. LLDrawable::~LLDrawable()
  127. {
  128. #if LL_DEBUG
  129. if (gDebugGL)
  130. {
  131. gPipeline.checkReferences(this);
  132. }
  133. #endif
  134. if (LLSpatialGroup::sNoDelete)
  135. {
  136. llwarns << "Illegal deletion of LLDrawable !" << llendl;
  137. llassert(false);
  138. }
  139. if (isDead())
  140. {
  141. --sNumZombieDrawables;
  142. }
  143. if (!mFaces.empty())
  144. {
  145. std::for_each(mFaces.begin(), mFaces.end(), DeletePointer());
  146. mFaces.clear();
  147. }
  148. #if LL_DEBUG
  149. if (!(sNumZombieDrawables % 10))
  150. {
  151. llinfos << "Zombie drawables: " << sNumZombieDrawables << llendl;
  152. }
  153. #endif
  154. }
  155. void LLDrawable::markDead()
  156. {
  157. if (isDead())
  158. {
  159. llwarns << "Marking dead multiple times !" << llendl;
  160. return;
  161. }
  162. #if LL_DEBUG
  163. sDeadList.push_back(this);
  164. #endif
  165. // Hold a LLPointer on 'this' to prevent it from getting destroyed during
  166. // this method execution, which may happen in cleanupReferences() for
  167. // spatial brigdes (detected by ASAN on viewer shutdown)... HB
  168. LLPointer<LLDrawable> drawablep = this;
  169. // Mark ourselves dead now.
  170. drawablep->setState(DEAD);
  171. if (mSpatialBridge)
  172. {
  173. mSpatialBridge->markDead();
  174. mSpatialBridge = NULL;
  175. }
  176. ++sNumZombieDrawables;
  177. // We are dead. Free up all of our references to other objects.
  178. cleanupReferences();
  179. }
  180. LLVOVolume* LLDrawable::getVOVolume() const
  181. {
  182. LLViewerObject* objectp = mVObjp.get();
  183. if (!isDead() && objectp && objectp->getPCode() == LL_PCODE_VOLUME)
  184. {
  185. return (LLVOVolume*)objectp;
  186. }
  187. return NULL;
  188. }
  189. const LLMatrix4& LLDrawable::getRenderMatrix() const
  190. {
  191. return isRoot() ? getWorldMatrix() : getParent()->getWorldMatrix();
  192. }
  193. bool LLDrawable::isLight() const
  194. {
  195. LLViewerObject* objectp = mVObjp.get();
  196. if (objectp && objectp->getPCode() == LL_PCODE_VOLUME && !isDead())
  197. {
  198. return ((LLVOVolume*)objectp)->getIsLight();
  199. }
  200. return false;
  201. }
  202. void LLDrawable::cleanupReferences()
  203. {
  204. LL_FAST_TIMER(FTM_CLEANUP_DRAWABLE);
  205. std::for_each(mFaces.begin(), mFaces.end(), DeletePointer());
  206. mFaces.clear();
  207. gPipeline.unlinkDrawable(this);
  208. removeFromOctree();
  209. // Cleanup references to other objects
  210. mVObjp = NULL;
  211. mParent = NULL;
  212. }
  213. void LLDrawable::removeFromOctree()
  214. {
  215. if (!mEntry)
  216. {
  217. return;
  218. }
  219. mEntry->removeData(this);
  220. if (mEntry->hasVOCacheEntry())
  221. {
  222. LLViewerRegion* regionp = getRegion();
  223. if (regionp)
  224. {
  225. regionp->removeActiveCacheEntry((LLVOCacheEntry*)mEntry->getVOCacheEntry(),
  226. this);
  227. }
  228. }
  229. mEntry = NULL;
  230. }
  231. #if LL_DEBUG && 0
  232. void LLDrawable::cleanupDeadDrawables()
  233. {
  234. for (S32 i = 0, count = sDeadList.size(); i < count; ++i)
  235. {
  236. if (sDeadList[i]->getNumRefs() > 1)
  237. {
  238. llwarns << "Dead drawable has " << sDeadList[i]->getNumRefs()
  239. << " remaining refs" << llendl;
  240. gPipeline.findReferences(sDeadList[i]);
  241. }
  242. }
  243. sDeadList.clear();
  244. }
  245. S32 LLDrawable::findReferences(LLDrawable* drawablep)
  246. {
  247. if (mParent == drawablep)
  248. {
  249. llinfos << this << ": parent reference" << llendl;
  250. return 1;
  251. }
  252. return 0;
  253. }
  254. #endif
  255. LLFace* LLDrawable::getFace(S32 i) const
  256. {
  257. if ((U32)i >= mFaces.size())
  258. {
  259. llwarns << "Invalid face index: " << i << " for a number of: "
  260. << mFaces.size() << " faces." << llendl;
  261. return NULL;
  262. }
  263. if (!mFaces[i])
  264. {
  265. llwarns << "Null face found." << llendl;
  266. return NULL;
  267. }
  268. return mFaces[i];
  269. }
  270. LLFace* LLDrawable::addFace(LLFacePool* poolp, LLViewerTexture* texturep)
  271. {
  272. LL_TRACY_TIMER(TRC_ALLOCATE_FACE);
  273. LLFace* face = new LLFace(this, mVObjp);
  274. if (!face)
  275. {
  276. llerrs << "Allocating new face: " << mFaces.size() << llendl;
  277. }
  278. mFaces.push_back(face);
  279. if (poolp)
  280. {
  281. face->setPool(poolp, texturep);
  282. }
  283. if (isState(UNLIT))
  284. {
  285. face->setState(LLFace::FULLBRIGHT);
  286. }
  287. return face;
  288. }
  289. LLFace* LLDrawable::addFace(const LLTextureEntry* te,
  290. LLViewerTexture* texturep)
  291. {
  292. LL_TRACY_TIMER(TRC_ALLOCATE_FACE);
  293. LLFace* face = new LLFace(this, mVObjp);
  294. if (!face)
  295. {
  296. llerrs << "Allocating new face: " << mFaces.size() << llendl;
  297. }
  298. face->setTEOffset(mFaces.size());
  299. face->setDiffuseMap(texturep);
  300. face->setPoolType(gPipeline.getPoolTypeFromTE(te, texturep));
  301. mFaces.push_back(face);
  302. if (isState(UNLIT))
  303. {
  304. face->setState(LLFace::FULLBRIGHT);
  305. }
  306. return face;
  307. }
  308. LLFace* LLDrawable::addFace(const LLTextureEntry* te,
  309. LLViewerTexture* texturep,
  310. LLViewerTexture* normalp)
  311. {
  312. LL_TRACY_TIMER(TRC_ALLOCATE_FACE);
  313. LLFace* face = new LLFace(this, mVObjp);
  314. if (!face)
  315. {
  316. llerrs << "Allocating new face: " << mFaces.size() << llendl;
  317. }
  318. face->setTEOffset(mFaces.size());
  319. face->setDiffuseMap(texturep);
  320. face->setNormalMap(normalp);
  321. face->setPoolType(gPipeline.getPoolTypeFromTE(te, texturep));
  322. mFaces.push_back(face);
  323. if (isState(UNLIT))
  324. {
  325. face->setState(LLFace::FULLBRIGHT);
  326. }
  327. return face;
  328. }
  329. LLFace* LLDrawable::addFace(const LLTextureEntry* te,
  330. LLViewerTexture* texturep,
  331. LLViewerTexture* normalp,
  332. LLViewerTexture* specularp)
  333. {
  334. LL_TRACY_TIMER(TRC_ALLOCATE_FACE);
  335. LLFace* face = new LLFace(this, mVObjp);
  336. if (!face)
  337. {
  338. llerrs << "Allocating new face: " << mFaces.size() << llendl;
  339. }
  340. face->setTEOffset(mFaces.size());
  341. face->setDiffuseMap(texturep);
  342. face->setNormalMap(normalp);
  343. face->setSpecularMap(specularp);
  344. face->setPoolType(gPipeline.getPoolTypeFromTE(te, texturep));
  345. mFaces.push_back(face);
  346. if (isState(UNLIT))
  347. {
  348. face->setState(LLFace::FULLBRIGHT);
  349. }
  350. return face;
  351. }
  352. void LLDrawable::setNumFaces(S32 new_faces, LLFacePool* poolp,
  353. LLViewerTexture* texturep)
  354. {
  355. S32 cur_faces = mFaces.size();
  356. if (new_faces == cur_faces)
  357. {
  358. return;
  359. }
  360. if (new_faces < cur_faces)
  361. {
  362. std::for_each(mFaces.begin() + new_faces, mFaces.end(),
  363. DeletePointer());
  364. mFaces.erase(mFaces.begin() + new_faces, mFaces.end());
  365. }
  366. else // (new_faces > cur_faces)
  367. {
  368. mFaces.reserve(new_faces);
  369. for (S32 i = mFaces.size(); i < new_faces; ++i)
  370. {
  371. addFace(poolp, texturep);
  372. }
  373. }
  374. llassert_always((S32)mFaces.size() == new_faces);
  375. }
  376. void LLDrawable::setNumFacesFast(S32 new_faces, LLFacePool* poolp,
  377. LLViewerTexture* texturep)
  378. {
  379. S32 cur_faces = mFaces.size();
  380. if (new_faces <= cur_faces && new_faces >= cur_faces / 2)
  381. {
  382. return;
  383. }
  384. if (new_faces < cur_faces)
  385. {
  386. std::for_each(mFaces.begin() + new_faces, mFaces.end(),
  387. DeletePointer());
  388. mFaces.erase(mFaces.begin() + new_faces, mFaces.end());
  389. }
  390. else // (new_faces > mFaces.size())
  391. {
  392. mFaces.reserve(new_faces);
  393. for (S32 i = mFaces.size(); i < new_faces; ++i)
  394. {
  395. addFace(poolp, texturep);
  396. }
  397. }
  398. llassert_always((S32)mFaces.size() == new_faces);
  399. }
  400. void LLDrawable::mergeFaces(LLDrawable* src)
  401. {
  402. U32 face_count = mFaces.size() + src->mFaces.size();
  403. mFaces.reserve(face_count);
  404. for (U32 i = 0, count = src->mFaces.size(); i < count; ++i)
  405. {
  406. LLFace* facep = src->mFaces[i];
  407. facep->setDrawable(this);
  408. mFaces.push_back(facep);
  409. }
  410. src->mFaces.clear();
  411. }
  412. void LLDrawable::deleteFaces(S32 offset, S32 count)
  413. {
  414. face_list_t::iterator face_begin = mFaces.begin() + offset;
  415. face_list_t::iterator face_end = face_begin + count;
  416. std::for_each(face_begin, face_end, DeletePointer());
  417. mFaces.erase(face_begin, face_end);
  418. }
  419. LLDrawable* LLDrawable::getRoot()
  420. {
  421. LLDrawable* drawablep = this;
  422. while (!drawablep->isRoot())
  423. {
  424. drawablep = drawablep->getParent();
  425. }
  426. return drawablep;
  427. }
  428. void LLDrawable::update()
  429. {
  430. llerrs << "This should not be called !" << llendl;
  431. }
  432. void LLDrawable::makeActive()
  433. {
  434. #if LL_DEBUG
  435. if (mVObjp.notNull())
  436. {
  437. U32 pcode = mVObjp->getPCode();
  438. if (pcode == LLViewerObject::LL_VO_WATER ||
  439. pcode == LLViewerObject::LL_VO_VOID_WATER ||
  440. pcode == LLViewerObject::LL_VO_SURFACE_PATCH ||
  441. pcode == LLViewerObject::LL_VO_PART_GROUP ||
  442. pcode == LLViewerObject::LL_VO_HUD_PART_GROUP ||
  443. pcode == LLViewerObject::LL_VO_CLOUDS ||
  444. pcode == LLViewerObject::LL_VO_SKY)
  445. {
  446. llerrs << "Static viewer object has active drawable !" << llendl;
  447. }
  448. }
  449. #endif
  450. if (!isState(ACTIVE)) // && mGeneration > 0)
  451. {
  452. setState(ACTIVE);
  453. // Parent must be made active first
  454. if (!isRoot() && !mParent->isActive())
  455. {
  456. mParent->makeActive();
  457. // NOTE: linked set will now NEVER become static
  458. mParent->setState(LLDrawable::ACTIVE_CHILD);
  459. }
  460. // All child objects must also be active
  461. llassert_always(mVObjp);
  462. LLViewerObject::const_child_list_t& child_list = mVObjp->getChildren();
  463. for (LLViewerObject::child_list_t::const_iterator
  464. iter = child_list.begin(), end = child_list.end();
  465. iter != end; ++iter)
  466. {
  467. LLViewerObject* child = *iter;
  468. LLDrawable* drawable = child->mDrawable;
  469. if (drawable)
  470. {
  471. drawable->makeActive();
  472. }
  473. }
  474. if (mVObjp->getPCode() == LL_PCODE_VOLUME)
  475. {
  476. gPipeline.markRebuild(this, LLDrawable::REBUILD_VOLUME);
  477. }
  478. updatePartition();
  479. }
  480. // This should not happen, but occasionally it does...
  481. else if (!isRoot() && !mParent->isActive())
  482. {
  483. mParent->makeActive();
  484. // NOTE: linked set will now NEVER become static
  485. mParent->setState(LLDrawable::ACTIVE_CHILD);
  486. }
  487. if (!isAvatar() && !isRoot() && !mParent->isActive())
  488. {
  489. llwarns << "failed !" << llendl;
  490. }
  491. }
  492. void LLDrawable::makeStatic(bool warning_enabled)
  493. {
  494. if (isState(ACTIVE) && !isState(ACTIVE_CHILD) && mVObjp.notNull() &&
  495. !mVObjp->isAttachment() && !mVObjp->isFlexible() &&
  496. !mVObjp->isAnimatedObject())
  497. {
  498. clearState(ACTIVE | ANIMATED_CHILD);
  499. if (mParent.notNull() && mParent->isActive() && warning_enabled)
  500. {
  501. llwarns_sparse << "Drawable becomes static with active parent !"
  502. << llendl;
  503. }
  504. LLViewerObject::const_child_list_t& child_list = mVObjp->getChildren();
  505. for (LLViewerObject::child_list_t::const_iterator
  506. iter = child_list.begin(), end = child_list.end();
  507. iter != end; ++iter)
  508. {
  509. LLViewerObject* child = *iter;
  510. LLDrawable* child_drawable = child->mDrawable;
  511. if (child_drawable)
  512. {
  513. if (child_drawable->getParent() != this)
  514. {
  515. llwarns << "Child drawable has unknown parent." << llendl;
  516. }
  517. child_drawable->makeStatic(warning_enabled);
  518. }
  519. }
  520. if (mVObjp->getPCode() == LL_PCODE_VOLUME)
  521. {
  522. gPipeline.markRebuild(this, LLDrawable::REBUILD_VOLUME);
  523. }
  524. if (mSpatialBridge)
  525. {
  526. mSpatialBridge->markDead();
  527. setSpatialBridge(NULL);
  528. }
  529. updatePartition();
  530. }
  531. llassert(isAvatar() || isRoot() || mParent->isStatic());
  532. }
  533. // Returns "distance" between target destination and resulting xfrom
  534. F32 LLDrawable::updateXform(bool undamped)
  535. {
  536. bool damped = !undamped;
  537. // Position
  538. LLVector3 old_pos(mXform.getPosition());
  539. LLVector3 target_pos;
  540. if (mXform.isRoot())
  541. {
  542. // get root position in your agent's region
  543. target_pos = mVObjp->getPositionAgent();
  544. }
  545. else
  546. {
  547. // parent-relative position
  548. target_pos = mVObjp->getPosition();
  549. }
  550. // Rotation
  551. LLQuaternion old_rot = mXform.getRotation();
  552. LLQuaternion target_rot = mVObjp->getRotation();
  553. bool no_target_omega = mVObjp->getAngularVelocity().isExactlyZero();
  554. // Scaling
  555. LLVector3 target_scale = mVObjp->getScale();
  556. LLVector3 old_scale = mCurrentScale;
  557. // Damping
  558. F32 dist_squared = 0.f;
  559. if (damped && isVisible())
  560. {
  561. F32 lerp_amt =
  562. llclamp(LLCriticalDamp::getInterpolant(OBJECT_DAMPING_TIME_CONSTANT),
  563. 0.f, 1.f);
  564. LLVector3 new_pos = lerp(old_pos, target_pos, lerp_amt);
  565. dist_squared = dist_vec_squared(new_pos, target_pos);
  566. LLQuaternion new_rot = nlerp(lerp_amt, old_rot, target_rot);
  567. dist_squared += (1.f - dot(new_rot, target_rot)) * 10.f;
  568. LLVector3 new_scale = lerp(old_scale, target_scale, lerp_amt);
  569. dist_squared += dist_vec_squared(new_scale, target_scale);
  570. if (dist_squared <= MAX_INTERPOLATE_DISTANCE_SQUARED &&
  571. dist_squared >= MIN_INTERPOLATE_DISTANCE_SQUARED *
  572. mDistanceWRTCamera * mDistanceWRTCamera)
  573. {
  574. // Interpolate
  575. target_pos = new_pos;
  576. target_rot = new_rot;
  577. target_scale = new_scale;
  578. }
  579. else if (no_target_omega)
  580. {
  581. // Snap to final position (only if no target omega is applied)
  582. dist_squared = 0.0f;
  583. if (getVOVolume() && !isRoot())
  584. {
  585. // Child prim snapping to some position, needs a rebuild
  586. gPipeline.markRebuild(this, LLDrawable::REBUILD_POSITION);
  587. }
  588. }
  589. }
  590. bool is_root = isRoot();
  591. if (old_scale != target_scale)
  592. {
  593. // Scale change requires immediate rebuild
  594. mCurrentScale = target_scale;
  595. gPipeline.markRebuild(this, LLDrawable::REBUILD_POSITION);
  596. }
  597. else if (!is_root && (dist_squared > 0.f || !no_target_omega))
  598. {
  599. // Child prim moving relative to parent, tag as needing to be rendered
  600. // atomically and rebuild
  601. dist_squared = 1.f; // keep this object on the move list
  602. if (!isState(LLDrawable::ANIMATED_CHILD))
  603. {
  604. setState(LLDrawable::ANIMATED_CHILD);
  605. gPipeline.markRebuild(this);
  606. mVObjp->dirtySpatialGroup();
  607. }
  608. }
  609. else if (!is_root && (old_pos != target_pos || target_rot != old_rot))
  610. {
  611. mVObjp->shrinkWrap();
  612. gPipeline.markRebuild(this);
  613. }
  614. else if (!getVOVolume() && !isAvatar())
  615. {
  616. movePartition();
  617. }
  618. // Update
  619. mXform.setPosition(target_pos);
  620. mXform.setRotation(target_rot);
  621. // No scale in drawable transforms: IT IS A RULE !
  622. mXform.setScale(LLVector3(1.f, 1.f, 1.f));
  623. mXform.updateMatrix();
  624. if (is_root && mVObjp->isAnimatedObject())
  625. {
  626. LLVOAvatarPuppet* puppet = mVObjp->getPuppetAvatar();
  627. if (puppet)
  628. {
  629. puppet->matchVolumeTransform();
  630. }
  631. }
  632. if (mSpatialBridge)
  633. {
  634. gPipeline.markMoved(mSpatialBridge, false);
  635. }
  636. return dist_squared;
  637. }
  638. void LLDrawable::moveUpdatePipeline(bool moved)
  639. {
  640. if (moved)
  641. {
  642. makeActive();
  643. }
  644. // Update the face centers.
  645. for (S32 i = 0, count = getNumFaces(); i < count; ++i)
  646. {
  647. LLFace* face = getFace(i);
  648. if (face)
  649. {
  650. face->updateCenterAgent();
  651. }
  652. }
  653. }
  654. void LLDrawable::movePartition()
  655. {
  656. LLSpatialPartition* part = getSpatialPartition();
  657. if (part)
  658. {
  659. part->move(this, getSpatialGroup());
  660. }
  661. }
  662. bool LLDrawable::updateMove()
  663. {
  664. if (isDead())
  665. {
  666. llwarns << "Update move on dead drawable !" << llendl;
  667. return true;
  668. }
  669. if (mVObjp.isNull())
  670. {
  671. return false;
  672. }
  673. makeActive();
  674. return isState(MOVE_UNDAMPED) ? updateMoveUndamped() : updateMoveDamped();
  675. }
  676. bool LLDrawable::updateMoveUndamped()
  677. {
  678. F32 dist_squared = updateXform(true);
  679. ++mGeneration;
  680. if (!isState(LLDrawable::INVISIBLE))
  681. {
  682. bool moved = dist_squared > 0.001f && dist_squared < 255.99f;
  683. moveUpdatePipeline(moved);
  684. mVObjp->updateText();
  685. }
  686. mVObjp->clearChanged(LLXform::MOVED);
  687. return true;
  688. }
  689. void LLDrawable::updatePartition()
  690. {
  691. if (!getVOVolume())
  692. {
  693. movePartition();
  694. }
  695. else if (mSpatialBridge)
  696. {
  697. gPipeline.markMoved(mSpatialBridge, false);
  698. }
  699. else
  700. {
  701. // A child prim moved and needs its verts regenerated
  702. gPipeline.markRebuild(this, LLDrawable::REBUILD_POSITION);
  703. }
  704. }
  705. bool LLDrawable::updateMoveDamped()
  706. {
  707. F32 dist_squared = updateXform(false);
  708. ++mGeneration;
  709. if (!isState(LLDrawable::INVISIBLE))
  710. {
  711. bool moved = dist_squared > 0.001f && dist_squared < 128.0f;
  712. moveUpdatePipeline(moved);
  713. mVObjp->updateText();
  714. }
  715. bool done_moving = dist_squared == 0.0f;
  716. if (done_moving)
  717. {
  718. mVObjp->clearChanged(LLXform::MOVED);
  719. }
  720. return done_moving;
  721. }
  722. void LLDrawable::updateDistance(LLCamera& camera, bool force_update)
  723. {
  724. if (LLViewerCamera::sCurCameraID != LLViewerCamera::CAMERA_WORLD)
  725. {
  726. llwarns << "Attempted to update distance for non-world camera."
  727. << llendl;
  728. return;
  729. }
  730. if (gShiftFrame)
  731. {
  732. return;
  733. }
  734. #if 0
  735. // Switch LOD with the spatial group to avoid artifacts
  736. LLSpatialGroup* sg = getSpatialGroup();
  737. if (sg && !sg->changeLOD())
  738. {
  739. return;
  740. }
  741. #endif
  742. LLVector3 pos;
  743. LLVOVolume* volume = getVOVolume();
  744. if (volume)
  745. {
  746. if (getGroup())
  747. {
  748. pos.set(getPositionGroup().getF32ptr());
  749. }
  750. else
  751. {
  752. pos = getPositionAgent();
  753. }
  754. if (isState(LLDrawable::HAS_ALPHA))
  755. {
  756. LLVector4a box;
  757. LLVector3 v;
  758. for (S32 i = 0, count = getNumFaces(); i < count; ++i)
  759. {
  760. LLFace* facep = getFace(i);
  761. if (facep && (force_update || facep->isInAlphaPool()))
  762. {
  763. box.setSub(facep->mExtents[1], facep->mExtents[0]);
  764. box.mul(0.25f);
  765. v = facep->mCenterLocal - camera.getOrigin();
  766. const LLVector3& at = camera.getAtAxis();
  767. for (U32 j = 0; j < 3; ++j)
  768. {
  769. v.mV[j] -= box[j] * at.mV[j];
  770. }
  771. facep->mDistance = v * camera.getAtAxis();
  772. }
  773. }
  774. }
  775. // Handle volumes in an animated object as a special case
  776. LLVOAvatar* av = volume->getAvatar();
  777. LLViewerRegion* region = volume->getRegion();
  778. #if 0 // SL-937: add dynamic box handling for rigged mesh on regular avs
  779. if (av && av->isPuppetAvatar() && region)
  780. #else
  781. if (av && region)
  782. #endif
  783. {
  784. const LLVector3* av_box = av->getLastAnimExtents();
  785. LLVector3 cam_offset =
  786. LLVector3::pointToBoxOffset(gViewerCamera.getOrigin(), av_box);
  787. mDistanceWRTCamera = llmax(ll_round(cam_offset.length(), 0.01f),
  788. 0.01f);
  789. mVObjp->updateLOD();
  790. return;
  791. }
  792. }
  793. else if (getGroup())
  794. {
  795. pos = LLVector3(getPositionGroup().getF32ptr());
  796. }
  797. pos -= camera.getOrigin();
  798. mDistanceWRTCamera = ll_round(pos.length(), 0.01f);
  799. mVObjp->updateLOD();
  800. }
  801. void LLDrawable::updateTexture()
  802. {
  803. if (isDead())
  804. {
  805. llwarns << "Dead drawable updating texture!" << llendl;
  806. return;
  807. }
  808. if (getNumFaces() != mVObjp->getNumTEs())
  809. {
  810. // Drawable is transitioning its face count
  811. return;
  812. }
  813. if (getVOVolume())
  814. {
  815. gPipeline.markRebuild(this, LLDrawable::REBUILD_MATERIAL);
  816. }
  817. }
  818. bool LLDrawable::updateGeometry()
  819. {
  820. return mVObjp.notNull() && mVObjp->updateGeometry(this);
  821. }
  822. void LLDrawable::shiftPos(const LLVector4a& shift_vector)
  823. {
  824. if (isDead() || mVObjp.isNull())
  825. {
  826. llwarns << "Shifting dead drawable" << llendl;
  827. return;
  828. }
  829. if (mParent)
  830. {
  831. mXform.setPosition(mVObjp->getPosition());
  832. }
  833. else
  834. {
  835. mXform.setPosition(mVObjp->getPositionAgent());
  836. }
  837. mXform.updateMatrix();
  838. if (isStatic())
  839. {
  840. LLVOVolume* volume = getVOVolume();
  841. bool rebuild = (!volume &&
  842. mRenderType != LLPipeline::RENDER_TYPE_TREE &&
  843. mRenderType != LLPipeline::RENDER_TYPE_TERRAIN &&
  844. mRenderType != LLPipeline::RENDER_TYPE_SKY);
  845. if (rebuild)
  846. {
  847. gPipeline.markRebuild(this);
  848. }
  849. for (S32 i = 0, count = getNumFaces(); i < count; ++i)
  850. {
  851. LLFace* facep = getFace(i);
  852. if (facep)
  853. {
  854. facep->mCenterAgent += LLVector3(shift_vector.getF32ptr());
  855. facep->mExtents[0].add(shift_vector);
  856. facep->mExtents[1].add(shift_vector);
  857. if (rebuild && facep->hasGeometry())
  858. {
  859. facep->clearVertexBuffer();
  860. }
  861. }
  862. }
  863. shift(shift_vector);
  864. }
  865. else if (mSpatialBridge)
  866. {
  867. mSpatialBridge->shiftPos(shift_vector);
  868. }
  869. else if (isAvatar())
  870. {
  871. shift(shift_vector);
  872. }
  873. mVObjp->onShift(shift_vector);
  874. }
  875. const LLVector3& LLDrawable::getBounds(LLVector3& min, LLVector3& max) const
  876. {
  877. mXform.getMinMax(min,max);
  878. return mXform.getPositionW();
  879. }
  880. void LLDrawable::updateSpatialExtents()
  881. {
  882. if (mVObjp.notNull())
  883. {
  884. const LLVector4a* exts = getSpatialExtents();
  885. LLVector4a extents[2] = { exts[0], exts[1] };
  886. mVObjp->updateSpatialExtents(extents[0], extents[1]);
  887. setSpatialExtents(extents[0], extents[1]);
  888. }
  889. updateBinRadius();
  890. if (mSpatialBridge.notNull())
  891. {
  892. getGroupPosition().splat(0.f);
  893. }
  894. }
  895. void LLDrawable::updateBinRadius()
  896. {
  897. if (mVObjp.notNull())
  898. {
  899. setBinRadius(llmin(mVObjp->getBinRadius(), 256.f));
  900. }
  901. else
  902. {
  903. setBinRadius(llmin(getRadius() * 4.f, 256.f));
  904. }
  905. }
  906. F32 LLDrawable::getVisibilityRadius() const
  907. {
  908. if (isDead())
  909. {
  910. return 0.f;
  911. }
  912. else if (isLight())
  913. {
  914. const LLVOVolume* vov = getVOVolume();
  915. if (vov)
  916. {
  917. return llmax(getRadius(), vov->getLightRadius());
  918. }
  919. //else llwarns ?
  920. }
  921. return getRadius();
  922. }
  923. bool LLDrawable::isVisible() const
  924. {
  925. if (LLViewerOctreeEntryData::isVisible())
  926. {
  927. return true;
  928. }
  929. LLViewerOctreeGroup* group = mEntry->getGroup();
  930. #if 1
  931. LLSpatialGroup* sgroup = getSpatialGroup();
  932. if ((group && group->isVisible()) || (sgroup && sgroup->isHUDGroup()))
  933. #else
  934. if (group && group->isVisible())
  935. #endif
  936. {
  937. LLViewerOctreeEntryData::setVisible();
  938. return true;
  939. }
  940. return false;
  941. }
  942. bool LLDrawable::isRecentlyVisible() const
  943. {
  944. // currently visible or visible in the previous frame.
  945. bool vis = LLViewerOctreeEntryData::isRecentlyVisible();
  946. if (!vis)
  947. {
  948. // two frames:the current one and the last one.
  949. constexpr U32 MIN_VIS_FRAME_RANGE = 2;
  950. vis = sCurVisible - getVisible() < MIN_VIS_FRAME_RANGE;
  951. }
  952. return vis;
  953. }
  954. void LLDrawable::setGroup(LLViewerOctreeGroup* groupp)
  955. {
  956. LLSpatialGroup* cur_groupp = (LLSpatialGroup*)getGroup();
  957. #if 0
  958. // Precondition: mGroupp MUST be null or DEAD or mGroupp MUST NOT contain
  959. // this
  960. llassert(!cur_groupp || cur_groupp->isDead() ||
  961. !cur_groupp->hasElement(this));
  962. #endif
  963. // Precondition: groupp MUST be null or groupp MUST contain this
  964. llassert(!groupp || (LLSpatialGroup*)groupp->hasElement(this));
  965. if (cur_groupp != groupp && getVOVolume())
  966. {
  967. // NULL out vertex buffer references for volumes on spatial group
  968. // change to maintain requirement that every face vertex buffer is
  969. // either NULL or points to a vertex buffer contained by its drawable's
  970. // spatial group
  971. for (S32 i = 0, count = getNumFaces(); i < count; ++i)
  972. {
  973. LLFace* facep = getFace(i);
  974. if (facep)
  975. {
  976. facep->clearVertexBuffer();
  977. }
  978. }
  979. }
  980. #if 0
  981. // Postcondition: if next group is NULL, previous group must be dead OR
  982. // NULL OR binIndex must be -1. If next group is NOT NULL, binIndex must
  983. // not be -1
  984. llassert(groupp == NULL ?
  985. cur_groupp == NULL || cur_groupp->isDead() || !getEntry() ||
  986. getEntry()->getBinIndex() == -1
  987. :
  988. getEntry() && getEntry()->getBinIndex() != -1);
  989. #endif
  990. LLViewerOctreeEntryData::setGroup(groupp);
  991. }
  992. LLSpatialPartition* LLDrawable::getSpatialPartition()
  993. {
  994. LLSpatialPartition* retval = NULL;
  995. if (!mVObjp || !getVOVolume() || isStatic())
  996. {
  997. retval = gPipeline.getSpatialPartition((LLViewerObject*)mVObjp);
  998. }
  999. else if (isRoot())
  1000. {
  1001. if (mSpatialBridge.notNull())
  1002. {
  1003. bool obsolete = false;
  1004. U32 type = mSpatialBridge->asPartition()->mPartitionType;
  1005. bool is_hud = mVObjp->isHUDAttachment();
  1006. // Was/became a HUD attachment ?
  1007. if ((type == LLViewerRegion::PARTITION_HUD) != is_hud)
  1008. {
  1009. obsolete = true;
  1010. }
  1011. else
  1012. {
  1013. bool is_animesh = mVObjp->isAnimatedObject() &&
  1014. mVObjp->getPuppetAvatar() != NULL;
  1015. // Was/became an animesh ?
  1016. if ((type == LLViewerRegion::PARTITION_PUPPET) != is_animesh)
  1017. {
  1018. obsolete = true;
  1019. }
  1020. // Was/became another kind of avatar attachment ?
  1021. else if ((type == LLViewerRegion::PARTITION_AVATAR) !=
  1022. (!is_hud && !is_animesh && mVObjp->isAttachment()))
  1023. {
  1024. obsolete = true;
  1025. }
  1026. }
  1027. if (obsolete)
  1028. {
  1029. // Remove obsolete bridge
  1030. mSpatialBridge->markDead();
  1031. setSpatialBridge(NULL);
  1032. }
  1033. }
  1034. // Must be an active volume
  1035. if (mSpatialBridge.isNull())
  1036. {
  1037. // The order is important here, since HUDs and puppets are or
  1038. // can be attachments...
  1039. if (mVObjp->isHUDAttachment())
  1040. {
  1041. setSpatialBridge(new LLHUDBridge(this, getRegion()));
  1042. }
  1043. else if (mVObjp->isAnimatedObject() && mVObjp->getPuppetAvatar())
  1044. {
  1045. setSpatialBridge(new LLPuppetBridge(this, getRegion()));
  1046. }
  1047. else if (mVObjp->isAttachment())
  1048. {
  1049. setSpatialBridge(new LLAvatarBridge(this, getRegion()));
  1050. }
  1051. else
  1052. {
  1053. setSpatialBridge(new LLVolumeBridge(this, getRegion()));
  1054. }
  1055. }
  1056. return mSpatialBridge->asPartition();
  1057. }
  1058. else
  1059. {
  1060. retval = getParent()->getSpatialPartition();
  1061. }
  1062. if (retval && mSpatialBridge.notNull())
  1063. {
  1064. mSpatialBridge->markDead();
  1065. setSpatialBridge(NULL);
  1066. }
  1067. return retval;
  1068. }
  1069. void LLDrawable::setVisible(LLCamera& camera,
  1070. std::vector<LLDrawable*>* results,
  1071. bool for_select)
  1072. {
  1073. LLViewerOctreeEntryData::setVisible();
  1074. #if 0
  1075. // Force visibility on all children as well
  1076. LLViewerObject::const_child_list_t& child_list = mVObjp->getChildren();
  1077. for (LLViewerObject::child_list_t::const_iterator
  1078. iter = child_list.begin(), end = child_list.end();
  1079. iter != end; ++iter)
  1080. {
  1081. LLViewerObject* child = *iter;
  1082. LLDrawable* child_drawable = child->mDrawable;
  1083. if (child_drawable)
  1084. {
  1085. child_drawable->LLViewerOctreeEntryData::setVisible();
  1086. }
  1087. }
  1088. #endif
  1089. #if 0 && LL_DEBUG
  1090. // crazy paranoid rules checking
  1091. if (getVOVolume())
  1092. {
  1093. if (!isRoot())
  1094. {
  1095. if (isActive() && !mParent->isActive())
  1096. {
  1097. llerrs << "Active drawable has static parent !" << llendl;
  1098. }
  1099. if (isStatic() && !mParent->isStatic())
  1100. {
  1101. llerrs << "Static drawable has active parent !" << llendl;
  1102. }
  1103. if (mSpatialBridge)
  1104. {
  1105. llerrs << "Child drawable has spatial bridge !" << llendl;
  1106. }
  1107. }
  1108. else if (isActive() && !mSpatialBridge)
  1109. {
  1110. llerrs << "Active root drawable has no spatial bridge !" << llendl;
  1111. }
  1112. else if (isStatic() && mSpatialBridge.notNull())
  1113. {
  1114. llerrs << "Static drawable has spatial bridge !" << llendl;
  1115. }
  1116. }
  1117. #endif
  1118. }
  1119. const LLVector3 LLDrawable::getPositionAgent() const
  1120. {
  1121. if (getVOVolume())
  1122. {
  1123. if (isActive())
  1124. {
  1125. LLVector3 pos;
  1126. if (!isRoot())
  1127. {
  1128. pos = mVObjp->getPosition();
  1129. }
  1130. return pos * getRenderMatrix();
  1131. }
  1132. return mVObjp->getPositionAgent();
  1133. }
  1134. return getWorldPosition();
  1135. }
  1136. bool LLDrawable::isAnimating() const
  1137. {
  1138. if (!getVObj())
  1139. {
  1140. return true;
  1141. }
  1142. if (getScale() != mVObjp->getScale())
  1143. {
  1144. return true;
  1145. }
  1146. U32 pcode = mVObjp->getPCode();
  1147. if (pcode == LLViewerObject::LL_VO_PART_GROUP)
  1148. {
  1149. return true;
  1150. }
  1151. if (pcode == LLViewerObject::LL_VO_HUD_PART_GROUP)
  1152. {
  1153. return true;
  1154. }
  1155. if (pcode == LLViewerObject::LL_VO_CLOUDS)
  1156. {
  1157. return true;
  1158. }
  1159. #if 0
  1160. if (!isRoot() && !mVObjp->getAngularVelocity().isExactlyZero())
  1161. {
  1162. // Target omega
  1163. return true;
  1164. }
  1165. #endif
  1166. return false;
  1167. }
  1168. //virtual
  1169. void LLDrawable::updateFaceSize(S32 idx)
  1170. {
  1171. if (mVObjp.notNull())
  1172. {
  1173. mVObjp->updateFaceSize(idx);
  1174. }
  1175. }