OctreeQuery.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. 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 URHO3D_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 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. 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 URHO3D_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 URHO3D_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 URHO3D_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. };
  158. /// Raycast result.
  159. struct URHO3D_API RayQueryResult
  160. {
  161. /// Construct with defaults.
  162. RayQueryResult() :
  163. drawable_(0),
  164. node_(0)
  165. {
  166. }
  167. /// Test for inequality, added to prevent GCC from complaining.
  168. 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_; }
  169. /// Hit position in world space.
  170. Vector3 position_;
  171. /// Hit normal in world space. Negation of ray direction if per-triangle data not available.
  172. Vector3 normal_;
  173. /// Distance from ray origin.
  174. float distance_;
  175. /// Drawable.
  176. Drawable* drawable_;
  177. /// Scene node.
  178. Node* node_;
  179. /// Drawable specific subobject if applicable.
  180. unsigned subObject_;
  181. };
  182. /// Raycast octree query.
  183. class URHO3D_API RayOctreeQuery
  184. {
  185. public:
  186. /// Construct with ray and query parameters.
  187. RayOctreeQuery(PODVector<RayQueryResult>& result, const Ray& ray, RayQueryLevel level = RAY_TRIANGLE,
  188. float maxDistance = M_INFINITY, unsigned char drawableFlags = DRAWABLE_ANY, unsigned viewMask = DEFAULT_VIEWMASK) :
  189. result_(result),
  190. ray_(ray),
  191. drawableFlags_(drawableFlags),
  192. viewMask_(viewMask),
  193. maxDistance_(maxDistance),
  194. level_(level)
  195. {
  196. }
  197. /// Result vector reference.
  198. PODVector<RayQueryResult>& result_;
  199. /// Ray.
  200. Ray ray_;
  201. /// Drawable flags to include.
  202. unsigned char drawableFlags_;
  203. /// Drawable layers to include.
  204. unsigned viewMask_;
  205. /// Maximum ray distance.
  206. float maxDistance_;
  207. /// Raycast detail level.
  208. RayQueryLevel level_;
  209. private:
  210. /// Prevent copy construction.
  211. RayOctreeQuery(const RayOctreeQuery& rhs);
  212. /// Prevent assignment.
  213. RayOctreeQuery& operator = (const RayOctreeQuery& rhs);
  214. };
  215. }