Octree.cpp 20 KB

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