OctreeQuery.h 8.3 KB

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