OctreeQuery.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Graphics/Drawable.h"
  6. #include "../Math/BoundingBox.h"
  7. #include "../Math/Frustum.h"
  8. #include "../Math/Ray.h"
  9. #include "../Math/Sphere.h"
  10. namespace Urho3D
  11. {
  12. class Drawable;
  13. class Node;
  14. /// Base class for octree queries.
  15. class URHO3D_API OctreeQuery
  16. {
  17. public:
  18. /// Construct with query parameters.
  19. OctreeQuery(Vector<Drawable*>& result, DrawableTypes drawableTypes, unsigned viewMask) :
  20. result_(result),
  21. drawableTypes_(drawableTypes),
  22. viewMask_(viewMask)
  23. {
  24. }
  25. /// Destruct.
  26. virtual ~OctreeQuery() = default;
  27. /// Prevent copy construction.
  28. OctreeQuery(const OctreeQuery& rhs) = delete;
  29. /// Prevent assignment.
  30. OctreeQuery& operator =(const OctreeQuery& rhs) = delete;
  31. /// Intersection test for an octant.
  32. virtual Intersection TestOctant(const BoundingBox& box, bool inside) = 0;
  33. /// Intersection test for drawables.
  34. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside) = 0;
  35. /// Result vector reference.
  36. Vector<Drawable*>& result_;
  37. /// Drawable flags to include.
  38. DrawableTypes drawableTypes_;
  39. /// Drawable layers to include.
  40. unsigned viewMask_;
  41. };
  42. /// Point octree query.
  43. /// @nobind
  44. class URHO3D_API PointOctreeQuery : public OctreeQuery
  45. {
  46. public:
  47. /// Construct with point and query parameters.
  48. PointOctreeQuery(Vector<Drawable*>& result, const Vector3& point, DrawableTypes drawableTypes = DrawableTypes::Any,
  49. unsigned viewMask = DEFAULT_VIEWMASK) :
  50. OctreeQuery(result, drawableTypes, viewMask),
  51. point_(point)
  52. {
  53. }
  54. /// Intersection test for an octant.
  55. Intersection TestOctant(const BoundingBox& box, bool inside) override;
  56. /// Intersection test for drawables.
  57. void TestDrawables(Drawable** start, Drawable** end, bool inside) override;
  58. /// Point.
  59. Vector3 point_;
  60. };
  61. /// %Sphere octree query.
  62. /// @nobind
  63. class URHO3D_API SphereOctreeQuery : public OctreeQuery
  64. {
  65. public:
  66. /// Construct with sphere and query parameters.
  67. SphereOctreeQuery(Vector<Drawable*>& result, const Sphere& sphere, DrawableTypes drawableTypes = DrawableTypes::Any,
  68. unsigned viewMask = DEFAULT_VIEWMASK) :
  69. OctreeQuery(result, drawableTypes, viewMask),
  70. sphere_(sphere)
  71. {
  72. }
  73. /// Intersection test for an octant.
  74. Intersection TestOctant(const BoundingBox& box, bool inside) override;
  75. /// Intersection test for drawables.
  76. void TestDrawables(Drawable** start, Drawable** end, bool inside) override;
  77. /// Sphere.
  78. Sphere sphere_;
  79. };
  80. /// Bounding box octree query.
  81. /// @nobind
  82. class URHO3D_API BoxOctreeQuery : public OctreeQuery
  83. {
  84. public:
  85. /// Construct with bounding box and query parameters.
  86. BoxOctreeQuery(Vector<Drawable*>& result, const BoundingBox& box, DrawableTypes drawableTypes = DrawableTypes::Any,
  87. unsigned viewMask = DEFAULT_VIEWMASK) :
  88. OctreeQuery(result, drawableTypes, viewMask),
  89. box_(box)
  90. {
  91. }
  92. /// Intersection test for an octant.
  93. Intersection TestOctant(const BoundingBox& box, bool inside) override;
  94. /// Intersection test for drawables.
  95. void TestDrawables(Drawable** start, Drawable** end, bool inside) override;
  96. /// Bounding box.
  97. BoundingBox box_;
  98. };
  99. /// %Frustum octree query.
  100. /// @nobind
  101. class URHO3D_API FrustumOctreeQuery : public OctreeQuery
  102. {
  103. public:
  104. /// Construct with frustum and query parameters.
  105. FrustumOctreeQuery(Vector<Drawable*>& result, const Frustum& frustum, DrawableTypes drawableTypes = DrawableTypes::Any,
  106. unsigned viewMask = DEFAULT_VIEWMASK) :
  107. OctreeQuery(result, drawableTypes, viewMask),
  108. frustum_(frustum)
  109. {
  110. }
  111. /// Intersection test for an octant.
  112. Intersection TestOctant(const BoundingBox& box, bool inside) override;
  113. /// Intersection test for drawables.
  114. void TestDrawables(Drawable** start, Drawable** end, bool inside) override;
  115. /// Frustum.
  116. Frustum frustum_;
  117. };
  118. /// General octree query result. Used for Lua bindings only.
  119. struct URHO3D_API OctreeQueryResult
  120. {
  121. /// Construct with defaults.
  122. OctreeQueryResult() :
  123. drawable_(nullptr),
  124. node_(nullptr)
  125. {
  126. }
  127. /// Test for inequality, added to prevent GCC from complaining.
  128. bool operator !=(const OctreeQueryResult& rhs) const { return drawable_ != rhs.drawable_ || node_ != rhs.node_; }
  129. /// Drawable.
  130. Drawable* drawable_;
  131. /// Scene node.
  132. Node* node_;
  133. };
  134. /// Graphics raycast detail level.
  135. enum RayQueryLevel
  136. {
  137. RAY_AABB = 0,
  138. RAY_OBB,
  139. RAY_TRIANGLE,
  140. RAY_TRIANGLE_UV
  141. };
  142. /// Raycast result.
  143. struct URHO3D_API RayQueryResult
  144. {
  145. /// Construct with defaults.
  146. RayQueryResult() :
  147. drawable_(nullptr),
  148. node_(nullptr)
  149. {
  150. }
  151. /// Test for inequality, added to prevent GCC from complaining.
  152. bool operator !=(const RayQueryResult& rhs) const
  153. {
  154. return position_ != rhs.position_ ||
  155. normal_ != rhs.normal_ ||
  156. textureUV_ != rhs.textureUV_ ||
  157. distance_ != rhs.distance_ ||
  158. drawable_ != rhs.drawable_ ||
  159. node_ != rhs.node_ ||
  160. subObject_ != rhs.subObject_;
  161. }
  162. /// Hit position in world space.
  163. Vector3 position_;
  164. /// Hit normal in world space. Negation of ray direction if per-triangle data not available.
  165. Vector3 normal_;
  166. /// Hit texture position.
  167. Vector2 textureUV_;
  168. /// Distance from ray origin.
  169. float distance_{};
  170. /// Drawable.
  171. Drawable* drawable_;
  172. /// Scene node.
  173. Node* node_;
  174. /// Drawable specific subobject if applicable.
  175. i32 subObject_{};
  176. };
  177. /// Raycast octree query.
  178. /// @nobind
  179. class URHO3D_API RayOctreeQuery
  180. {
  181. public:
  182. /// Construct with ray and query parameters.
  183. RayOctreeQuery(Vector<RayQueryResult>& result, const Ray& ray, RayQueryLevel level = RAY_TRIANGLE,
  184. float maxDistance = M_INFINITY, DrawableTypes drawableTypes = DrawableTypes::Any, unsigned viewMask = DEFAULT_VIEWMASK) :
  185. result_(result),
  186. ray_(ray),
  187. drawableTypes_(drawableTypes),
  188. viewMask_(viewMask),
  189. maxDistance_(maxDistance),
  190. level_(level)
  191. {
  192. }
  193. /// Prevent copy construction.
  194. RayOctreeQuery(const RayOctreeQuery& rhs) = delete;
  195. /// Prevent assignment.
  196. RayOctreeQuery& operator =(const RayOctreeQuery& rhs) = delete;
  197. /// Result vector reference.
  198. Vector<RayQueryResult>& result_;
  199. /// Ray.
  200. Ray ray_;
  201. /// Drawable types to include.
  202. DrawableTypes drawableTypes_;
  203. /// Drawable layers to include.
  204. unsigned viewMask_;
  205. /// Maximum ray distance.
  206. float maxDistance_;
  207. /// Raycast detail level.
  208. RayQueryLevel level_;
  209. };
  210. /// @nobind
  211. class URHO3D_API AllContentOctreeQuery : public OctreeQuery
  212. {
  213. public:
  214. /// Construct.
  215. AllContentOctreeQuery(Vector<Drawable*>& result, DrawableTypes drawableTypes, unsigned viewMask) :
  216. OctreeQuery(result, drawableTypes, viewMask)
  217. {
  218. }
  219. /// Intersection test for an octant.
  220. Intersection TestOctant(const BoundingBox& box, bool inside) override;
  221. /// Intersection test for drawables.
  222. void TestDrawables(Drawable** start, Drawable** end, bool inside) override;
  223. };
  224. }