TileModelCompiler 4.06 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/* -*-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.
*
* 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_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 <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:
        const MaskLayerVector&                    _maskLayers;
        const 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