Octree.cpp 20 KB

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