OctreeQuery.h 7.2 KB

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