StencilVolumeNode 3.73 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
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
/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2015 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
#ifndef OSGEARTHSYMBOLOGY_STENCIL_VOLUME_NODE_H
#define OSGEARTHSYMBOLOGY_STENCIL_VOLUME_NODE_H 1

#include <osgEarthSymbology/Common>
#include <osgEarthSymbology/Geometry>
#include <osgEarth/MaskNode>
#include <osgEarth/ThreadingUtils>
#include <osg/Group>
#include <osg/Vec4>
#include <queue>

namespace osgEarth { namespace Symbology 
{
    class OSGEARTHSYMBOLOGY_EXPORT StencilVolumeNode : public osgEarth::MaskNode
    {
    public:
        /**
         * Constructs a stencil masking node.
         *
         * @param preRenderChildrenToDepthBuffer
         *     Normally, this node will render the stencil volumes first and then
         *     render the children (which will be masked by the stencil). If you need
         *     to pre-render the children to the depth buffer, set this to TRUE. You
         *     need to do this is you are creating a straight render mask.
         *
         * @param inverted
         *     Inverts the stencil buffer, masking the opposite pixels that would
         *     normally be masked.
         */
        StencilVolumeNode(
            bool preRenderChildrenToDepthBuffer =false,
            bool inverted =false );

        /**
         * Copy constructor
         */
        StencilVolumeNode( const StencilVolumeNode& rhs, const osg::CopyOp& op =osg::CopyOp::DEEP_COPY_ALL );

        /** dtor */
        virtual ~StencilVolumeNode() { }

        META_Node(osgEarth::Symbology,StencilVolumeNode);
        
        //override
        virtual void traverse( osg::NodeVisitor& nv );

    public: // props

        // sets the render bins and returns the next available bin.
        int setBaseRenderBin( int bin );

        // adds stenciling volume geometry (safely during the next update traversal, if necessary)
        void addVolumes( osg::Node* node, bool onNextUpdate =true );
   
    public: // utilties

        /**
         * Generates a full screen quad that you can use to colorize 
         * stencil volumes
         */
        static osg::Node* createFullScreenQuad( const osg::Vec4f& color );


    public: //osg::Group overrides

        virtual bool addChild( Node *child );
        virtual bool insertChild( unsigned int index, Node *child );
        virtual bool removeChildren(unsigned int pos,unsigned int numChildrenToRemove);
        virtual bool replaceChild( Node *origChild, Node* newChild );
        virtual bool setChild( unsigned  int i, Node* node );
        virtual osg::BoundingSphere computeBound() const;

    protected:
        void init();

        osg::ref_ptr<osg::Group> _root;
        osg::Group* _stencilGroup1;
        osg::Group* _stencilGroup2;
        osg::Group* _depthPass;
        osg::Group* _renderPass;
        bool _inverted;
        bool _preRenderChildrenToDepthBuffer;

        Threading::Mutex _addVolumesMutex;
        std::queue< osg::ref_ptr<osg::Node> > _volumesToAdd;
        void addQueuedVolumes();
    };


} } // namespace osgEarth::Symbology

#endif // OSGEARTHSYMBOLOGY_STENCIL_VOLUME_NODE_H