OctreeQuery.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "BoundingBox.h"
  25. #include "Drawable.h"
  26. #include "Frustum.h"
  27. #include "Ray.h"
  28. #include "Sphere.h"
  29. class Drawable;
  30. class Node;
  31. /// Base class for octree queries.
  32. class OctreeQuery
  33. {
  34. public:
  35. /// Construct with query parameters.
  36. OctreeQuery(PODVector<Drawable*>& result, unsigned char drawableFlags, unsigned viewMask) :
  37. result_(result),
  38. drawableFlags_(drawableFlags),
  39. viewMask_(viewMask)
  40. {
  41. }
  42. /// Destruct.
  43. virtual ~OctreeQuery()
  44. {
  45. }
  46. /// Intersection test for an octant.
  47. virtual Intersection TestOctant(const BoundingBox& box, bool inside) = 0;
  48. /// Intersection test for drawables.
  49. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside) = 0;
  50. /// Result vector reference.
  51. PODVector<Drawable*>& result_;
  52. /// Drawable flags to include.
  53. unsigned char drawableFlags_;
  54. /// Drawable layers to include.
  55. unsigned viewMask_;
  56. };
  57. /// Point octree query.
  58. class PointOctreeQuery : public OctreeQuery
  59. {
  60. public:
  61. /// Construct with point and query parameters.
  62. PointOctreeQuery(PODVector<Drawable*>& result, const Vector3& point, unsigned char drawableFlags = DRAWABLE_ANY,
  63. unsigned viewMask = DEFAULT_VIEWMASK) :
  64. OctreeQuery(result, drawableFlags, viewMask),
  65. point_(point)
  66. {
  67. }
  68. /// Intersection test for an octant.
  69. virtual Intersection TestOctant(const BoundingBox& box, bool inside);
  70. /// Intersection test for drawables.
  71. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside);
  72. /// Point.
  73. Vector3 point_;
  74. };
  75. /// %Sphere octree query.
  76. class SphereOctreeQuery : public OctreeQuery
  77. {
  78. public:
  79. /// Construct with sphere and query parameters.
  80. SphereOctreeQuery(PODVector<Drawable*>& result, const Sphere& sphere, unsigned char drawableFlags = DRAWABLE_ANY,
  81. unsigned viewMask = DEFAULT_VIEWMASK) :
  82. OctreeQuery(result, drawableFlags, viewMask),
  83. sphere_(sphere)
  84. {
  85. }
  86. /// Intersection test for an octant.
  87. virtual Intersection TestOctant(const BoundingBox& box, bool inside);
  88. /// Intersection test for drawables.
  89. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside);
  90. /// Sphere.
  91. Sphere sphere_;
  92. };
  93. /// Bounding box octree query.
  94. class BoxOctreeQuery : public OctreeQuery
  95. {
  96. public:
  97. /// Construct with bounding box and query parameters.
  98. BoxOctreeQuery(PODVector<Drawable*>& result, const BoundingBox& box, unsigned char drawableFlags = DRAWABLE_ANY,
  99. unsigned viewMask = DEFAULT_VIEWMASK) :
  100. OctreeQuery(result, drawableFlags, viewMask),
  101. box_(box)
  102. {
  103. }
  104. /// Intersection test for an octant.
  105. virtual Intersection TestOctant(const BoundingBox& box, bool inside);
  106. /// Intersection test for drawables.
  107. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside);
  108. /// Bounding box.
  109. BoundingBox box_;
  110. };
  111. /// %Frustum octree query.
  112. class FrustumOctreeQuery : public OctreeQuery
  113. {
  114. public:
  115. /// Construct with frustum and query parameters.
  116. FrustumOctreeQuery(PODVector<Drawable*>& result, const Frustum& frustum, unsigned char drawableFlags = DRAWABLE_ANY,
  117. unsigned viewMask = DEFAULT_VIEWMASK) :
  118. OctreeQuery(result, drawableFlags, viewMask),
  119. frustum_(frustum)
  120. {
  121. }
  122. /// Intersection test for an octant.
  123. virtual Intersection TestOctant(const BoundingBox& box, bool inside);
  124. /// Intersection test for drawables.
  125. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside);
  126. /// Frustum.
  127. Frustum frustum_;
  128. };
  129. /// Graphics raycast detail level.
  130. enum RayQueryLevel
  131. {
  132. RAY_AABB_NOSUBOBJECTS = 0,
  133. RAY_AABB,
  134. RAY_OBB,
  135. RAY_TRIANGLE
  136. };
  137. /// Raycast result.
  138. struct RayQueryResult
  139. {
  140. /// Drawable.
  141. Drawable* drawable_;
  142. /// Scene node.
  143. Node* node_;
  144. /// Distance from ray origin.
  145. float distance_;
  146. /// Drawable specific subobject if applicable.
  147. unsigned subObject_;
  148. };
  149. /// Raycast octree query.
  150. class RayOctreeQuery
  151. {
  152. public:
  153. /// Construct with ray and query parameters.
  154. RayOctreeQuery(PODVector<RayQueryResult>& result, const Ray& ray, RayQueryLevel level = RAY_TRIANGLE,
  155. float maxDistance = M_INFINITY, unsigned char drawableFlags = DRAWABLE_ANY, unsigned viewMask = DEFAULT_VIEWMASK) :
  156. result_(result),
  157. ray_(ray),
  158. level_(level),
  159. maxDistance_(maxDistance),
  160. drawableFlags_(drawableFlags),
  161. viewMask_(viewMask)
  162. {
  163. }
  164. /// Result vector reference.
  165. PODVector<RayQueryResult>& result_;
  166. /// Ray.
  167. Ray ray_;
  168. /// Drawable flags to include.
  169. unsigned char drawableFlags_;
  170. /// Drawable layers to include.
  171. unsigned viewMask_;
  172. /// Maximum ray distance.
  173. float maxDistance_;
  174. /// Raycast detail level.
  175. RayQueryLevel level_;
  176. };