Octree.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "Set.h"
  26. class Drawable;
  27. class Octree;
  28. class OctreeQuery;
  29. class RayOctreeQuery;
  30. static const int NUM_OCTANTS = 8;
  31. #include "stdio.h"
  32. /// Octree octant
  33. class Octant
  34. {
  35. public:
  36. /// Construct
  37. Octant(const BoundingBox& box, unsigned level, Octant* parent, Octree* root);
  38. /// Destruct. Move drawables to root if available (detach if not) and free child octants
  39. virtual ~Octant();
  40. /// Return or create a child octant
  41. Octant* GetOrCreateChild(unsigned index);
  42. /// Delete child octant
  43. void DeleteChild(unsigned index);
  44. /// Delete child octant by pointer
  45. void DeleteChild(Octant* octant);
  46. /// Insert a drawable object by checking for fit recursively
  47. void InsertDrawable(Drawable* drawable);
  48. /// Check if a drawable object fits
  49. bool CheckDrawableSize(Drawable* drawable) const;
  50. /// Add a drawable object to this octant
  51. void AddDrawable(Drawable* drawable)
  52. {
  53. drawable->SetOctant(this);
  54. drawables_.Push(drawable);
  55. IncDrawableCount();
  56. }
  57. /// Remove a drawable object from this octant
  58. void RemoveDrawable(Drawable* drawable, bool resetOctant = true)
  59. {
  60. for (PODVector<Drawable*>::Iterator i = drawables_.Begin(); i != drawables_.End(); ++i)
  61. {
  62. if (*i == drawable)
  63. {
  64. if (resetOctant)
  65. drawable->SetOctant(0);
  66. drawables_.Erase(i);
  67. DecDrawableCount();
  68. return;
  69. }
  70. }
  71. }
  72. /// Return world bounding box
  73. const BoundingBox& GetWorldBoundingBox() const { return worldBoundingBox_; }
  74. /// Return bounding box used for fitting drawable objects
  75. const BoundingBox& GetCullingBox() const { return cullingBox_; }
  76. /// Return subdivision level
  77. unsigned GetLevel() const { return level_; }
  78. /// Return parent octant
  79. Octant* GetParent() const { return parent_; }
  80. /// Return octree root
  81. Octree* GetRoot() const { return root_; }
  82. /// Return true if there are no drawable objects in this octant and child octants
  83. bool IsEmpty() { return numDrawables_ == 0; }
  84. /// Reset root pointer recursively. Called when the whole octree is being destroyed
  85. void ResetRoot();
  86. /// Draw bounds to the debug graphics recursively
  87. void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  88. protected:
  89. /// Return drawable objects by a query, called internally
  90. void GetDrawablesInternal(OctreeQuery& query, unsigned mask) const;
  91. /// Return drawable objects by a ray query, called internally
  92. void GetDrawablesInternal(RayOctreeQuery& query) const;
  93. /// Free child octants. If drawable objects still exist, move them to root
  94. void Release();
  95. /// Increase drawable object count recursively
  96. void IncDrawableCount()
  97. {
  98. ++numDrawables_;
  99. if (parent_)
  100. parent_->IncDrawableCount();
  101. }
  102. /// Decrease drawable object count recursively and remove octant if it becomes empty
  103. void DecDrawableCount()
  104. {
  105. Octant* parent = parent_;
  106. --numDrawables_;
  107. if (!numDrawables_)
  108. {
  109. if (parent)
  110. parent->DeleteChild(this);
  111. }
  112. if (parent)
  113. parent->DecDrawableCount();
  114. }
  115. /// World bounding box
  116. BoundingBox worldBoundingBox_;
  117. /// Bounding box used for drawable object fitting
  118. BoundingBox cullingBox_;
  119. /// Subdivision level
  120. unsigned level_;
  121. /// Parent octant
  122. Octant* parent_;
  123. /// Child octants
  124. Octant* children_[NUM_OCTANTS];
  125. /// Octree root
  126. Octree* root_;
  127. /// Drawable objects
  128. PODVector<Drawable*> drawables_;
  129. /// Number of drawable objects in this octant and child octants
  130. unsigned numDrawables_;
  131. };
  132. /// Octree component. Should be added only to the root scene node
  133. class Octree : public Component, public Octant
  134. {
  135. OBJECT(Octree);
  136. public:
  137. /// Construct
  138. Octree(Context* context);
  139. /// Destruct
  140. ~Octree();
  141. /// Register object factory
  142. static void RegisterObject(Context* context);
  143. /// Handle attribute change
  144. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& value);
  145. /// Resize octree. If octree is not empty, drawable objects will be temporarily moved to the root
  146. void Resize(const BoundingBox& box, unsigned numLevels);
  147. /// Update and reinsert drawable objects
  148. void Update(const FrameInfo& frame);
  149. /// Return drawable objects by a query
  150. void GetDrawables(OctreeQuery& query) const;
  151. /// Return drawable objects by a ray query
  152. void GetDrawables(RayOctreeQuery& query) const;
  153. /// Return subdivision levels
  154. unsigned GetNumLevels() const { return numLevels_; }
  155. /// Mark drawable object as requiring an update
  156. void QueueUpdate(Drawable* drawable);
  157. /// Mark drawable object as requiring a reinsertion
  158. void QueueReinsertion(Drawable* drawable);
  159. /// Remove drawable object from update list
  160. void CancelUpdate(Drawable* drawable);
  161. /// Remove drawable object from reinsertion list
  162. void CancelReinsertion(Drawable* drawable);
  163. /// Add debug geometry to the debug graphics
  164. void DrawDebugGeometry(bool depthTest);
  165. private:
  166. /// Set of drawable objects that require update
  167. Set<Drawable*> drawableUpdates_;
  168. /// Set of drawable objects that require reinsertion
  169. Set<Drawable*> drawableReinsertions_;
  170. /// Subdivision level
  171. unsigned numLevels_;
  172. };