OctreeQuery.h 6.7 KB

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