OctreeQuery.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "Frustum.h"
  26. #include "Ray.h"
  27. #include "Sphere.h"
  28. class OcclusionBuffer;
  29. class Drawable;
  30. class Node;
  31. /// Base class for octree queries
  32. class OctreeQuery
  33. {
  34. public:
  35. /// Construct with result vector, include/exclude flags and whether to get only occluders or shadowcasters
  36. OctreeQuery(PODVector<Drawable*>& result, unsigned char drawableFlags, bool occludersOnly, bool shadowCastersOnly) :
  37. result_(result),
  38. drawableFlags_(drawableFlags),
  39. occludersOnly_(occludersOnly),
  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, unsigned& mask) const = 0;
  49. /// Intersection test for a drawable
  50. virtual Intersection TestDrawable(const BoundingBox& box, unsigned& mask) const = 0;
  51. /// Result vector reference
  52. PODVector<Drawable*>& result_;
  53. /// Drawable flags to include
  54. unsigned char drawableFlags_;
  55. /// Get occluders only flag
  56. bool occludersOnly_;
  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, bool occludersOnly = false,
  66. bool shadowCastersOnly = false) :
  67. OctreeQuery(result, drawableFlags, occludersOnly, shadowCastersOnly),
  68. point_(point)
  69. {
  70. }
  71. /// Intersection test for an octant
  72. virtual Intersection TestOctant(const BoundingBox& box, unsigned& mask) const;
  73. /// Intersection test for a drawable
  74. virtual Intersection TestDrawable(const BoundingBox& box, unsigned& mask) 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, bool occludersOnly = false,
  84. bool shadowCastersOnly = false) :
  85. OctreeQuery(result, drawableFlags, occludersOnly, shadowCastersOnly),
  86. sphere_(sphere)
  87. {
  88. }
  89. /// Intersection test for an octant
  90. virtual Intersection TestOctant(const BoundingBox& box, unsigned& mask) const;
  91. /// Intersection test for a drawable
  92. virtual Intersection TestDrawable(const BoundingBox& box, unsigned& mask) 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, bool occludersOnly = false,
  102. bool shadowCastersOnly = false) :
  103. OctreeQuery(result, drawableFlags, occludersOnly, shadowCastersOnly),
  104. box_(box)
  105. {
  106. }
  107. /// Intersection test for an octant
  108. virtual Intersection TestOctant(const BoundingBox& box, unsigned& mask) const;
  109. /// Intersection test for a drawable
  110. virtual Intersection TestDrawable(const BoundingBox& box, unsigned& mask) 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, bool occludersOnly = false,
  120. bool shadowCastersOnly = false) :
  121. OctreeQuery(result, drawableFlags, occludersOnly, shadowCastersOnly),
  122. frustum_(frustum)
  123. {
  124. }
  125. /// Intersection test for an octant
  126. virtual Intersection TestDrawable(const BoundingBox& box, unsigned& mask) const;
  127. /// Intersection test for a drawable
  128. virtual Intersection TestOctant(const BoundingBox& box, unsigned& mask) const;
  129. /// Frustum
  130. Frustum frustum_;
  131. };
  132. /// Frustum octree query with occlusion
  133. class OccludedFrustumOctreeQuery : public OctreeQuery
  134. {
  135. public:
  136. /// Construct with frustum, occlusion buffer pointer and query parameters
  137. OccludedFrustumOctreeQuery(PODVector<Drawable*>& result, const Frustum& frustum, OcclusionBuffer* buffer,
  138. unsigned char drawableFlags, bool occludersOnly = false, bool shadowCastersOnly = false) :
  139. OctreeQuery(result, drawableFlags, occludersOnly, shadowCastersOnly),
  140. frustum_(frustum),
  141. buffer_(buffer)
  142. {
  143. }
  144. /// Intersection test for an octant
  145. virtual Intersection TestOctant(const BoundingBox& box, unsigned& mask) const;
  146. /// Intersection test for a drawable
  147. virtual Intersection TestDrawable(const BoundingBox& box, unsigned& mask) const;
  148. /// Frustum
  149. Frustum frustum_;
  150. /// Occlusion buffer
  151. OcclusionBuffer* buffer_;
  152. };
  153. /// Graphics raycast detail level
  154. enum RayQueryLevel
  155. {
  156. RAY_AABB_NOSUBOBJECTS = 0,
  157. RAY_AABB,
  158. RAY_OBB,
  159. RAY_TRIANGLE
  160. };
  161. /// Raycast result
  162. struct RayQueryResult
  163. {
  164. /// Construct
  165. RayQueryResult() :
  166. subObject_(M_MAX_UNSIGNED)
  167. {
  168. }
  169. /// Drawable
  170. Drawable* drawable_;
  171. /// Scene node
  172. Node* node_;
  173. /// Distance from ray origin
  174. float distance_;
  175. /// Drawable specific subobject if applicable
  176. unsigned subObject_;
  177. };
  178. /// Ray octree query
  179. class RayOctreeQuery
  180. {
  181. public:
  182. /// Construct with ray and query parameters
  183. RayOctreeQuery(PODVector<RayQueryResult>& result, const Ray& ray, unsigned char drawableFlags, bool occludersOnly = false,
  184. bool shadowCastersOnly = false, float maxDistance = M_INFINITY, RayQueryLevel level = RAY_TRIANGLE) :
  185. ray_(ray),
  186. result_(result),
  187. drawableFlags_(drawableFlags),
  188. occludersOnly_(occludersOnly),
  189. shadowCastersOnly_(shadowCastersOnly),
  190. maxDistance_(maxDistance),
  191. level_(level)
  192. {
  193. }
  194. /// Ray
  195. Ray ray_;
  196. /// Result vector reference
  197. PODVector<RayQueryResult>& result_;
  198. /// Drawable flags to include
  199. unsigned char drawableFlags_;
  200. /// Get occluders only flag
  201. bool occludersOnly_;
  202. /// Get shadowcasters only flag
  203. bool shadowCastersOnly_;
  204. /// Maximum ray distance
  205. float maxDistance_;
  206. /// Raycast detail level
  207. RayQueryLevel level_;
  208. };