Octree.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. #pragma once
  24. #include "Drawable.h"
  25. #include "List.h"
  26. #include "Mutex.h"
  27. #include "OctreeQuery.h"
  28. class Octree;
  29. static const int NUM_OCTANTS = 8;
  30. /// %Octree octant
  31. class Octant
  32. {
  33. public:
  34. /// Construct.
  35. Octant(const BoundingBox& box, unsigned level, Octant* parent, Octree* root);
  36. /// Destruct. Move drawables to root if available (detach if not) and free child octants.
  37. virtual ~Octant();
  38. /// Return or create a child octant.
  39. Octant* GetOrCreateChild(unsigned index);
  40. /// Delete child octant.
  41. void DeleteChild(unsigned index);
  42. /// Delete child octant by pointer.
  43. void DeleteChild(Octant* octant);
  44. /// Insert a drawable object by checking for fit recursively.
  45. void InsertDrawable(Drawable* drawable, const Vector3& boxCenter, const Vector3& boxSize);
  46. /// Check if a drawable object fits.
  47. bool CheckDrawableSize(const Vector3& boxSize) const;
  48. /// Add a drawable object to this octant.
  49. void AddDrawable(Drawable* drawable)
  50. {
  51. drawable->SetOctant(this);
  52. drawables_.Push(drawable);
  53. IncDrawableCount();
  54. }
  55. /// Remove a drawable object from this octant.
  56. void RemoveDrawable(Drawable* drawable, bool resetOctant = true)
  57. {
  58. PODVector<Drawable*>::Iterator i = drawables_.Find(drawable);
  59. if (i != drawables_.End())
  60. {
  61. if (resetOctant)
  62. drawable->SetOctant(0);
  63. drawables_.Erase(i);
  64. DecDrawableCount();
  65. }
  66. }
  67. /// Return world bounding box.
  68. const BoundingBox& GetWorldBoundingBox() const { return worldBoundingBox_; }
  69. /// Return bounding box used for fitting drawable objects.
  70. const BoundingBox& GetCullingBox() const { return cullingBox_; }
  71. /// Return subdivision level.
  72. unsigned GetLevel() const { return level_; }
  73. /// Return parent octant.
  74. Octant* GetParent() const { return parent_; }
  75. /// Return octree root.
  76. Octree* GetRoot() const { return root_; }
  77. /// Return true if there are no drawable objects in this octant and child octants.
  78. bool IsEmpty() { return numDrawables_ == 0; }
  79. /// Reset root pointer recursively. Called when the whole octree is being destroyed.
  80. void ResetRoot();
  81. /// Draw bounds to the debug graphics recursively.
  82. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  83. protected:
  84. /// Return drawable objects by a query, called internally.
  85. void GetDrawablesInternal(OctreeQuery& query, bool inside) const;
  86. /// Return drawable objects by a ray query, called internally.
  87. void GetDrawablesInternal(RayOctreeQuery& query) const;
  88. /// Return drawable objects only for a threaded ray query, called internally.
  89. void GetDrawablesOnlyInternal(RayOctreeQuery& query, PODVector<Drawable*>& drawables) const;
  90. /// Free child octants. If drawable objects still exist, move them to root.
  91. void Release();
  92. /// Increase drawable object count recursively.
  93. void IncDrawableCount()
  94. {
  95. ++numDrawables_;
  96. if (parent_)
  97. parent_->IncDrawableCount();
  98. }
  99. /// Decrease drawable object count recursively and remove octant if it becomes empty.
  100. void DecDrawableCount()
  101. {
  102. Octant* parent = parent_;
  103. --numDrawables_;
  104. if (!numDrawables_)
  105. {
  106. if (parent)
  107. parent->DeleteChild(this);
  108. }
  109. if (parent)
  110. parent->DecDrawableCount();
  111. }
  112. /// World bounding box.
  113. BoundingBox worldBoundingBox_;
  114. /// Bounding box used for drawable object fitting.
  115. BoundingBox cullingBox_;
  116. /// World bounding box center.
  117. Vector3 center_;
  118. /// World bounding box half size.
  119. Vector3 halfSize_;
  120. /// Subdivision level.
  121. unsigned level_;
  122. /// Parent octant.
  123. Octant* parent_;
  124. /// Child octants.
  125. Octant* children_[NUM_OCTANTS];
  126. /// Octree root.
  127. Octree* root_;
  128. /// Drawable objects.
  129. PODVector<Drawable*> drawables_;
  130. /// Number of drawable objects in this octant and child octants.
  131. unsigned numDrawables_;
  132. };
  133. /// %Octree component. Should be added only to the root scene node
  134. class Octree : public Component, public Octant
  135. {
  136. friend void RaycastDrawablesWork(const WorkItem* item, unsigned threadIndex);
  137. OBJECT(Octree);
  138. public:
  139. /// Construct.
  140. Octree(Context* context);
  141. /// Destruct.
  142. ~Octree();
  143. /// Register object factory.
  144. static void RegisterObject(Context* context);
  145. /// Handle attribute change.
  146. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  147. /// Resize octree. If octree is not empty, drawable objects will be temporarily moved to the root.
  148. void Resize(const BoundingBox& box, unsigned numLevels);
  149. /// Update and reinsert drawable objects.
  150. void Update(const FrameInfo& frame);
  151. /// Add a drawable manually, which can optionally be unculled.
  152. void AddManualDrawable(Drawable* drawable, bool culling);
  153. /// Remove a manually added drawable.
  154. void RemoveManualDrawable(Drawable* drawable);
  155. /// Return drawable objects by a query.
  156. void GetDrawables(OctreeQuery& query) const;
  157. /// Return unculled drawables by drawable type. The destination vector will not be cleared.
  158. void GetUnculledDrawables(PODVector<Drawable*>& dest, unsigned char drawableFlags) const;
  159. /// Return drawable objects by a ray query.
  160. void Raycast(RayOctreeQuery& query) const;
  161. /// Return the closest drawable object by a ray query.
  162. void RaycastSingle(RayOctreeQuery& query) const;
  163. /// Return subdivision levels.
  164. unsigned GetNumLevels() const { return numLevels_; }
  165. /// Mark drawable object as requiring an update.
  166. void QueueUpdate(Drawable* drawable);
  167. /// Mark drawable object as requiring a reinsertion. Is thread-safe.
  168. void QueueReinsertion(Drawable* drawable);
  169. /// Remove drawable object from update list.
  170. void CancelUpdate(Drawable* drawable);
  171. /// Remove drawable object from reinsertion list.
  172. void CancelReinsertion(Drawable* drawable);
  173. /// Add debug geometry to the debug renderer.
  174. void DrawDebugGeometry(bool depthTest);
  175. protected:
  176. /// Handle node being assigned.
  177. virtual void OnNodeSet(Node* node);
  178. private:
  179. /// Update drawable objects marked for update. Updates are executed in worker threads.
  180. void UpdateDrawables(const FrameInfo& frame);
  181. /// Reinsert moved drawable objects into the octree.
  182. void ReinsertDrawables(const FrameInfo& frame);
  183. /// Scene.
  184. Scene* scene_;
  185. /// Drawable objects that require update.
  186. PODVector<Drawable*> drawableUpdates_;
  187. /// Drawable objects that require reinsertion.
  188. PODVector<Drawable*> drawableReinsertions_;
  189. /// Mutex for octree reinsertions.
  190. Mutex octreeMutex_;
  191. /// Unculled drawables.
  192. Vector<WeakPtr<Drawable> > unculledDrawables_;
  193. /// Current threaded ray query.
  194. mutable RayOctreeQuery* rayQuery_;
  195. /// Drawable list for threaded ray query.
  196. mutable PODVector<Drawable*> rayGetDrawables_;
  197. /// Threaded ray query intermediate results.
  198. mutable Vector<PODVector<RayQueryResult> > rayQueryResults_;
  199. /// Subdivision level.
  200. unsigned numLevels_;
  201. };