OctreeQuery.h 8.0 KB

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