OctreeQuery.h 8.4 KB

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