/* -*-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 OSGEARTH_TRITON_DRAWABLE_H
#define OSGEARTH_TRITON_DRAWABLE_H

#include <osg/Drawable>
#include <osg/RenderInfo>
#include <osg/TextureCubeMap>
#include <osg/Version>
#include <osg/Texture2D>

#include <osgEarth/MapNode>
#include <osgEarth/Terrain>
#include <osgEarth/NativeProgramAdapter>

const unsigned int TRITON_OCEAN_MASK = 0x4; // 0100

namespace osgEarth { namespace Triton
{
    class TritonContext;

    /**
     * Custom drawable for rendering the Triton ocean effects
     */
    class TritonDrawable : public osg::Drawable
    {
    public:
        TritonDrawable(osgEarth::MapNode* mapNode=NULL, TritonContext* TRITON =0L);
        META_Object(Triton, TritonDrawable);
     
    public: // osg::Drawable

        // custom draw (called with an active GC)
        void drawImplementation(osg::RenderInfo& ri) const;
        
        void setupHeightMap(osgEarth::MapNode* mapNode);

#if OSG_VERSION_GREATER_THAN(3,3,1)
        osg::BoundingBox computeBoundingBox() const
#else
        osg::BoundingBox computeBound() const
#endif
            { return osg::BoundingBox(); }
            
        void SetPlanarReflectionMap(osg::Texture2D* map) {_planarReflectionMap = map;};
        void SetPlanarReflectionProjection(osg::RefMatrix * proj) {_planarReflectionProjection = proj;};

    protected:
        virtual ~TritonDrawable() { }

        osg::observer_ptr<TritonContext>  _TRITON;
        osg::observer_ptr<osgEarth::MapNode> _mapNode;
        osg::ref_ptr<osg::TextureCubeMap> _cubeMap;
        osg::BoundingBox                  _bbox;
        osg::ref_ptr<osg::Texture2D> _heightMap;
        osg::ref_ptr<osg::Camera> _heightCamera;
        osg::observer_ptr<osgEarth::TerrainCallback> _terrainChangedCallback;
        
        osg::ref_ptr< osg::Texture2D >       _planarReflectionMap;
        osg::ref_ptr< osg::RefMatrix >       _planarReflectionProjection;

        mutable osg::buffered_object<osgEarth::NativeProgramAdapterCollection> _adapters;
        
        TritonDrawable(const TritonDrawable& copy, const osg::CopyOp& op=osg::CopyOp::SHALLOW_COPY) { }
    };

} } // namespace osgEarth::Triton

#endif // OSGEARTH_TRITON_DRAWABLE_H
