PageRenderTime 33ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/lldrawable.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 1565 lines | 1215 code | 240 blank | 110 comment | 229 complexity | e586f4a47880e74cfc42d2f832f06b06 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lldrawable.cpp
  3. * @brief LLDrawable class implementation
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "lldrawable.h"
  28. // library includes
  29. #include "material_codes.h"
  30. // viewer includes
  31. #include "llcriticaldamp.h"
  32. #include "llface.h"
  33. #include "lllightconstants.h"
  34. #include "llmatrix4a.h"
  35. #include "llsky.h"
  36. #include "llsurfacepatch.h"
  37. #include "llviewercamera.h"
  38. #include "llviewerregion.h"
  39. #include "llvolume.h"
  40. #include "llvoavatar.h"
  41. #include "llvovolume.h"
  42. #include "llvosurfacepatch.h" // for debugging
  43. #include "llworld.h"
  44. #include "pipeline.h"
  45. #include "llspatialpartition.h"
  46. #include "llviewerobjectlist.h"
  47. #include "llviewerwindow.h"
  48. const F32 MIN_INTERPOLATE_DISTANCE_SQUARED = 0.001f * 0.001f;
  49. const F32 MAX_INTERPOLATE_DISTANCE_SQUARED = 10.f * 10.f;
  50. const F32 OBJECT_DAMPING_TIME_CONSTANT = 0.06f;
  51. const F32 MIN_SHADOW_CASTER_RADIUS = 2.0f;
  52. static LLFastTimer::DeclareTimer FTM_CULL_REBOUND("Cull Rebound");
  53. ////////////////////////
  54. //
  55. // Inline implementations.
  56. //
  57. //
  58. //////////////////////////////
  59. //
  60. // Drawable code
  61. //
  62. //
  63. // static
  64. U32 LLDrawable::sCurVisible = 0;
  65. U32 LLDrawable::sNumZombieDrawables = 0;
  66. F32 LLDrawable::sCurPixelAngle = 0;
  67. LLDynamicArrayPtr<LLPointer<LLDrawable> > LLDrawable::sDeadList;
  68. #define FORCE_INVISIBLE_AREA 16.f
  69. // static
  70. void LLDrawable::incrementVisible()
  71. {
  72. sCurVisible++;
  73. sCurPixelAngle = (F32) gViewerWindow->getWindowHeightRaw()/LLViewerCamera::getInstance()->getView();
  74. }
  75. void LLDrawable::init()
  76. {
  77. // mXform
  78. mParent = NULL;
  79. mRenderType = 0;
  80. mCurrentScale = LLVector3(1,1,1);
  81. mDistanceWRTCamera = 0.0f;
  82. mPositionGroup.clear();
  83. mExtents[0].clear();
  84. mExtents[1].clear();
  85. mQuietCount = 0;
  86. mState = 0;
  87. mVObjp = NULL;
  88. // mFaces
  89. mSpatialGroupp = NULL;
  90. mVisible = sCurVisible - 2;//invisible for the current frame and the last frame.
  91. mRadius = 0.f;
  92. mGeneration = -1;
  93. mBinRadius = 1.f;
  94. mSpatialBridge = NULL;
  95. }
  96. // static
  97. void LLDrawable::initClass()
  98. {
  99. }
  100. void LLDrawable::destroy()
  101. {
  102. if (gDebugGL)
  103. {
  104. gPipeline.checkReferences(this);
  105. }
  106. if (isDead())
  107. {
  108. sNumZombieDrawables--;
  109. }
  110. if (LLSpatialGroup::sNoDelete)
  111. {
  112. llerrs << "Illegal deletion of LLDrawable!" << llendl;
  113. }
  114. std::for_each(mFaces.begin(), mFaces.end(), DeletePointer());
  115. mFaces.clear();
  116. /*if (!(sNumZombieDrawables % 10))
  117. {
  118. llinfos << "- Zombie drawables: " << sNumZombieDrawables << llendl;
  119. }*/
  120. }
  121. void LLDrawable::markDead()
  122. {
  123. if (isDead())
  124. {
  125. llwarns << "Warning! Marking dead multiple times!" << llendl;
  126. return;
  127. }
  128. if (mSpatialBridge)
  129. {
  130. mSpatialBridge->markDead();
  131. mSpatialBridge = NULL;
  132. }
  133. sNumZombieDrawables++;
  134. // We're dead. Free up all of our references to other objects
  135. setState(DEAD);
  136. cleanupReferences();
  137. // sDeadList.put(this);
  138. }
  139. LLVOVolume* LLDrawable::getVOVolume() const
  140. {
  141. LLViewerObject* objectp = mVObjp;
  142. if ( !isDead() && objectp && (objectp->getPCode() == LL_PCODE_VOLUME))
  143. {
  144. return ((LLVOVolume*)objectp);
  145. }
  146. else
  147. {
  148. return NULL;
  149. }
  150. }
  151. const LLMatrix4& LLDrawable::getRenderMatrix() const
  152. {
  153. return isRoot() ? getWorldMatrix() : getParent()->getWorldMatrix();
  154. }
  155. BOOL LLDrawable::isLight() const
  156. {
  157. LLViewerObject* objectp = mVObjp;
  158. if ( objectp && (objectp->getPCode() == LL_PCODE_VOLUME) && !isDead())
  159. {
  160. return ((LLVOVolume*)objectp)->getIsLight();
  161. }
  162. else
  163. {
  164. return FALSE;
  165. }
  166. }
  167. static LLFastTimer::DeclareTimer FTM_CLEANUP_DRAWABLE("Cleanup Drawable");
  168. static LLFastTimer::DeclareTimer FTM_DEREF_DRAWABLE("Deref");
  169. static LLFastTimer::DeclareTimer FTM_DELETE_FACES("Faces");
  170. void LLDrawable::cleanupReferences()
  171. {
  172. LLFastTimer t(FTM_CLEANUP_DRAWABLE);
  173. {
  174. LLFastTimer t(FTM_DELETE_FACES);
  175. std::for_each(mFaces.begin(), mFaces.end(), DeletePointer());
  176. mFaces.clear();
  177. }
  178. gObjectList.removeDrawable(this);
  179. gPipeline.unlinkDrawable(this);
  180. {
  181. LLFastTimer t(FTM_DEREF_DRAWABLE);
  182. // Cleanup references to other objects
  183. mVObjp = NULL;
  184. mParent = NULL;
  185. }
  186. }
  187. void LLDrawable::cleanupDeadDrawables()
  188. {
  189. /*
  190. S32 i;
  191. for (i = 0; i < sDeadList.count(); i++)
  192. {
  193. if (sDeadList[i]->getNumRefs() > 1)
  194. {
  195. llwarns << "Dead drawable has " << sDeadList[i]->getNumRefs() << " remaining refs" << llendl;
  196. gPipeline.findReferences(sDeadList[i]);
  197. }
  198. }
  199. */
  200. sDeadList.reset();
  201. }
  202. S32 LLDrawable::findReferences(LLDrawable *drawablep)
  203. {
  204. S32 count = 0;
  205. if (mParent == drawablep)
  206. {
  207. llinfos << this << ": parent reference" << llendl;
  208. count++;
  209. }
  210. return count;
  211. }
  212. LLFace* LLDrawable::addFace(LLFacePool *poolp, LLViewerTexture *texturep)
  213. {
  214. LLMemType mt(LLMemType::MTYPE_DRAWABLE);
  215. LLFace *face = new LLFace(this, mVObjp);
  216. if (!face) llerrs << "Allocating new Face: " << mFaces.size() << llendl;
  217. if (face)
  218. {
  219. mFaces.push_back(face);
  220. if (poolp)
  221. {
  222. face->setPool(poolp, texturep);
  223. }
  224. if (isState(UNLIT))
  225. {
  226. face->setState(LLFace::FULLBRIGHT);
  227. }
  228. }
  229. return face;
  230. }
  231. LLFace* LLDrawable::addFace(const LLTextureEntry *te, LLViewerTexture *texturep)
  232. {
  233. LLMemType mt(LLMemType::MTYPE_DRAWABLE);
  234. LLFace *face;
  235. face = new LLFace(this, mVObjp);
  236. face->setTEOffset(mFaces.size());
  237. face->setTexture(texturep);
  238. face->setPoolType(gPipeline.getPoolTypeFromTE(te, texturep));
  239. mFaces.push_back(face);
  240. if (isState(UNLIT))
  241. {
  242. face->setState(LLFace::FULLBRIGHT);
  243. }
  244. return face;
  245. }
  246. void LLDrawable::setNumFaces(const S32 newFaces, LLFacePool *poolp, LLViewerTexture *texturep)
  247. {
  248. if (newFaces == (S32)mFaces.size())
  249. {
  250. return;
  251. }
  252. else if (newFaces < (S32)mFaces.size())
  253. {
  254. std::for_each(mFaces.begin() + newFaces, mFaces.end(), DeletePointer());
  255. mFaces.erase(mFaces.begin() + newFaces, mFaces.end());
  256. }
  257. else // (newFaces > mFaces.size())
  258. {
  259. mFaces.reserve(newFaces);
  260. for (int i = mFaces.size(); i<newFaces; i++)
  261. {
  262. addFace(poolp, texturep);
  263. }
  264. }
  265. llassert_always(mFaces.size() == newFaces);
  266. }
  267. void LLDrawable::setNumFacesFast(const S32 newFaces, LLFacePool *poolp, LLViewerTexture *texturep)
  268. {
  269. if (newFaces <= (S32)mFaces.size() && newFaces >= (S32)mFaces.size()/2)
  270. {
  271. return;
  272. }
  273. else if (newFaces < (S32)mFaces.size())
  274. {
  275. std::for_each(mFaces.begin() + newFaces, mFaces.end(), DeletePointer());
  276. mFaces.erase(mFaces.begin() + newFaces, mFaces.end());
  277. }
  278. else // (newFaces > mFaces.size())
  279. {
  280. mFaces.reserve(newFaces);
  281. for (int i = mFaces.size(); i<newFaces; i++)
  282. {
  283. addFace(poolp, texturep);
  284. }
  285. }
  286. llassert_always(mFaces.size() == newFaces) ;
  287. }
  288. void LLDrawable::mergeFaces(LLDrawable* src)
  289. {
  290. U32 face_count = mFaces.size() + src->mFaces.size();
  291. mFaces.reserve(face_count);
  292. for (U32 i = 0; i < src->mFaces.size(); i++)
  293. {
  294. LLFace* facep = src->mFaces[i];
  295. facep->setDrawable(this);
  296. mFaces.push_back(facep);
  297. }
  298. src->mFaces.clear();
  299. }
  300. void LLDrawable::deleteFaces(S32 offset, S32 count)
  301. {
  302. face_list_t::iterator face_begin = mFaces.begin() + offset;
  303. face_list_t::iterator face_end = face_begin + count;
  304. std::for_each(face_begin, face_end, DeletePointer());
  305. mFaces.erase(face_begin, face_end);
  306. }
  307. void LLDrawable::update()
  308. {
  309. llerrs << "Shouldn't be called!" << llendl;
  310. }
  311. void LLDrawable::updateMaterial()
  312. {
  313. }
  314. void LLDrawable::makeActive()
  315. {
  316. #if !LL_RELEASE_FOR_DOWNLOAD
  317. if (mVObjp.notNull())
  318. {
  319. U32 pcode = mVObjp->getPCode();
  320. if (pcode == LLViewerObject::LL_VO_WATER ||
  321. pcode == LLViewerObject::LL_VO_VOID_WATER ||
  322. pcode == LLViewerObject::LL_VO_SURFACE_PATCH ||
  323. pcode == LLViewerObject::LL_VO_PART_GROUP ||
  324. pcode == LLViewerObject::LL_VO_HUD_PART_GROUP ||
  325. pcode == LLViewerObject::LL_VO_GROUND ||
  326. pcode == LLViewerObject::LL_VO_SKY)
  327. {
  328. llerrs << "Static viewer object has active drawable!" << llendl;
  329. }
  330. }
  331. #endif
  332. if (!isState(ACTIVE)) // && mGeneration > 0)
  333. {
  334. setState(ACTIVE);
  335. //parent must be made active first
  336. if (!isRoot() && !mParent->isActive())
  337. {
  338. mParent->makeActive();
  339. }
  340. //all child objects must also be active
  341. llassert_always(mVObjp);
  342. LLViewerObject::const_child_list_t& child_list = mVObjp->getChildren();
  343. for (LLViewerObject::child_list_t::const_iterator iter = child_list.begin();
  344. iter != child_list.end(); iter++)
  345. {
  346. LLViewerObject* child = *iter;
  347. LLDrawable* drawable = child->mDrawable;
  348. if (drawable)
  349. {
  350. drawable->makeActive();
  351. }
  352. }
  353. if (mVObjp->getPCode() == LL_PCODE_VOLUME)
  354. {
  355. if (mVObjp->isFlexible())
  356. {
  357. return;
  358. }
  359. }
  360. if (mVObjp->getPCode() == LL_PCODE_VOLUME)
  361. {
  362. gPipeline.markRebuild(this, LLDrawable::REBUILD_VOLUME, TRUE);
  363. }
  364. updatePartition();
  365. }
  366. if (isRoot())
  367. {
  368. mQuietCount = 0;
  369. }
  370. else
  371. {
  372. getParent()->mQuietCount = 0;
  373. }
  374. }
  375. void LLDrawable::makeStatic(BOOL warning_enabled)
  376. {
  377. if (isState(ACTIVE))
  378. {
  379. clearState(ACTIVE);
  380. if (mParent.notNull() && mParent->isActive() && warning_enabled)
  381. {
  382. LL_WARNS_ONCE("Drawable") << "Drawable becomes static with active parent!" << LL_ENDL;
  383. }
  384. LLViewerObject::const_child_list_t& child_list = mVObjp->getChildren();
  385. for (LLViewerObject::child_list_t::const_iterator iter = child_list.begin();
  386. iter != child_list.end(); iter++)
  387. {
  388. LLViewerObject* child = *iter;
  389. LLDrawable* child_drawable = child->mDrawable;
  390. if (child_drawable)
  391. {
  392. if (child_drawable->getParent() != this)
  393. {
  394. llwarns << "Child drawable has unknown parent." << llendl;
  395. }
  396. child_drawable->makeStatic(warning_enabled);
  397. }
  398. }
  399. if (mVObjp->getPCode() == LL_PCODE_VOLUME)
  400. {
  401. gPipeline.markRebuild(this, LLDrawable::REBUILD_VOLUME, TRUE);
  402. }
  403. if (mSpatialBridge)
  404. {
  405. mSpatialBridge->markDead();
  406. setSpatialBridge(NULL);
  407. }
  408. }
  409. updatePartition();
  410. }
  411. // Returns "distance" between target destination and resulting xfrom
  412. F32 LLDrawable::updateXform(BOOL undamped)
  413. {
  414. BOOL damped = !undamped;
  415. // Position
  416. LLVector3 old_pos(mXform.getPosition());
  417. LLVector3 target_pos;
  418. if (mXform.isRoot())
  419. {
  420. // get root position in your agent's region
  421. target_pos = mVObjp->getPositionAgent();
  422. }
  423. else
  424. {
  425. // parent-relative position
  426. target_pos = mVObjp->getPosition();
  427. }
  428. // Rotation
  429. LLQuaternion old_rot(mXform.getRotation());
  430. LLQuaternion target_rot = mVObjp->getRotation();
  431. //scaling
  432. LLVector3 target_scale = mVObjp->getScale();
  433. LLVector3 old_scale = mCurrentScale;
  434. LLVector3 dest_scale = target_scale;
  435. // Damping
  436. F32 dist_squared = 0.f;
  437. F32 camdist2 = (mDistanceWRTCamera * mDistanceWRTCamera);
  438. if (damped && isVisible())
  439. {
  440. F32 lerp_amt = llclamp(LLCriticalDamp::getInterpolant(OBJECT_DAMPING_TIME_CONSTANT), 0.f, 1.f);
  441. LLVector3 new_pos = lerp(old_pos, target_pos, lerp_amt);
  442. dist_squared = dist_vec_squared(new_pos, target_pos);
  443. LLQuaternion new_rot = nlerp(lerp_amt, old_rot, target_rot);
  444. dist_squared += (1.f - dot(new_rot, target_rot)) * 10.f;
  445. LLVector3 new_scale = lerp(old_scale, target_scale, lerp_amt);
  446. dist_squared += dist_vec_squared(new_scale, target_scale);
  447. if ((dist_squared >= MIN_INTERPOLATE_DISTANCE_SQUARED * camdist2) &&
  448. (dist_squared <= MAX_INTERPOLATE_DISTANCE_SQUARED))
  449. {
  450. // interpolate
  451. target_pos = new_pos;
  452. target_rot = new_rot;
  453. target_scale = new_scale;
  454. }
  455. else
  456. {
  457. // snap to final position
  458. dist_squared = 0.0f;
  459. if (getVOVolume() && !isRoot())
  460. { //child prim snapping to some position, needs a rebuild
  461. gPipeline.markRebuild(this, LLDrawable::REBUILD_POSITION, TRUE);
  462. }
  463. }
  464. }
  465. if ((mCurrentScale != target_scale) ||
  466. (!isRoot() &&
  467. (dist_squared >= MIN_INTERPOLATE_DISTANCE_SQUARED ||
  468. !mVObjp->getAngularVelocity().isExactlyZero() ||
  469. target_pos != mXform.getPosition() ||
  470. target_rot != mXform.getRotation())))
  471. { //child prim moving or scale change requires immediate rebuild
  472. gPipeline.markRebuild(this, LLDrawable::REBUILD_POSITION, TRUE);
  473. }
  474. else if (!getVOVolume() && !isAvatar())
  475. {
  476. movePartition();
  477. }
  478. // Update
  479. mXform.setPosition(target_pos);
  480. mXform.setRotation(target_rot);
  481. mXform.setScale(LLVector3(1,1,1)); //no scale in drawable transforms (IT'S A RULE!)
  482. mXform.updateMatrix();
  483. mCurrentScale = target_scale;
  484. if (mSpatialBridge)
  485. {
  486. gPipeline.markMoved(mSpatialBridge, FALSE);
  487. }
  488. return dist_squared;
  489. }
  490. void LLDrawable::setRadius(F32 radius)
  491. {
  492. if (mRadius != radius)
  493. {
  494. mRadius = radius;
  495. }
  496. }
  497. void LLDrawable::moveUpdatePipeline(BOOL moved)
  498. {
  499. if (moved)
  500. {
  501. makeActive();
  502. }
  503. // Update the face centers.
  504. for (S32 i = 0; i < getNumFaces(); i++)
  505. {
  506. getFace(i)->updateCenterAgent();
  507. }
  508. }
  509. void LLDrawable::movePartition()
  510. {
  511. LLSpatialPartition* part = getSpatialPartition();
  512. if (part)
  513. {
  514. part->move(this, getSpatialGroup());
  515. }
  516. }
  517. BOOL LLDrawable::updateMove()
  518. {
  519. if (isDead())
  520. {
  521. llwarns << "Update move on dead drawable!" << llendl;
  522. return TRUE;
  523. }
  524. if (mVObjp.isNull())
  525. {
  526. return FALSE;
  527. }
  528. makeActive();
  529. BOOL done;
  530. if (isState(MOVE_UNDAMPED))
  531. {
  532. done = updateMoveUndamped();
  533. }
  534. else
  535. {
  536. done = updateMoveDamped();
  537. }
  538. return done;
  539. }
  540. BOOL LLDrawable::updateMoveUndamped()
  541. {
  542. F32 dist_squared = updateXform(TRUE);
  543. mGeneration++;
  544. if (!isState(LLDrawable::INVISIBLE))
  545. {
  546. BOOL moved = (dist_squared > 0.001f && dist_squared < 255.99f);
  547. moveUpdatePipeline(moved);
  548. mVObjp->updateText();
  549. }
  550. mVObjp->clearChanged(LLXform::MOVED);
  551. return TRUE;
  552. }
  553. void LLDrawable::updatePartition()
  554. {
  555. if (!getVOVolume())
  556. {
  557. movePartition();
  558. }
  559. else if (mSpatialBridge)
  560. {
  561. gPipeline.markMoved(mSpatialBridge, FALSE);
  562. }
  563. else
  564. {
  565. //a child prim moved and needs its verts regenerated
  566. gPipeline.markRebuild(this, LLDrawable::REBUILD_POSITION, TRUE);
  567. }
  568. }
  569. BOOL LLDrawable::updateMoveDamped()
  570. {
  571. F32 dist_squared = updateXform(FALSE);
  572. mGeneration++;
  573. if (!isState(LLDrawable::INVISIBLE))
  574. {
  575. BOOL moved = (dist_squared > 0.001f && dist_squared < 128.0f);
  576. moveUpdatePipeline(moved);
  577. mVObjp->updateText();
  578. }
  579. BOOL done_moving = (dist_squared == 0.0f) ? TRUE : FALSE;
  580. if (done_moving)
  581. {
  582. mVObjp->clearChanged(LLXform::MOVED);
  583. }
  584. return done_moving;
  585. }
  586. void LLDrawable::updateDistance(LLCamera& camera, bool force_update)
  587. {
  588. if (LLViewerCamera::sCurCameraID != LLViewerCamera::CAMERA_WORLD)
  589. {
  590. llwarns << "Attempted to update distance for non-world camera." << llendl;
  591. return;
  592. }
  593. //switch LOD with the spatial group to avoid artifacts
  594. //LLSpatialGroup* sg = getSpatialGroup();
  595. LLVector3 pos;
  596. //if (!sg || sg->changeLOD())
  597. {
  598. LLVOVolume* volume = getVOVolume();
  599. if (volume)
  600. {
  601. if (getSpatialGroup())
  602. {
  603. pos.set(getPositionGroup().getF32ptr());
  604. }
  605. else
  606. {
  607. pos = getPositionAgent();
  608. }
  609. if (isState(LLDrawable::HAS_ALPHA))
  610. {
  611. for (S32 i = 0; i < getNumFaces(); i++)
  612. {
  613. LLFace* facep = getFace(i);
  614. if (force_update || facep->getPoolType() == LLDrawPool::POOL_ALPHA)
  615. {
  616. LLVector4a box;
  617. box.setSub(facep->mExtents[1], facep->mExtents[0]);
  618. box.mul(0.25f);
  619. LLVector3 v = (facep->mCenterLocal-camera.getOrigin());
  620. const LLVector3& at = camera.getAtAxis();
  621. for (U32 j = 0; j < 3; j++)
  622. {
  623. v.mV[j] -= box[j] * at.mV[j];
  624. }
  625. facep->mDistance = v * camera.getAtAxis();
  626. }
  627. }
  628. }
  629. }
  630. else
  631. {
  632. pos = LLVector3(getPositionGroup().getF32ptr());
  633. }
  634. pos -= camera.getOrigin();
  635. mDistanceWRTCamera = llround(pos.magVec(), 0.01f);
  636. mVObjp->updateLOD();
  637. }
  638. }
  639. void LLDrawable::updateTexture()
  640. {
  641. LLMemType mt(LLMemType::MTYPE_DRAWABLE);
  642. if (isDead())
  643. {
  644. llwarns << "Dead drawable updating texture!" << llendl;
  645. return;
  646. }
  647. if (getNumFaces() != mVObjp->getNumTEs())
  648. { //drawable is transitioning its face count
  649. return;
  650. }
  651. if (getVOVolume())
  652. {
  653. /*if (isActive())
  654. {
  655. if (isRoot())
  656. {
  657. mQuietCount = 0;
  658. }
  659. else
  660. {
  661. getParent()->mQuietCount = 0;
  662. }
  663. }*/
  664. gPipeline.markRebuild(this, LLDrawable::REBUILD_MATERIAL, TRUE);
  665. }
  666. }
  667. BOOL LLDrawable::updateGeometry(BOOL priority)
  668. {
  669. llassert(mVObjp.notNull());
  670. BOOL res = mVObjp->updateGeometry(this);
  671. return res;
  672. }
  673. void LLDrawable::shiftPos(const LLVector4a &shift_vector)
  674. {
  675. if (isDead())
  676. {
  677. llwarns << "Shifting dead drawable" << llendl;
  678. return;
  679. }
  680. if (mParent)
  681. {
  682. mXform.setPosition(mVObjp->getPosition());
  683. }
  684. else
  685. {
  686. mXform.setPosition(mVObjp->getPositionAgent());
  687. }
  688. mXform.setRotation(mVObjp->getRotation());
  689. mXform.setScale(1,1,1);
  690. mXform.updateMatrix();
  691. if (isStatic())
  692. {
  693. LLVOVolume* volume = getVOVolume();
  694. if (!volume)
  695. {
  696. gPipeline.markRebuild(this, LLDrawable::REBUILD_ALL, TRUE);
  697. }
  698. for (S32 i = 0; i < getNumFaces(); i++)
  699. {
  700. LLFace *facep = getFace(i);
  701. facep->mCenterAgent += LLVector3(shift_vector.getF32ptr());
  702. facep->mExtents[0].add(shift_vector);
  703. facep->mExtents[1].add(shift_vector);
  704. if (!volume && facep->hasGeometry())
  705. {
  706. facep->clearVertexBuffer();
  707. }
  708. }
  709. mExtents[0].add(shift_vector);
  710. mExtents[1].add(shift_vector);
  711. mPositionGroup.add(shift_vector);
  712. }
  713. else if (mSpatialBridge)
  714. {
  715. mSpatialBridge->shiftPos(shift_vector);
  716. }
  717. else if (isAvatar())
  718. {
  719. mExtents[0].add(shift_vector);
  720. mExtents[1].add(shift_vector);
  721. mPositionGroup.add(shift_vector);
  722. }
  723. mVObjp->onShift(shift_vector);
  724. }
  725. const LLVector3& LLDrawable::getBounds(LLVector3& min, LLVector3& max) const
  726. {
  727. mXform.getMinMax(min,max);
  728. return mXform.getPositionW();
  729. }
  730. const LLVector4a* LLDrawable::getSpatialExtents() const
  731. {
  732. return mExtents;
  733. }
  734. void LLDrawable::setSpatialExtents(const LLVector3& min, const LLVector3& max)
  735. {
  736. mExtents[0].load3(min.mV);
  737. mExtents[1].load3(max.mV);
  738. }
  739. void LLDrawable::setSpatialExtents(const LLVector4a& min, const LLVector4a& max)
  740. {
  741. mExtents[0] = min;
  742. mExtents[1] = max;
  743. }
  744. void LLDrawable::setPositionGroup(const LLVector4a& pos)
  745. {
  746. mPositionGroup = pos;
  747. }
  748. void LLDrawable::updateSpatialExtents()
  749. {
  750. if (mVObjp)
  751. {
  752. mVObjp->updateSpatialExtents(mExtents[0], mExtents[1]);
  753. }
  754. updateBinRadius();
  755. if (mSpatialBridge.notNull())
  756. {
  757. mPositionGroup.splat(0.f);
  758. }
  759. }
  760. void LLDrawable::updateBinRadius()
  761. {
  762. if (mVObjp.notNull())
  763. {
  764. mBinRadius = llmin(mVObjp->getBinRadius(), 256.f);
  765. }
  766. else
  767. {
  768. mBinRadius = llmin(getRadius()*4.f, 256.f);
  769. }
  770. }
  771. void LLDrawable::updateSpecialHoverCursor(BOOL enabled)
  772. {
  773. // TODO: maintain a list of objects that have special
  774. // hover cursors, then use that list for per-frame
  775. // hover cursor selection. JC
  776. }
  777. F32 LLDrawable::getVisibilityRadius() const
  778. {
  779. if (isDead())
  780. {
  781. return 0.f;
  782. }
  783. else if (isLight())
  784. {
  785. const LLVOVolume *vov = getVOVolume();
  786. if (vov)
  787. {
  788. return llmax(getRadius(), vov->getLightRadius());
  789. } else {
  790. // llwarns ?
  791. }
  792. }
  793. return getRadius();
  794. }
  795. void LLDrawable::updateUVMinMax()
  796. {
  797. }
  798. void LLDrawable::setSpatialGroup(LLSpatialGroup *groupp)
  799. {
  800. /*if (mSpatialGroupp && (groupp != mSpatialGroupp))
  801. {
  802. mSpatialGroupp->setState(LLSpatialGroup::GEOM_DIRTY);
  803. }*/
  804. if (mSpatialGroupp != groupp && getVOVolume())
  805. { //NULL out vertex buffer references for volumes on spatial group change to maintain
  806. //requirement that every face vertex buffer is either NULL or points to a vertex buffer
  807. //contained by its drawable's spatial group
  808. for (S32 i = 0; i < getNumFaces(); ++i)
  809. {
  810. LLFace* facep = getFace(i);
  811. facep->clearVertexBuffer();
  812. }
  813. }
  814. mSpatialGroupp = groupp;
  815. }
  816. LLSpatialPartition* LLDrawable::getSpatialPartition()
  817. {
  818. LLSpatialPartition* retval = NULL;
  819. if (!mVObjp ||
  820. !getVOVolume() ||
  821. isStatic())
  822. {
  823. retval = gPipeline.getSpatialPartition((LLViewerObject*) mVObjp);
  824. }
  825. else if (isRoot())
  826. { //must be an active volume
  827. if (!mSpatialBridge)
  828. {
  829. if (mVObjp->isHUDAttachment())
  830. {
  831. setSpatialBridge(new LLHUDBridge(this));
  832. }
  833. else
  834. {
  835. setSpatialBridge(new LLVolumeBridge(this));
  836. }
  837. }
  838. return mSpatialBridge->asPartition();
  839. }
  840. else
  841. {
  842. retval = getParent()->getSpatialPartition();
  843. }
  844. if (retval && mSpatialBridge.notNull())
  845. {
  846. mSpatialBridge->markDead();
  847. setSpatialBridge(NULL);
  848. }
  849. return retval;
  850. }
  851. const S32 MIN_VIS_FRAME_RANGE = 2 ; //two frames:the current one and the last one.
  852. //static
  853. S32 LLDrawable::getMinVisFrameRange()
  854. {
  855. return MIN_VIS_FRAME_RANGE ;
  856. }
  857. BOOL LLDrawable::isRecentlyVisible() const
  858. {
  859. //currently visible or visible in the previous frame.
  860. BOOL vis = isVisible() || (sCurVisible - mVisible < MIN_VIS_FRAME_RANGE) ;
  861. if(!vis)
  862. {
  863. LLSpatialGroup* group = getSpatialGroup();
  864. if (group && group->isRecentlyVisible())
  865. {
  866. mVisible = sCurVisible;
  867. vis = TRUE ;
  868. }
  869. }
  870. return vis ;
  871. }
  872. BOOL LLDrawable::isVisible() const
  873. {
  874. if (mVisible == sCurVisible)
  875. {
  876. return TRUE;
  877. }
  878. #if 0
  879. //disabling this code fixes DEV-20105. Leaving in place in case some other bug pops up as a a result.
  880. //should be safe to just always ask the spatial group for visibility.
  881. if (isActive())
  882. {
  883. if (isRoot())
  884. {
  885. LLSpatialGroup* group = mSpatialBridge.notNull() ? mSpatialBridge->getSpatialGroup() :
  886. getSpatialGroup();
  887. if (group && group->isVisible())
  888. {
  889. mVisible = sCurVisible;
  890. return TRUE;
  891. }
  892. }
  893. else
  894. {
  895. if (getParent()->isVisible())
  896. {
  897. mVisible = sCurVisible;
  898. return TRUE;
  899. }
  900. }
  901. }
  902. else
  903. #endif
  904. {
  905. LLSpatialGroup* group = getSpatialGroup();
  906. if (group && group->isVisible())
  907. {
  908. mVisible = sCurVisible;
  909. return TRUE;
  910. }
  911. }
  912. return FALSE;
  913. }
  914. //=======================================
  915. // Spatial Partition Bridging Drawable
  916. //=======================================
  917. LLSpatialBridge::LLSpatialBridge(LLDrawable* root, BOOL render_by_group, U32 data_mask)
  918. : LLSpatialPartition(data_mask, render_by_group, GL_STREAM_DRAW_ARB)
  919. {
  920. mDrawable = root;
  921. root->setSpatialBridge(this);
  922. mRenderType = mDrawable->mRenderType;
  923. mDrawableType = mDrawable->mRenderType;
  924. mPartitionType = LLViewerRegion::PARTITION_VOLUME;
  925. mOctree->balance();
  926. llassert(mDrawable);
  927. llassert(mDrawable->getRegion());
  928. LLSpatialPartition *part = mDrawable->getRegion()->getSpatialPartition(mPartitionType);
  929. llassert(part);
  930. if (part)
  931. {
  932. part->put(this);
  933. }
  934. }
  935. LLSpatialBridge::~LLSpatialBridge()
  936. {
  937. LLSpatialGroup* group = getSpatialGroup();
  938. if (group)
  939. {
  940. group->mSpatialPartition->remove(this, group);
  941. }
  942. }
  943. void LLSpatialBridge::updateSpatialExtents()
  944. {
  945. LLSpatialGroup* root = (LLSpatialGroup*) mOctree->getListener(0);
  946. {
  947. LLFastTimer ftm(FTM_CULL_REBOUND);
  948. root->rebound();
  949. }
  950. LLVector4a offset;
  951. LLVector4a size = root->mBounds[1];
  952. //VECTORIZE THIS
  953. LLMatrix4a mat;
  954. mat.loadu(mDrawable->getXform()->getWorldMatrix());
  955. LLVector4a t;
  956. t.splat(0.f);
  957. LLVector4a center;
  958. mat.affineTransform(t, center);
  959. mat.rotate(root->mBounds[0], offset);
  960. center.add(offset);
  961. LLVector4a v[4];
  962. //get 4 corners of bounding box
  963. mat.rotate(size,v[0]);
  964. LLVector4a scale;
  965. scale.set(-1.f, -1.f, 1.f);
  966. scale.mul(size);
  967. mat.rotate(scale, v[1]);
  968. scale.set(1.f, -1.f, -1.f);
  969. scale.mul(size);
  970. mat.rotate(scale, v[2]);
  971. scale.set(-1.f, 1.f, -1.f);
  972. scale.mul(size);
  973. mat.rotate(scale, v[3]);
  974. LLVector4a& newMin = mExtents[0];
  975. LLVector4a& newMax = mExtents[1];
  976. newMin = newMax = center;
  977. for (U32 i = 0; i < 4; i++)
  978. {
  979. LLVector4a delta;
  980. delta.setAbs(v[i]);
  981. LLVector4a min;
  982. min.setSub(center, delta);
  983. LLVector4a max;
  984. max.setAdd(center, delta);
  985. newMin.setMin(newMin, min);
  986. newMax.setMax(newMax, max);
  987. }
  988. LLVector4a diagonal;
  989. diagonal.setSub(newMax, newMin);
  990. mRadius = diagonal.getLength3().getF32() * 0.5f;
  991. mPositionGroup.setAdd(newMin,newMax);
  992. mPositionGroup.mul(0.5f);
  993. updateBinRadius();
  994. }
  995. void LLSpatialBridge::updateBinRadius()
  996. {
  997. mBinRadius = llmin( mOctree->getSize()[0]*0.5f, 256.f);
  998. }
  999. LLCamera LLSpatialBridge::transformCamera(LLCamera& camera)
  1000. {
  1001. LLCamera ret = camera;
  1002. LLXformMatrix* mat = mDrawable->getXform();
  1003. LLVector3 center = LLVector3(0,0,0) * mat->getWorldMatrix();
  1004. LLQuaternion rotation = LLQuaternion(mat->getWorldMatrix());
  1005. LLVector3 delta = ret.getOrigin() - center;
  1006. LLQuaternion rot = ~mat->getRotation();
  1007. delta *= rot;
  1008. LLVector3 lookAt = ret.getAtAxis();
  1009. LLVector3 up_axis = ret.getUpAxis();
  1010. LLVector3 left_axis = ret.getLeftAxis();
  1011. lookAt *= rot;
  1012. up_axis *= rot;
  1013. left_axis *= rot;
  1014. if (!delta.isFinite())
  1015. {
  1016. delta.clearVec();
  1017. }
  1018. ret.setOrigin(delta);
  1019. ret.setAxes(lookAt, left_axis, up_axis);
  1020. return ret;
  1021. }
  1022. void LLDrawable::setVisible(LLCamera& camera, std::vector<LLDrawable*>* results, BOOL for_select)
  1023. {
  1024. mVisible = sCurVisible;
  1025. #if 0 && !LL_RELEASE_FOR_DOWNLOAD
  1026. //crazy paranoid rules checking
  1027. if (getVOVolume())
  1028. {
  1029. if (!isRoot())
  1030. {
  1031. if (isActive() && !mParent->isActive())
  1032. {
  1033. llerrs << "Active drawable has static parent!" << llendl;
  1034. }
  1035. if (isStatic() && !mParent->isStatic())
  1036. {
  1037. llerrs << "Static drawable has active parent!" << llendl;
  1038. }
  1039. if (mSpatialBridge)
  1040. {
  1041. llerrs << "Child drawable has spatial bridge!" << llendl;
  1042. }
  1043. }
  1044. else if (isActive() && !mSpatialBridge)
  1045. {
  1046. llerrs << "Active root drawable has no spatial bridge!" << llendl;
  1047. }
  1048. else if (isStatic() && mSpatialBridge.notNull())
  1049. {
  1050. llerrs << "Static drawable has spatial bridge!" << llendl;
  1051. }
  1052. }
  1053. #endif
  1054. }
  1055. class LLOctreeMarkNotCulled: public LLOctreeTraveler<LLDrawable>
  1056. {
  1057. public:
  1058. LLCamera* mCamera;
  1059. LLOctreeMarkNotCulled(LLCamera* camera_in) : mCamera(camera_in) { }
  1060. virtual void traverse(const LLOctreeNode<LLDrawable>* node)
  1061. {
  1062. LLSpatialGroup* group = (LLSpatialGroup*) node->getListener(0);
  1063. group->setVisible();
  1064. LLOctreeTraveler<LLDrawable>::traverse(node);
  1065. }
  1066. void visit(const LLOctreeNode<LLDrawable>* branch)
  1067. {
  1068. gPipeline.markNotCulled((LLSpatialGroup*) branch->getListener(0), *mCamera);
  1069. }
  1070. };
  1071. void LLSpatialBridge::setVisible(LLCamera& camera_in, std::vector<LLDrawable*>* results, BOOL for_select)
  1072. {
  1073. if (!gPipeline.hasRenderType(mDrawableType))
  1074. {
  1075. return;
  1076. }
  1077. //HACK don't draw attachments for avatars that haven't been visible in more than a frame
  1078. LLViewerObject *vobj = mDrawable->getVObj();
  1079. if (vobj && vobj->isAttachment() && !vobj->isHUDAttachment())
  1080. {
  1081. LLDrawable* av;
  1082. LLDrawable* parent = mDrawable->getParent();
  1083. if (parent)
  1084. {
  1085. LLViewerObject* objparent = parent->getVObj();
  1086. av = objparent->mDrawable;
  1087. LLSpatialGroup* group = av->getSpatialGroup();
  1088. BOOL impostor = FALSE;
  1089. BOOL loaded = FALSE;
  1090. if (objparent->isAvatar())
  1091. {
  1092. LLVOAvatar* avatarp = (LLVOAvatar*) objparent;
  1093. if (avatarp->isVisible())
  1094. {
  1095. impostor = objparent->isAvatar() && ((LLVOAvatar*) objparent)->isImpostor();
  1096. loaded = objparent->isAvatar() && ((LLVOAvatar*) objparent)->isFullyLoaded();
  1097. }
  1098. else
  1099. {
  1100. return;
  1101. }
  1102. }
  1103. if (!group ||
  1104. LLDrawable::getCurrentFrame() - av->mVisible > 1 ||
  1105. impostor ||
  1106. !loaded)
  1107. {
  1108. return;
  1109. }
  1110. }
  1111. }
  1112. LLSpatialGroup* group = (LLSpatialGroup*) mOctree->getListener(0);
  1113. group->rebound();
  1114. LLVector4a center;
  1115. center.setAdd(mExtents[0], mExtents[1]);
  1116. center.mul(0.5f);
  1117. LLVector4a size;
  1118. size.setSub(mExtents[1], mExtents[0]);
  1119. size.mul(0.5f);
  1120. if ((LLPipeline::sShadowRender && camera_in.AABBInFrustum(center, size)) ||
  1121. LLPipeline::sImpostorRender ||
  1122. (camera_in.AABBInFrustumNoFarClip(center, size) &&
  1123. AABBSphereIntersect(mExtents[0], mExtents[1], camera_in.getOrigin(), camera_in.mFrustumCornerDist)))
  1124. {
  1125. if (!LLPipeline::sImpostorRender &&
  1126. !LLPipeline::sShadowRender &&
  1127. LLPipeline::calcPixelArea(center, size, camera_in) < FORCE_INVISIBLE_AREA)
  1128. {
  1129. return;
  1130. }
  1131. LLDrawable::setVisible(camera_in);
  1132. if (for_select)
  1133. {
  1134. results->push_back(mDrawable);
  1135. if (mDrawable->getVObj())
  1136. {
  1137. LLViewerObject::const_child_list_t& child_list = mDrawable->getVObj()->getChildren();
  1138. for (LLViewerObject::child_list_t::const_iterator iter = child_list.begin();
  1139. iter != child_list.end(); iter++)
  1140. {
  1141. LLViewerObject* child = *iter;
  1142. LLDrawable* drawable = child->mDrawable;
  1143. results->push_back(drawable);
  1144. }
  1145. }
  1146. }
  1147. else
  1148. {
  1149. LLCamera trans_camera = transformCamera(camera_in);
  1150. LLOctreeMarkNotCulled culler(&trans_camera);
  1151. culler.traverse(mOctree);
  1152. }
  1153. }
  1154. }
  1155. void LLSpatialBridge::updateDistance(LLCamera& camera_in, bool force_update)
  1156. {
  1157. if (mDrawable == NULL)
  1158. {
  1159. markDead();
  1160. return;
  1161. }
  1162. if (mDrawable->getVObj())
  1163. {
  1164. if (mDrawable->getVObj()->isAttachment())
  1165. {
  1166. LLDrawable* parent = mDrawable->getParent();
  1167. if (parent && parent->getVObj())
  1168. {
  1169. LLVOAvatar* av = parent->getVObj()->asAvatar();
  1170. if (av && av->isImpostor())
  1171. {
  1172. return;
  1173. }
  1174. }
  1175. }
  1176. LLCamera camera = transformCamera(camera_in);
  1177. mDrawable->updateDistance(camera, force_update);
  1178. LLViewerObject::const_child_list_t& child_list = mDrawable->getVObj()->getChildren();
  1179. for (LLViewerObject::child_list_t::const_iterator iter = child_list.begin();
  1180. iter != child_list.end(); iter++)
  1181. {
  1182. LLViewerObject* child = *iter;
  1183. LLDrawable* drawable = child->mDrawable;
  1184. if (!drawable)
  1185. {
  1186. continue;
  1187. }
  1188. if (!drawable->isAvatar())
  1189. {
  1190. drawable->updateDistance(camera, force_update);
  1191. }
  1192. }
  1193. }
  1194. }
  1195. void LLSpatialBridge::makeActive()
  1196. { //it is an error to make a spatial bridge active (it's already active)
  1197. llerrs << "makeActive called on spatial bridge" << llendl;
  1198. }
  1199. void LLSpatialBridge::move(LLDrawable *drawablep, LLSpatialGroup *curp, BOOL immediate)
  1200. {
  1201. LLSpatialPartition::move(drawablep, curp, immediate);
  1202. gPipeline.markMoved(this, FALSE);
  1203. }
  1204. BOOL LLSpatialBridge::updateMove()
  1205. {
  1206. llassert_always(mDrawable);
  1207. llassert_always(mDrawable->mVObjp);
  1208. llassert_always(mDrawable->getRegion());
  1209. LLSpatialPartition* part = mDrawable->getRegion()->getSpatialPartition(mPartitionType);
  1210. llassert_always(part);
  1211. mOctree->balance();
  1212. if (part)
  1213. {
  1214. part->move(this, getSpatialGroup(), TRUE);
  1215. }
  1216. return TRUE;
  1217. }
  1218. void LLSpatialBridge::shiftPos(const LLVector4a& vec)
  1219. {
  1220. mExtents[0].add(vec);
  1221. mExtents[1].add(vec);
  1222. mPositionGroup.add(vec);
  1223. }
  1224. void LLSpatialBridge::cleanupReferences()
  1225. {
  1226. LLDrawable::cleanupReferences();
  1227. if (mDrawable)
  1228. {
  1229. mDrawable->setSpatialGroup(NULL);
  1230. if (mDrawable->getVObj())
  1231. {
  1232. LLViewerObject::const_child_list_t& child_list = mDrawable->getVObj()->getChildren();
  1233. for (LLViewerObject::child_list_t::const_iterator iter = child_list.begin();
  1234. iter != child_list.end(); iter++)
  1235. {
  1236. LLViewerObject* child = *iter;
  1237. LLDrawable* drawable = child->mDrawable;
  1238. if (drawable)
  1239. {
  1240. drawable->setSpatialGroup(NULL);
  1241. }
  1242. }
  1243. }
  1244. LLDrawable* drawablep = mDrawable;
  1245. mDrawable = NULL;
  1246. drawablep->setSpatialBridge(NULL);
  1247. }
  1248. }
  1249. const LLVector3 LLDrawable::getPositionAgent() const
  1250. {
  1251. if (getVOVolume())
  1252. {
  1253. if (isActive())
  1254. {
  1255. LLVector3 pos(0,0,0);
  1256. if (!isRoot())
  1257. {
  1258. pos = mVObjp->getPosition();
  1259. }
  1260. return pos * getRenderMatrix();
  1261. }
  1262. else
  1263. {
  1264. return mVObjp->getPositionAgent();
  1265. }
  1266. }
  1267. else
  1268. {
  1269. return getWorldPosition();
  1270. }
  1271. }
  1272. BOOL LLDrawable::isAnimating() const
  1273. {
  1274. if (!getVObj())
  1275. {
  1276. return TRUE;
  1277. }
  1278. if (getScale() != mVObjp->getScale())
  1279. {
  1280. return TRUE;
  1281. }
  1282. if (mVObjp->getPCode() == LLViewerObject::LL_VO_PART_GROUP)
  1283. {
  1284. return TRUE;
  1285. }
  1286. if (mVObjp->getPCode() == LLViewerObject::LL_VO_HUD_PART_GROUP)
  1287. {
  1288. return TRUE;
  1289. }
  1290. if (!isRoot() && !mVObjp->getAngularVelocity().isExactlyZero())
  1291. {
  1292. return TRUE;
  1293. }
  1294. return FALSE;
  1295. }
  1296. void LLDrawable::updateFaceSize(S32 idx)
  1297. {
  1298. if (mVObjp.notNull())
  1299. {
  1300. mVObjp->updateFaceSize(idx);
  1301. }
  1302. }
  1303. LLBridgePartition::LLBridgePartition()
  1304. : LLSpatialPartition(0, FALSE, 0)
  1305. {
  1306. mDrawableType = LLPipeline::RENDER_TYPE_AVATAR;
  1307. mPartitionType = LLViewerRegion::PARTITION_BRIDGE;
  1308. mLODPeriod = 16;
  1309. mSlopRatio = 0.25f;
  1310. }
  1311. LLHUDBridge::LLHUDBridge(LLDrawable* drawablep)
  1312. : LLVolumeBridge(drawablep)
  1313. {
  1314. mDrawableType = LLPipeline::RENDER_TYPE_HUD;
  1315. mPartitionType = LLViewerRegion::PARTITION_HUD;
  1316. mSlopRatio = 0.0f;
  1317. }
  1318. F32 LLHUDBridge::calcPixelArea(LLSpatialGroup* group, LLCamera& camera)
  1319. {
  1320. return 1024.f;
  1321. }
  1322. void LLHUDBridge::shiftPos(const LLVector4a& vec)
  1323. {
  1324. //don't shift hud bridges on region crossing
  1325. }