/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2008-2012 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.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* 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_SPLAT_SPLAT_TERRAIN_EFFECT_H
#define OSGEARTH_SPLAT_SPLAT_TERRAIN_EFFECT_H

#include "SplatCatalog"
#include "SplatCoverageLegend"
#include "SplatShaders"
#include "Export"

#include "Coverage"
#include "Zone"

#include <osgEarth/TerrainEffect>
#include <osg/Image>
#include <osg/Uniform>
#include <osg/Texture2DArray>
#include <osgDB/Options>

using namespace osgEarth;

namespace osgEarth { namespace Splat
{
    /**
     * Effect that applies texture splatting to the terrain.
     */
    class OSGEARTHSPLAT_EXPORT SplatTerrainEffect : public TerrainEffect
    {
    public:
        /** constructor */
        SplatTerrainEffect();

        /**
         * Sets the OSG DB options to use when performing I/O
         */
        void setDBOptions(const osgDB::Options* dbo);

        /**
         * Sets the coverage source
         */
        void setCoverage(Coverage* coverage) { _coverage = coverage; }
        Coverage* getCoverage() const { return _coverage.get(); }

        /**
         * Sets the splatting zones
         */
        void setZones(const Zones& zones) { _zones = zones; }
        const Zones& getZones() const { return _zones; }


    public: // TerrainEffect interface

        void onInstall(TerrainEngineNode* engine);

        void onUninstall(TerrainEngineNode* engine);


    protected:
        virtual ~SplatTerrainEffect() { }

        // index-aligned with the surface biome region vector:
        SplatTextureDefVector               _textureDefs;

        int                                 _splatTexUnit;
        osg::ref_ptr<osg::Uniform>          _splatTexUniform;
        osg::ref_ptr<osg::Uniform>          _coverageTexUniform;
        int                                 _lutTexUnit;
        osg::ref_ptr<osg::Uniform>          _lutTexUniform;
        osg::ref_ptr<osg::Uniform>          _scaleOffsetUniform;
        osg::ref_ptr<osg::Uniform>          _warpUniform;
        osg::ref_ptr<osg::Uniform>          _blurUniform;
        float                               _renderOrder;
        int                                 _noiseTexUnit;
        osg::ref_ptr<osg::Texture>          _noiseTex;
        osg::ref_ptr<osg::Uniform>          _noiseTexUniform;
        osg::ref_ptr<osg::Uniform>          _noiseScaleUniform;
        osg::ref_ptr<osg::Uniform>          _useBilinearUniform;

        bool                                _editMode;
        bool                                _gpuNoise;

        osg::ref_ptr<const osgDB::Options>  _dbo;

        osg::ref_ptr<Coverage>              _coverage;

        Zones                               _zones;

    };

} } // namespace osgEarth::Splat

#endif // OSGEARTH_SPLAT_SPLAT_TERRAIN_EFFECT_H
