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_TILE 00020 #define OSGEARTH_ENGINE_OSGTERRAIN_TILE 1 00021 00022 #include "Common" 00023 #include "OSGTileFactory" 00024 #include "TransparentLayer" 00025 #include "CustomTerrainTechnique" 00026 #include <osgEarth/Locators> 00027 #include <osgEarth/Profile> 00028 #include <osgEarth/TerrainOptions> 00029 #include <osgEarth/Map> 00030 #include <osgEarth/ThreadingUtils> 00031 #include "Terrain" 00032 #include "Tile" 00033 #include <list> 00034 #include <queue> 00035 #include <iterator> 00036 00037 class TileFactory; 00038 class TerrainTechnique; 00039 00040 using namespace osgEarth; 00041 00042 typedef std::map<UID, CustomColorLayer> ColorLayersByUID; 00043 00044 //------------------------------------------------------------------------ 00045 00046 // Records a tile change request so that we can update tiles piecemeal 00047 class TileUpdate 00048 { 00049 public: 00050 enum Action { 00051 ADD_IMAGE_LAYER, 00052 REMOVE_IMAGE_LAYER, 00053 MOVE_IMAGE_LAYER, 00054 UPDATE_IMAGE_LAYER, 00055 UPDATE_ALL_IMAGE_LAYERS, 00056 UPDATE_ELEVATION, 00057 UPDATE_ALL 00058 }; 00059 00060 TileUpdate( Action action, UID layerUID =(UID)-1 ) 00061 : _action(action), _layerUID(layerUID) { } 00062 00063 Action getAction() const { return _action; } 00064 00065 UID getLayerUID() const { return _layerUID; } 00066 00067 private: 00068 Action _action; 00069 UID _layerUID; 00070 }; 00071 00072 //------------------------------------------------------------------------ 00073 00074 class Tile : public osg::Node 00075 { 00076 public: 00077 Tile( const TileKey& key, GeoLocator* keyLocator, bool quickReleaseGLObjects ); 00078 00080 const TileKey& getKey() const { return _key; } 00081 00083 class Terrain* getTerrain() { return _terrain.get(); } 00084 const class Terrain* getTerrain() const { return _terrain.get(); } 00085 00086 // attaches this tile to a terrain and registers it. 00087 void attachToTerrain( Terrain* terrain ); 00088 00090 void init(); 00091 00093 const MaskLayerVector& getTerrainMasks() { return _masks; } 00094 void setTerrainMasks(const MaskLayerVector& terrainMask) { _masks.clear(); std::copy( terrainMask.begin(), terrainMask.end(), std::back_inserter(_masks) ); } 00095 00097 bool getHasBeenTraversed() const { return _hasBeenTraversed; } 00098 00100 Threading::ReadWriteMutex& getTileLayersMutex() { return _tileLayersMutex; } 00101 00102 // marks a request to regenerate the tile based on the specified change(s). 00103 virtual void queueTileUpdate( TileUpdate::Action action, int index =-1 ); 00104 00105 void applyImmediateTileUpdate( TileUpdate::Action action, int index =-1 ); 00106 00107 virtual bool cancelActiveTasks() { return true; } 00108 00110 float getVerticalScale() const { return _verticalScale; } 00111 void setVerticalScale( float verticalScale ); 00112 00113 osgTerrain::Locator* getLocator() const { return _locator.get(); } 00114 00115 const osgTerrain::TileID& getTileId() const { return _tileId; } 00116 00117 bool getDirty() const { return _dirty; } 00118 void setDirty( bool value ) { _dirty = value; } 00119 00120 void setTerrainTechnique( TerrainTechnique* value ); 00121 TerrainTechnique* getTerrainTechnique() const { return _tech.get(); } 00122 00124 void removeCustomColorLayer( UID layerUID, bool writeLock =true ); 00125 bool getCustomColorLayer( UID layerUID, CustomColorLayer& output, bool readLock =true ) const; 00126 void getCustomColorLayers( ColorLayersByUID& out, bool readLock =true ) const; 00127 void setCustomColorLayers( const ColorLayersByUID& in, bool writeLock =true ); 00128 void setCustomColorLayer( const CustomColorLayer& colorLayer, bool writeLock =true ); 00129 00130 osgTerrain::HeightFieldLayer* getElevationLayer() const { return _elevationLayer.get(); } 00131 void setElevationLayer( osgTerrain::HeightFieldLayer* value ) { _elevationLayer = value; } 00132 00133 public: // OVERRIDES 00134 00135 virtual void traverse( class osg::NodeVisitor& nv ); 00136 00140 virtual void releaseGLObjects(osg::State* = 0) const; 00141 00142 virtual osg::BoundingSphere computeBound() const; 00143 00144 protected: 00145 00146 virtual ~Tile(); 00147 00148 bool _hasBeenTraversed; 00149 bool _quickReleaseGLObjects; 00150 bool _parentTileSet; 00151 bool _dirty; 00152 00153 TileKey _key; 00154 osgTerrain::TileID _tileId; 00155 osg::ref_ptr<GeoLocator> _locator; 00156 osg::observer_ptr<Terrain> _terrain; 00157 MaskLayerVector _masks; 00158 00159 Threading::ReadWriteMutex _tileLayersMutex; 00160 ColorLayersByUID _colorLayers; 00161 float _verticalScale; 00162 00163 osg::ref_ptr<osgTerrain::HeightFieldLayer> _elevationLayer; 00164 osg::ref_ptr<TerrainTechnique> _tech; 00165 00166 00167 public: 00168 friend class TileFrame; 00169 }; 00170 00171 class TileVector : public std::vector< osg::ref_ptr<Tile> > { }; 00172 00173 // -------------------------------------------------------------------------- 00174 00178 class TileFrame 00179 { 00180 public: 00181 TileFrame( Tile* tile ); 00182 00183 TileKey _tileKey; 00184 ColorLayersByUID _colorLayers; 00185 osg::ref_ptr< osgTerrain::HeightFieldLayer > _elevationLayer; 00186 osg::ref_ptr< osgTerrain::Locator > _locator; 00187 float _sampleRatio; 00188 MaskLayerVector _masks; 00189 00190 // convenience funciton to pull out a layer by its UID. 00191 bool getCustomColorLayer( UID layerUID, CustomColorLayer& out ) const { 00192 ColorLayersByUID::const_iterator i = _colorLayers.find( layerUID ); 00193 if ( i != _colorLayers.end() ) { 00194 out = i->second; 00195 return true; 00196 } 00197 return false; 00198 } 00199 }; 00200 00201 #endif // OSGEARTH_ENGINE_OSGTERRAIN_CUSTOM_TILE