CullStack 9.86 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
/* -*-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_CULLSTACK
#define OSG_CULLSTACK 1

#include <osg/CullingSet>
#include <osg/CullSettings>
#include <osg/Viewport>
#include <osg/fast_back_stack>
#include <osg/Transform>

namespace osg {

/** A CullStack class which accumulates the current project, modelview matrices
and the CullingSet. */
class OSG_EXPORT CullStack : public osg::CullSettings
{

    public:
    
    
        CullStack();
        CullStack(const CullStack& cs);
        
        ~CullStack();
    
        typedef std::vector<ShadowVolumeOccluder>   OccluderList;

        void reset();
        
        void pushCullingSet();
        void popCullingSet();

        void setOccluderList(const ShadowVolumeOccluderList& svol) { _occluderList = svol; }
        ShadowVolumeOccluderList& getOccluderList() { return _occluderList; }
        const ShadowVolumeOccluderList& getOccluderList() const { return _occluderList; }

        void pushViewport(osg::Viewport* viewport);
        void popViewport();

        void pushProjectionMatrix(osg::RefMatrix* matrix);
        void popProjectionMatrix();

        void pushModelViewMatrix(osg::RefMatrix* matrix, Transform::ReferenceFrame referenceFrame);
        void popModelViewMatrix();

        inline float getFrustumVolume() { if (_frustumVolume<0.0f) computeFrustumVolume(); return _frustumVolume; }


        /** Compute the pixel size of an object at position v, with specified radius.*/
        float pixelSize(const Vec3& v,float radius) const
        {
            return getCurrentCullingSet().pixelSize(v,radius);
        }
        
        /** Compute the pixel size of the bounding sphere.*/
        float pixelSize(const BoundingSphere& bs) const
        {
            return pixelSize(bs.center(),bs.radius());
        }

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

        inline void disableAndPushOccludersCurrentMask(NodePath& nodePath)
        {
            getCurrentCullingSet().disableAndPushOccludersCurrentMask(nodePath);
        }

        inline void popOccludersCurrentMask(NodePath& nodePath)
        {
            getCurrentCullingSet().popOccludersCurrentMask(nodePath);
        }

        inline bool isCulled(const std::vector<Vec3>& vertices)
        {
            return getCurrentCullingSet().isCulled(vertices);
        }

        inline bool isCulled(const BoundingBox& bb)
        {
            return bb.valid() && getCurrentCullingSet().isCulled(bb);
        }
        
        inline bool isCulled(const BoundingSphere& bs)
        {
            return getCurrentCullingSet().isCulled(bs);
        }
        
        inline bool isCulled(const osg::Node& node)
        {
            return node.isCullingActive() && getCurrentCullingSet().isCulled(node.getBound());
        }

        inline void pushCurrentMask()
        {
            getCurrentCullingSet().pushCurrentMask();
        }
        
        inline void popCurrentMask()
        {
            getCurrentCullingSet().popCurrentMask();
        }
        
        
        typedef std::vector< CullingSet > CullingStack;

        inline CullingStack& getClipSpaceCullingStack() { return _clipspaceCullingStack; }
        
        inline CullingStack& getProjectionCullingStack() { return _projectionCullingStack; }
        
        inline CullingStack& getModelViewCullingStack() { return _modelviewCullingStack; }
        
        inline CullingSet& getCurrentCullingSet() { return *_back_modelviewCullingStack; }
        inline const CullingSet& getCurrentCullingSet() const { return *_back_modelviewCullingStack; }
        
        inline osg::Viewport* getViewport();
        inline osg::RefMatrix* getModelViewMatrix();
        inline osg::RefMatrix* getProjectionMatrix();
        inline osg::Matrix getWindowMatrix();
        inline const osg::RefMatrix* getMVPW();
        
        inline const osg::Vec3& getReferenceViewPoint() const { return _referenceViewPoints.back(); }
        inline void pushReferenceViewPoint(const osg::Vec3& viewPoint) { _referenceViewPoints.push_back(viewPoint); }
        inline void popReferenceViewPoint() { _referenceViewPoints.pop_back(); }

        inline const osg::Vec3& getEyeLocal() const { return _eyePointStack.back(); }

        inline const osg::Vec3& getViewPointLocal() const { return _viewPointStack.back(); }

        inline const osg::Vec3 getUpLocal() const
        {
            const osg::Matrix& matrix = *_modelviewStack.back();
            return osg::Vec3(matrix(0,1),matrix(1,1),matrix(2,1));
        }

        inline const osg::Vec3 getLookVectorLocal() const
        {
            const osg::Matrix& matrix = *_modelviewStack.back();
            return osg::Vec3(-matrix(0,2),-matrix(1,2),-matrix(2,2));
        }
        

    protected:
    
        // base set of shadow volume occluder to use in culling.
        ShadowVolumeOccluderList                                    _occluderList;

        typedef fast_back_stack< ref_ptr<RefMatrix> >                  MatrixStack;

        MatrixStack                                                 _projectionStack;

        MatrixStack                                                 _modelviewStack;
        MatrixStack                                                 _MVPW_Stack;

        typedef fast_back_stack<ref_ptr<Viewport> >                 ViewportStack;
        ViewportStack                                               _viewportStack;
        
        typedef fast_back_stack<Vec3>                               EyePointStack;
        EyePointStack                                               _referenceViewPoints;
        EyePointStack                                               _eyePointStack;
        EyePointStack                                               _viewPointStack;

        CullingStack                                                _clipspaceCullingStack;
        CullingStack                                                _projectionCullingStack;

        CullingStack                                                _modelviewCullingStack;
        unsigned int                                                _index_modelviewCullingStack;
        CullingSet*                                                 _back_modelviewCullingStack;

        void computeFrustumVolume();
        float                                                       _frustumVolume;

        unsigned int                                                _bbCornerNear;
        unsigned int                                                _bbCornerFar;

        ref_ptr<osg::RefMatrix>                                     _identity;
        
        typedef std::vector< osg::ref_ptr<osg::RefMatrix> > MatrixList;
        MatrixList _reuseMatrixList;
        unsigned int _currentReuseMatrixIndex;
        
        inline osg::RefMatrix* createOrReuseMatrix(const osg::Matrix& value);

        
};

inline osg::Viewport* CullStack::getViewport()
{
    if (!_viewportStack.empty())
    {
        return _viewportStack.back().get();
    }
    else
    {
        return 0L;
    }
}

inline osg::RefMatrix* CullStack::getModelViewMatrix()
{
    if (!_modelviewStack.empty())
    {
        return _modelviewStack.back().get();
    }
    else
    {
        return _identity.get();
    }
}

inline osg::RefMatrix* CullStack::getProjectionMatrix()
{
    if (!_projectionStack.empty())
    {
        return _projectionStack.back().get();
    }
    else
    {
        return _identity.get();
    }
}

inline osg::Matrix CullStack::getWindowMatrix()
{
    if (!_viewportStack.empty())
    {
        osg::Viewport* viewport = _viewportStack.back().get();
        return viewport->computeWindowMatrix();
    }
    else
    {
        return *_identity;
    }
}

inline const osg::RefMatrix* CullStack::getMVPW()
{
    if (!_MVPW_Stack.empty())
    {
        if (!_MVPW_Stack.back())
        {
            _MVPW_Stack.back() = createOrReuseMatrix(*getModelViewMatrix());
            (*_MVPW_Stack.back()) *= *(getProjectionMatrix());
            (*_MVPW_Stack.back()) *= getWindowMatrix();
        }
        return _MVPW_Stack.back().get();
    }
    else
    {
        return _identity.get();
    }
}

inline RefMatrix* CullStack::createOrReuseMatrix(const osg::Matrix& value)
{
    // skip of any already reused matrix.
    while (_currentReuseMatrixIndex<_reuseMatrixList.size() && 
           _reuseMatrixList[_currentReuseMatrixIndex]->referenceCount()>1)
    {
        ++_currentReuseMatrixIndex;
    }

    // if still within list, element must be singularly referenced
    // there return it to be reused.
    if (_currentReuseMatrixIndex<_reuseMatrixList.size())
    {
        RefMatrix* matrix = _reuseMatrixList[_currentReuseMatrixIndex++].get();
        matrix->set(value);
        return matrix;
    }

    // otherwise need to create new matrix.
    osg::RefMatrix* matrix = new RefMatrix(value);
    _reuseMatrixList.push_back(matrix);
    ++_currentReuseMatrixIndex;
    return matrix;
}

}    // end of namespace

#endif