CullingSet 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSG_CullingSet
#define OSG_CullingSet 1

#include <osg/Polytope>
#include <osg/ShadowVolumeOccluder>
#include <osg/Viewport>

#include <math.h>


namespace osg {

#define COMPILE_WITH_SHADOW_OCCLUSION_CULLING

/** A CullingSet class which contains a frustum and a list of occluders. */
class OSG_EXPORT CullingSet : public Referenced
{

    public:

        typedef std::pair< osg::ref_ptr<osg::StateSet>, osg::Polytope > StateFrustumPair;
        typedef std::vector< StateFrustumPair > StateFrustumList;   
    
        CullingSet();

        CullingSet(const CullingSet& cs):
            Referenced(),
            _mask(cs._mask),
            _frustum(cs._frustum),
            _stateFrustumList(cs._stateFrustumList),
            _occluderList(cs._occluderList),
            _pixelSizeVector(cs._pixelSizeVector),
            _smallFeatureCullingPixelSize(cs._smallFeatureCullingPixelSize)
        {
        }

        CullingSet(const CullingSet& cs,const Matrix& matrix, const Vec4& pixelSizeVector):
            _mask(cs._mask),
            _frustum(cs._frustum),
            _stateFrustumList(cs._stateFrustumList),
            _occluderList(cs._occluderList),
            _pixelSizeVector(pixelSizeVector),
            _smallFeatureCullingPixelSize(cs._smallFeatureCullingPixelSize)
        {
            _frustum.transformProvidingInverse(matrix);
            for(OccluderList::iterator itr=_occluderList.begin();
                itr!=_occluderList.end();
                ++itr)
            {
                itr->transformProvidingInverse(matrix);
            }
        }
        
        CullingSet& operator = (const CullingSet& cs)
        {
            if (this==&cs) return *this;
            
            _mask = cs._mask;
            _frustum = cs._frustum;
            _stateFrustumList = cs._stateFrustumList;
            _occluderList = cs._occluderList;
            _pixelSizeVector = cs._pixelSizeVector;
            _smallFeatureCullingPixelSize = cs._smallFeatureCullingPixelSize;
            
            return *this;
        }


        inline void set(const CullingSet& cs)
        {
            _mask = cs._mask;
            _frustum = cs._frustum;
            _stateFrustumList = cs._stateFrustumList;
            _occluderList = cs._occluderList;
            _pixelSizeVector = cs._pixelSizeVector;
            _smallFeatureCullingPixelSize = cs._smallFeatureCullingPixelSize;
        }
        
        inline void set(const CullingSet& cs,const Matrix& matrix, const Vec4& pixelSizeVector)
        {
            _mask = cs._mask;
            _stateFrustumList = cs._stateFrustumList;
            _occluderList = cs._occluderList;
            _pixelSizeVector = pixelSizeVector;
            _smallFeatureCullingPixelSize = cs._smallFeatureCullingPixelSize;

            //_frustum = cs._frustum;
            //_frustum.transformProvidingInverse(matrix);
            
            _frustum.setAndTransformProvidingInverse(cs._frustum,matrix);
            
            for(StateFrustumList::iterator sitr=_stateFrustumList.begin();
                sitr!=_stateFrustumList.end();
                ++sitr)
            {
                sitr->second.transformProvidingInverse(matrix);
            }

            for(OccluderList::iterator oitr=_occluderList.begin();
                oitr!=_occluderList.end();
                ++oitr)
            {
                oitr->transformProvidingInverse(matrix);
            }
        
        }
        
        typedef std::vector<ShadowVolumeOccluder>   OccluderList;

        typedef int Mask;

        enum MaskValues
        {
            NO_CULLING                  = 0x0,
            VIEW_FRUSTUM_SIDES_CULLING  = 0x1,
            NEAR_PLANE_CULLING          = 0x2,
            FAR_PLANE_CULLING           = 0x4,
            VIEW_FRUSTUM_CULLING        = VIEW_FRUSTUM_SIDES_CULLING|
                                          NEAR_PLANE_CULLING|
                                          FAR_PLANE_CULLING,
            SMALL_FEATURE_CULLING       = 0x8,
            SHADOW_OCCLUSION_CULLING    = 0x10,
            DEFAULT_CULLING             = VIEW_FRUSTUM_SIDES_CULLING|
                                          SMALL_FEATURE_CULLING|
                                          SHADOW_OCCLUSION_CULLING,
            ENABLE_ALL_CULLING          = VIEW_FRUSTUM_CULLING|
                                          SMALL_FEATURE_CULLING|
                                          SHADOW_OCCLUSION_CULLING
        };

        void setCullingMask(Mask mask) { _mask = mask; }
        Mask getCullingMask() const { return _mask; }

        void setFrustum(Polytope& cv) { _frustum = cv; }

        Polytope& getFrustum() { return _frustum; }
        const Polytope& getFrustum() const { return _frustum; }    
        
        void addStateFrustum(StateSet* stateset, Polytope& polytope) { _stateFrustumList.push_back(StateFrustumPair(stateset,polytope)); }
        
        void getStateFrustumList(StateFrustumList& sfl) { _stateFrustumList = sfl; }
        StateFrustumList& getStateFrustumList() { return _stateFrustumList; }

        void addOccluder(ShadowVolumeOccluder& cv) { _occluderList.push_back(cv); }

        void setPixelSizeVector(const Vec4& v) { _pixelSizeVector = v; }

        Vec4& getPixelSizeVector() { return _pixelSizeVector; }
        const Vec4& getPixelSizeVector() const { return _pixelSizeVector; }

        void setSmallFeatureCullingPixelSize(float value) { _smallFeatureCullingPixelSize=value; }
        
        float& getSmallFeatureCullingPixelSize() { return _smallFeatureCullingPixelSize; }

        float getSmallFeatureCullingPixelSize() const { return _smallFeatureCullingPixelSize; }
    
    
        /** Compute the pixel of an object at position v, with specified radius.*/
        float pixelSize(const Vec3& v,float radius) const { return radius/(v*_pixelSizeVector); }

        /** Compute the pixel of a bounding sphere.*/
        float pixelSize(const BoundingSphere& bs) const { return bs.radius()/(bs.center()*_pixelSizeVector); }

        /** Compute the pixel of an object at position v, with specified radius. fabs()ed to always be positive. */
        float clampedPixelSize(const Vec3& v,float radius) const { return fabs(pixelSize(v, radius)); }

        /** Compute the pixel of a bounding sphere. fabs()ed to always be positive. */
        float clampedPixelSize(const BoundingSphere& bs) const { return fabs(pixelSize(bs)); }


        inline bool isCulled(const std::vector<Vec3>& vertices)
        {
            if (_mask&VIEW_FRUSTUM_CULLING)
            {
                // is it outside the view frustum...
                if (!_frustum.contains(vertices)) return true;
            }
            
            if (_mask&SMALL_FEATURE_CULLING)
            {
            }

            if (_mask&SHADOW_OCCLUSION_CULLING)
            {
                // is it in one of the shadow occluder volumes.
                if (!_occluderList.empty())
                {
                    for(OccluderList::iterator itr=_occluderList.begin();
                        itr!=_occluderList.end();
                        ++itr)
                    {
                        if (itr->contains(vertices)) return true;
                    }
                }
            }
                        
            return false;
        }
        
        inline bool isCulled(const BoundingBox& bb)
        {
            if (_mask&VIEW_FRUSTUM_CULLING)
            {
                // is it outside the view frustum...
                if (!_frustum.contains(bb)) return true;
            }
            
            if (_mask&SMALL_FEATURE_CULLING)
            {
            }

            if (_mask&SHADOW_OCCLUSION_CULLING)
            {
                // is it in one of the shadow occluder volumes.
                if (!_occluderList.empty())
                {
                    for(OccluderList::iterator itr=_occluderList.begin();
                        itr!=_occluderList.end();
                        ++itr)
                    {
                        if (itr->contains(bb)) return true;
                    }
                }
            }
                        
            return false;
        }
        
        inline bool isCulled(const BoundingSphere& bs)
        {
            if (_mask&VIEW_FRUSTUM_CULLING)
            {
                // is it outside the view frustum...
                if (!_frustum.contains(bs)) return true;
            }
            
            if (_mask&SMALL_FEATURE_CULLING)
            {
                if (((bs.center()*_pixelSizeVector)*_smallFeatureCullingPixelSize)>bs.radius()) return true;
            }
#ifdef COMPILE_WITH_SHADOW_OCCLUSION_CULLING
            if (_mask&SHADOW_OCCLUSION_CULLING)
            {
                // is it in one of the shadow occluder volumes.
                if (!_occluderList.empty())
                {
                    for(OccluderList::iterator itr=_occluderList.begin();
                        itr!=_occluderList.end();
                        ++itr)
                    {
                        if (itr->contains(bs)) return true;
                    }
                }
            }
#endif            
            return false;
        }
        
        inline void pushCurrentMask()
        {
            _frustum.pushCurrentMask();
            
            if (!_stateFrustumList.empty())
            {
                for(StateFrustumList::iterator itr=_stateFrustumList.begin();
                    itr!=_stateFrustumList.end();
                    ++itr)
                {
                    itr->second.pushCurrentMask();
                }
            }


#ifdef COMPILE_WITH_SHADOW_OCCLUSION_CULLING
            if (!_occluderList.empty())
            {
                for(OccluderList::iterator itr=_occluderList.begin();
                    itr!=_occluderList.end();
                    ++itr)
                {
                    itr->pushCurrentMask();
                }
            }
#endif
        }
        
        inline void popCurrentMask()
        {
            _frustum.popCurrentMask();

            if (!_stateFrustumList.empty())
            {
                for(StateFrustumList::iterator itr=_stateFrustumList.begin();
                    itr!=_stateFrustumList.end();
                    ++itr)
                {
                    itr->second.popCurrentMask();
                }
            }

#ifdef COMPILE_WITH_SHADOW_OCCLUSION_CULLING
            if (!_occluderList.empty())
            {
                for(OccluderList::iterator itr=_occluderList.begin();
                    itr!=_occluderList.end();
                    ++itr)
                {
                    itr->popCurrentMask();
                }
            }
#endif
        }
        
        void disableAndPushOccludersCurrentMask(NodePath& nodePath);

        void popOccludersCurrentMask(NodePath& nodePath);
        
        static osg::Vec4 computePixelSizeVector(const Viewport& W, const Matrix& P, const Matrix& M);

        virtual ~CullingSet();
        

    protected:
    

        Mask                _mask;
        Polytope            _frustum;
        StateFrustumList    _stateFrustumList;        
        OccluderList        _occluderList;        
        Vec4                _pixelSizeVector;
        float               _smallFeatureCullingPixelSize;

};

}    // end of namespace

#endif