OctreeQuery.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  30. {
  31. class Drawable;
  32. class Node;
  33. /// Base class for octree queries.
  34. class OctreeQuery
  35. {
  36. public:
  37. /// Construct with query parameters.
  38. OctreeQuery(PODVector<Drawable*>& result, unsigned char drawableFlags, unsigned viewMask) :
  39. result_(result),
  40. drawableFlags_(drawableFlags),
  41. viewMask_(viewMask)
  42. {
  43. }
  44. /// Destruct.
  45. virtual ~OctreeQuery()
  46. {
  47. }
  48. /// Intersection test for an octant.
  49. virtual Intersection TestOctant(const BoundingBox& box, bool inside) = 0;
  50. /// Intersection test for drawables.
  51. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside) = 0;
  52. /// Result vector reference.
  53. PODVector<Drawable*>& result_;
  54. /// Drawable flags to include.
  55. unsigned char drawableFlags_;
  56. /// Drawable layers to include.
  57. unsigned viewMask_;
  58. };
  59. /// Point octree query.
  60. class PointOctreeQuery : public OctreeQuery
  61. {
  62. public:
  63. /// Construct with point and query parameters.
  64. PointOctreeQuery(PODVector<Drawable*>& result, const Vector3& point, unsigned char drawableFlags = DRAWABLE_ANY,
  65. unsigned viewMask = DEFAULT_VIEWMASK) :
  66. OctreeQuery(result, drawableFlags, viewMask),
  67. point_(point)
  68. {
  69. }
  70. /// Intersection test for an octant.
  71. virtual Intersection TestOctant(const BoundingBox& box, bool inside);
  72. /// Intersection test for drawables.
  73. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside);
  74. /// Point.
  75. Vector3 point_;
  76. };
  77. /// %Sphere octree query.
  78. class SphereOctreeQuery : public OctreeQuery
  79. {
  80. public:
  81. /// Construct with sphere and query parameters.
  82. SphereOctreeQuery(PODVector<Drawable*>& result, const Sphere& sphere, unsigned char drawableFlags = DRAWABLE_ANY,
  83. unsigned viewMask = DEFAULT_VIEWMASK) :
  84. OctreeQuery(result, drawableFlags, viewMask),
  85. sphere_(sphere)
  86. {
  87. }
  88. /// Intersection test for an octant.
  89. virtual Intersection TestOctant(const BoundingBox& box, bool inside);
  90. /// Intersection test for drawables.
  91. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside);
  92. /// Sphere.
  93. Sphere sphere_;
  94. };
  95. /// Bounding box octree query.
  96. class BoxOctreeQuery : public OctreeQuery
  97. {
  98. public:
  99. /// Construct with bounding box and query parameters.
  100. BoxOctreeQuery(PODVector<Drawable*>& result, const BoundingBox& box, unsigned char drawableFlags = DRAWABLE_ANY,
  101. unsigned viewMask = DEFAULT_VIEWMASK) :
  102. OctreeQuery(result, drawableFlags, viewMask),
  103. box_(box)
  104. {
  105. }
  106. /// Intersection test for an octant.
  107. virtual Intersection TestOctant(const BoundingBox& box, bool inside);
  108. /// Intersection test for drawables.
  109. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside);
  110. /// Bounding box.
  111. BoundingBox box_;
  112. };
  113. /// %Frustum octree query.
  114. class FrustumOctreeQuery : public OctreeQuery
  115. {
  116. public:
  117. /// Construct with frustum and query parameters.
  118. FrustumOctreeQuery(PODVector<Drawable*>& result, const Frustum& frustum, unsigned char drawableFlags = DRAWABLE_ANY,
  119. unsigned viewMask = DEFAULT_VIEWMASK) :
  120. OctreeQuery(result, drawableFlags, viewMask),
  121. frustum_(frustum)
  122. {
  123. }
  124. /// Intersection test for an octant.
  125. virtual Intersection TestOctant(const BoundingBox& box, bool inside);
  126. /// Intersection test for drawables.
  127. virtual void TestDrawables(Drawable** start, Drawable** end, bool inside);
  128. /// Frustum.
  129. Frustum frustum_;
  130. };
  131. /// Graphics raycast detail level.
  132. enum RayQueryLevel
  133. {
  134. RAY_AABB_NOSUBOBJECTS = 0,
  135. RAY_AABB,
  136. RAY_OBB,
  137. RAY_TRIANGLE
  138. };
  139. /// Raycast result.
  140. struct RayQueryResult
  141. {
  142. /// Drawable.
  143. Drawable* drawable_;
  144. /// Scene node.
  145. Node* node_;
  146. /// Distance from ray origin.
  147. float distance_;
  148. /// Drawable specific subobject if applicable.
  149. unsigned subObject_;
  150. };
  151. /// Raycast octree query.
  152. class RayOctreeQuery
  153. {
  154. public:
  155. /// Construct with ray and query parameters.
  156. RayOctreeQuery(PODVector<RayQueryResult>& result, const Ray& ray, RayQueryLevel level = RAY_TRIANGLE,
  157. float maxDistance = M_INFINITY, unsigned char drawableFlags = DRAWABLE_ANY, unsigned viewMask = DEFAULT_VIEWMASK) :
  158. result_(result),
  159. ray_(ray),
  160. drawableFlags_(drawableFlags),
  161. viewMask_(viewMask),
  162. maxDistance_(maxDistance),
  163. level_(level)
  164. {
  165. }
  166. /// Result vector reference.
  167. PODVector<RayQueryResult>& result_;
  168. /// Ray.
  169. Ray ray_;
  170. /// Drawable flags to include.
  171. unsigned char drawableFlags_;
  172. /// Drawable layers to include.
  173. unsigned viewMask_;
  174. /// Maximum ray distance.
  175. float maxDistance_;
  176. /// Raycast detail level.
  177. RayQueryLevel level_;
  178. };
  179. }