OctreeQuery.h 7.6 KB

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