Octree.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "DebugRenderer.h"
  26. #include "Profiler.h"
  27. #include "Octree.h"
  28. #include "Scene.h"
  29. #include "Sort.h"
  30. #include "WorkQueue.h"
  31. #include "DebugNew.h"
  32. #ifdef _MSC_VER
  33. #pragma warning(disable:4355)
  34. #endif
  35. static const float DEFAULT_OCTREE_SIZE = 1000.0f;
  36. static const int DEFAULT_OCTREE_LEVELS = 8;
  37. static const int RAYCASTS_PER_WORK_ITEM = 4;
  38. void RaycastDrawablesWork(const WorkItem* item, unsigned threadIndex)
  39. {
  40. Octree* octree = reinterpret_cast<Octree*>(item->aux_);
  41. Drawable** start = reinterpret_cast<Drawable**>(item->start_);
  42. Drawable** end = reinterpret_cast<Drawable**>(item->end_);
  43. const RayOctreeQuery& query = *octree->rayQuery_;
  44. PODVector<RayQueryResult>& results = octree->rayQueryResults_[threadIndex];
  45. while (start != end)
  46. {
  47. Drawable* drawable = *start;
  48. drawable->ProcessRayQuery(query, results);
  49. ++start;
  50. }
  51. }
  52. void UpdateDrawablesWork(const WorkItem* item, unsigned threadIndex)
  53. {
  54. const FrameInfo& frame = *(reinterpret_cast<FrameInfo*>(item->aux_));
  55. WeakPtr<Drawable>* start = reinterpret_cast<WeakPtr<Drawable>*>(item->start_);
  56. WeakPtr<Drawable>* end = reinterpret_cast<WeakPtr<Drawable>*>(item->end_);
  57. while (start != end)
  58. {
  59. Drawable* drawable = *start;
  60. if (drawable)
  61. {
  62. drawable->Update(frame);
  63. drawable->updateQueued_ = false;
  64. }
  65. ++start;
  66. }
  67. }
  68. inline bool CompareRayQueryResults(const RayQueryResult& lhs, const RayQueryResult& rhs)
  69. {
  70. return lhs.distance_ < rhs.distance_;
  71. }
  72. Octant::Octant(const BoundingBox& box, unsigned level, Octant* parent, Octree* root) :
  73. worldBoundingBox_(box),
  74. level_(level),
  75. parent_(parent),
  76. root_(root),
  77. numDrawables_(0)
  78. {
  79. center_ = worldBoundingBox_.Center();
  80. halfSize_ = worldBoundingBox_.Size() * 0.5f;
  81. cullingBox_ = BoundingBox(worldBoundingBox_.min_ - halfSize_, worldBoundingBox_.max_ + halfSize_);
  82. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  83. children_[i] = 0;
  84. }
  85. Octant::~Octant()
  86. {
  87. Release();
  88. }
  89. Octant* Octant::GetOrCreateChild(unsigned index)
  90. {
  91. if (children_[index])
  92. return children_[index];
  93. Vector3 newMin = worldBoundingBox_.min_;
  94. Vector3 newMax = worldBoundingBox_.max_;
  95. Vector3 oldCenter = worldBoundingBox_.Center();
  96. if (index & 1)
  97. newMin.x_ = oldCenter.x_;
  98. else
  99. newMax.x_ = oldCenter.x_;
  100. if (index & 2)
  101. newMin.y_ = oldCenter.y_;
  102. else
  103. newMax.y_ = oldCenter.y_;
  104. if (index & 4)
  105. newMin.z_ = oldCenter.z_;
  106. else
  107. newMax.z_ = oldCenter.z_;
  108. children_[index] = new Octant(BoundingBox(newMin, newMax), level_ + 1, this, root_);
  109. return children_[index];
  110. }
  111. void Octant::DeleteChild(unsigned index)
  112. {
  113. delete children_[index];
  114. children_[index] = 0;
  115. }
  116. void Octant::DeleteChild(Octant* octant)
  117. {
  118. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  119. {
  120. if (children_[i] == octant)
  121. {
  122. delete octant;
  123. children_[i] = 0;
  124. return;
  125. }
  126. }
  127. }
  128. void Octant::InsertDrawable(Drawable* drawable, const Vector3& boxCenter, const Vector3& boxSize)
  129. {
  130. // If size OK or outside, stop recursion & insert here. Also, if drawable is not occluded, must stay in the root octant
  131. // so that hierarchic octant occlusion does not erroneously hide the drawable
  132. if ((!drawable->IsOccludee() && this == root_) || CheckDrawableSize(boxSize) ||
  133. cullingBox_.IsInside(drawable->GetWorldBoundingBox()) != INSIDE)
  134. {
  135. Octant* oldOctant = drawable->octant_;
  136. if (oldOctant != this)
  137. {
  138. // Add first, then remove, because drawable count going to zero deletes the octree branch in question
  139. AddDrawable(drawable);
  140. if (oldOctant)
  141. oldOctant->RemoveDrawable(drawable, false);
  142. }
  143. return;
  144. }
  145. unsigned x = boxCenter.x_ < center_.x_ ? 0 : 1;
  146. unsigned y = boxCenter.y_ < center_.y_ ? 0 : 2;
  147. unsigned z = boxCenter.z_ < center_.z_ ? 0 : 4;
  148. GetOrCreateChild(x + y + z)->InsertDrawable(drawable, boxCenter, boxSize);
  149. }
  150. bool Octant::CheckDrawableSize(const Vector3& boxSize) const
  151. {
  152. // If max split level, size always OK
  153. if (level_ != root_->GetNumLevels())
  154. return boxSize.x_ >= halfSize_.x_ || boxSize.y_ >= halfSize_.y_ || boxSize.z_ >= halfSize_.z_;
  155. else
  156. return true;
  157. }
  158. void Octant::ResetRoot()
  159. {
  160. root_ = 0;
  161. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  162. {
  163. if (children_[i])
  164. children_[i]->ResetRoot();
  165. }
  166. }
  167. void Octant::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  168. {
  169. if (debug && debug->IsInside(worldBoundingBox_))
  170. {
  171. debug->AddBoundingBox(worldBoundingBox_, Color(0.25f, 0.25f, 0.25f), depthTest);
  172. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  173. {
  174. if (children_[i])
  175. children_[i]->DrawDebugGeometry(debug, depthTest);
  176. }
  177. }
  178. }
  179. void Octant::GetDrawablesInternal(OctreeQuery& query, bool inside) const
  180. {
  181. if (this != root_)
  182. {
  183. Intersection res = query.TestOctant(cullingBox_, inside);
  184. if (res == INSIDE)
  185. inside = true;
  186. else if (res == OUTSIDE)
  187. {
  188. // Fully outside, so cull this octant, its children & drawables
  189. return;
  190. }
  191. }
  192. if (drawables_.Size())
  193. {
  194. Drawable** ptr = const_cast<Drawable**>(&drawables_.Front());
  195. Drawable** end = ptr + drawables_.Size();
  196. while (ptr != end)
  197. {
  198. Drawable* drawable = *ptr;
  199. if ((drawable->GetDrawableFlags() & query.drawableFlags_) && drawable->IsVisible() &&
  200. (drawable->GetViewMask() & query.viewMask_) && (query.TestDrawable(drawable, inside) != OUTSIDE))
  201. query.result_.Push(drawable);
  202. ++ptr;
  203. }
  204. }
  205. if (children_[0])
  206. children_[0]->GetDrawablesInternal(query, inside);
  207. if (children_[1])
  208. children_[1]->GetDrawablesInternal(query, inside);
  209. if (children_[2])
  210. children_[2]->GetDrawablesInternal(query, inside);
  211. if (children_[3])
  212. children_[3]->GetDrawablesInternal(query, inside);
  213. if (children_[4])
  214. children_[4]->GetDrawablesInternal(query, inside);
  215. if (children_[5])
  216. children_[5]->GetDrawablesInternal(query, inside);
  217. if (children_[6])
  218. children_[6]->GetDrawablesInternal(query, inside);
  219. if (children_[7])
  220. children_[7]->GetDrawablesInternal(query, inside);
  221. }
  222. void Octant::GetDrawablesInternal(RayOctreeQuery& query) const
  223. {
  224. if (!numDrawables_)
  225. return;
  226. float octantDist = query.ray_.HitDistance(cullingBox_);
  227. if (octantDist > query.maxDistance_)
  228. return;
  229. if (drawables_.Size())
  230. {
  231. Drawable** ptr = const_cast<Drawable**>(&drawables_.Front());
  232. Drawable** end = ptr + drawables_.Size();
  233. while (ptr != end)
  234. {
  235. Drawable* drawable = *ptr;
  236. if ((drawable->GetDrawableFlags() & query.drawableFlags_) && drawable->IsVisible() &&
  237. (drawable->GetViewMask() & query.viewMask_))
  238. drawable->ProcessRayQuery(query, query.result_);
  239. ++ptr;
  240. }
  241. }
  242. if (children_[0])
  243. children_[0]->GetDrawablesInternal(query);
  244. if (children_[1])
  245. children_[1]->GetDrawablesInternal(query);
  246. if (children_[2])
  247. children_[2]->GetDrawablesInternal(query);
  248. if (children_[3])
  249. children_[3]->GetDrawablesInternal(query);
  250. if (children_[4])
  251. children_[4]->GetDrawablesInternal(query);
  252. if (children_[5])
  253. children_[5]->GetDrawablesInternal(query);
  254. if (children_[6])
  255. children_[6]->GetDrawablesInternal(query);
  256. if (children_[7])
  257. children_[7]->GetDrawablesInternal(query);
  258. }
  259. void Octant::GetDrawablesOnlyInternal(RayOctreeQuery& query, PODVector<Drawable*>& drawables) const
  260. {
  261. if (!numDrawables_)
  262. return;
  263. float octantDist = query.ray_.HitDistance(cullingBox_);
  264. if (octantDist > query.maxDistance_)
  265. return;
  266. if (drawables_.Size())
  267. {
  268. Drawable** ptr = const_cast<Drawable**>(&drawables_.Front());
  269. Drawable** end = ptr + drawables_.Size();
  270. while (ptr != end)
  271. {
  272. Drawable* drawable = *ptr;
  273. if ((drawable->GetDrawableFlags() & query.drawableFlags_) && drawable->IsVisible() &&
  274. (drawable->GetViewMask() & query.viewMask_))
  275. drawables.Push(drawable);
  276. ++ptr;
  277. }
  278. }
  279. if (children_[0])
  280. children_[0]->GetDrawablesOnlyInternal(query, drawables);
  281. if (children_[1])
  282. children_[1]->GetDrawablesOnlyInternal(query, drawables);
  283. if (children_[2])
  284. children_[2]->GetDrawablesOnlyInternal(query, drawables);
  285. if (children_[3])
  286. children_[3]->GetDrawablesOnlyInternal(query, drawables);
  287. if (children_[4])
  288. children_[4]->GetDrawablesOnlyInternal(query, drawables);
  289. if (children_[5])
  290. children_[5]->GetDrawablesOnlyInternal(query, drawables);
  291. if (children_[6])
  292. children_[6]->GetDrawablesOnlyInternal(query, drawables);
  293. if (children_[7])
  294. children_[7]->GetDrawablesOnlyInternal(query, drawables);
  295. }
  296. void Octant::Release()
  297. {
  298. if (root_ && this != root_)
  299. {
  300. // Remove the drawables (if any) from this octant to the root octant
  301. for (PODVector<Drawable*>::Iterator i = drawables_.Begin(); i != drawables_.End(); ++i)
  302. {
  303. (*i)->SetOctant(root_);
  304. root_->drawables_.Push(*i);
  305. root_->QueueReinsertion(*i);
  306. }
  307. drawables_.Clear();
  308. numDrawables_ = 0;
  309. }
  310. else if (!root_)
  311. {
  312. // If the whole octree is being destroyed, just detach the drawables
  313. for (PODVector<Drawable*>::Iterator i = drawables_.Begin(); i != drawables_.End(); ++i)
  314. (*i)->SetOctant(0);
  315. }
  316. for (unsigned i = 0; i < NUM_OCTANTS; ++i)
  317. DeleteChild(i);
  318. }
  319. OBJECTTYPESTATIC(Octree);
  320. Octree::Octree(Context* context) :
  321. Component(context),
  322. Octant(BoundingBox(-DEFAULT_OCTREE_SIZE, DEFAULT_OCTREE_SIZE), 0, 0, this),
  323. numLevels_(DEFAULT_OCTREE_LEVELS)
  324. {
  325. // Resize threaded ray query intermediate result vector according to number of worker threads
  326. rayQueryResults_.Resize(GetSubsystem<WorkQueue>()->GetNumThreads() + 1);
  327. }
  328. Octree::~Octree()
  329. {
  330. // Reset root pointer from all child octants now so that they do not move their drawables to root
  331. ResetRoot();
  332. }
  333. void Octree::RegisterObject(Context* context)
  334. {
  335. context->RegisterFactory<Octree>();
  336. Vector3 defaultBoundsMin = Vector3::ONE * DEFAULT_OCTREE_SIZE;
  337. Vector3 defaultBoundsMax = -Vector3::ONE * DEFAULT_OCTREE_SIZE;
  338. ATTRIBUTE(Octree, VAR_VECTOR3, "Bounding Box Min", worldBoundingBox_.min_, defaultBoundsMin, AM_DEFAULT);
  339. ATTRIBUTE(Octree, VAR_VECTOR3, "Bounding Box Max", worldBoundingBox_.max_, defaultBoundsMax, AM_DEFAULT);
  340. ATTRIBUTE(Octree, VAR_INT, "Number of Levels", numLevels_, DEFAULT_OCTREE_LEVELS, AM_DEFAULT);
  341. }
  342. void Octree::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  343. {
  344. // If any of the (size) attributes change, resize the octree
  345. Serializable::OnSetAttribute(attr, src);
  346. Resize(worldBoundingBox_, numLevels_);
  347. }
  348. void Octree::Resize(const BoundingBox& box, unsigned numLevels)
  349. {
  350. PROFILE(ResizeOctree);
  351. numLevels = Max((int)numLevels, 1);
  352. // If drawables exist, they are temporarily moved to the root
  353. Release();
  354. Vector3 halfSize = box.Size() * 0.5f;
  355. worldBoundingBox_ = box;
  356. cullingBox_ = BoundingBox(worldBoundingBox_.min_ - halfSize, worldBoundingBox_.max_ + halfSize);
  357. numDrawables_ = drawables_.Size();
  358. numLevels_ = numLevels;
  359. }
  360. void Octree::Update(const FrameInfo& frame)
  361. {
  362. UpdateDrawables(frame);
  363. ReinsertDrawables(frame);
  364. }
  365. void Octree::AddManualDrawable(Drawable* drawable)
  366. {
  367. if (!drawable || drawable->GetOctant())
  368. return;
  369. AddDrawable(drawable);
  370. }
  371. void Octree::RemoveManualDrawable(Drawable* drawable)
  372. {
  373. if (!drawable)
  374. return;
  375. Octant* octant = drawable->GetOctant();
  376. if (octant && octant->GetRoot() == this)
  377. octant->RemoveDrawable(drawable);
  378. }
  379. void Octree::GetDrawables(OctreeQuery& query) const
  380. {
  381. query.result_.Clear();
  382. GetDrawablesInternal(query, false);
  383. }
  384. void Octree::Raycast(RayOctreeQuery& query) const
  385. {
  386. PROFILE(Raycast);
  387. query.result_.Clear();
  388. WorkQueue* queue = GetSubsystem<WorkQueue>();
  389. // If no worker threads or no triangle-level testing, do not create work items
  390. if (!queue->GetNumThreads() || query.level_ < RAY_TRIANGLE)
  391. GetDrawablesInternal(query);
  392. else
  393. {
  394. // Threaded ray query: first get the drawables
  395. rayQuery_ = &query;
  396. rayQueryDrawables_.Clear();
  397. GetDrawablesOnlyInternal(query, rayQueryDrawables_);
  398. // Check that amount of drawables is large enough to justify threading
  399. if (rayQueryDrawables_.Size() > RAYCASTS_PER_WORK_ITEM)
  400. {
  401. for (unsigned i = 0; i < rayQueryResults_.Size(); ++i)
  402. rayQueryResults_[i].Clear();
  403. WorkItem item;
  404. item.workFunction_ = RaycastDrawablesWork;
  405. item.aux_ = const_cast<Octree*>(this);
  406. PODVector<Drawable*>::Iterator start = rayQueryDrawables_.Begin();
  407. while (start != rayQueryDrawables_.End())
  408. {
  409. PODVector<Drawable*>::Iterator end = rayQueryDrawables_.End();
  410. if (end - start > RAYCASTS_PER_WORK_ITEM)
  411. end = start + RAYCASTS_PER_WORK_ITEM;
  412. item.start_ = &(*start);
  413. item.end_ = &(*end);
  414. queue->AddWorkItem(item);
  415. start = end;
  416. }
  417. // Merge per-thread results
  418. queue->Complete();
  419. for (unsigned i = 0; i < rayQueryResults_.Size(); ++i)
  420. query.result_.Insert(query.result_.End(), rayQueryResults_[i].Begin(), rayQueryResults_[i].End());
  421. }
  422. else
  423. {
  424. for (PODVector<Drawable*>::Iterator i = rayQueryDrawables_.Begin(); i != rayQueryDrawables_.End(); ++i)
  425. (*i)->ProcessRayQuery(query, query.result_);
  426. }
  427. }
  428. Sort(query.result_.Begin(), query.result_.End(), CompareRayQueryResults);
  429. }
  430. void Octree::RaycastSingle(RayOctreeQuery& query) const
  431. {
  432. PROFILE(Raycast);
  433. query.result_.Clear();
  434. rayQueryDrawables_.Clear();
  435. GetDrawablesOnlyInternal(query, rayQueryDrawables_);
  436. // Sort by increasing hit distance to AABB
  437. for (PODVector<Drawable*>::Iterator i = rayQueryDrawables_.Begin(); i != rayQueryDrawables_.End(); ++i)
  438. {
  439. Drawable* drawable = *i;
  440. drawable->SetSortValue(query.ray_.HitDistance(drawable->GetWorldBoundingBox()));
  441. }
  442. Sort(rayQueryDrawables_.Begin(), rayQueryDrawables_.End(), CompareDrawables);
  443. // Then do the actual test according to the query, and early-out as possible
  444. float closestHit = M_INFINITY;
  445. for (PODVector<Drawable*>::Iterator i = rayQueryDrawables_.Begin(); i != rayQueryDrawables_.End(); ++i)
  446. {
  447. Drawable* drawable = *i;
  448. if (drawable->GetSortValue() <= Min(closestHit, query.maxDistance_))
  449. {
  450. unsigned oldSize = query.result_.Size();
  451. drawable->ProcessRayQuery(query, query.result_);
  452. if (query.result_.Size() > oldSize)
  453. closestHit = Min(closestHit, query.result_.Back().distance_);
  454. }
  455. else
  456. break;
  457. }
  458. if (query.result_.Size() > 1)
  459. {
  460. Sort(query.result_.Begin(), query.result_.End(), CompareRayQueryResults);
  461. query.result_.Resize(1);
  462. }
  463. }
  464. void Octree::QueueUpdate(Drawable* drawable)
  465. {
  466. drawableUpdates_.Push(WeakPtr<Drawable>(drawable));
  467. drawable->updateQueued_ = true;
  468. }
  469. void Octree::QueueReinsertion(Drawable* drawable)
  470. {
  471. Scene* scene = GetScene();
  472. if (scene && scene->IsThreadedUpdate())
  473. {
  474. MutexLock lock(octreeMutex_);
  475. drawableReinsertions_.Push(WeakPtr<Drawable>(drawable));
  476. }
  477. else
  478. drawableReinsertions_.Push(WeakPtr<Drawable>(drawable));
  479. drawable->reinsertionQueued_ = true;
  480. }
  481. void Octree::DrawDebugGeometry(bool depthTest)
  482. {
  483. PROFILE(OctreeDrawDebug);
  484. DebugRenderer* debug = GetComponent<DebugRenderer>();
  485. if (debug)
  486. Octant::DrawDebugGeometry(debug, depthTest);
  487. }
  488. void Octree::UpdateDrawables(const FrameInfo& frame)
  489. {
  490. // Let drawables update themselves before reinsertion
  491. if (drawableUpdates_.Empty())
  492. return;
  493. PROFILE(UpdateDrawables);
  494. Scene* scene = GetScene();
  495. WorkQueue* queue = GetSubsystem<WorkQueue>();
  496. scene->BeginThreadedUpdate();
  497. WorkItem item;
  498. item.workFunction_ = UpdateDrawablesWork;
  499. item.aux_ = const_cast<FrameInfo*>(&frame);
  500. Vector<WeakPtr<Drawable> >::Iterator start = drawableUpdates_.Begin();
  501. while (start != drawableUpdates_.End())
  502. {
  503. Vector<WeakPtr<Drawable> >::Iterator end = drawableUpdates_.End();
  504. if (end - start > DRAWABLES_PER_WORK_ITEM)
  505. end = start + DRAWABLES_PER_WORK_ITEM;
  506. item.start_ = &(*start);
  507. item.end_ = &(*end);
  508. queue->AddWorkItem(item);
  509. start = end;
  510. }
  511. queue->Complete();
  512. scene->EndThreadedUpdate();
  513. drawableUpdates_.Clear();
  514. }
  515. void Octree::ReinsertDrawables(const FrameInfo& frame)
  516. {
  517. if (drawableReinsertions_.Empty())
  518. return;
  519. PROFILE(ReinsertDrawables);
  520. // Reinsert drawables into the octree
  521. for (Vector<WeakPtr<Drawable> >::Iterator i = drawableReinsertions_.Begin(); i != drawableReinsertions_.End(); ++i)
  522. {
  523. Drawable* drawable = *i;
  524. if (!drawable)
  525. continue;
  526. Octant* octant = drawable->GetOctant();
  527. if (!octant)
  528. continue;
  529. const BoundingBox& box = drawable->GetWorldBoundingBox();
  530. Vector3 boxCenter = box.Center();
  531. Vector3 boxSize = box.Size();
  532. if (octant == this)
  533. {
  534. // Handle root octant as special case: if outside the root, do not reinsert
  535. if (GetCullingBox().IsInside(box) == INSIDE && !CheckDrawableSize(boxSize))
  536. InsertDrawable(drawable, boxCenter, boxSize);
  537. }
  538. else
  539. {
  540. // Break if drawable no longer belongs to this octree (could theoretically happen as we do not explicitly clean up
  541. // drawables from the reinsertion list as they leave the octree)
  542. if (octant->GetRoot() != this)
  543. continue;
  544. // Otherwise reinsert if outside current octant or if size does not fit octant size
  545. if (octant->GetCullingBox().IsInside(box) != INSIDE || !octant->CheckDrawableSize(boxSize))
  546. InsertDrawable(drawable, boxCenter, boxSize);
  547. }
  548. drawable->reinsertionQueued_ = false;
  549. }
  550. drawableReinsertions_.Clear();
  551. }