osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarthDrivers/engine_osgterrain/CustomTerrain

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003 * Copyright 2008-2010 Pelican Mapping
00004 * http://osgearth.org
00005 *
00006 * osgEarth is free software; you can redistribute it and/or modify
00007 * it under the terms of the GNU Lesser General Public License as published by
00008 * the Free Software Foundation; either version 2 of the License, or
00009 * (at your option) any later version.
00010 *
00011 * This program is distributed in the hope that it will be useful,
00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 * GNU Lesser General Public License for more details.
00015 *
00016 * You should have received a copy of the GNU Lesser General Public License
00017 * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018 */
00019 #ifndef OSGEARTH_ENGINE_OSGTERRAIN_CUSTOM_TERRAIN
00020 #define OSGEARTH_ENGINE_OSGTERRAIN_CUSTOM_TERRAIN 1
00021 
00022 #error
00023 #include "Common"
00024 #include "CustomTile"
00025 #include "OSGTileFactory"
00026 #include <osgEarth/TaskService>
00027 #include <osgEarth/Locators>
00028 #include <osgEarth/Profile>
00029 #include <osgEarth/TerrainOptions>
00030 #include <osgEarth/Map>
00031 #include <osgEarth/ThreadingUtils>
00032 #include <osgTerrain/Terrain>
00033 #include <osgTerrain/TerrainTile>
00034 #include <list>
00035 #include <queue>
00036 #include <vector>
00037 
00038 class TileFactory;
00039 class CustomTerrain;
00040 class CustomTile;
00041 class CustomTileVector;
00042 
00043 #if 0
00044 typedef std::list< osg::ref_ptr<osgTerrain::TerrainTile> > TerrainTileList;
00045 
00049 struct OSGEARTH_EXPORT TerrainCallback : public osg::Referenced
00050 {
00051     virtual void onTerrainTilesUpdated( const TerrainTileList& tiles ) { }
00052 };
00053 typedef std::list< osg::ref_ptr<TerrainCallback> > TerrainCallbackList;
00054 #endif
00055 
00056 using namespace osgEarth;
00057 
00067 class CustomTerrain : public osgTerrain::Terrain
00068 {
00069 public:
00070     CustomTerrain(
00071         const MapFrame& update_mapf,
00072         const MapFrame& cull_mapf,
00073         OSGTileFactory* factory,
00074         bool quickReleaseGLObjects );
00075 
00076     virtual const char* libraryName() const { return "osgEarth"; }
00077     virtual const char* className() const { return "CustomTerrain"; }
00078 
00079 public:
00084     void incrementRevision();
00085 
00089     int getRevision() const;
00090 
00091     OSGTileFactory* getTileFactory();
00092     bool getQuickReleaseGLObjects() const { return _quickReleaseGLObjects; }
00093 
00094     TaskService* getImageryTaskService(int layerId);
00095     TaskService* getElevationTaskService();
00096     TaskService* getTileGenerationTaskSerivce();
00097 
00098     const MapFrame& getUpdateThreadMapFrame() { return _update_mapf; }
00099     const MapFrame& getCullThreadMapFrame() { return _cull_mapf; }
00100 
00104     unsigned int getNumTasksRemaining() const;
00105 
00106     virtual void traverse( osg::NodeVisitor &nv );
00107 
00113     void updateTaskServiceThreads( const MapFrame& mapf );
00114 
00115     bool updateBudgetRemaining() const;
00116 
00117     //void addTerrainCallback( TerrainCallback* cb );
00118 
00119 protected:
00120 
00121         ~CustomTerrain();
00122 
00123     typedef std::map< osgTerrain::TileID, osg::ref_ptr<CustomTile> > TileTable;
00124 
00125     typedef std::queue< osg::ref_ptr<CustomTile> >  TileQueue;
00126     typedef std::list< osg::ref_ptr<CustomTile> >   TileList;
00127     typedef std::vector< osg::ref_ptr<CustomTile> > TileVector;
00128     typedef std::queue< osgTerrain::TileID >        TileIDQueue;
00129 
00130     Threading::ReadWriteMutex _tilesMutex;
00131     TileTable  _tiles;
00132     TileList   _tilesToShutDown;
00133     TileQueue  _tilesToRelease;
00134 
00135     Threading::Mutex _tilesToReleaseMutex;
00136 
00137 public:
00138     void releaseGLObjectsForTiles(osg::State*);
00139 
00140     void registerTile( CustomTile* newTile );
00141 
00142     void getCustomTile( const osgTerrain::TileID&, osg::ref_ptr<CustomTile>& out_tile, bool lock =true );
00143     void getCustomTiles( TileVector& out_tiles );
00144 
00145     const LoadingPolicy& getLoadingPolicy() const;
00146 
00147 private:
00148 
00149     TaskService* createTaskService( const std::string& name, int id, int numThreads );
00150     TaskService* getTaskService( int id );
00151 
00152     void refreshFamily( 
00153         const MapInfo& info, const TileKey& key, Relative* family, bool tileTableLocked );
00154 
00155     int _revision; 
00156     OpenThreads::Mutex _revisionMutex;
00157 
00158     osg::ref_ptr<OSGTileFactory> _tileFactory;
00159     typedef std::map< int, osg::ref_ptr< TaskService > > TaskServiceMap;
00160     TaskServiceMap _taskServices;
00161 
00162     osg::ref_ptr<const Profile> _profile;
00163     OpenThreads::Mutex _taskServiceMutex;
00164 
00165     bool _alwaysUpdate;
00166     int _numLoadingThreads;
00167     int _onDemandDelay; // #frames
00168     void setDelay( unsigned frames );
00169     void decDelay();
00170 
00171         bool _registeredWithReleaseGLCallback;
00172     //TerrainCallbackList _terrainCallbacks;
00173 
00174     LoadingPolicy _loadingPolicy;
00175 
00176     // store a separate map frame for each of the traversal threads
00177     const MapFrame& _update_mapf; // map frame for the main/update traversal thread
00178     const MapFrame& _cull_mapf;   // map frame for the cull traversal thread
00179 
00180     bool _quickReleaseGLObjects;
00181     bool _quickReleaseCallbackInstalled;
00182 
00183     UID _elevationTaskServiceUID;
00184 };
00185 
00186 #endif // OSGEARTH_ENGINE_OSGTERRAIN_CUSTOM_TERRAIN
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines