| 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 | |
|---|
| 14 | #ifndef OSG_CULLSTACK |
|---|
| 15 | #define OSG_CULLSTACK 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/CullingSet> |
|---|
| 18 | #include <osg/CullSettings> |
|---|
| 19 | #include <osg/Viewport> |
|---|
| 20 | #include <osg/fast_back_stack> |
|---|
| 21 | #include <osg/Transform> |
|---|
| 22 | |
|---|
| 23 | namespace osg { |
|---|
| 24 | |
|---|
| 25 | /** A CullStack class which accumulates the current project, modelview matrices |
|---|
| 26 | and the CullingSet. */ |
|---|
| 27 | class OSG_EXPORT CullStack : public osg::CullSettings |
|---|
| 28 | { |
|---|
| 29 | |
|---|
| 30 | public: |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | CullStack(); |
|---|
| 34 | CullStack(const CullStack& cs); |
|---|
| 35 | |
|---|
| 36 | ~CullStack(); |
|---|
| 37 | |
|---|
| 38 | typedef std::vector<ShadowVolumeOccluder> OccluderList; |
|---|
| 39 | |
|---|
| 40 | void reset(); |
|---|
| 41 | |
|---|
| 42 | void setOccluderList(const ShadowVolumeOccluderList& svol) { _occluderList = svol; } |
|---|
| 43 | ShadowVolumeOccluderList& getOccluderList() { return _occluderList; } |
|---|
| 44 | const ShadowVolumeOccluderList& getOccluderList() const { return _occluderList; } |
|---|
| 45 | |
|---|
| 46 | void pushViewport(osg::Viewport* viewport); |
|---|
| 47 | void popViewport(); |
|---|
| 48 | |
|---|
| 49 | void pushProjectionMatrix(osg::RefMatrix* matrix); |
|---|
| 50 | void popProjectionMatrix(); |
|---|
| 51 | |
|---|
| 52 | void pushModelViewMatrix(osg::RefMatrix* matrix, Transform::ReferenceFrame referenceFrame); |
|---|
| 53 | void popModelViewMatrix(); |
|---|
| 54 | |
|---|
| 55 | inline float getFrustumVolume() { if (_frustumVolume<0.0f) computeFrustumVolume(); return _frustumVolume; } |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | /** Compute the pixel size of an object at position v, with specified radius.*/ |
|---|
| 59 | float pixelSize(const Vec3& v,float radius) const |
|---|
| 60 | { |
|---|
| 61 | return getCurrentCullingSet().pixelSize(v,radius); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** Compute the pixel size of the bounding sphere.*/ |
|---|
| 65 | float pixelSize(const BoundingSphere& bs) const |
|---|
| 66 | { |
|---|
| 67 | return pixelSize(bs.center(),bs.radius()); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /** Compute the pixel size of an object at position v, with specified radius. fabs()ed to always be positive. */ |
|---|
| 71 | float clampedPixelSize(const Vec3& v,float radius) const |
|---|
| 72 | { |
|---|
| 73 | return getCurrentCullingSet().clampedPixelSize(v,radius); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /** Compute the pixel size of the bounding sphere. fabs()ed to always be positive. */ |
|---|
| 77 | float clampedPixelSize(const BoundingSphere& bs) const |
|---|
| 78 | { |
|---|
| 79 | return clampedPixelSize(bs.center(),bs.radius()); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | inline void disableAndPushOccludersCurrentMask(NodePath& nodePath) |
|---|
| 83 | { |
|---|
| 84 | getCurrentCullingSet().disableAndPushOccludersCurrentMask(nodePath); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | inline void popOccludersCurrentMask(NodePath& nodePath) |
|---|
| 88 | { |
|---|
| 89 | getCurrentCullingSet().popOccludersCurrentMask(nodePath); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | inline bool isCulled(const std::vector<Vec3>& vertices) |
|---|
| 93 | { |
|---|
| 94 | return getCurrentCullingSet().isCulled(vertices); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | inline bool isCulled(const BoundingBox& bb) |
|---|
| 98 | { |
|---|
| 99 | return bb.valid() && getCurrentCullingSet().isCulled(bb); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | inline bool isCulled(const BoundingSphere& bs) |
|---|
| 103 | { |
|---|
| 104 | return getCurrentCullingSet().isCulled(bs); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | inline bool isCulled(const osg::Node& node) |
|---|
| 108 | { |
|---|
| 109 | return node.isCullingActive() && getCurrentCullingSet().isCulled(node.getBound()); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | inline void pushCurrentMask() |
|---|
| 113 | { |
|---|
| 114 | getCurrentCullingSet().pushCurrentMask(); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | inline void popCurrentMask() |
|---|
| 118 | { |
|---|
| 119 | getCurrentCullingSet().popCurrentMask(); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | typedef std::vector< CullingSet > CullingStack; |
|---|
| 124 | |
|---|
| 125 | inline CullingStack& getClipSpaceCullingStack() { return _clipspaceCullingStack; } |
|---|
| 126 | |
|---|
| 127 | inline CullingStack& getProjectionCullingStack() { return _projectionCullingStack; } |
|---|
| 128 | |
|---|
| 129 | inline CullingStack& getModelViewCullingStack() { return _modelviewCullingStack; } |
|---|
| 130 | |
|---|
| 131 | inline CullingSet& getCurrentCullingSet() { return *_back_modelviewCullingStack; } |
|---|
| 132 | inline const CullingSet& getCurrentCullingSet() const { return *_back_modelviewCullingStack; } |
|---|
| 133 | |
|---|
| 134 | inline osg::Viewport* getViewport(); |
|---|
| 135 | inline osg::RefMatrix* getModelViewMatrix(); |
|---|
| 136 | inline osg::RefMatrix* getProjectionMatrix(); |
|---|
| 137 | inline osg::Matrix getWindowMatrix(); |
|---|
| 138 | inline const osg::RefMatrix* getMVPW(); |
|---|
| 139 | |
|---|
| 140 | inline const osg::Vec3& getReferenceViewPoint() const { return _referenceViewPoints.back(); } |
|---|
| 141 | inline void pushReferenceViewPoint(const osg::Vec3& viewPoint) { _referenceViewPoints.push_back(viewPoint); } |
|---|
| 142 | inline void popReferenceViewPoint() { _referenceViewPoints.pop_back(); } |
|---|
| 143 | |
|---|
| 144 | inline const osg::Vec3& getEyeLocal() const { return _eyePointStack.back(); } |
|---|
| 145 | |
|---|
| 146 | inline const osg::Vec3& getViewPointLocal() const { return _viewPointStack.back(); } |
|---|
| 147 | |
|---|
| 148 | inline const osg::Vec3 getUpLocal() const |
|---|
| 149 | { |
|---|
| 150 | const osg::Matrix& matrix = *_modelviewStack.back(); |
|---|
| 151 | return osg::Vec3(matrix(0,1),matrix(1,1),matrix(2,1)); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | inline const osg::Vec3 getLookVectorLocal() const |
|---|
| 155 | { |
|---|
| 156 | const osg::Matrix& matrix = *_modelviewStack.back(); |
|---|
| 157 | return osg::Vec3(-matrix(0,2),-matrix(1,2),-matrix(2,2)); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | protected: |
|---|
| 163 | |
|---|
| 164 | void pushCullingSet(); |
|---|
| 165 | void popCullingSet(); |
|---|
| 166 | |
|---|
| 167 | // base set of shadow volume occluder to use in culling. |
|---|
| 168 | ShadowVolumeOccluderList _occluderList; |
|---|
| 169 | |
|---|
| 170 | typedef fast_back_stack< ref_ptr<RefMatrix> > MatrixStack; |
|---|
| 171 | |
|---|
| 172 | MatrixStack _projectionStack; |
|---|
| 173 | |
|---|
| 174 | MatrixStack _modelviewStack; |
|---|
| 175 | MatrixStack _MVPW_Stack; |
|---|
| 176 | |
|---|
| 177 | typedef fast_back_stack<ref_ptr<Viewport> > ViewportStack; |
|---|
| 178 | ViewportStack _viewportStack; |
|---|
| 179 | |
|---|
| 180 | typedef fast_back_stack<Vec3> EyePointStack; |
|---|
| 181 | EyePointStack _referenceViewPoints; |
|---|
| 182 | EyePointStack _eyePointStack; |
|---|
| 183 | EyePointStack _viewPointStack; |
|---|
| 184 | |
|---|
| 185 | CullingStack _clipspaceCullingStack; |
|---|
| 186 | CullingStack _projectionCullingStack; |
|---|
| 187 | |
|---|
| 188 | CullingStack _modelviewCullingStack; |
|---|
| 189 | unsigned int _index_modelviewCullingStack; |
|---|
| 190 | CullingSet* _back_modelviewCullingStack; |
|---|
| 191 | |
|---|
| 192 | void computeFrustumVolume(); |
|---|
| 193 | float _frustumVolume; |
|---|
| 194 | |
|---|
| 195 | unsigned int _bbCornerNear; |
|---|
| 196 | unsigned int _bbCornerFar; |
|---|
| 197 | |
|---|
| 198 | ref_ptr<osg::RefMatrix> _identity; |
|---|
| 199 | |
|---|
| 200 | typedef std::vector< osg::ref_ptr<osg::RefMatrix> > MatrixList; |
|---|
| 201 | MatrixList _reuseMatrixList; |
|---|
| 202 | unsigned int _currentReuseMatrixIndex; |
|---|
| 203 | |
|---|
| 204 | inline osg::RefMatrix* createOrReuseMatrix(const osg::Matrix& value); |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | }; |
|---|
| 208 | |
|---|
| 209 | inline osg::Viewport* CullStack::getViewport() |
|---|
| 210 | { |
|---|
| 211 | if (!_viewportStack.empty()) |
|---|
| 212 | { |
|---|
| 213 | return _viewportStack.back().get(); |
|---|
| 214 | } |
|---|
| 215 | else |
|---|
| 216 | { |
|---|
| 217 | return 0L; |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | inline osg::RefMatrix* CullStack::getModelViewMatrix() |
|---|
| 222 | { |
|---|
| 223 | if (!_modelviewStack.empty()) |
|---|
| 224 | { |
|---|
| 225 | return _modelviewStack.back().get(); |
|---|
| 226 | } |
|---|
| 227 | else |
|---|
| 228 | { |
|---|
| 229 | return _identity.get(); |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | inline osg::RefMatrix* CullStack::getProjectionMatrix() |
|---|
| 234 | { |
|---|
| 235 | if (!_projectionStack.empty()) |
|---|
| 236 | { |
|---|
| 237 | return _projectionStack.back().get(); |
|---|
| 238 | } |
|---|
| 239 | else |
|---|
| 240 | { |
|---|
| 241 | return _identity.get(); |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | inline osg::Matrix CullStack::getWindowMatrix() |
|---|
| 246 | { |
|---|
| 247 | if (!_viewportStack.empty()) |
|---|
| 248 | { |
|---|
| 249 | osg::Viewport* viewport = _viewportStack.back().get(); |
|---|
| 250 | return viewport->computeWindowMatrix(); |
|---|
| 251 | } |
|---|
| 252 | else |
|---|
| 253 | { |
|---|
| 254 | return *_identity; |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | inline const osg::RefMatrix* CullStack::getMVPW() |
|---|
| 259 | { |
|---|
| 260 | if (!_MVPW_Stack.empty()) |
|---|
| 261 | { |
|---|
| 262 | if (!_MVPW_Stack.back()) |
|---|
| 263 | { |
|---|
| 264 | _MVPW_Stack.back() = createOrReuseMatrix(*getModelViewMatrix()); |
|---|
| 265 | (*_MVPW_Stack.back()) *= *(getProjectionMatrix()); |
|---|
| 266 | (*_MVPW_Stack.back()) *= getWindowMatrix(); |
|---|
| 267 | } |
|---|
| 268 | return _MVPW_Stack.back().get(); |
|---|
| 269 | } |
|---|
| 270 | else |
|---|
| 271 | { |
|---|
| 272 | return _identity.get(); |
|---|
| 273 | } |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | inline RefMatrix* CullStack::createOrReuseMatrix(const osg::Matrix& value) |
|---|
| 277 | { |
|---|
| 278 | // skip of any already reused matrix. |
|---|
| 279 | while (_currentReuseMatrixIndex<_reuseMatrixList.size() && |
|---|
| 280 | _reuseMatrixList[_currentReuseMatrixIndex]->referenceCount()>1) |
|---|
| 281 | { |
|---|
| 282 | ++_currentReuseMatrixIndex; |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | // if still within list, element must be singularly referenced |
|---|
| 286 | // there return it to be reused. |
|---|
| 287 | if (_currentReuseMatrixIndex<_reuseMatrixList.size()) |
|---|
| 288 | { |
|---|
| 289 | RefMatrix* matrix = _reuseMatrixList[_currentReuseMatrixIndex++].get(); |
|---|
| 290 | matrix->set(value); |
|---|
| 291 | return matrix; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | // otherwise need to create new matrix. |
|---|
| 295 | osg::RefMatrix* matrix = new RefMatrix(value); |
|---|
| 296 | _reuseMatrixList.push_back(matrix); |
|---|
| 297 | ++_currentReuseMatrixIndex; |
|---|
| 298 | return matrix; |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | } // end of namespace |
|---|
| 302 | |
|---|
| 303 | #endif |
|---|