CullingSet 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_CullingSet
  14. #define OSG_CullingSet 1
  15. #include <osg/Polytope>
  16. #include <osg/ShadowVolumeOccluder>
  17. #include <osg/Viewport>
  18. #include <math.h>
  19. namespace osg {
  20. #define COMPILE_WITH_SHADOW_OCCLUSION_CULLING
  21. /** A CullingSet class which contains a frustum and a list of occluders. */
  22. class OSG_EXPORT CullingSet : public Referenced
  23. {
  24. public:
  25. typedef std::pair< osg::ref_ptr<osg::StateSet>, osg::Polytope > StateFrustumPair;
  26. typedef std::vector< StateFrustumPair > StateFrustumList;
  27. CullingSet();
  28. CullingSet(const CullingSet& cs):
  29. Referenced(),
  30. _mask(cs._mask),
  31. _frustum(cs._frustum),
  32. _stateFrustumList(cs._stateFrustumList),
  33. _occluderList(cs._occluderList),
  34. _pixelSizeVector(cs._pixelSizeVector),
  35. _smallFeatureCullingPixelSize(cs._smallFeatureCullingPixelSize)
  36. {
  37. }
  38. CullingSet(const CullingSet& cs,const Matrix& matrix, const Vec4& pixelSizeVector):
  39. _mask(cs._mask),
  40. _frustum(cs._frustum),
  41. _stateFrustumList(cs._stateFrustumList),
  42. _occluderList(cs._occluderList),
  43. _pixelSizeVector(pixelSizeVector),
  44. _smallFeatureCullingPixelSize(cs._smallFeatureCullingPixelSize)
  45. {
  46. _frustum.transformProvidingInverse(matrix);
  47. for(OccluderList::iterator itr=_occluderList.begin();
  48. itr!=_occluderList.end();
  49. ++itr)
  50. {
  51. itr->transformProvidingInverse(matrix);
  52. }
  53. }
  54. CullingSet& operator = (const CullingSet& cs)
  55. {
  56. if (this==&cs) return *this;
  57. _mask = cs._mask;
  58. _frustum = cs._frustum;
  59. _stateFrustumList = cs._stateFrustumList;
  60. _occluderList = cs._occluderList;
  61. _pixelSizeVector = cs._pixelSizeVector;
  62. _smallFeatureCullingPixelSize = cs._smallFeatureCullingPixelSize;
  63. return *this;
  64. }
  65. inline void set(const CullingSet& cs)
  66. {
  67. _mask = cs._mask;
  68. _frustum = cs._frustum;
  69. _stateFrustumList = cs._stateFrustumList;
  70. _occluderList = cs._occluderList;
  71. _pixelSizeVector = cs._pixelSizeVector;
  72. _smallFeatureCullingPixelSize = cs._smallFeatureCullingPixelSize;
  73. }
  74. inline void set(const CullingSet& cs,const Matrix& matrix, const Vec4& pixelSizeVector)
  75. {
  76. _mask = cs._mask;
  77. _stateFrustumList = cs._stateFrustumList;
  78. _occluderList = cs._occluderList;
  79. _pixelSizeVector = pixelSizeVector;
  80. _smallFeatureCullingPixelSize = cs._smallFeatureCullingPixelSize;
  81. //_frustum = cs._frustum;
  82. //_frustum.transformProvidingInverse(matrix);
  83. _frustum.setAndTransformProvidingInverse(cs._frustum,matrix);
  84. for(StateFrustumList::iterator sitr=_stateFrustumList.begin();
  85. sitr!=_stateFrustumList.end();
  86. ++sitr)
  87. {
  88. sitr->second.transformProvidingInverse(matrix);
  89. }
  90. for(OccluderList::iterator oitr=_occluderList.begin();
  91. oitr!=_occluderList.end();
  92. ++oitr)
  93. {
  94. oitr->transformProvidingInverse(matrix);
  95. }
  96. }
  97. typedef std::vector<ShadowVolumeOccluder> OccluderList;
  98. typedef int Mask;
  99. enum MaskValues
  100. {
  101. NO_CULLING = 0x0,
  102. VIEW_FRUSTUM_SIDES_CULLING = 0x1,
  103. NEAR_PLANE_CULLING = 0x2,
  104. FAR_PLANE_CULLING = 0x4,
  105. VIEW_FRUSTUM_CULLING = VIEW_FRUSTUM_SIDES_CULLING|
  106. NEAR_PLANE_CULLING|
  107. FAR_PLANE_CULLING,
  108. SMALL_FEATURE_CULLING = 0x8,
  109. SHADOW_OCCLUSION_CULLING = 0x10,
  110. DEFAULT_CULLING = VIEW_FRUSTUM_SIDES_CULLING|
  111. SMALL_FEATURE_CULLING|
  112. SHADOW_OCCLUSION_CULLING,
  113. ENABLE_ALL_CULLING = VIEW_FRUSTUM_CULLING|
  114. SMALL_FEATURE_CULLING|
  115. SHADOW_OCCLUSION_CULLING
  116. };
  117. void setCullingMask(Mask mask) { _mask = mask; }
  118. Mask getCullingMask() const { return _mask; }
  119. void setFrustum(Polytope& cv) { _frustum = cv; }
  120. Polytope& getFrustum() { return _frustum; }
  121. const Polytope& getFrustum() const { return _frustum; }
  122. void addStateFrustum(StateSet* stateset, Polytope& polytope) { _stateFrustumList.push_back(StateFrustumPair(stateset,polytope)); }
  123. void getStateFrustumList(StateFrustumList& sfl) { _stateFrustumList = sfl; }
  124. StateFrustumList& getStateFrustumList() { return _stateFrustumList; }
  125. void addOccluder(ShadowVolumeOccluder& cv) { _occluderList.push_back(cv); }
  126. void setPixelSizeVector(const Vec4& v) { _pixelSizeVector = v; }
  127. Vec4& getPixelSizeVector() { return _pixelSizeVector; }
  128. const Vec4& getPixelSizeVector() const { return _pixelSizeVector; }
  129. /** Threshold at which small features are culled.
  130. \param value Bounding volume size in screen space. Default is 2.0. */
  131. void setSmallFeatureCullingPixelSize(float value) { _smallFeatureCullingPixelSize=value; }
  132. float& getSmallFeatureCullingPixelSize() { return _smallFeatureCullingPixelSize; }
  133. float getSmallFeatureCullingPixelSize() const { return _smallFeatureCullingPixelSize; }
  134. /** Compute the pixel of an object at position v, with specified radius.*/
  135. float pixelSize(const Vec3& v,float radius) const { return radius/(v*_pixelSizeVector); }
  136. /** Compute the pixel of a bounding sphere.*/
  137. float pixelSize(const BoundingSphere& bs) const { return bs.radius()/(bs.center()*_pixelSizeVector); }
  138. /** Compute the pixel of an object at position v, with specified radius. fabs()ed to always be positive. */
  139. float clampedPixelSize(const Vec3& v,float radius) const { return fabs(pixelSize(v, radius)); }
  140. /** Compute the pixel of a bounding sphere. fabs()ed to always be positive. */
  141. float clampedPixelSize(const BoundingSphere& bs) const { return fabs(pixelSize(bs)); }
  142. inline bool isCulled(const std::vector<Vec3>& vertices)
  143. {
  144. if (_mask&VIEW_FRUSTUM_CULLING)
  145. {
  146. // is it outside the view frustum...
  147. if (!_frustum.contains(vertices)) return true;
  148. }
  149. if (_mask&SMALL_FEATURE_CULLING)
  150. {
  151. }
  152. if (_mask&SHADOW_OCCLUSION_CULLING)
  153. {
  154. // is it in one of the shadow occluder volumes.
  155. if (!_occluderList.empty())
  156. {
  157. for(OccluderList::iterator itr=_occluderList.begin();
  158. itr!=_occluderList.end();
  159. ++itr)
  160. {
  161. if (itr->contains(vertices)) return true;
  162. }
  163. }
  164. }
  165. return false;
  166. }
  167. inline bool isCulled(const BoundingBox& bb)
  168. {
  169. if (_mask&VIEW_FRUSTUM_CULLING)
  170. {
  171. // is it outside the view frustum...
  172. if (!_frustum.contains(bb)) return true;
  173. }
  174. if (_mask&SMALL_FEATURE_CULLING)
  175. {
  176. }
  177. if (_mask&SHADOW_OCCLUSION_CULLING)
  178. {
  179. // is it in one of the shadow occluder volumes.
  180. if (!_occluderList.empty())
  181. {
  182. for(OccluderList::iterator itr=_occluderList.begin();
  183. itr!=_occluderList.end();
  184. ++itr)
  185. {
  186. if (itr->contains(bb)) return true;
  187. }
  188. }
  189. }
  190. return false;
  191. }
  192. inline bool isCulled(const BoundingSphere& bs)
  193. {
  194. if (_mask&VIEW_FRUSTUM_CULLING)
  195. {
  196. // is it outside the view frustum...
  197. if (!_frustum.contains(bs)) return true;
  198. }
  199. if (_mask&SMALL_FEATURE_CULLING)
  200. {
  201. if (((bs.center()*_pixelSizeVector)*_smallFeatureCullingPixelSize)>bs.radius()) return true;
  202. }
  203. #ifdef COMPILE_WITH_SHADOW_OCCLUSION_CULLING
  204. if (_mask&SHADOW_OCCLUSION_CULLING)
  205. {
  206. // is it in one of the shadow occluder volumes.
  207. if (!_occluderList.empty())
  208. {
  209. for(OccluderList::iterator itr=_occluderList.begin();
  210. itr!=_occluderList.end();
  211. ++itr)
  212. {
  213. if (itr->contains(bs)) return true;
  214. }
  215. }
  216. }
  217. #endif
  218. return false;
  219. }
  220. inline void pushCurrentMask()
  221. {
  222. _frustum.pushCurrentMask();
  223. if (!_stateFrustumList.empty())
  224. {
  225. for(StateFrustumList::iterator itr=_stateFrustumList.begin();
  226. itr!=_stateFrustumList.end();
  227. ++itr)
  228. {
  229. itr->second.pushCurrentMask();
  230. }
  231. }
  232. #ifdef COMPILE_WITH_SHADOW_OCCLUSION_CULLING
  233. if (!_occluderList.empty())
  234. {
  235. for(OccluderList::iterator itr=_occluderList.begin();
  236. itr!=_occluderList.end();
  237. ++itr)
  238. {
  239. itr->pushCurrentMask();
  240. }
  241. }
  242. #endif
  243. }
  244. inline void popCurrentMask()
  245. {
  246. _frustum.popCurrentMask();
  247. if (!_stateFrustumList.empty())
  248. {
  249. for(StateFrustumList::iterator itr=_stateFrustumList.begin();
  250. itr!=_stateFrustumList.end();
  251. ++itr)
  252. {
  253. itr->second.popCurrentMask();
  254. }
  255. }
  256. #ifdef COMPILE_WITH_SHADOW_OCCLUSION_CULLING
  257. if (!_occluderList.empty())
  258. {
  259. for(OccluderList::iterator itr=_occluderList.begin();
  260. itr!=_occluderList.end();
  261. ++itr)
  262. {
  263. itr->popCurrentMask();
  264. }
  265. }
  266. #endif
  267. }
  268. inline void resetCullingMask()
  269. {
  270. _frustum.setResultMask(_frustum.getCurrentMask());
  271. }
  272. void disableAndPushOccludersCurrentMask(NodePath& nodePath);
  273. void popOccludersCurrentMask(NodePath& nodePath);
  274. static osg::Vec4 computePixelSizeVector(const Viewport& W, const Matrix& P, const Matrix& M);
  275. virtual ~CullingSet();
  276. protected:
  277. Mask _mask;
  278. Polytope _frustum;
  279. StateFrustumList _stateFrustumList;
  280. OccluderList _occluderList;
  281. Vec4 _pixelSizeVector;
  282. float _smallFeatureCullingPixelSize;
  283. };
  284. } // end of namespace
  285. #endif