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/fg_props.hxx>
45 #include <Main/viewer.hxx>
46 #include <Objects/obj.hxx>
48 #include "newcache.hxx"
49 #include "scenery.hxx"
50 #include "tilemgr.hxx"
52 #define TEST_LAST_HIT_CACHE
55 FGTileMgr global_tile_mgr;
59 SGLockedQueue<FGTileEntry *> FGTileMgr::attach_queue;
60 SGLockedQueue<FGDeferredModel *> FGTileMgr::model_queue;
62 queue<FGTileEntry *> FGTileMgr::attach_queue;
63 queue<FGDeferredModel *> FGTileMgr::model_queue;
64 #endif // ENABLE_THREADS
65 queue<FGTileEntry *> FGTileMgr::delete_queue;
69 FGTileMgr::FGTileMgr():
80 FGTileMgr::~FGTileMgr() {
84 // Initialize the Tile Manager subsystem
85 int FGTileMgr::init() {
86 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing Tile Manager subsystem." );
92 // instead it's just a lot easier to let any pending work flush
93 // through, rather than trying to arrest the queue and nuke all
94 // the various work at all the various stages and get everything
95 // cleaned up properly.
97 while ( ! attach_queue.empty() ) {
101 while ( ! model_queue.empty() ) {
102 #ifdef ENABLE_THREADS
103 FGDeferredModel* dm = model_queue.pop();
105 FGDeferredModel* dm = model_queue.front();
117 previous_bucket.make_bad();
118 current_bucket.make_bad();
120 longitude = latitude = -1000.0;
121 last_longitude = last_latitude = -1000.0;
127 // schedule a tile for loading
128 void FGTileMgr::sched_tile( const SGBucket& b ) {
129 // see if tile already exists in the cache
130 FGTileEntry *t = tile_cache.get_tile( b );
133 // make space in the cache
134 while ( (int)tile_cache.get_size() > max_cache_size ) {
135 long index = tile_cache.get_oldest_tile();
137 FGTileEntry *old = tile_cache.get_tile( index );
138 old->disconnect_ssg_nodes();
139 delete_queue.push( old );
140 tile_cache.clear_entry( index );
142 // nothing to free ?!? forge ahead
147 // create a new entry
148 FGTileEntry *e = new FGTileEntry( b );
150 // insert the tile into the cache
151 if ( tile_cache.insert_tile( e ) ) {
152 // Schedule tile for loading
155 // insert failed (cache full with no available entries to
156 // delete.) Try again later
163 // schedule a needed buckets for loading
164 void FGTileMgr::schedule_needed( double vis, SGBucket curr_bucket) {
165 // sanity check (unfortunately needed!)
166 if ( longitude < -180.0 || longitude > 180.0
167 || latitude < -90.0 || latitude > 90.0 )
169 SG_LOG( SG_TERRAIN, SG_ALERT,
170 "Attempting to schedule tiles for bogus latitude and" );
171 SG_LOG( SG_TERRAIN, SG_ALERT,
172 "longitude. This is a FATAL error. Exiting!" );
176 SG_LOG( SG_TERRAIN, SG_INFO,
177 "scheduling needed tiles for " << longitude << " " << latitude );
179 // vis = fgGetDouble("/environment/visibility-m");
181 double tile_width = curr_bucket.get_width_m();
182 double tile_height = curr_bucket.get_height_m();
183 // cout << "tile width = " << tile_width << " tile_height = "
184 // << tile_height !<< endl;
186 xrange = (int)(vis / tile_width) + 1;
187 yrange = (int)(vis / tile_height) + 1;
188 if ( xrange < 1 ) { xrange = 1; }
189 if ( yrange < 1 ) { yrange = 1; }
190 // cout << "xrange = " << xrange << " yrange = " << yrange << endl;
192 // note * 2 at end doubles cache size (for fdm and viewer)
193 max_cache_size = (2*xrange + 2) * (2*yrange + 2) * 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
242 double visibility_meters = fgGetDouble("/environment/visibility-m");
243 schedule_needed(visibility_meters, current_bucket);
245 // do we really want to lose this? CLO
247 // Now force a load of the center tile and inner ring so we
248 // have something to see in our first frame.
250 for ( i = 0; i < 9; ++i ) {
251 if ( load_queue.size() ) {
252 SG_LOG( SG_TERRAIN, SG_DEBUG,
253 "Load queue not empty, loading a tile" );
255 SGBucket pending = load_queue.front();
256 load_queue.pop_front();
257 load_tile( pending );
264 // given the current lon/lat (in degrees), fill in the array of local
265 // chunks. If the chunk isn't already in the cache, then read it from
267 int FGTileMgr::update( double lon, double lat, double visibility_meters ) {
268 sgdVec3 abs_pos_vector;
269 sgdCopyVec3(abs_pos_vector , globals->get_current_view()->get_absolute_view_pos());
270 return update( lon, lat, visibility_meters, abs_pos_vector,
271 current_bucket, previous_bucket,
272 globals->get_scenery()->get_center() );
276 int FGTileMgr::update( double lon, double lat, double visibility_meters,
277 sgdVec3 abs_pos_vector, SGBucket p_current,
278 SGBucket p_previous, Point3D center ) {
279 // SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update() for "
280 // << lon << " " << lat );
284 current_bucket = p_current;
285 previous_bucket = p_previous;
287 // SG_LOG( SG_TERRAIN, SG_DEBUG, "lon "<< lonlat[LON] <<
288 // " lat " << lonlat[LAT] );
290 // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating Tile list for " << current_bucket );
292 setCurrentTile( longitude, latitude);
294 // do tile load scheduling.
295 // Note that we need keep track of both viewer buckets and fdm buckets.
296 if ( state == Running ) {
297 SG_LOG( SG_TERRAIN, SG_DEBUG, "State == Running" );
298 if (!(current_bucket == previous_bucket )) {
299 // We've moved to a new bucket, we need to schedule any
300 // needed tiles for loading.
301 schedule_needed(visibility_meters, current_bucket);
303 } else if ( state == Start || state == Inited ) {
304 SG_LOG( SG_TERRAIN, SG_INFO, "State == Start || Inited" );
308 // load the next tile in the load queue (or authorize the next
309 // load in the case of the threaded tile pager)
314 // load the next model in the load queue. Currently this must
315 // happen in the render thread because model loading can trigger
316 // texture loading which involves use of the opengl api.
317 if ( !model_queue.empty() ) {
318 // cout << "loading next model ..." << endl;
319 // load the next tile in the queue
320 #ifdef ENABLE_THREADS
321 FGDeferredModel* dm = model_queue.pop();
323 FGDeferredModel* dm = model_queue.front();
327 ssgTexturePath( (char *)(dm->get_texture_path().c_str()) );
329 = ssgLoad( (char *)(dm->get_model_path().c_str()) );
330 if ( obj_model != NULL ) {
331 dm->get_obj_trans()->addKid( obj_model );
333 dm->get_tile()->dec_pending_models();
338 // cout << "current elevation (ssg) == " << scenery.get_cur_elev() << endl;
342 previous_bucket = current_bucket;
344 // activate loader thread one out of every 5 frames
345 if ( counter_hack == 0 ) {
346 // Notify the tile loader that it can load another tile
349 counter_hack = (counter_hack + 1) % 5;
352 if ( !attach_queue.empty() ) {
353 #ifdef ENABLE_THREADS
354 FGTileEntry* e = attach_queue.pop();
356 FGTileEntry* e = attach_queue.front();
359 e->add_ssg_nodes( globals->get_scenery()->get_terrain_branch(),
360 globals->get_scenery()->get_gnd_lights_branch(),
361 globals->get_scenery()->get_rwy_lights_branch() );
362 // cout << "Adding ssg nodes for "
365 // cout << "delete queue = " << delete_queue.size() << endl;
366 if ( !delete_queue.empty() ) {
367 FGTileEntry* e = delete_queue.front();
368 if ( e->free_tile() ) {
374 // no reason to update this if we haven't moved...
375 if ( longitude != last_longitude || latitude != last_latitude ) {
376 // update current elevation...
377 if (updateCurrentElevAtPos(abs_pos_vector, center)) {
378 last_longitude = longitude;
379 last_latitude = latitude;
390 // timer event driven call to scheduler for the purpose of refreshing the tile timestamps
391 void FGTileMgr::refresh_view_timestamps() {
392 SG_LOG( SG_TERRAIN, SG_INFO,
393 "Refreshing timestamps for " << current_bucket.get_center_lon()
394 << " " << current_bucket.get_center_lat() );
395 schedule_needed(fgGetDouble("/environment/visibility-m"), current_bucket);
399 // check and set current tile and scenery center...
400 void FGTileMgr::setCurrentTile(double longitude, double latitude) {
402 // check tile cache entry...
403 current_bucket.set_bucket( longitude, latitude );
404 if ( tile_cache.exists( current_bucket ) ) {
405 current_tile = tile_cache.get_tile( current_bucket );
406 globals->get_scenery()->set_next_center( current_tile->center );
408 SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
409 globals->get_scenery()->set_next_center( Point3D(0.0) );
414 int FGTileMgr::updateCurrentElevAtPos(sgdVec3 abs_pos_vector, Point3D center) {
423 // overridden with actual values if a terrain intersection is
425 double hit_elev = -9999.0;
426 double hit_radius = 0.0;
427 sgdVec3 hit_normal = { 0.0, 0.0, 0.0 };
430 if ( fabs(sc[0]) > 1.0 || fabs(sc[1]) > 1.0 || fabs(sc[2]) > 1.0 ) {
431 // scenery center has been properly defined so any hit
432 // should be valid (and not just luck)
433 hit = fgCurrentElev(abs_pos_vector,
435 current_tile->get_terra_transform(),
443 globals->get_scenery()->set_cur_elev( hit_elev );
444 globals->get_scenery()->set_cur_radius( hit_radius );
445 globals->get_scenery()->set_cur_normal( hit_normal );
447 globals->get_scenery()->set_cur_elev( -9999.0 );
448 globals->get_scenery()->set_cur_radius( 0.0 );
449 globals->get_scenery()->set_cur_normal( hit_normal );
455 void FGTileMgr::prep_ssg_nodes(float vis) {
457 // traverse the potentially viewable tile list and update range
458 // selector and transform
460 // just setup and call new function...
463 sgCopyVec3( up, globals->get_current_view()->get_world_up() );
466 center = globals->get_scenery()->get_center();
467 prep_ssg_nodes( vis, up, center );
472 void FGTileMgr::prep_ssg_nodes(float vis, sgVec3 up, Point3D center) {
474 // traverse the potentially viewable tile list and update range
475 // selector and transform
478 tile_cache.reset_traversal();
480 while ( ! tile_cache.at_end() ) {
481 // cout << "processing a tile" << endl;
482 if ( (e = tile_cache.get_current()) ) {
483 e->prep_ssg_node( center, up, vis);
485 SG_LOG(SG_INPUT, SG_ALERT, "warning ... empty tile in cache");