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_STANDARD_TERRAIN 00020 #define OSGEARTH_ENGINE_OSGTERRAIN_STANDARD_TERRAIN 1 00021 00022 #include "Common" 00023 #include "Tile" 00024 #include "CustomTerrainTechnique" 00025 #include "OSGTileFactory" 00026 #include <osgEarth/Locators> 00027 #include <osgEarth/Profile> 00028 #include <osgEarth/TerrainOptions> 00029 #include <osgEarth/Map> 00030 #include <osgEarth/ThreadingUtils> 00031 #include <list> 00032 #include <queue> 00033 #include <vector> 00034 00035 class TileFactory; 00036 00037 using namespace osgEarth; 00038 00041 class Terrain : public osg::Group //osgTerrain::Terrain 00042 { 00043 public: 00044 Terrain( 00045 const MapFrame& update_mapf, 00046 const MapFrame& cull_mapf, 00047 OSGTileFactory* factory, 00048 bool quickReleaseGLObjects ); 00049 00050 virtual const char* libraryName() const { return "osgEarth"; } 00051 00052 virtual const char* className() const { return "Terrain"; } 00053 00054 public: 00055 00056 OSGTileFactory* getTileFactory() { return _tileFactory.get(); } 00057 00058 bool getQuickReleaseGLObjects() const { return _quickReleaseGLObjects; } 00059 00060 const MapFrame& getUpdateThreadMapFrame() { return _update_mapf; } 00061 00062 const MapFrame& getCullThreadMapFrame() { return _cull_mapf; } 00063 00064 virtual Tile* createTile( const TileKey& key, GeoLocator* locator ) const; 00065 00066 void setTechniquePrototype( TerrainTechnique* tech ); 00067 00068 TerrainTechnique* cloneTechnique() const; 00069 00070 void setSampleRatio( float value ); 00071 00072 float getSampleRatio() const { return _sampleRatio; } 00073 00074 void setVerticalScale( float value ); 00075 00076 float getVerticalScale() const { return _verticalScale; } 00077 00078 virtual void traverse( osg::NodeVisitor &nv ); 00079 00080 protected: 00081 00082 virtual ~Terrain(); 00083 00084 // subclass can override to notify of running tasks 00085 virtual unsigned getNumActiveTasks() const { return 0; } 00086 00087 // subclass can override this to perform addition UPDATE traversal operations 00088 virtual void updateTraversal( osg::NodeVisitor& nv ) { } 00089 00090 typedef std::map< osgTerrain::TileID, osg::ref_ptr<Tile> > TileTable; 00091 00092 typedef std::queue< osg::ref_ptr<Tile> > TileQueue; 00093 typedef std::list< osg::ref_ptr<Tile> > TileList; 00094 typedef std::vector< osg::ref_ptr<Tile> > TileVector; 00095 typedef std::queue< osgTerrain::TileID > TileIDQueue; 00096 00097 Threading::ReadWriteMutex _tilesMutex; 00098 TileTable _tiles; 00099 TileList _tilesToShutDown; 00100 TileVector _tilesToRelease; 00101 Threading::Mutex _tilesToReleaseMutex; 00102 00103 float _sampleRatio; 00104 float _verticalScale; 00105 00106 public: 00107 00108 void releaseGLObjectsForTiles( osg::State* state ); 00109 00110 void registerTile( Tile* newTile ); 00111 00113 void getTiles( TileVector& out_tiles ); 00114 00116 template<typename T> 00117 void getTile(const osgTerrain::TileID& id, osg::ref_ptr<T>& out_tile, bool lock =true ) const { 00118 if ( lock ) { 00119 Threading::ScopedReadLock lock( const_cast<Terrain*>(this)->_tilesMutex ); 00120 TileTable::const_iterator i = _tiles.find( id ); 00121 out_tile = i != _tiles.end()? static_cast<T*>(i->second.get()) : 0L; 00122 } 00123 else { 00124 TileTable::const_iterator i = _tiles.find( id ); 00125 out_tile = i != _tiles.end()? static_cast<T*>(i->second.get()) : 0L; 00126 } 00127 } 00128 00129 protected: 00130 00131 osg::ref_ptr<OSGTileFactory> _tileFactory; 00132 osg::ref_ptr<const Profile> _profile; 00133 00134 bool _alwaysUpdate; 00135 int _onDemandDelay; // #frames 00136 00137 void setDelay( unsigned frames ); 00138 void decDelay(); 00139 00140 bool _registeredWithReleaseGLCallback; 00141 00142 // store a separate map frame for each of the traversal threads 00143 const MapFrame& _update_mapf; // map frame for the main/update traversal thread 00144 const MapFrame& _cull_mapf; // map frame for the cull traversal thread 00145 00146 bool _quickReleaseGLObjects; 00147 bool _quickReleaseCallbackInstalled; 00148 00149 osg::ref_ptr<TerrainTechnique> _techPrototype; 00150 }; 00151 00152 #endif // OSGEARTH_ENGINE_OSGTERRAIN_STANDARD_TERRAIN