/* osgEarth - Geospatial SDK for OpenSceneGraph
* Copyright 2018 Pelican Mapping
* MIT License
*/
#ifndef OSGEARTH_DRIVERS_MP_TERRAIN_ENGINE_TILE_MODEL_COMPILER
#define OSGEARTH_DRIVERS_MP_TERRAIN_ENGINE_TILE_MODEL_COMPILER 1

#include "Common"
#include "TileModel"
#include "TileNode"
#include "MPTerrainEngineOptions"

#include <osgEarth/Map>
#include <osgEarth/Locators>
#include <osgEarth/Progress>
#include <osgEarth/MaskLayer>
#include <osgEarth/ModelLayer>
#include "MapFrame"

#include <osg/Node>
#include <osg/StateSet>
#include <osg/Drawable>
#include <osg/Array>

namespace osgEarth { namespace Drivers { namespace MPTerrainEngine
{
    using namespace osgEarth;

    /**
     * Cache used by the TileModelCompiler. This lets us share common data across
     * compilations. Since the compiler is thread-locked, we just have a separate
     * cache per compiler.
     *
     * Important Note! Any array you store in the cache MUST have it's OWN 
     * unique VBO. Call array->setVertexBufferObject( new osg::VertexBufferObject() )
     * to assign one. This will prevent non-thread-safe buffer object sharing.
     */
    struct CompilerCache
    {
        // Texture coordinate array cache def
        struct TexCoordTableKey {
            osg::ref_ptr<const GeoLocator> _locator;
            osg::Vec4d                     _mat;
            unsigned                       _cols, _rows;
        };
        
        typedef std::pair< TexCoordTableKey, osg::ref_ptr<osg::Vec2Array> > LocatorTexCoordPair;
        
        struct TexCoordArrayCache : public std::list<LocatorTexCoordPair>
        {
            osg::ref_ptr<osg::Vec2Array>& get( const osg::Vec4d& mat, unsigned cols, unsigned rows );
        };

        TexCoordArrayCache _surfaceTexCoordArrays;
        TexCoordArrayCache _skirtTexCoordArrays;
    };


    /**
     * Builds the actual tile geometry.
     *
     * When used with the KeyNodeFactory, there will be exactly one instance of this
     * class per thread. So, we can expand this to include caches for commonly shared
     * data like texture coordinate or color arrays.
     */
    class TileModelCompiler : public osg::Referenced
    {
    public:
        TileModelCompiler(
            const MaskLayerVector&        maskLayers,
            const ModelLayerVector&       modelLayers,
            int                           textureImageUnit,
            bool                          optimizeTriangleOrientation,
            const MPTerrainEngineOptions& options);

        /**
         * Compiles a tile model into a TileNode.
         */
        TileNode* compile(
            TileModel*        model,
            const MapFrame&   frame,
            ProgressCallback* progress);

    protected:
        MaskLayerVector                           _maskLayers;
        ModelLayerVector                          _modelLayers;
        int                                       _textureImageUnit;
        bool                                      _optimizeTriOrientation;
        const MPTerrainEngineOptions&             _options;
        CompilerCache                             _cache;
        bool                                      _debug;
    };

} } } // namespace osgEarth::Drivers::MPTerrainEngine

#endif // OSGEARTH_DRIVERS_MP_TERRAIN_ENGINE_TILE_MODEL_COMPILER
