root/OpenSceneGraph/trunk/src/osgTerrain/Terrain.cpp @ 13041

Revision 13041, 5.6 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
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#include <osgTerrain/Terrain>
15#include <osgUtil/UpdateVisitor>
16
17#include <iterator>
18
19#include <OpenThreads/ScopedLock>
20
21using namespace osg;
22using namespace osgTerrain;
23
24Terrain::Terrain():
25    _sampleRatio(1.0),
26    _verticalScale(1.0),
27    _blendingPolicy(TerrainTile::INHERIT),
28    _equalizeBoundaries(false)
29{
30    setNumChildrenRequiringUpdateTraversal(1);
31}
32
33Terrain::Terrain(const Terrain& ts, const osg::CopyOp& copyop):
34    osg::CoordinateSystemNode(ts,copyop),
35    _sampleRatio(ts._sampleRatio),
36    _verticalScale(ts._verticalScale),
37    _blendingPolicy(ts._blendingPolicy),
38    _equalizeBoundaries(ts._equalizeBoundaries),
39    _terrainTechnique(ts._terrainTechnique)
40{
41    setNumChildrenRequiringUpdateTraversal(getNumChildrenRequiringUpdateTraversal()+1);
42}
43
44
45Terrain::~Terrain()
46{
47    OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_mutex);
48
49    for(TerrainTileSet::iterator itr = _terrainTileSet.begin();
50        itr != _terrainTileSet.end();
51        ++itr)
52    {
53        const_cast<TerrainTile*>(*itr)->_terrain = 0;
54    }
55
56    _terrainTileSet.clear();
57    _terrainTileMap.clear();
58}
59
60void Terrain::setSampleRatio(float ratio)
61{
62    if (_sampleRatio == ratio) return;
63    _sampleRatio  = ratio;
64    dirtyRegisteredTiles();
65}
66
67void Terrain::setVerticalScale(float scale)
68{
69    if (_verticalScale == scale) return;
70    _verticalScale = scale;
71    dirtyRegisteredTiles();
72}
73
74void Terrain::setEqualizeBoundaries(bool equalizeBoundaries)
75{
76  if(_equalizeBoundaries == equalizeBoundaries) return;
77  _equalizeBoundaries = equalizeBoundaries;
78  dirtyRegisteredTiles();
79}
80
81void Terrain::setBlendingPolicy(TerrainTile::BlendingPolicy policy)
82{
83    if (_blendingPolicy == policy) return;
84    _blendingPolicy = policy;
85    dirtyRegisteredTiles();
86}
87
88void Terrain::traverse(osg::NodeVisitor& nv)
89{
90    if (nv.getVisitorType()==osg::NodeVisitor::UPDATE_VISITOR)
91    {
92        // need to check if any TerrainTechniques need to have their update called on them.
93        osgUtil::UpdateVisitor* uv = dynamic_cast<osgUtil::UpdateVisitor*>(&nv);
94        if (uv)
95        {
96            typedef std::list< osg::ref_ptr<TerrainTile> >  TerrainTileList;
97            TerrainTileList tiles;
98            {
99                OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_mutex);
100                std::copy(_updateTerrainTileSet.begin(), _updateTerrainTileSet.end(), std::back_inserter(tiles));
101                _updateTerrainTileSet.clear();
102            }
103
104            for(TerrainTileList::iterator itr = tiles.begin();
105                itr != tiles.end();
106                ++itr)
107            {
108                TerrainTile* tile = itr->get();
109                tile->traverse(nv);
110            }
111        }
112    }
113
114    Group::traverse(nv);
115}
116
117void Terrain::updateTerrainTileOnNextFrame(TerrainTile* terrainTile)
118{
119    OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_mutex);
120    _updateTerrainTileSet.insert(terrainTile);
121}
122
123
124TerrainTile* Terrain::getTile(const TileID& tileID)
125{
126    OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_mutex);
127
128    // OSG_NOTICE<<"Terrain::getTile("<<tileID.level<<", "<<tileID.x<<", "<<tileID.y<<")"<<std::endl;
129
130    TerrainTileMap::iterator itr = _terrainTileMap.find(tileID);
131    if (itr == _terrainTileMap.end()) return 0;
132
133    return itr->second;
134}
135
136const TerrainTile* Terrain::getTile(const TileID& tileID) const
137{
138    OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_mutex);
139
140    TerrainTileMap::const_iterator itr = _terrainTileMap.find(tileID);
141    if (itr == _terrainTileMap.end()) return 0;
142
143    return itr->second;
144}
145
146void Terrain::dirtyRegisteredTiles(int dirtyMask)
147{
148    OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_mutex);
149    for(TerrainTileSet::iterator itr = _terrainTileSet.begin();
150        itr != _terrainTileSet.end();
151        ++itr)
152    {
153        (const_cast<TerrainTile*>(*itr))->setDirtyMask(dirtyMask);
154    }
155}
156
157static unsigned int s_maxNumTiles = 0;
158void Terrain::registerTerrainTile(TerrainTile* tile)
159{
160    if (!tile) return;
161
162    OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_mutex);
163
164    if (tile->getTileID().valid())
165    {
166        _terrainTileMap[tile->getTileID()] = tile;
167    }
168
169    _terrainTileSet.insert(tile);
170
171    if (_terrainTileSet.size() > s_maxNumTiles) s_maxNumTiles = _terrainTileSet.size();
172
173    // OSG_NOTICE<<"Terrain::registerTerrainTile "<<tile<<" total number of tile "<<_terrainTileSet.size()<<" max = "<<s_maxNumTiles<<std::endl;
174    // OSG_NOTICE<<"  tileID("<<tile->getTileID().level<<", "<<tile->getTileID().x<<", "<<tile->getTileID().y<<")"<<std::endl;
175}
176
177void Terrain::unregisterTerrainTile(TerrainTile* tile)
178{
179    if (!tile) return;
180
181    OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_mutex);
182
183    if (tile->getTileID().valid())
184    {
185        _terrainTileMap.erase(tile->getTileID());
186    }
187
188    _terrainTileSet.erase(tile);
189    _updateTerrainTileSet.erase(tile);
190
191    // OSG_NOTICE<<"Terrain::unregisterTerrainTile "<<tile<<" total number of tile "<<_terrainTileSet.size()<<" max = "<<s_maxNumTiles<<std::endl;
192}
Note: See TracBrowser for help on using the browser.