/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2008-2013 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 OSGEARTH_ENGINE_OSGTERRAIN_MULTIPASS_TERRAIN_TECHNIQUE
#define OSGEARTH_ENGINE_OSGTERRAIN_MULTIPASS_TERRAIN_TECHNIQUE 1

#include "CustomTerrainTechnique"
#include "TransparentLayer"

#include <osg/MatrixTransform>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Version>

#include <osgTerrain/TerrainTechnique>
#include <osgTerrain/Locator>

#include <osgEarth/TextureCompositor>

namespace osgEarth_engine_osgterrain
{
    class MultiPassTerrainTechnique : public TerrainTechnique
    {
    public:

        MultiPassTerrainTechnique( TextureCompositor* texCompositor =0L );

        /** Copy constructor using CopyOp to manage deep vs shallow copy.*/
        MultiPassTerrainTechnique(const MultiPassTerrainTechnique&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
        
        META_Object( osgEarth, MultiPassTerrainTechnique );

        virtual void init();
        
        virtual osgTerrain::Locator* computeMasterLocator();
        
        virtual osg::Vec3d computeCenterModel(osgTerrain::Locator* masterLocator);
        
        virtual void generateGeometry(osgTerrain::Locator* masterLocator, const osg::Vec3d& centerModel);     

	    virtual osg::Geometry* createGeometryPrototype(osgTerrain::Locator* masterLocator, const osg::Vec3d& centerModel);

	    osg::Geode* createPass(unsigned int order, const CustomColorLayer* layer, osgTerrain::Locator* masterLocator, const osg::Vec3d& centerModel, osg::Geometry* geometry); 

        /** Traverse the terain subgraph.*/
        virtual void traverse(osg::NodeVisitor& nv);

        /** If State is non-zero, this function releases any associated OpenGL objects for
        * the specified graphics context. Otherwise, releases OpenGL objects
        * for all graphics contexts. */
        virtual void releaseGLObjects(osg::State* = 0) const;


    private:

        void updateTransparency();
        void updateGeometry();

        virtual ~MultiPassTerrainTechnique();

        osg::ref_ptr<osg::MatrixTransform>  _transform;
        osg::ref_ptr<osg::Group>            _passes;            
        bool                                _terrainTileInitialized;
        osg::ref_ptr<TextureCompositor>     _texCompositor;
    };

} // namespace osgEarth_engine_osgterrain

#endif //OSGEARTH_ENGINE_OSGTERRAIN_MULTIPASS_TERRAIN_TECHNIQUE
