OctreeQuery.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // Copyright (c) 2008-2013 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 "BoundingBox.h"
  24. #include "Drawable.h"
  25. #include "Frustum.h"
  26. #include "Ray.h"
  27. #include "Sphere.h"
  28. namespace Urho3D
  29. {
  30. class Drawable;
  31. class Node;
  32. /// Base class for octree queries.
  33. class URHO3D_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. };
  58. /// Point octree query.
  59. class URHO3D_API PointOctreeQuery : public OctreeQuery
  60. {
  61. public:
  62. /// Construct with point and query parameters.
  63. PointOctreeQuery(PODVector<Drawable*>& result, const Vector3& point, unsigned char drawableFlags = DRAWABLE_ANY,
  64. unsigned viewMask = DEFAULT_VIEWMASK) :
  65. OctreeQuery(result, drawableFlags, viewMask),
  66. point_(point)
  67. {
  68. }
  69. /// Intersection test for an octant.
  70. virtual Intersection TestOctant(const BoundingBox& box, bool inside);
  71. /// Intersection test for drawables.
  72. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside);
  73. /// Point.
  74. Vector3 point_;
  75. };
  76. /// %Sphere octree query.
  77. class URHO3D_API SphereOctreeQuery : public OctreeQuery
  78. {
  79. public:
  80. /// Construct with sphere and query parameters.
  81. SphereOctreeQuery(PODVector<Drawable*>& result, const Sphere& sphere, unsigned char drawableFlags = DRAWABLE_ANY,
  82. unsigned viewMask = DEFAULT_VIEWMASK) :
  83. OctreeQuery(result, drawableFlags, viewMask),
  84. sphere_(sphere)
  85. {
  86. }
  87. /// Intersection test for an octant.
  88. virtual Intersection TestOctant(const BoundingBox& box, bool inside);
  89. /// Intersection test for drawables.
  90. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside);
  91. /// Sphere.
  92. Sphere sphere_;
  93. };
  94. /// Bounding box octree query.
  95. class URHO3D_API BoxOctreeQuery : public OctreeQuery
  96. {
  97. public:
  98. /// Construct with bounding box and query parameters.
  99. BoxOctreeQuery(PODVector<Drawable*>& result, const BoundingBox& box, unsigned char drawableFlags = DRAWABLE_ANY,
  100. unsigned viewMask = DEFAULT_VIEWMASK) :
  101. OctreeQuery(result, drawableFlags, viewMask),
  102. box_(box)
  103. {
  104. }
  105. /// Intersection test for an octant.
  106. virtual Intersection TestOctant(const BoundingBox& box, bool inside);
  107. /// Intersection test for drawables.
  108. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside);
  109. /// Bounding box.
  110. BoundingBox box_;
  111. };
  112. /// %Frustum octree query.
  113. class URHO3D_API FrustumOctreeQuery : public OctreeQuery
  114. {
  115. public:
  116. /// Construct with frustum and query parameters.
  117. FrustumOctreeQuery(PODVector<Drawable*>& result, const Frustum& frustum, unsigned char drawableFlags = DRAWABLE_ANY,
  118. unsigned viewMask = DEFAULT_VIEWMASK) :
  119. OctreeQuery(result, drawableFlags, viewMask),
  120. frustum_(frustum)
  121. {
  122. }
  123. /// Intersection test for an octant.
  124. virtual Intersection TestOctant(const BoundingBox& box, bool inside);
  125. /// Intersection test for drawables.
  126. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside);
  127. /// Frustum.
  128. Frustum frustum_;
  129. };
  130. /// Graphics raycast detail level.
  131. enum RayQueryLevel
  132. {
  133. RAY_AABB_NOSUBOBJECTS = 0,
  134. RAY_AABB,
  135. RAY_OBB,
  136. RAY_TRIANGLE
  137. };
  138. /// Raycast result.
  139. struct RayQueryResult
  140. {
  141. RayQueryResult() :
  142. drawable_(0),
  143. node_(0)
  144. {
  145. }
  146. bool operator != (const RayQueryResult& rhs) const { return position_!=rhs.position_ || normal_!=rhs.normal_ || distance_!=rhs.distance_ || drawable_!=rhs.drawable_ || node_!=rhs.node_ || subObject_!=rhs.subObject_; }
  147. /// Hit position in world space.
  148. Vector3 position_;
  149. /// Hit normal in world space. Negation of ray direction if per-triangle data not available.
  150. Vector3 normal_;
  151. /// Distance from ray origin.
  152. float distance_;
  153. /// Drawable.
  154. Drawable* drawable_;
  155. /// Scene node.
  156. Node* node_;
  157. /// Drawable specific subobject if applicable.
  158. unsigned subObject_;
  159. };
  160. /// Raycast octree query.
  161. class URHO3D_API RayOctreeQuery
  162. {
  163. public:
  164. /// Construct with ray and query parameters.
  165. RayOctreeQuery(PODVector<RayQueryResult>& result, const Ray& ray, RayQueryLevel level = RAY_TRIANGLE,
  166. float maxDistance = M_INFINITY, unsigned char drawableFlags = DRAWABLE_ANY, unsigned viewMask = DEFAULT_VIEWMASK) :
  167. result_(result),
  168. ray_(ray),
  169. drawableFlags_(drawableFlags),
  170. viewMask_(viewMask),
  171. maxDistance_(maxDistance),
  172. level_(level)
  173. {
  174. }
  175. /// Result vector reference.
  176. PODVector<RayQueryResult>& result_;
  177. /// Ray.
  178. Ray ray_;
  179. /// Drawable flags to include.
  180. unsigned char drawableFlags_;
  181. /// Drawable layers to include.
  182. unsigned viewMask_;
  183. /// Maximum ray distance.
  184. float maxDistance_;
  185. /// Raycast detail level.
  186. RayQueryLevel level_;
  187. };
  188. }