osgEarth 2.1.1
|
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_TILE 00020 #define OSGEARTH_ENGINE_OSGTERRAIN_CUSTOM_TILE 1 00021 00022 #error 00023 #include "Common" 00024 #include "OSGTileFactory" 00025 #include "TransparentLayer" 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 00037 class TileFactory; //MapEngine; 00038 class CustomTerrain; 00039 00040 using namespace osgEarth; 00041 00042 //------------------------------------------------------------------------ 00043 00044 // Records a tile change request so that we can update tiles piecemeal 00045 struct TileUpdate 00046 { 00047 enum Action { 00048 ADD_IMAGE_LAYER, 00049 REMOVE_IMAGE_LAYER, 00050 MOVE_IMAGE_LAYER, 00051 UPDATE_IMAGE_LAYER, 00052 UPDATE_ALL_IMAGE_LAYERS, 00053 UPDATE_ELEVATION, 00054 UPDATE_ALL 00055 }; 00056 00057 TileUpdate( Action action, UID layerUID =(UID)-1 ) 00058 : _action(action), _layerUID(layerUID) { } 00059 00060 Action getAction() const { return _action; } 00061 00062 UID getLayerUID() const { return _layerUID; } 00063 00064 private: 00065 Action _action; 00066 UID _layerUID; 00067 }; 00068 00069 //------------------------------------------------------------------------ 00070 00071 // Please refer the refreshFamily() method for more info on this structure. 00072 struct Relative 00073 { 00074 int getImageLOD(unsigned int layerID) 00075 { 00076 LayerIDtoLODMap::iterator itr = imageLODs.find(layerID); 00077 if (itr != imageLODs.end()) return itr->second; 00078 return -1; 00079 } 00080 00081 bool expected; 00082 int elevLOD; 00083 typedef std::map<unsigned int, int> LayerIDtoLODMap; 00084 LayerIDtoLODMap imageLODs; 00085 osgTerrain::TileID tileID; 00086 //TileKey key; 00087 00088 enum Direction { 00089 PARENT =0, 00090 WEST =1, 00091 NORTH =2, 00092 EAST =3, 00093 SOUTH =4 00094 }; 00095 00096 Relative() { } // : key(0,0,0,0L) { } 00097 }; 00098 00099 //------------------------------------------------------------------------ 00100 00101 typedef std::map<UID, CustomColorLayer> ColorLayersByUID; 00102 00103 //------------------------------------------------------------------------ 00104 00105 class CustomTile : public osgTerrain::TerrainTile 00106 { 00107 public: 00108 CustomTile( const TileKey& key, GeoLocator* keyLocator, bool quickReleaseGLObjects ); 00109 00110 virtual const char* libraryName() const { return "osgEarth"; } 00111 virtual const char* className() const { return "CustomTile"; } 00112 00113 00115 const TileKey& getKey() const; 00116 00120 void setUseLayerRequests( bool ); 00121 bool getUseLayerRequests() const { return _useLayerRequests; } 00122 00124 void setUsePerLayerUpdates( bool ); 00125 00129 int getTerrainRevision() const; 00130 00134 void setTerrainRevision( int revision ); 00135 00140 bool isInSyncWithTerrain() const; 00141 00145 void incrementTileRevision(); 00146 00150 int getTileRevision() const; 00151 00153 void servicePendingImageRequests( const MapFrame& mapf, int stamp ); 00154 00155 void servicePendingElevationRequests( const MapFrame& mapf, int stamp, bool tileTableLocked ); 00156 00157 // returns TRUE if the tile was modified as a result of a completed request. 00158 bool serviceCompletedRequests( const MapFrame& mapf, bool tileTableLocked ); 00159 00161 void setHasElevationHint( bool hasElevation ); 00162 00164 bool isElevationLayerUpToDate() const; 00165 00167 int getElevationLOD() const; 00168 void setElevationLOD( int lod ); 00169 00171 class CustomTerrain* getCustomTerrain(); 00172 const class CustomTerrain* getCustomTerrain() const; 00173 00175 osg::Vec3dArray* getTerrainMaskGeometry() { return _mask.get(); } 00176 void setTerrainMaskGeometry(osg::Vec3dArray* terrainMask) { _mask = terrainMask; } 00177 00178 bool getHasBeenTraversed() const; 00179 00180 Threading::ReadWriteMutex& getTileLayersMutex(); 00181 00182 bool cancelRequests(); 00183 00184 void updateImagery( ImageLayer* layer, const MapFrame& mapf, OSGTileFactory* factory ); 00185 00186 bool getUseTileGenRequest() const; 00187 00188 Relative* getFamily() { return _family; } 00189 00190 // marks a request to regenerate the tile based on the specified change(s). 00191 void queueTileUpdate( TileUpdate::Action action, int index =-1 ); 00192 00193 void applyImmediateTileUpdate( TileUpdate::Action action, int index =-1 ); 00194 00195 void resetElevationRequests( const MapFrame& mapf ); 00196 00197 float getVerticalScale() const; 00198 void setVerticalScale(float verticalScale); 00199 00200 // we don't use the TerrainTile color layer list, we use our own 00201 void removeCustomColorLayer( UID layerUID, bool writeLock =true ); 00202 bool getCustomColorLayer( UID layerUID, CustomColorLayer& output, bool readLock =true ) const; 00203 void getCustomColorLayers( ColorLayersByUID& out, bool readLock =true ) const; 00204 void setCustomColorLayers( const ColorLayersByUID& in, bool writeLock =true ); 00205 00206 void setCustomColorLayer( const CustomColorLayer& colorLayer, bool writeLock =true ); 00207 00208 00209 public: // OVERRIDES 00210 00211 virtual void traverse( class osg::NodeVisitor& nv ); 00212 00216 virtual void releaseGLObjects(osg::State* = 0) const; 00217 00218 virtual osg::BoundingSphere computeBound() const; 00219 00220 protected: 00221 00222 ~CustomTile(); 00223 00224 00225 private: 00226 00227 int _terrainRevision; 00228 int _tileRevision; 00229 bool _useLayerRequests; 00230 bool _requestsInstalled; 00231 bool _usePerLayerUpdates; 00232 bool _hasElevation; 00233 bool _elevationLayerDirty; 00234 bool _colorLayersDirty; 00235 bool _elevationLayerRequested; 00236 bool _elevationLayerUpToDate; 00237 int _elevationLOD; 00238 bool _hasBeenTraversed; 00239 bool _useTileGenRequest; 00240 bool _sequentialImagery; 00241 bool _quickReleaseGLObjects; 00242 00243 typedef std::queue<TileUpdate> TileUpdateQueue; 00244 TileUpdateQueue _tileUpdates; 00245 00246 TileKey _key; 00247 osg::ref_ptr<GeoLocator> _keyLocator; 00248 TaskRequestList _requests; 00249 osg::ref_ptr<TaskRequest> _elevRequest; 00250 osg::ref_ptr<TaskRequest> _elevPlaceholderRequest; 00251 osg::ref_ptr<TaskRequest> _tileGenRequest; 00252 00253 osg::observer_ptr<osgTerrain::Terrain> _CustomTerrain; 00254 00255 osg::ref_ptr<osg::Vec3dArray> _mask; 00256 00257 Relative _family[5]; 00258 Threading::ReadWriteMutex _tileLayersMutex; 00259 00261 void installRequests( const MapFrame& mapf, int stamp ); 00262 bool readyForNewElevation(); 00263 bool readyForNewImagery(osgEarth::ImageLayer* layer, int currentLOD); 00264 00265 float _verticalScale; 00266 00267 ColorLayersByUID _colorLayers; 00268 00269 public: 00270 friend class CustomTileFrame; 00271 }; 00272 00273 class CustomTileVector : public std::vector< osg::ref_ptr<CustomTile> > { }; 00274 00275 // -------------------------------------------------------------------------- 00276 00277 class CustomTileFrame 00278 { 00279 public: 00280 CustomTileFrame( CustomTile* tile ) 00281 : _tileKey(tile->getKey()) 00282 { 00283 Threading::ScopedReadLock sharedLock( tile->_tileLayersMutex ); 00284 _colorLayers = tile->_colorLayers; 00285 _elevationLayer = tile->getElevationLayer(); 00286 _locator = tile->getLocator(); 00287 _sampleRatio = tile->getTerrain()->getSampleRatio(); 00288 _mask = tile->getTerrainMaskGeometry(); 00289 } 00290 TileKey _tileKey; 00291 ColorLayersByUID _colorLayers; 00292 osg::ref_ptr< osgTerrain::Layer > _elevationLayer; 00293 osg::ref_ptr< osgTerrain::Locator > _locator; 00294 float _sampleRatio; 00295 osg::ref_ptr< osg::Vec3dArray > _mask; 00296 00297 bool getCustomColorLayer( UID layerUID, CustomColorLayer& out ) const { 00298 ColorLayersByUID::const_iterator i = _colorLayers.find( layerUID ); 00299 if ( i != _colorLayers.end() ) { 00300 out = i->second; 00301 return true; 00302 } 00303 return false; 00304 } 00305 }; 00306 00307 // -------------------------------------------------------------------------- 00308 00309 00310 #endif // OSGEARTH_ENGINE_OSGTERRAIN_CUSTOM_TILE