/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2008-2009 Pelican Ventures, Inc.
 * 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 OSGEARTHFEATURES_STENCIL_VOLUME_NODE_H
#define OSGEARTHFEATURES_STENCIL_VOLUME_NODE_H 1

#include <osgEarthFeatures/Common>
#include <osgEarthFeatures/Filter>
#include <osgEarthFeatures/Geometry>
#include <osgEarth/MaskNode>
#include <osg/Group>
#include <osg/Vec4>

namespace osgEarth { namespace Features 
{
    class OSGEARTHFEATURES_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 );

        StencilVolumeNode( const StencilVolumeNode& rhs, const osg::CopyOp& op =osg::CopyOp::DEEP_COPY_ALL );

        META_Node(osgEarth::Features,StencilVolumeNode);
        
        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
        void addVolumes( osg::Node* node );

        // adds the node to draw once the stencil is in place.
        //void setChild( osg::Node* node );

    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;
    };


    class OSGEARTHFEATURES_EXPORT StencilVolumeFactory
    {
    public:
        static osg::Geode* createVolume(
            Geometry*            geom,
            double               offset,
            double               height,
            const FilterContext& context );
    };

} } // namespace osgEarth::Features

#endif // OSGEARTHFEATURES_STENCIL_VOLUME_NODE_H

