| 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_SHADOWVOLUMEOCCLUDER |
|---|
| 15 | #define OSG_SHADOWVOLUMEOCCLUDER 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Polytope> |
|---|
| 18 | #include <osg/ConvexPlanarOccluder> |
|---|
| 19 | #include <osg/Node> |
|---|
| 20 | |
|---|
| 21 | namespace osg { |
|---|
| 22 | |
|---|
| 23 | class CullStack; |
|---|
| 24 | |
|---|
| 25 | /** ShadowVolumeOccluder is a helper class for implementing shadow occlusion culling. */ |
|---|
| 26 | class OSG_EXPORT ShadowVolumeOccluder |
|---|
| 27 | { |
|---|
| 28 | |
|---|
| 29 | public: |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | typedef std::vector<Polytope> HoleList; |
|---|
| 33 | |
|---|
| 34 | ShadowVolumeOccluder(const ShadowVolumeOccluder& svo): |
|---|
| 35 | _volume(svo._volume), |
|---|
| 36 | _nodePath(svo._nodePath), |
|---|
| 37 | _projectionMatrix(svo._projectionMatrix), |
|---|
| 38 | _occluderVolume(svo._occluderVolume), |
|---|
| 39 | _holeList(svo._holeList) {} |
|---|
| 40 | |
|---|
| 41 | ShadowVolumeOccluder(): |
|---|
| 42 | _volume(0.0f) {} |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | bool operator < (const ShadowVolumeOccluder& svo) const { return getVolume()>svo.getVolume(); } // not greater volume first. |
|---|
| 46 | |
|---|
| 47 | /** compute the shadow volume occluder. */ |
|---|
| 48 | bool computeOccluder(const NodePath& nodePath,const ConvexPlanarOccluder& occluder,CullStack& cullStack,bool createDrawables=false); |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | inline void disableResultMasks(); |
|---|
| 52 | |
|---|
| 53 | inline void pushCurrentMask(); |
|---|
| 54 | inline void popCurrentMask(); |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | /** return true if the matrix passed in matches the projection matrix that this ShadowVolumeOccluder is |
|---|
| 58 | * associated with.*/ |
|---|
| 59 | bool matchProjectionMatrix(const osg::Matrix& matrix) const |
|---|
| 60 | { |
|---|
| 61 | if (_projectionMatrix.valid()) return matrix==*_projectionMatrix; |
|---|
| 62 | else return false; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | /** Set the NodePath which describes which node in the scene graph |
|---|
| 67 | * that this occluder is attached to. */ |
|---|
| 68 | inline void setNodePath(NodePath& nodePath) { _nodePath = nodePath; } |
|---|
| 69 | inline NodePath& getNodePath() { return _nodePath; } |
|---|
| 70 | inline const NodePath& getNodePath() const { return _nodePath; } |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | /** get the volume of the occluder minus its holes, in eye coords, the volume is normalized by dividing by |
|---|
| 74 | * the volume of the view frustum in eye coords.*/ |
|---|
| 75 | float getVolume() const { return _volume; } |
|---|
| 76 | |
|---|
| 77 | /** return the occluder polytope.*/ |
|---|
| 78 | Polytope& getOccluder() { return _occluderVolume; } |
|---|
| 79 | |
|---|
| 80 | /** return the const occluder polytope.*/ |
|---|
| 81 | const Polytope& getOccluder() const { return _occluderVolume; } |
|---|
| 82 | |
|---|
| 83 | /** return the list of holes.*/ |
|---|
| 84 | HoleList& getHoleList() { return _holeList; } |
|---|
| 85 | |
|---|
| 86 | /** return the const list of holes.*/ |
|---|
| 87 | const HoleList& getHoleList() const { return _holeList; } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | /** return true if the specified vertex list is contained entirely |
|---|
| 91 | * within this shadow occluder volume.*/ |
|---|
| 92 | bool contains(const std::vector<Vec3>& vertices); |
|---|
| 93 | |
|---|
| 94 | /** return true if the specified bounding sphere is contained entirely |
|---|
| 95 | * within this shadow occluder volume.*/ |
|---|
| 96 | bool contains(const BoundingSphere& bound); |
|---|
| 97 | |
|---|
| 98 | /** return true if the specified bounding box is contained entirely |
|---|
| 99 | * within this shadow occluder volume.*/ |
|---|
| 100 | bool contains(const BoundingBox& bound); |
|---|
| 101 | |
|---|
| 102 | inline void transformProvidingInverse(const osg::Matrix& matrix) |
|---|
| 103 | { |
|---|
| 104 | _occluderVolume.transformProvidingInverse(matrix); |
|---|
| 105 | for(HoleList::iterator itr=_holeList.begin(); |
|---|
| 106 | itr!=_holeList.end(); |
|---|
| 107 | ++itr) |
|---|
| 108 | { |
|---|
| 109 | itr->transformProvidingInverse(matrix); |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | protected: |
|---|
| 115 | |
|---|
| 116 | float _volume; |
|---|
| 117 | NodePath _nodePath; |
|---|
| 118 | ref_ptr<const RefMatrix> _projectionMatrix; |
|---|
| 119 | Polytope _occluderVolume; |
|---|
| 120 | HoleList _holeList; |
|---|
| 121 | }; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | /** A list of ShadowVolumeOccluder, used by CollectOccluderVisitor and CullVistor's.*/ |
|---|
| 125 | typedef std::vector<ShadowVolumeOccluder> ShadowVolumeOccluderList; |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | inline void ShadowVolumeOccluder::disableResultMasks() |
|---|
| 129 | { |
|---|
| 130 | //std::cout<<"ShadowVolumeOccluder::disableResultMasks() - _occluderVolume.getMaskStack().size()="<<_occluderVolume.getMaskStack().size()<<" "<<_occluderVolume.getCurrentMask()<<std::endl; |
|---|
| 131 | _occluderVolume.setResultMask(0); |
|---|
| 132 | for(HoleList::iterator itr=_holeList.begin(); |
|---|
| 133 | itr!=_holeList.end(); |
|---|
| 134 | ++itr) |
|---|
| 135 | { |
|---|
| 136 | itr->setResultMask(0); |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | inline void ShadowVolumeOccluder::pushCurrentMask() |
|---|
| 141 | { |
|---|
| 142 | //std::cout<<"ShadowVolumeOccluder::pushCurrentMasks() - _occluderVolume.getMaskStack().size()="<<_occluderVolume.getMaskStack().size()<<" "<<_occluderVolume.getCurrentMask()<<std::endl; |
|---|
| 143 | _occluderVolume.pushCurrentMask(); |
|---|
| 144 | if (!_holeList.empty()) |
|---|
| 145 | { |
|---|
| 146 | for(HoleList::iterator itr=_holeList.begin(); |
|---|
| 147 | itr!=_holeList.end(); |
|---|
| 148 | ++itr) |
|---|
| 149 | { |
|---|
| 150 | itr->pushCurrentMask(); |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | inline void ShadowVolumeOccluder::popCurrentMask() |
|---|
| 156 | { |
|---|
| 157 | _occluderVolume.popCurrentMask(); |
|---|
| 158 | if (!_holeList.empty()) |
|---|
| 159 | { |
|---|
| 160 | for(HoleList::iterator itr=_holeList.begin(); |
|---|
| 161 | itr!=_holeList.end(); |
|---|
| 162 | ++itr) |
|---|
| 163 | { |
|---|
| 164 | itr->popCurrentMask(); |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | //std::cout<<"ShadowVolumeOccluder::popCurrentMasks() - _occluderVolume.getMaskStack().size()="<<_occluderVolume.getMaskStack().size()<<" "<<_occluderVolume.getCurrentMask()<<std::endl; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | } // end of namespace |
|---|
| 171 | |
|---|
| 172 | #endif |
|---|