2
0

OctreeQuery.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #ifndef RENDERER_OCTREEQUERY_H
  24. #define RENDERER_OCTREEQUERY_H
  25. #include "BoundingBox.h"
  26. #include "Frustum.h"
  27. #include "Ray.h"
  28. #include "Sphere.h"
  29. #include <vector>
  30. class OcclusionBuffer;
  31. class VolumeNode;
  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(std::vector<VolumeNode*>& result, unsigned nodeFlags, bool occludersOnly, bool shadowCastersOnly) :
  38. mResult(result),
  39. mNodeFlags(nodeFlags),
  40. mOccludersOnly(occludersOnly),
  41. mShadowCastersOnly(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 scene node
  51. virtual Intersection testNode(const BoundingBox& box, unsigned& mask) const = 0;
  52. //! Result vector reference
  53. std::vector<VolumeNode*>& mResult;
  54. //! Scene node flags to include
  55. unsigned mNodeFlags;
  56. //! Get occluders only flag
  57. bool mOccludersOnly;
  58. //! Get shadowcasters only flag
  59. bool mShadowCastersOnly;
  60. };
  61. //! Point octree query
  62. class PointOctreeQuery : public OctreeQuery
  63. {
  64. public:
  65. //! Construct with point and query parameters
  66. PointOctreeQuery(std::vector<VolumeNode*>& result, const Vector3& point, unsigned nodeFlags, bool occludersOnly = false,
  67. bool shadowCastersOnly = false) :
  68. OctreeQuery(result, nodeFlags, occludersOnly, shadowCastersOnly),
  69. mPoint(point)
  70. {
  71. }
  72. //! Intersection test for an octant
  73. virtual Intersection testOctant(const BoundingBox& box, unsigned& mask) const;
  74. //! Intersection test for a scene node
  75. virtual Intersection testNode(const BoundingBox& box, unsigned& mask) const;
  76. //! Point
  77. Vector3 mPoint;
  78. };
  79. //! Sphere octree query
  80. class SphereOctreeQuery : public OctreeQuery
  81. {
  82. public:
  83. //! Construct with sphere and query parameters
  84. SphereOctreeQuery(std::vector<VolumeNode*>& result, const Sphere& sphere, unsigned nodeFlags, bool occludersOnly = false,
  85. bool shadowCastersOnly = false) :
  86. OctreeQuery(result, nodeFlags, occludersOnly, shadowCastersOnly),
  87. mSphere(sphere)
  88. {
  89. }
  90. //! Intersection test for an octant
  91. virtual Intersection testOctant(const BoundingBox& box, unsigned& mask) const;
  92. //! Intersection test for a scene node
  93. virtual Intersection testNode(const BoundingBox& box, unsigned& mask) const;
  94. //! Sphere
  95. Sphere mSphere;
  96. };
  97. //! Bounding box octree query
  98. class BoxOctreeQuery : public OctreeQuery
  99. {
  100. public:
  101. //! Construct with bounding box and query parameters
  102. BoxOctreeQuery(std::vector<VolumeNode*>& result, const BoundingBox& box, unsigned nodeFlags, bool occludersOnly = false,
  103. bool shadowCastersOnly = false) :
  104. OctreeQuery(result, nodeFlags, occludersOnly, shadowCastersOnly),
  105. mBox(box)
  106. {
  107. }
  108. //! Intersection test for an octant
  109. virtual Intersection testOctant(const BoundingBox& box, unsigned& mask) const;
  110. //! Intersection test for a scene node
  111. virtual Intersection testNode(const BoundingBox& box, unsigned& mask) const;
  112. //! Bounding box
  113. BoundingBox mBox;
  114. };
  115. //! Frustum octree query
  116. class FrustumOctreeQuery : public OctreeQuery
  117. {
  118. public:
  119. //! Construct with frustum and query parameters
  120. FrustumOctreeQuery(std::vector<VolumeNode*>& result, const Frustum& frustum, unsigned nodeFlags, bool occludersOnly = false,
  121. bool shadowCastersOnly = false) :
  122. OctreeQuery(result, nodeFlags, occludersOnly, shadowCastersOnly),
  123. mFrustum(frustum)
  124. {
  125. }
  126. //! Intersection test for an octant
  127. virtual Intersection testNode(const BoundingBox& box, unsigned& mask) const;
  128. //! Intersection test for a scene node
  129. virtual Intersection testOctant(const BoundingBox& box, unsigned& mask) const;
  130. //! Frustum
  131. Frustum mFrustum;
  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(std::vector<VolumeNode*>& result, const Frustum& frustum, OcclusionBuffer* buffer,
  139. unsigned nodeFlags, bool occludersOnly = false, bool shadowCastersOnly = false) :
  140. OctreeQuery(result, nodeFlags, occludersOnly, shadowCastersOnly),
  141. mFrustum(frustum),
  142. mBuffer(buffer)
  143. {
  144. }
  145. //! Intersection test for an octant
  146. virtual Intersection testOctant(const BoundingBox& box, unsigned& mask) const;
  147. //! Intersection test for a scene node
  148. virtual Intersection testNode(const BoundingBox& box, unsigned& mask) const;
  149. //! Frustum
  150. Frustum mFrustum;
  151. //! Occlusion buffer
  152. OcclusionBuffer* mBuffer;
  153. };
  154. //! Renderer 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. mSubObject(M_MAX_UNSIGNED)
  168. {
  169. }
  170. //! Scene node
  171. VolumeNode* mNode;
  172. //! Distance from ray origin
  173. float mDistance;
  174. //! Scene node specific subobject (instance, bone etc.)
  175. unsigned mSubObject;
  176. };
  177. //! Ray octree query
  178. class RayOctreeQuery
  179. {
  180. public:
  181. //! Construct with ray and query parameters
  182. RayOctreeQuery(std::vector<RayQueryResult>& result, const Ray& ray, unsigned nodeFlags, bool occludersOnly = false,
  183. bool shadowCastersOnly = false, float maxDistance = M_INFINITY, RayQueryLevel level = RAY_TRIANGLE) :
  184. mRay(ray),
  185. mResult(result),
  186. mNodeFlags(nodeFlags),
  187. mOccludersOnly(occludersOnly),
  188. mShadowCastersOnly(shadowCastersOnly),
  189. mMaxDistance(maxDistance),
  190. mLevel(level)
  191. {
  192. }
  193. //! Ray
  194. Ray mRay;
  195. //! Result vector reference
  196. std::vector<RayQueryResult>& mResult;
  197. //! Scene node flags to include
  198. unsigned mNodeFlags;
  199. //! Get occluders only flag
  200. bool mOccludersOnly;
  201. //! Get shadowcasters only flag
  202. bool mShadowCastersOnly;
  203. //! Maximum ray distance
  204. float mMaxDistance;
  205. //! Raycast detail level
  206. RayQueryLevel mLevel;
  207. };
  208. #endif // RENDERER_OCTREEQUERY_H