OctreeQuery.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. /// @nobind
  63. class URHO3D_API PointOctreeQuery : public OctreeQuery
  64. {
  65. public:
  66. /// Construct with point and query parameters.
  67. PointOctreeQuery(PODVector<Drawable*>& result, const Vector3& point, unsigned char drawableFlags = DRAWABLE_ANY,
  68. unsigned viewMask = DEFAULT_VIEWMASK) :
  69. OctreeQuery(result, drawableFlags, viewMask),
  70. point_(point)
  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. /// Point.
  78. Vector3 point_;
  79. };
  80. /// %Sphere octree query.
  81. /// @nobind
  82. class URHO3D_API SphereOctreeQuery : public OctreeQuery
  83. {
  84. public:
  85. /// Construct with sphere and query parameters.
  86. SphereOctreeQuery(PODVector<Drawable*>& result, const Sphere& sphere, unsigned char drawableFlags = DRAWABLE_ANY,
  87. unsigned viewMask = DEFAULT_VIEWMASK) :
  88. OctreeQuery(result, drawableFlags, viewMask),
  89. sphere_(sphere)
  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. /// Sphere.
  97. Sphere sphere_;
  98. };
  99. /// Bounding box octree query.
  100. /// @nobind
  101. class URHO3D_API BoxOctreeQuery : public OctreeQuery
  102. {
  103. public:
  104. /// Construct with bounding box and query parameters.
  105. BoxOctreeQuery(PODVector<Drawable*>& result, const BoundingBox& box, unsigned char drawableFlags = DRAWABLE_ANY,
  106. unsigned viewMask = DEFAULT_VIEWMASK) :
  107. OctreeQuery(result, drawableFlags, viewMask),
  108. box_(box)
  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. /// Bounding box.
  116. BoundingBox box_;
  117. };
  118. /// %Frustum octree query.
  119. /// @nobind
  120. class URHO3D_API FrustumOctreeQuery : public OctreeQuery
  121. {
  122. public:
  123. /// Construct with frustum and query parameters.
  124. FrustumOctreeQuery(PODVector<Drawable*>& result, const Frustum& frustum, unsigned char drawableFlags = DRAWABLE_ANY,
  125. unsigned viewMask = DEFAULT_VIEWMASK) :
  126. OctreeQuery(result, drawableFlags, viewMask),
  127. frustum_(frustum)
  128. {
  129. }
  130. /// Intersection test for an octant.
  131. Intersection TestOctant(const BoundingBox& box, bool inside) override;
  132. /// Intersection test for drawables.
  133. void TestDrawables(Drawable** start, Drawable** end, bool inside) override;
  134. /// Frustum.
  135. Frustum frustum_;
  136. };
  137. /// General octree query result. Used for Lua bindings only.
  138. struct URHO3D_API OctreeQueryResult
  139. {
  140. /// Construct with defaults.
  141. OctreeQueryResult() :
  142. drawable_(nullptr),
  143. node_(nullptr)
  144. {
  145. }
  146. /// Test for inequality, added to prevent GCC from complaining.
  147. bool operator !=(const OctreeQueryResult& rhs) const { return drawable_ != rhs.drawable_ || node_ != rhs.node_; }
  148. /// Drawable.
  149. Drawable* drawable_;
  150. /// Scene node.
  151. Node* node_;
  152. };
  153. /// Graphics raycast detail level.
  154. enum RayQueryLevel
  155. {
  156. RAY_AABB = 0,
  157. RAY_OBB,
  158. RAY_TRIANGLE,
  159. RAY_TRIANGLE_UV
  160. };
  161. /// Raycast result.
  162. struct URHO3D_API RayQueryResult
  163. {
  164. /// Construct with defaults.
  165. RayQueryResult() :
  166. drawable_(nullptr),
  167. node_(nullptr)
  168. {
  169. }
  170. /// Test for inequality, added to prevent GCC from complaining.
  171. bool operator !=(const RayQueryResult& rhs) const
  172. {
  173. return position_ != rhs.position_ ||
  174. normal_ != rhs.normal_ ||
  175. textureUV_ != rhs.textureUV_ ||
  176. distance_ != rhs.distance_ ||
  177. drawable_ != rhs.drawable_ ||
  178. node_ != rhs.node_ ||
  179. subObject_ != rhs.subObject_;
  180. }
  181. /// Hit position in world space.
  182. Vector3 position_;
  183. /// Hit normal in world space. Negation of ray direction if per-triangle data not available.
  184. Vector3 normal_;
  185. /// Hit texture position.
  186. Vector2 textureUV_;
  187. /// Distance from ray origin.
  188. float distance_{};
  189. /// Drawable.
  190. Drawable* drawable_;
  191. /// Scene node.
  192. Node* node_;
  193. /// Drawable specific subobject if applicable.
  194. unsigned subObject_{};
  195. };
  196. /// Raycast octree query.
  197. /// @nobind
  198. class URHO3D_API RayOctreeQuery
  199. {
  200. public:
  201. /// Construct with ray and query parameters.
  202. RayOctreeQuery(PODVector<RayQueryResult>& result, const Ray& ray, RayQueryLevel level = RAY_TRIANGLE,
  203. float maxDistance = M_INFINITY, unsigned char drawableFlags = DRAWABLE_ANY, unsigned viewMask = DEFAULT_VIEWMASK) :
  204. result_(result),
  205. ray_(ray),
  206. drawableFlags_(drawableFlags),
  207. viewMask_(viewMask),
  208. maxDistance_(maxDistance),
  209. level_(level)
  210. {
  211. }
  212. /// Prevent copy construction.
  213. RayOctreeQuery(const RayOctreeQuery& rhs) = delete;
  214. /// Prevent assignment.
  215. RayOctreeQuery& operator =(const RayOctreeQuery& rhs) = delete;
  216. /// Result vector reference.
  217. PODVector<RayQueryResult>& result_;
  218. /// Ray.
  219. Ray ray_;
  220. /// Drawable flags to include.
  221. unsigned char drawableFlags_;
  222. /// Drawable layers to include.
  223. unsigned viewMask_;
  224. /// Maximum ray distance.
  225. float maxDistance_;
  226. /// Raycast detail level.
  227. RayQueryLevel level_;
  228. };
  229. /// @nobind
  230. class URHO3D_API AllContentOctreeQuery : public OctreeQuery
  231. {
  232. public:
  233. /// Construct.
  234. AllContentOctreeQuery(PODVector<Drawable*>& result, unsigned char drawableFlags, unsigned viewMask) :
  235. OctreeQuery(result, drawableFlags, viewMask)
  236. {
  237. }
  238. /// Intersection test for an octant.
  239. Intersection TestOctant(const BoundingBox& box, bool inside) override;
  240. /// Intersection test for drawables.
  241. void TestDrawables(Drawable** start, Drawable** end, bool inside) override;
  242. };
  243. }