sceneCullingVolume.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SCENECULLINGVOLUME_H_
  23. #define _SCENECULLINGVOLUME_H_
  24. #ifndef _MPLANESET_H_
  25. #include "math/mPlaneSet.h"
  26. #endif
  27. /// A volume used to include or exclude space in a scene.
  28. ///
  29. /// Culling volumes are represented as sets of clipping planes.
  30. ///
  31. /// @note Culling is performed in world space so the plane data for culling volumes
  32. /// must be in world space too.
  33. class SceneCullingVolume
  34. {
  35. public:
  36. /// Type of culling.
  37. enum Type
  38. {
  39. Includer,
  40. Occluder,
  41. };
  42. protected:
  43. /// What type of culling volume this is.
  44. Type mType;
  45. ///
  46. F32 mSortPoint;
  47. /// The set of clipping planes that defines the clipping volume for this culler.
  48. PlaneSetF mClippingPlanes;
  49. /// Test the given bounds against this culling volume.
  50. ///
  51. /// Note that we allow false positives here for includers. This will only cause an
  52. /// occasional object to be classified as intersecting when in fact it is outside.
  53. /// This is still better though than requiring the expensive intersection tests for
  54. /// all intersecting objects.
  55. ///
  56. /// @return True if the culling volume accepts the given bounds.
  57. template< typename B > bool _testBounds( const B& bounds ) const
  58. {
  59. if( isOccluder() )
  60. return getPlanes().isContained( bounds );
  61. else
  62. return ( getPlanes().testPotentialIntersection( bounds ) != GeometryOutside );
  63. }
  64. public:
  65. /// Create an *uninitialized* culling volume.
  66. SceneCullingVolume() : mType(Includer), mSortPoint(1.f) {}
  67. ///
  68. SceneCullingVolume( Type type, const PlaneSetF& planes )
  69. : mType( type ), mSortPoint( 1.f ), mClippingPlanes( planes ) {}
  70. /// Return the type of volume defined by this culling volume, i.e. whether it includes
  71. /// or excludes space.
  72. Type getType() const { return mType; }
  73. /// Return true if this is an inclusion volume.
  74. bool isIncluder() const { return ( getType() == Includer ); }
  75. /// Return true if this is an occlusion volume.
  76. bool isOccluder() const { return ( getType() == Occluder ); }
  77. /// Return the set of clipping planes that defines the culling volume.
  78. const PlaneSetF& getPlanes() const { return mClippingPlanes; }
  79. /// @name Sorting
  80. ///
  81. /// Before testing, culling volumes will be sorted by decreasing probability of causing
  82. /// test positives. Thus, the sort point of a volume should be a rough metric of the amount
  83. /// of scene/screen space it covers.
  84. ///
  85. /// Note that sort points for occluders are independent of sort points for includers.
  86. /// @{
  87. /// Return the sort point value of the volume. The larger the value, the more likely the
  88. /// volume is to cause positive test results with bounding volumes.
  89. F32 getSortPoint() const { return mSortPoint; }
  90. ///
  91. void setSortPoint( F32 value ) { mSortPoint = value; }
  92. /// @}
  93. /// @name Testing
  94. /// @{
  95. /// Return true if the volume accepts the given AABB.
  96. bool test( const Box3F& aabb ) const { return _testBounds( aabb ); }
  97. /// Return true if the volume accepts the given OBB.
  98. bool test( const OrientedBox3F& obb ) const { return _testBounds( obb ); }
  99. /// Return true if the volume accepts the given sphere.
  100. bool test( const SphereF& sphere ) const { return _testBounds( sphere ); }
  101. /// @}
  102. };
  103. #endif // !_SCENECULLINGVOLUME_H_