1 // tilemgr.cxx -- routines to handle dynamic management of scenery tiles
3 // Written by Curtis Olson, started January 1998.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include <simgear/constants.h>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/math/point3d.hxx>
39 #include <simgear/math/polar3d.hxx>
40 #include <simgear/math/sg_geodesy.hxx>
41 #include <simgear/math/vector.hxx>
43 #include <Main/globals.hxx>
44 #include <Main/viewer.hxx>
45 #include <Objects/obj.hxx>
47 #ifndef FG_NEW_ENVIRONMENT
48 # include <WeatherCM/FGLocalWeatherDatabase.h>
50 # include <Environment/environment.hxx>
53 #include "newcache.hxx"
54 #include "scenery.hxx"
55 #include "tilemgr.hxx"
57 #define TEST_LAST_HIT_CACHE
59 extern ssgRoot *scene;
60 extern ssgBranch *terrain;
61 extern ssgBranch *ground;
64 FGTileMgr global_tile_mgr;
68 SGLockedQueue<FGTileEntry *> FGTileMgr::attach_queue;
69 SGLockedQueue<FGDeferredModel *> FGTileMgr::model_queue;
71 queue<FGTileEntry *> FGTileMgr::attach_queue;
72 queue<FGDeferredModel *> FGTileMgr::model_queue;
73 #endif // ENABLE_THREADS
77 FGTileMgr::FGTileMgr():
86 FGTileMgr::~FGTileMgr() {
90 // Initialize the Tile Manager subsystem
91 int FGTileMgr::init() {
92 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing Tile Manager subsystem." );
98 // instead it's just a lot easier to let any pending work flush
99 // through, rather than trying to arrest the queue and nuke all
100 // the various work at all the various stages and get everything
101 // cleaned up properly.
103 while ( ! attach_queue.empty() ) {
107 while ( ! model_queue.empty() ) {
108 #ifdef ENABLE_THREADS
109 FGDeferredModel* dm = model_queue.pop();
111 FGDeferredModel* dm = model_queue.front();
123 previous_bucket.make_bad();
124 current_bucket.make_bad();
126 longitude = latitude = -1000.0;
127 last_longitude = last_latitude = -1000.0;
133 // schedule a tile for loading
134 void FGTileMgr::sched_tile( const SGBucket& b ) {
135 // see if tile already exists in the cache
136 FGTileEntry *t = tile_cache.get_tile( b );
139 // create a new entry
140 FGTileEntry *e = new FGTileEntry( b );
142 // insert the tile into the cache
143 if ( tile_cache.insert_tile( e ) ) {
144 // Schedule tile for loading
147 // insert failed (cache full with no available entries to
148 // delete.) Try again later
155 // schedule a needed buckets for loading
156 void FGTileMgr::schedule_needed() {
157 // sanity check (unfortunately needed!)
158 if ( longitude < -180.0 || longitude > 180.0
159 || latitude < -90.0 || latitude > 90.0 )
161 SG_LOG( SG_TERRAIN, SG_ALERT,
162 "Attempting to schedule tiles for bogus latitude and" );
163 SG_LOG( SG_TERRAIN, SG_ALERT,
164 "longitude. This is a FATAL error. Exiting!" );
168 SG_LOG( SG_TERRAIN, SG_INFO,
169 "scheduling needed tiles for " << longitude << " " << latitude );
171 #ifndef FG_NEW_ENVIRONMENT
172 if ( WeatherDatabase != NULL ) {
173 vis = WeatherDatabase->getWeatherVisibility();
178 vis = current_environment.get_visibility_m();
180 // cout << "visibility = " << vis << endl;
182 double tile_width = current_bucket.get_width_m();
183 double tile_height = current_bucket.get_height_m();
184 // cout << "tile width = " << tile_width << " tile_height = "
185 // << tile_height !<< endl;
187 xrange = (int)(vis / tile_width) + 1;
188 yrange = (int)(vis / tile_height) + 1;
189 if ( xrange < 1 ) { xrange = 1; }
190 if ( yrange < 1 ) { yrange = 1; }
191 // cout << "xrange = " << xrange << " yrange = " << yrange << endl;
193 tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
197 // schedule center tile first so it can be loaded first
198 b = sgBucketOffset( longitude, latitude, 0, 0 );
203 // schedule next ring of 8 tiles
204 for ( x = -1; x <= 1; ++x ) {
205 for ( y = -1; y <= 1; ++y ) {
206 if ( x != 0 || y != 0 ) {
207 b = sgBucketOffset( longitude, latitude, x, y );
213 // schedule remaining tiles
214 for ( x = -xrange; x <= xrange; ++x ) {
215 for ( y = -yrange; y <= yrange; ++y ) {
216 if ( x < -1 || x > 1 || y < -1 || y > 1 ) {
217 SGBucket b = sgBucketOffset( longitude, latitude, x, y );
225 void FGTileMgr::initialize_queue()
227 // First time through or we have teleported, initialize the
228 // system and load all relavant tiles
230 SG_LOG( SG_TERRAIN, SG_INFO, "Updating Tile list for " << current_bucket );
231 // cout << "tile cache size = " << tile_cache.get_size() << endl;
233 // wipe/initialize tile cache
234 // tile_cache.init();
235 previous_bucket.make_bad();
237 // build the local area list and schedule tiles for loading
239 // start with the center tile and work out in concentric
244 // do we really want to lose this? CLO
246 // Now force a load of the center tile and inner ring so we
247 // have something to see in our first frame.
249 for ( i = 0; i < 9; ++i ) {
250 if ( load_queue.size() ) {
251 SG_LOG( SG_TERRAIN, SG_DEBUG,
252 "Load queue not empty, loading a tile" );
254 SGBucket pending = load_queue.front();
255 load_queue.pop_front();
256 load_tile( pending );
263 // given the current lon/lat (in degrees), fill in the array of local
264 // chunks. If the chunk isn't already in the cache, then read it from
266 int FGTileMgr::update( double lon, double lat ) {
267 // SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update() for "
268 // << lon << " " << lat );
272 // SG_LOG( SG_TERRAIN, SG_DEBUG, "lon "<< lonlat[LON] <<
273 // " lat " << lonlat[LAT] );
275 current_bucket.set_bucket( longitude, latitude );
276 // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating Tile list for " << current_bucket );
278 if ( tile_cache.exists( current_bucket ) ) {
279 current_tile = tile_cache.get_tile( current_bucket );
280 scenery.set_next_center( current_tile->center );
282 SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
285 if ( state == Running ) {
286 SG_LOG( SG_TERRAIN, SG_DEBUG, "State == Running" );
287 if ( !(current_bucket == previous_bucket) ) {
288 // We've moved to a new bucket, we need to schedule any
289 // needed tiles for loading.
292 } else if ( state == Start || state == Inited ) {
293 SG_LOG( SG_TERRAIN, SG_INFO, "State == Start || Inited" );
297 // load the next tile in the load queue (or authorize the next
298 // load in the case of the threaded tile pager)
302 // load the next model in the load queue. Currently this must
303 // happen in the render thread because model loading can trigger
304 // texture loading which involves use of the opengl api.
305 if ( !model_queue.empty() ) {
306 cout << "loading next model ..." << endl;
307 // load the next tile in the queue
308 #ifdef ENABLE_THREADS
309 FGDeferredModel* dm = model_queue.pop();
311 FGDeferredModel* dm = model_queue.front();
315 ssgTexturePath( (char *)(dm->get_texture_path().c_str()) );
317 = ssgLoad( (char *)(dm->get_model_path().c_str()) );
318 if ( obj_model != NULL ) {
319 dm->get_obj_trans()->addKid( obj_model );
321 dm->get_tile()->dec_pending_models();
326 // cout << "current elevation (ssg) == " << scenery.get_cur_elev() << endl;
328 previous_bucket = current_bucket;
329 last_longitude = longitude;
330 last_latitude = latitude;
332 // activate loader thread one out of every 5 frames
333 if ( counter_hack == 0 ) {
334 // Notify the tile loader that it can load another tile
339 counter_hack = (counter_hack + 1) % 5;
341 if ( !attach_queue.empty() ) {
342 #ifdef ENABLE_THREADS
343 FGTileEntry* e = attach_queue.pop();
345 FGTileEntry* e = attach_queue.front();
348 e->add_ssg_nodes( terrain, ground );
349 // cout << "Adding ssg nodes for "
354 scenery.get_center()[0],
355 scenery.get_center()[1],
356 scenery.get_center()[2] );
359 if ( scenery.center == Point3D(0.0) ) {
361 cout << "initializing scenery current elevation ... " << endl;
362 sgdVec3 tmp_abs_view_pos;
364 Point3D geod_pos = Point3D( longitude * SGD_DEGREES_TO_RADIANS,
365 latitude * SGD_DEGREES_TO_RADIANS,
367 Point3D tmp = sgGeodToCart( geod_pos );
368 scenery.center = tmp;
369 sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
371 // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
375 if ( fgCurrentElev(tmp_abs_view_pos, sc, &hit_list,
376 &tmp_elev, &scenery.cur_radius, scenery.cur_normal) )
378 scenery.set_cur_elev( tmp_elev );
380 scenery.set_cur_elev( 0.0 );
382 cout << "result = " << scenery.get_cur_elev() << endl;
385 // cout << "abs view pos = " << current_view.abs_view_pos
386 // << " view pos = " << current_view.view_pos << endl;
390 if ( fgCurrentElev(globals->get_current_view()->get_abs_view_pos(),
392 &tmp_elev, &tmp_radius, tmp_normal) )
394 scenery.set_cur_elev( tmp_elev );
395 scenery.set_cur_radius( tmp_radius );
396 scenery.set_cur_normal( tmp_normal );
398 scenery.set_cur_elev( -9999.0 );
400 // cout << "Current elevation = " << scenery.get_cur_elev() << endl;
409 void FGTileMgr::prep_ssg_nodes() {
412 #ifndef FG_NEW_ENVIRONMENT
413 if ( WeatherDatabase ) {
414 vis = WeatherDatabase->getWeatherVisibility();
419 vis = current_environment.get_visibility_m();
421 // cout << "visibility = " << vis << endl;
423 // traverse the potentially viewable tile list and update range
424 // selector and transform
427 tile_cache.reset_traversal();
429 while ( ! tile_cache.at_end() ) {
430 // cout << "processing a tile" << endl;
431 if ( (e = tile_cache.get_current()) ) {
432 e->prep_ssg_node( scenery.get_center(), vis);
434 cout << "warning ... empty tile in cache" << endl;