OctreeQuery.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // Copyright (c) 2008-2014 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. /// General octree query result. Used for Lua bindings only.
  131. struct URHO3D_API OctreeQueryResult
  132. {
  133. /// Construct with defaults.
  134. OctreeQueryResult() :
  135. drawable_(0),
  136. node_(0)
  137. {
  138. }
  139. /// Test for inequality, added to prevent GCC from complaining.
  140. bool operator != (const OctreeQueryResult& rhs) const { return drawable_ != rhs.drawable_ || node_ != rhs.node_; }
  141. /// Drawable.
  142. Drawable* drawable_;
  143. /// Scene node.
  144. Node* node_;
  145. };
  146. /// Graphics raycast detail level.
  147. enum RayQueryLevel
  148. {
  149. RAY_AABB = 0,
  150. RAY_OBB,
  151. RAY_TRIANGLE
  152. };
  153. /// Raycast result.
  154. struct URHO3D_API RayQueryResult
  155. {
  156. /// Construct with defaults.
  157. RayQueryResult() :
  158. drawable_(0),
  159. node_(0)
  160. {
  161. }
  162. /// Test for inequality, added to prevent GCC from complaining.
  163. 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_; }
  164. /// Hit position in world space.
  165. Vector3 position_;
  166. /// Hit normal in world space. Negation of ray direction if per-triangle data not available.
  167. Vector3 normal_;
  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. unsigned subObject_;
  176. };
  177. /// Raycast octree query.
  178. class URHO3D_API RayOctreeQuery
  179. {
  180. public:
  181. /// Construct with ray and query parameters.
  182. RayOctreeQuery(PODVector<RayQueryResult>& result, const Ray& ray, RayQueryLevel level = RAY_TRIANGLE,
  183. float maxDistance = M_INFINITY, unsigned char drawableFlags = DRAWABLE_ANY, unsigned viewMask = DEFAULT_VIEWMASK) :
  184. result_(result),
  185. ray_(ray),
  186. drawableFlags_(drawableFlags),
  187. viewMask_(viewMask),
  188. maxDistance_(maxDistance),
  189. level_(level)
  190. {
  191. }
  192. /// Result vector reference.
  193. PODVector<RayQueryResult>& result_;
  194. /// Ray.
  195. Ray ray_;
  196. /// Drawable flags to include.
  197. unsigned char drawableFlags_;
  198. /// Drawable layers to include.
  199. unsigned viewMask_;
  200. /// Maximum ray distance.
  201. float maxDistance_;
  202. /// Raycast detail level.
  203. RayQueryLevel level_;
  204. };
  205. }