]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilemgr.cxx
99a7cf915813961fe82c1672049c65832d4b8ea8
[flightgear.git] / src / Scenery / tilemgr.cxx
1 // tilemgr.cxx -- routines to handle dynamic management of scenery tiles
2 //
3 // Written by Curtis Olson, started January 1998.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <algorithm>
29 #include <functional>
30
31 #include <boost/lexical_cast.hpp>
32
33 #include <osgViewer/Viewer>
34 #include <osgDB/Registry>
35
36 #include <simgear/constants.h>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/structure/exception.hxx>
39 #include <simgear/scene/model/modellib.hxx>
40 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
41 #include <simgear/scene/tsync/terrasync.hxx>
42 #include <simgear/misc/strutils.hxx>
43 #include <simgear/scene/material/matlib.hxx>
44
45 #include <Main/globals.hxx>
46 #include <Main/fg_props.hxx>
47 #include <Viewer/renderer.hxx>
48 #include <Viewer/splash.hxx>
49 #include <Scripting/NasalSys.hxx>
50 #include <Scripting/NasalModelData.hxx>
51
52 #include "scenery.hxx"
53 #include "SceneryPager.hxx"
54 #include "tilemgr.hxx"
55
56 using flightgear::SceneryPager;
57
58 class FGTileMgr::TileManagerListener : public SGPropertyChangeListener
59 {
60 public:
61     TileManagerListener(FGTileMgr* manager) :
62         _manager(manager),
63         _useVBOsProp(fgGetNode("/sim/rendering/use-vbos", true)),
64         _enableCacheProp(fgGetNode("/sim/tile-cache/enable", true)),
65         _pagedLODMaximumProp(fgGetNode("/sim/rendering/max-paged-lod", true))
66     {
67         _useVBOsProp->addChangeListener(this, true);
68       
69         _enableCacheProp->addChangeListener(this, true);
70         if (_enableCacheProp->getType() == simgear::props::NONE) {
71             _enableCacheProp->setBoolValue(true);
72         }
73       
74         if (_pagedLODMaximumProp->getType() == simgear::props::NONE) {
75             // not set, use OSG default / environment value variable
76             osg::ref_ptr<osgViewer::Viewer> viewer(globals->get_renderer()->getViewer());
77             int current = viewer->getDatabasePager()->getTargetMaximumNumberOfPageLOD();
78             _pagedLODMaximumProp->setIntValue(current);
79         }
80         _pagedLODMaximumProp->addChangeListener(this, true);
81     }
82     
83     ~TileManagerListener()
84     {
85         _useVBOsProp->removeChangeListener(this);
86         _enableCacheProp->removeChangeListener(this);
87         _pagedLODMaximumProp->removeChangeListener(this);
88     }
89     
90     virtual void valueChanged(SGPropertyNode* prop)
91     {
92         if (prop == _useVBOsProp) {
93             bool useVBOs = prop->getBoolValue();
94             _manager->_options->setPluginStringData("SimGear::USE_VBOS",
95                                                 useVBOs ? "ON" : "OFF");
96         } else if (prop == _enableCacheProp) {
97             _manager->_enableCache = prop->getBoolValue();
98         } else if (prop == _pagedLODMaximumProp) {
99           int v = prop->getIntValue();
100           osg::ref_ptr<osgViewer::Viewer> viewer(globals->get_renderer()->getViewer());
101           viewer->getDatabasePager()->setTargetMaximumNumberOfPageLOD(v);
102         }
103     }
104     
105 private:
106     FGTileMgr* _manager;
107     SGPropertyNode_ptr _useVBOsProp,
108       _enableCacheProp,
109       _pagedLODMaximumProp;
110 };
111
112 FGTileMgr::FGTileMgr():
113     state( Start ),
114     last_state( Running ),
115     scheduled_visibility(100.0),
116     _terra_sync(NULL),
117     _listener(NULL),
118     _visibilityMeters(fgGetNode("/environment/visibility-m", true)),
119     _maxTileRangeM(fgGetNode("/sim/rendering/static-lod/bare", true)),
120     _disableNasalHooks(fgGetNode("/sim/temp/disable-scenery-nasal", true)),
121     _scenery_loaded(fgGetNode("/sim/sceneryloaded", true)),
122     _scenery_override(fgGetNode("/sim/sceneryloaded-override", true)),
123     _pager(FGScenery::getPagerSingleton()),
124     _enableCache(true)
125 {
126 }
127
128
129 FGTileMgr::~FGTileMgr()
130 {
131    }
132
133
134 // Initialize the Tile Manager subsystem
135 void FGTileMgr::init()
136 {
137     reinit();
138 }
139
140 void FGTileMgr::shutdown()
141 {
142     delete _listener;
143     _listener = NULL;
144
145     FGScenery* scenery = globals->get_scenery();
146     if (scenery && scenery->get_terrain_branch()) {
147         osg::Group* group = scenery->get_terrain_branch();
148         group->removeChildren(0, group->getNumChildren());
149     }
150     // clear OSG cache
151     osgDB::Registry::instance()->clearObjectCache();
152     state = Start; // need to init again
153 }
154
155 void FGTileMgr::reinit()
156 {
157     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing Tile Manager subsystem." );
158     _terra_sync = static_cast<simgear::SGTerraSync*> (globals->get_subsystem("terrasync"));
159
160   // drops the previous options reference
161     _options = new simgear::SGReaderWriterOptions;
162     _listener = new TileManagerListener(this);
163     
164     materialLibChanged();
165     _options->setPropertyNode(globals->get_props());
166     
167     osgDB::FilePathList &fp = _options->getDatabasePathList();
168     const string_list &sc = globals->get_fg_scenery();
169     fp.clear();
170     std::copy(sc.begin(), sc.end(), back_inserter(fp));
171     _options->setPluginStringData("SimGear::FG_ROOT", globals->get_fg_root());
172     
173     if (_terra_sync) {
174       _options->setPluginStringData("SimGear::TERRASYNC_ROOT", fgGetString("/sim/terrasync/scenery-dir"));
175     }
176     
177     if (!_disableNasalHooks->getBoolValue())
178       _options->setModelData(new FGNasalModelDataProxy);
179
180     _options->setPluginStringData("SimGear::ROUGH_LOD_RANGE", fgGetString("/sim/rendering/static-lod/rough", boost::lexical_cast<string>(SG_OBJECT_RANGE)));
181   
182     if (state != Start)
183     {
184       // protect against multiple scenery reloads and properly reset flags,
185       // otherwise aircraft fall through the ground while reloading scenery
186       if (_scenery_loaded->getBoolValue() == false) {
187         SG_LOG( SG_TERRAIN, SG_INFO, "/sim/sceneryloaded already false, avoiding duplicate re-init of tile manager" );
188         return;
189       }
190     }
191   
192     _scenery_loaded->setBoolValue(false);
193     fgSetDouble("/sim/startup/splash-alpha", 1.0);
194     
195     materialLibChanged();
196
197     // remove all old scenery nodes from scenegraph and clear cache
198     osg::Group* group = globals->get_scenery()->get_terrain_branch();
199     group->removeChildren(0, group->getNumChildren());
200     tile_cache.init();
201     
202     // clear OSG cache, except on initial start-up
203     if (state != Start)
204     {
205         osgDB::Registry::instance()->clearObjectCache();
206     }
207     
208     state = Inited;
209     
210     previous_bucket.make_bad();
211     current_bucket.make_bad();
212     scheduled_visibility = 100.0;
213
214     // force an update now
215     update(0.0);
216 }
217
218 void FGTileMgr::materialLibChanged()
219 {
220     _options->setMaterialLib(globals->get_matlib());
221 }
222
223 /* schedule a tile for loading, keep request for given amount of time.
224  * Returns true if tile is already loaded. */
225 bool FGTileMgr::sched_tile( const SGBucket& b, double priority, bool current_view, double duration)
226 {
227     // see if tile already exists in the cache
228     TileEntry *t = tile_cache.get_tile( b );
229     if (!t)
230     {
231         // create a new entry
232         t = new TileEntry( b );
233         SG_LOG( SG_TERRAIN, SG_INFO, "sched_tile: new tile entry for:" << b );
234
235
236         // insert the tile into the cache, update will generate load request
237         if ( tile_cache.insert_tile( t ) )
238         {
239             // Attach to scene graph
240
241             t->addToSceneGraph(globals->get_scenery()->get_terrain_branch());
242         } else
243         {
244             // insert failed (cache full with no available entries to
245             // delete.)  Try again later
246             delete t;
247             return false;
248         }
249
250         SG_LOG( SG_TERRAIN, SG_DEBUG, "  New tile cache size " << (int)tile_cache.get_size() );
251     }
252
253     // update tile's properties
254     tile_cache.request_tile(t,priority,current_view,duration);
255
256     return t->is_loaded();
257 }
258
259 /* schedule needed buckets for the current view position for loading,
260  * keep request for given amount of time */
261 void FGTileMgr::schedule_needed(const SGBucket& curr_bucket, double vis)
262 {
263     // sanity check (unfortunately needed!)
264     if (!curr_bucket.isValid() )
265     {
266         SG_LOG( SG_TERRAIN, SG_ALERT,
267                 "Attempting to schedule tiles for invalid bucket" );
268         return;
269     }
270
271     double tile_width = curr_bucket.get_width_m();
272     double tile_height = curr_bucket.get_height_m();
273     SG_LOG( SG_TERRAIN, SG_INFO,
274             "scheduling needed tiles for " << curr_bucket
275            << ", tile-width-m:" << tile_width << ", tile-height-m:" << tile_height);
276
277     
278     // cout << "tile width = " << tile_width << "  tile_height = "
279     //      << tile_height << endl;
280
281     double tileRangeM = std::min(vis,_maxTileRangeM->getDoubleValue());
282     int xrange = (int)(tileRangeM / tile_width) + 1;
283     int yrange = (int)(tileRangeM / tile_height) + 1;
284     if ( xrange < 1 ) { xrange = 1; }
285     if ( yrange < 1 ) { yrange = 1; }
286
287     // make the cache twice as large to avoid losing terrain when switching
288     // between aircraft and tower views
289     tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) * 2 );
290     // cout << "xrange = " << xrange << "  yrange = " << yrange << endl;
291     // cout << "max cache size = " << tile_cache.get_max_cache_size()
292     //      << " current cache size = " << tile_cache.get_size() << endl;
293
294     // clear flags of all tiles belonging to the previous view set 
295     tile_cache.clear_current_view();
296
297     // update timestamps, so all tiles scheduled now are *newer* than any tile previously loaded
298     osg::FrameStamp* framestamp
299             = globals->get_renderer()->getViewer()->getFrameStamp();
300     tile_cache.set_current_time(framestamp->getReferenceTime());
301
302     SGBucket b;
303
304     int x, y;
305
306     /* schedule all tiles, use distance-based loading priority,
307      * so tiles are loaded in innermost-to-outermost sequence. */
308     for ( x = -xrange; x <= xrange; ++x )
309     {
310         for ( y = -yrange; y <= yrange; ++y )
311         {
312             SGBucket b = curr_bucket.sibling(x, y);
313             if (!b.isValid()) {
314                 continue;
315             }
316             
317             float priority = (-1.0) * (x*x+y*y);
318             sched_tile( b, priority, true, 0.0 );
319             
320             if (_terra_sync) {
321                 _terra_sync->scheduleTile(b);
322             }
323         }
324     }
325 }
326
327 /**
328  * Update the various queues maintained by the tilemgr (private
329  * internal function, do not call directly.)
330  */
331 void FGTileMgr::update_queues(bool& isDownloadingScenery)
332 {
333     osg::FrameStamp* framestamp
334         = globals->get_renderer()->getViewer()->getFrameStamp();
335     double current_time = framestamp->getReferenceTime();
336     double vis = _visibilityMeters->getDoubleValue();
337     TileEntry *e;
338     int loading=0;
339     int sz=0;
340     
341     tile_cache.set_current_time( current_time );
342     tile_cache.reset_traversal();
343
344     while ( ! tile_cache.at_end() )
345     {
346         e = tile_cache.get_current();
347         if ( e )
348         {
349             // Prepare the ssg nodes corresponding to each tile.
350             // Set the ssg transform and update it's range selector
351             // based on current visibilty
352             e->prep_ssg_node(vis);
353             
354             if (!e->is_loaded()) {
355                 bool nonExpiredOrCurrent = !e->is_expired(current_time) || e->is_current_view();
356                 bool downloading = isTileDirSyncing(e->tileFileName);
357                 isDownloadingScenery |= downloading;
358                 if ( !downloading && nonExpiredOrCurrent) {
359                     // schedule tile for loading with osg pager
360                     _pager->queueRequest(e->tileFileName,
361                                          e->getNode(),
362                                          e->get_priority(),
363                                          framestamp,
364                                          e->getDatabaseRequest(),
365                                          _options.get());
366                     loading++;
367                 }
368             } // of tile not loaded case
369         } else {
370             SG_LOG(SG_TERRAIN, SG_ALERT, "Warning: empty tile in cache!");
371         }
372         tile_cache.next();
373         sz++;
374     }
375
376     int drop_count = sz - tile_cache.get_max_cache_size();
377     bool dropTiles = false;
378     if (_enableCache) {
379       dropTiles = ( drop_count > 0 ) && ((loading==0)||(drop_count > 10));
380     } else {
381       dropTiles = true;
382       drop_count = sz; // no limit on tiles to drop
383     }
384   
385     if (dropTiles)
386     {
387         long drop_index = _enableCache ? tile_cache.get_drop_tile() :
388                                          tile_cache.get_first_expired_tile();
389         while ( drop_index > -1 )
390         {
391             // schedule tile for deletion with osg pager
392             TileEntry* old = tile_cache.get_tile(drop_index);
393             SG_LOG(SG_TERRAIN, SG_DEBUG, "Dropping:" << old->get_tile_bucket());
394
395             tile_cache.clear_entry(drop_index);
396             
397             osg::ref_ptr<osg::Object> subgraph = old->getNode();
398             old->removeFromSceneGraph();
399             delete old;
400             // zeros out subgraph ref_ptr, so subgraph is owned by
401             // the pager and will be deleted in the pager thread.
402             _pager->queueDeleteRequest(subgraph);
403           
404             if (!_enableCache)
405                 drop_index = tile_cache.get_first_expired_tile();
406             // limit tiles dropped to drop_count
407             else if (--drop_count > 0)
408                 drop_index = tile_cache.get_drop_tile();
409             else
410                drop_index = -1;
411         }
412     } // of dropping tiles loop
413 }
414
415 // given the current lon/lat (in degrees), fill in the array of local
416 // chunks.  If the chunk isn't already in the cache, then read it from
417 // disk.
418 void FGTileMgr::update(double)
419 {
420     double vis = _visibilityMeters->getDoubleValue();
421     schedule_tiles_at(globals->get_view_position(), vis);
422
423     bool waitingOnTerrasync = false;
424     update_queues(waitingOnTerrasync);
425
426     // scenery loading check, triggers after each sim (tile manager) reinit
427     if (!_scenery_loaded->getBoolValue())
428     {
429         bool fdmInited = fgGetBool("sim/fdm-initialized");
430         bool positionFinalized = fgGetBool("sim/position-finalized");
431         bool sceneryOverride = _scenery_override->getBoolValue();
432         
433         
434     // we are done if final position is set and the scenery & FDM are done.
435     // scenery-override can ignore the last two, but not position finalization.
436         if (positionFinalized && (sceneryOverride || (isSceneryLoaded() && fdmInited)))
437         {
438             _scenery_loaded->setBoolValue(true);
439             fgSplashProgress("");
440         }
441         else
442         {
443             if (!positionFinalized) {
444                 fgSplashProgress("finalize-position");
445             } else if (waitingOnTerrasync) {
446                 fgSplashProgress("downloading-scenery");
447             } else {
448                 fgSplashProgress("loading-scenery");
449             }
450             
451             // be nice to loader threads while waiting for initial scenery, reduce to 20fps
452             SGTimeStamp::sleepForMSec(50);
453         }
454     }
455 }
456
457 // schedule tiles for the viewer bucket
458 // (FDM/AI/groundcache/... should use "schedule_scenery" instead)
459 void FGTileMgr::schedule_tiles_at(const SGGeod& location, double range_m)
460 {
461     // SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update() for "
462     //         << longitude << " " << latitude );
463
464     current_bucket = SGBucket( location );
465
466     // schedule more tiles when visibility increased considerably
467     // TODO Calculate tile size - instead of using fixed value (5000m)
468     if (range_m - scheduled_visibility > 5000.0)
469         previous_bucket.make_bad();
470
471     // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating tile list for "
472     //         << current_bucket );
473     fgSetInt( "/environment/current-tile-id", current_bucket.gen_index() );
474
475     // do tile load scheduling.
476     // Note that we need keep track of both viewer buckets and fdm buckets.
477     if ( state == Running ) {
478         if (last_state != state)
479         {
480             SG_LOG( SG_TERRAIN, SG_DEBUG, "State == Running" );
481         }
482         if (current_bucket != previous_bucket) {
483             // We've moved to a new bucket, we need to schedule any
484             // needed tiles for loading.
485             SG_LOG( SG_TERRAIN, SG_INFO, "FGTileMgr: at " << location << ", scheduling needed for:" << current_bucket
486                    << ", visibility=" << range_m);
487             scheduled_visibility = range_m;
488             schedule_needed(current_bucket, range_m);
489         }
490         
491         // save bucket
492         previous_bucket = current_bucket;
493     } else if ( state == Start || state == Inited ) {
494         SG_LOG( SG_TERRAIN, SG_DEBUG, "State == Start || Inited" );
495         // do not update bucket yet (position not valid in initial loop)
496         state = Running;
497         previous_bucket.make_bad();
498     }
499     last_state = state;
500 }
501
502 /** Schedules scenery for given position. Load request remains valid for given duration
503  * (duration=0.0 => nothing is loaded).
504  * Used for FDM/AI/groundcache/... requests. Viewer uses "schedule_tiles_at" instead.
505  * Returns true when all tiles for the given position are already loaded, false otherwise.
506  */
507 bool FGTileMgr::schedule_scenery(const SGGeod& position, double range_m, double duration)
508 {
509     // sanity check (unfortunately needed!)
510     if (!position.isValid())
511         return false;
512     const float priority = 0.0;
513     bool available = true;
514
515     SGBucket bucket(position);
516     available = sched_tile( bucket, priority, false, duration );
517   
518     if ((!available)&&(duration==0.0)) {
519         SG_LOG( SG_TERRAIN, SG_DEBUG, "schedule_scenery: Scheduling tile at bucket:" << bucket << " return false" );
520         return false;
521     }
522
523     SGVec3d cartPos = SGVec3d::fromGeod(position);
524
525     // Traverse all tiles required to be there for the given visibility.
526     double tile_width = bucket.get_width_m();
527     double tile_height = bucket.get_height_m();
528     double tile_r = 0.5*sqrt(tile_width*tile_width + tile_height*tile_height);
529     double max_dist = tile_r + range_m;
530     double max_dist2 = max_dist*max_dist;
531     
532     int xrange = (int)fabs(range_m / tile_width) + 1;
533     int yrange = (int)fabs(range_m / tile_height) + 1;
534
535     for ( int x = -xrange; x <= xrange; ++x )
536     {
537         for ( int y = -yrange; y <= yrange; ++y )
538         {
539             // We have already checked for the center tile.
540             if ( x != 0 || y != 0 )
541             {
542                 SGBucket b = bucket.sibling(x, y );
543                 if (!b.isValid()) {
544                     continue;
545                 }
546                 
547                 double distance2 = distSqr(cartPos, SGVec3d::fromGeod(b.get_center()));
548                 // Do not ask if it is just the next tile but way out of range.
549                 if (distance2 <= max_dist2)
550                 {
551                     available &= sched_tile( b, priority, false, duration );
552                     if ((!available)&&(duration==0.0))
553                         return false;
554                 }
555             }
556         }
557     }
558
559     return available;
560 }
561
562 // Returns true if tiles around current view position have been loaded
563 bool FGTileMgr::isSceneryLoaded()
564 {
565     double range_m = 100.0;
566     if (scheduled_visibility < range_m)
567         range_m = scheduled_visibility;
568
569     return schedule_scenery(globals->get_view_position(), range_m, 0.0);
570 }
571
572 bool FGTileMgr::isTileDirSyncing(const std::string& tileFileName) const
573 {
574     if (!_terra_sync) {
575         return false;
576     }
577     
578     std::string nameWithoutExtension = tileFileName.substr(0, tileFileName.size() - 4);
579     long int bucketIndex = simgear::strutils::to_int(nameWithoutExtension);
580     SGBucket bucket(bucketIndex);
581     
582     return _terra_sync->isTileDirPending(bucket.gen_base_path());
583 }
584