| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGTERRAIN_TERRAINTILE |
|---|
| 15 | #define OSGTERRAIN_TERRAINTILE 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Group> |
|---|
| 18 | #include <osg/CoordinateSystemNode> |
|---|
| 19 | |
|---|
| 20 | #include <osgDB/ReaderWriter> |
|---|
| 21 | |
|---|
| 22 | #include <osgTerrain/TerrainTechnique> |
|---|
| 23 | #include <osgTerrain/Layer> |
|---|
| 24 | #include <osgTerrain/Locator> |
|---|
| 25 | |
|---|
| 26 | namespace osgTerrain { |
|---|
| 27 | |
|---|
| 28 | class Terrain; |
|---|
| 29 | |
|---|
| 30 | class OSGTERRAIN_EXPORT TileID |
|---|
| 31 | { |
|---|
| 32 | public: |
|---|
| 33 | |
|---|
| 34 | TileID(); |
|---|
| 35 | |
|---|
| 36 | TileID(int in_level, int in_x, int in_y); |
|---|
| 37 | |
|---|
| 38 | bool operator == (const TileID& rhs) const |
|---|
| 39 | { |
|---|
| 40 | return (level==rhs.level) && (x==rhs.x) && (y==rhs.y); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | bool operator != (const TileID& rhs) const |
|---|
| 44 | { |
|---|
| 45 | return (level!=rhs.level) || (x!=rhs.x) || (y!=rhs.y); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | bool operator < (const TileID& rhs) const |
|---|
| 49 | { |
|---|
| 50 | if (level<rhs.level) return true; |
|---|
| 51 | if (level>rhs.level) return false; |
|---|
| 52 | if (x<rhs.x) return true; |
|---|
| 53 | if (x>rhs.x) return false; |
|---|
| 54 | return y<rhs.y; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | bool valid() const { return level>=0; } |
|---|
| 58 | |
|---|
| 59 | int level; |
|---|
| 60 | int x; |
|---|
| 61 | int y; |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | /** Terrain provides a framework for loosely coupling height field data with height rendering algorithms. |
|---|
| 66 | * This allows TerrainTechnique's to be plugged in at runtime.*/ |
|---|
| 67 | class OSGTERRAIN_EXPORT TerrainTile : public osg::Group |
|---|
| 68 | { |
|---|
| 69 | public: |
|---|
| 70 | |
|---|
| 71 | TerrainTile(); |
|---|
| 72 | |
|---|
| 73 | /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ |
|---|
| 74 | TerrainTile(const TerrainTile&,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY); |
|---|
| 75 | |
|---|
| 76 | META_Node(osgTerrain, TerrainTile); |
|---|
| 77 | |
|---|
| 78 | virtual void traverse(osg::NodeVisitor& nv); |
|---|
| 79 | |
|---|
| 80 | /** Call init on any attached TerrainTechnique.*/ |
|---|
| 81 | void init(int dirtyMask, bool assumeMultiThreaded); |
|---|
| 82 | |
|---|
| 83 | /** Set the Terrain that this Terrain tile is a member of.*/ |
|---|
| 84 | void setTerrain(Terrain* ts); |
|---|
| 85 | |
|---|
| 86 | /** Get the Terrain that this Terrain tile is a member of.*/ |
|---|
| 87 | Terrain* getTerrain() { return _terrain; } |
|---|
| 88 | |
|---|
| 89 | /** Get the const Terrain that this Terrain tile is a member of.*/ |
|---|
| 90 | const Terrain* getTerrain() const { return _terrain; } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | /** Set the TileID (layer, x,y) of the TerrainTile. |
|---|
| 94 | * The TileID is used so it can be located by its neighbours |
|---|
| 95 | * via the enclosing Terrain node that manages a map of TileID to TerraiTiles.*/ |
|---|
| 96 | void setTileID(const TileID& tileID); |
|---|
| 97 | |
|---|
| 98 | /** Get the TileID (layer, x,y) of the TerrainTile.*/ |
|---|
| 99 | const TileID& getTileID() const { return _tileID; } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | /** Set the TerrainTechnique*/ |
|---|
| 103 | void setTerrainTechnique(TerrainTechnique* terrainTechnique); |
|---|
| 104 | |
|---|
| 105 | /** Get the TerrainTechnique*/ |
|---|
| 106 | TerrainTechnique* getTerrainTechnique() { return _terrainTechnique.get(); } |
|---|
| 107 | |
|---|
| 108 | /** Get the const TerrainTechnique*/ |
|---|
| 109 | const TerrainTechnique* getTerrainTechnique() const { return _terrainTechnique.get(); } |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | /** Set the coordinate frame locator of the terrain node. |
|---|
| 113 | * The locator takes non-dimensional s,t coordinates into the X,Y,Z world coords and back.*/ |
|---|
| 114 | void setLocator(Locator* locator) { _locator = locator; } |
|---|
| 115 | |
|---|
| 116 | /** Get the coordinate frame locator of the terrain node.*/ |
|---|
| 117 | Locator* getLocator() { return _locator.get(); } |
|---|
| 118 | |
|---|
| 119 | /** Get the const coordinate frame locator of the terrain node.*/ |
|---|
| 120 | const Locator* getLocator() const { return _locator.get(); } |
|---|
| 121 | |
|---|
| 122 | /** Set the layer to use to define the elevations of the terrain.*/ |
|---|
| 123 | void setElevationLayer(Layer* layer); |
|---|
| 124 | |
|---|
| 125 | /** Get the layer to use to define the elevations of the terrain.*/ |
|---|
| 126 | Layer* getElevationLayer() { return _elevationLayer.get(); } |
|---|
| 127 | |
|---|
| 128 | /** Get the const layer to use to define the elevations of the terrain.*/ |
|---|
| 129 | const Layer* getElevationLayer() const { return _elevationLayer.get(); } |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | /** Set a color layer with specified layer number.*/ |
|---|
| 133 | void setColorLayer(unsigned int i, Layer* layer); |
|---|
| 134 | |
|---|
| 135 | /** Get color layer with specified layer number.*/ |
|---|
| 136 | Layer* getColorLayer(unsigned int i) { return i<_colorLayers.size() ? _colorLayers[i].get() : 0; } |
|---|
| 137 | |
|---|
| 138 | /** Set const color layer with specified layer number.*/ |
|---|
| 139 | const Layer* getColorLayer(unsigned int i) const { return i<_colorLayers.size() ? _colorLayers[i].get() : 0; } |
|---|
| 140 | |
|---|
| 141 | /** Get the number of colour layers.*/ |
|---|
| 142 | unsigned int getNumColorLayers() const { return _colorLayers.size(); } |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | /** Set hint to whether the TerrainTechnique should create per vertex normals for lighting purposes.*/ |
|---|
| 146 | void setRequiresNormals(bool flag) { _requiresNormals = flag; } |
|---|
| 147 | |
|---|
| 148 | /** Get whether the TerrainTechnique should create per vertex normals for lighting purposes.*/ |
|---|
| 149 | bool getRequiresNormals() const { return _requiresNormals; } |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | /** Set the hint to whether the TerrainTechnique should treat the invalid Layer entries that at are neigbours to valid entries with the default value.*/ |
|---|
| 153 | void setTreatBoundariesToValidDataAsDefaultValue(bool flag) { _treatBoundariesToValidDataAsDefaultValue = flag; } |
|---|
| 154 | |
|---|
| 155 | /** Get whether the TeatBoundariesToValidDataAsDefaultValue hint.*/ |
|---|
| 156 | bool getTreatBoundariesToValidDataAsDefaultValue() const { return _treatBoundariesToValidDataAsDefaultValue; } |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | enum BlendingPolicy |
|---|
| 160 | { |
|---|
| 161 | INHERIT, /** Default - check for the any BlendingPolicy set on the enclosing osgTerrain::Terrain node, and if it's also INHERIT then assume ENABLE_BLENDING_WHEN_ALPHA_PRESENT. */ |
|---|
| 162 | DO_NOT_SET_BLENDING, |
|---|
| 163 | ENABLE_BLENDING, |
|---|
| 164 | ENABLE_BLENDING_WHEN_ALPHA_PRESENT /** check colour layers for alpha value and if present enable blending. */ |
|---|
| 165 | }; |
|---|
| 166 | |
|---|
| 167 | /** Set the policy to use when deciding whether to enable/disable blending and use of transparent bin.*/ |
|---|
| 168 | void setBlendingPolicy(BlendingPolicy policy) { _blendingPolicy = policy; } |
|---|
| 169 | |
|---|
| 170 | /** Get the policy to use when deciding whether to enable/disable blending and use of transparent bin.*/ |
|---|
| 171 | BlendingPolicy getBlendingPolicy() const { return _blendingPolicy; } |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | enum DirtyMask |
|---|
| 175 | { |
|---|
| 176 | NOT_DIRTY = 0, |
|---|
| 177 | IMAGERY_DIRTY = 1<<0, |
|---|
| 178 | ELEVATION_DIRTY = 1<<1, |
|---|
| 179 | LEFT_EDGE_DIRTY = 1<<2, |
|---|
| 180 | RIGHT_EDGE_DIRTY = 1<<3, |
|---|
| 181 | TOP_EDGE_DIRTY = 1<<4, |
|---|
| 182 | TOP_LEFT_CORNER_DIRTY = 1<<5, |
|---|
| 183 | TOP_RIGHT_CORNER_DIRTY = 1<<6, |
|---|
| 184 | BOTTOM_EDGE_DIRTY = 1<<7, |
|---|
| 185 | BOTTOM_LEFT_CORNER_DIRTY = 1<<8, |
|---|
| 186 | BOTTOM_RIGHT_CORNER_DIRTY = 1<<9, |
|---|
| 187 | EDGES_DIRTY = LEFT_EDGE_DIRTY | RIGHT_EDGE_DIRTY | TOP_EDGE_DIRTY | BOTTOM_EDGE_DIRTY | |
|---|
| 188 | TOP_LEFT_CORNER_DIRTY | TOP_RIGHT_CORNER_DIRTY | BOTTOM_LEFT_CORNER_DIRTY | BOTTOM_RIGHT_CORNER_DIRTY, |
|---|
| 189 | ALL_DIRTY = IMAGERY_DIRTY | ELEVATION_DIRTY | EDGES_DIRTY |
|---|
| 190 | }; |
|---|
| 191 | |
|---|
| 192 | /** Set the dirty flag on/off.*/ |
|---|
| 193 | void setDirty(bool dirty) { setDirtyMask(dirty ? ALL_DIRTY : NOT_DIRTY); } |
|---|
| 194 | |
|---|
| 195 | /** return true if the any of the DirtyMask are set.*/ |
|---|
| 196 | int getDirty() const { return _dirtyMask!=NOT_DIRTY; } |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | /** Set the dirty flag on/off.*/ |
|---|
| 200 | void setDirtyMask(int dirtyMask); |
|---|
| 201 | |
|---|
| 202 | /** return true if the tile is dirty and needs to be updated,*/ |
|---|
| 203 | int getDirtyMask() const { return _dirtyMask; } |
|---|
| 204 | |
|---|
| 205 | |
|---|
| 206 | /** Compute the bounding volume of the terrain by computing the union of the bounding volumes of all layers.*/ |
|---|
| 207 | virtual osg::BoundingSphere computeBound() const; |
|---|
| 208 | |
|---|
| 209 | /** Callback for post processing loaded TerrainTile, and for filling in missing elements such as external external imagery.*/ |
|---|
| 210 | struct TileLoadedCallback : public osg::Referenced |
|---|
| 211 | { |
|---|
| 212 | virtual bool deferExternalLayerLoading() const = 0; |
|---|
| 213 | virtual void loaded(osgTerrain::TerrainTile* tile, const osgDB::ReaderWriter::Options* options) const = 0; |
|---|
| 214 | }; |
|---|
| 215 | |
|---|
| 216 | static void setTileLoadedCallback(TileLoadedCallback* lc); |
|---|
| 217 | static osg::ref_ptr<TileLoadedCallback>& getTileLoadedCallback(); |
|---|
| 218 | |
|---|
| 219 | /** If State is non-zero, this function releases any associated OpenGL objects for |
|---|
| 220 | * the specified graphics context. Otherwise, releases OpenGL objects |
|---|
| 221 | * for all graphics contexts. */ |
|---|
| 222 | virtual void releaseGLObjects(osg::State* = 0) const; |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | protected: |
|---|
| 226 | |
|---|
| 227 | virtual ~TerrainTile(); |
|---|
| 228 | |
|---|
| 229 | typedef std::vector< osg::ref_ptr<Layer> > Layers; |
|---|
| 230 | |
|---|
| 231 | friend class Terrain; |
|---|
| 232 | |
|---|
| 233 | Terrain* _terrain; |
|---|
| 234 | |
|---|
| 235 | int _dirtyMask; |
|---|
| 236 | bool _hasBeenTraversal; |
|---|
| 237 | |
|---|
| 238 | TileID _tileID; |
|---|
| 239 | |
|---|
| 240 | osg::ref_ptr<TerrainTechnique> _terrainTechnique; |
|---|
| 241 | osg::ref_ptr<Locator> _locator; |
|---|
| 242 | |
|---|
| 243 | osg::ref_ptr<Layer> _elevationLayer; |
|---|
| 244 | |
|---|
| 245 | Layers _colorLayers; |
|---|
| 246 | |
|---|
| 247 | bool _requiresNormals; |
|---|
| 248 | bool _treatBoundariesToValidDataAsDefaultValue; |
|---|
| 249 | BlendingPolicy _blendingPolicy; |
|---|
| 250 | }; |
|---|
| 251 | |
|---|
| 252 | /** Helper callback for managing optional sets of layers, that loading of is deffered to this callback, |
|---|
| 253 | * with this callback working out which layers to load, and how to create fallback versions of the layers. |
|---|
| 254 | */ |
|---|
| 255 | class OSGTERRAIN_EXPORT WhiteListTileLoadedCallback : public TerrainTile::TileLoadedCallback |
|---|
| 256 | { |
|---|
| 257 | public: |
|---|
| 258 | |
|---|
| 259 | WhiteListTileLoadedCallback(); |
|---|
| 260 | |
|---|
| 261 | void allow(const std::string& setname) { _setWhiteList.insert(setname); } |
|---|
| 262 | |
|---|
| 263 | void setMinimumNumOfLayers(unsigned int numLayers) { _minumumNumberOfLayers = numLayers; } |
|---|
| 264 | unsigned int getMinimumNumOfLayers() const { return _minumumNumberOfLayers; } |
|---|
| 265 | |
|---|
| 266 | void setReplaceSwitchLayer(bool replaceSwitchLayer) { _replaceSwitchLayer = replaceSwitchLayer; } |
|---|
| 267 | bool getReplaceSwitchLayer() const { return _replaceSwitchLayer; } |
|---|
| 268 | |
|---|
| 269 | void setAllowAll(bool allowAll) { _allowAll = allowAll; } |
|---|
| 270 | bool getAllowAll() const { return _allowAll; } |
|---|
| 271 | |
|---|
| 272 | bool layerAcceptable(const std::string& setname) const; |
|---|
| 273 | bool readImageLayer(osgTerrain::ImageLayer* imageLayer, const osgDB::ReaderWriter::Options* options) const; |
|---|
| 274 | |
|---|
| 275 | virtual bool deferExternalLayerLoading() const; |
|---|
| 276 | |
|---|
| 277 | virtual void loaded(osgTerrain::TerrainTile* tile, const osgDB::ReaderWriter::Options* options) const; |
|---|
| 278 | |
|---|
| 279 | protected: |
|---|
| 280 | |
|---|
| 281 | virtual ~WhiteListTileLoadedCallback(); |
|---|
| 282 | |
|---|
| 283 | typedef std::set<std::string> SetWhiteList; |
|---|
| 284 | SetWhiteList _setWhiteList; |
|---|
| 285 | unsigned int _minumumNumberOfLayers; |
|---|
| 286 | bool _replaceSwitchLayer; |
|---|
| 287 | bool _allowAll; |
|---|
| 288 | |
|---|
| 289 | }; |
|---|
| 290 | |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | #endif |
|---|