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 <Objects/obj.hxx>
46 #ifndef FG_OLD_WEATHER
47 # include <WeatherCM/FGLocalWeatherDatabase.h>
49 # include <Weather/weather.hxx>
52 #include "newcache.hxx"
53 #include "scenery.hxx"
54 #include "tilemgr.hxx"
56 #define TEST_LAST_HIT_CACHE
58 extern ssgRoot *scene;
59 extern ssgBranch *terrain;
60 extern ssgBranch *ground;
63 FGTileMgr global_tile_mgr;
67 SGLockedQueue<FGTileEntry *> FGTileMgr::attach_queue;
68 SGLockedQueue<FGDeferredModel *> FGTileMgr::model_queue;
70 queue<FGTileEntry *> FGTileMgr::attach_queue;
71 queue<FGDeferredModel *> FGTileMgr::model_queue;
72 #endif // ENABLE_THREADS
76 FGTileMgr::FGTileMgr():
85 FGTileMgr::~FGTileMgr() {
89 // Initialize the Tile Manager subsystem
90 int FGTileMgr::init() {
91 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing Tile Manager subsystem." );
97 // instead it's just a lot easier to let any pending work flush
98 // through, rather than trying to arrest the queue and nuke all
99 // the various work at all the various stages and get everything
100 // cleaned up properly.
102 while ( ! attach_queue.empty() ) {
106 while ( ! model_queue.empty() ) {
107 #ifdef ENABLE_THREADS
108 FGDeferredModel* dm = model_queue.pop();
110 FGDeferredModel* dm = model_queue.front();
122 previous_bucket.make_bad();
123 current_bucket.make_bad();
125 longitude = latitude = -1000.0;
126 last_longitude = last_latitude = -1000.0;
132 // schedule a tile for loading
133 void FGTileMgr::sched_tile( const SGBucket& b ) {
134 // see if tile already exists in the cache
135 FGTileEntry *t = tile_cache.get_tile( b );
138 // create a new entry
139 FGTileEntry *e = new FGTileEntry( b );
141 // insert the tile into the cache
142 if ( tile_cache.insert_tile( e ) ) {
143 // Schedule tile for loading
146 // insert failed (cache full with no available entries to
147 // delete.) Try again later
154 // schedule a needed buckets for loading
155 void FGTileMgr::schedule_needed() {
156 #ifndef FG_OLD_WEATHER
157 if ( WeatherDatabase != NULL ) {
158 vis = WeatherDatabase->getWeatherVisibility();
163 vis = current_weather.get_visibility();
165 // cout << "visibility = " << vis << endl;
167 double tile_width = current_bucket.get_width_m();
168 double tile_height = current_bucket.get_height_m();
169 // cout << "tile width = " << tile_width << " tile_height = "
170 // << tile_height !<< endl;
172 xrange = (int)(vis / tile_width) + 1;
173 yrange = (int)(vis / tile_height) + 1;
174 if ( xrange < 1 ) { xrange = 1; }
175 if ( yrange < 1 ) { yrange = 1; }
176 // cout << "xrange = " << xrange << " yrange = " << yrange << endl;
178 tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
182 // schedule center tile first so it can be loaded first
183 b = sgBucketOffset( longitude, latitude, 0, 0 );
188 // schedule next ring of 8 tiles
189 for ( x = -1; x <= 1; ++x ) {
190 for ( y = -1; y <= 1; ++y ) {
191 if ( x != 0 || y != 0 ) {
192 b = sgBucketOffset( longitude, latitude, x, y );
198 // schedule remaining tiles
199 for ( x = -xrange; x <= xrange; ++x ) {
200 for ( y = -yrange; y <= yrange; ++y ) {
201 if ( x < -1 || x > 1 || y < -1 || y > 1 ) {
202 SGBucket b = sgBucketOffset( longitude, latitude, x, y );
210 void FGTileMgr::initialize_queue()
212 // First time through or we have teleported, initialize the
213 // system and load all relavant tiles
215 SG_LOG( SG_TERRAIN, SG_INFO, "Updating Tile list for " << current_bucket );
216 // cout << "tile cache size = " << tile_cache.get_size() << endl;
218 // wipe/initialize tile cache
219 // tile_cache.init();
220 previous_bucket.make_bad();
222 // build the local area list and schedule tiles for loading
224 // start with the center tile and work out in concentric
229 // do we really want to lose this? CLO
231 // Now force a load of the center tile and inner ring so we
232 // have something to see in our first frame.
234 for ( i = 0; i < 9; ++i ) {
235 if ( load_queue.size() ) {
236 SG_LOG( SG_TERRAIN, SG_DEBUG,
237 "Load queue not empty, loading a tile" );
239 SGBucket pending = load_queue.front();
240 load_queue.pop_front();
241 load_tile( pending );
248 // given the current lon/lat (in degrees), fill in the array of local
249 // chunks. If the chunk isn't already in the cache, then read it from
251 int FGTileMgr::update( double lon, double lat ) {
252 SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update()" );
254 // FGInterface *f = current_aircraft.fdm_state;
256 // lonlat for this update
257 // longitude = f->get_Longitude() * SGD_RADIANS_TO_DEGREES;
258 // latitude = f->get_Latitude() * SGD_RADIANS_TO_DEGREES;
261 // SG_LOG( SG_TERRAIN, SG_DEBUG, "lon "<< lonlat[LON] <<
262 // " lat " << lonlat[LAT] );
264 current_bucket.set_bucket( longitude, latitude );
265 // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating Tile list for " << current_bucket );
267 if ( tile_cache.exists( current_bucket ) ) {
268 current_tile = tile_cache.get_tile( current_bucket );
269 scenery.next_center = current_tile->center;
271 SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
274 if ( state == Running ) {
275 if ( !(current_bucket == previous_bucket) ) {
276 // We've moved to a new bucket, we need to schedule any
277 // needed tiles for loading.
280 } else if ( state == Start || state == Inited ) {
285 // load the next tile in the load queue (or authorize the next
286 // load in the case of the threaded tile pager)
289 // load the next model in the load queue. Currently this must
290 // happen in the render thread because model loading can trigger
291 // texture loading which involves use of the opengl api.
292 if ( !model_queue.empty() ) {
293 cout << "loading next model ..." << endl;
294 // load the next tile in the queue
295 #ifdef ENABLE_THREADS
296 FGDeferredModel* dm = model_queue.pop();
298 FGDeferredModel* dm = model_queue.front();
302 ssgTexturePath( (char *)(dm->get_texture_path().c_str()) );
304 = ssgLoad( (char *)(dm->get_model_path().c_str()) );
305 if ( obj_model != NULL ) {
306 dm->get_obj_trans()->addKid( obj_model );
308 dm->get_tile()->dec_pending_models();
313 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
315 previous_bucket = current_bucket;
316 last_longitude = longitude;
317 last_latitude = latitude;
319 // activate loader thread one out of every 5 frames
320 if ( counter_hack == 0 ) {
321 // Notify the tile loader that it can load another tile
324 if ( !attach_queue.empty() ) {
325 #ifdef ENABLE_THREADS
326 FGTileEntry* e = attach_queue.pop();
328 FGTileEntry* e = attach_queue.front();
331 e->add_ssg_nodes( terrain, ground );
332 // cout << "Adding ssg nodes for "
335 counter_hack = (counter_hack + 1) % 5;
337 sgdSetVec3( sc, scenery.center[0], scenery.center[1], scenery.center[2] );
339 if ( scenery.center == Point3D(0.0) ) {
341 cout << "initializing scenery current elevation ... " << endl;
342 sgdVec3 tmp_abs_view_pos;
344 Point3D geod_pos = Point3D( longitude * SGD_DEGREES_TO_RADIANS,
345 latitude * SGD_DEGREES_TO_RADIANS,
347 Point3D tmp = sgGeodToCart( geod_pos );
348 scenery.center = tmp;
349 sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
351 // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
355 if ( fgCurrentElev(tmp_abs_view_pos, sc, &hit_list,
356 &tmp_elev, &scenery.cur_radius, scenery.cur_normal) )
358 scenery.cur_elev = tmp_elev;
360 scenery.cur_elev = 0.0;
362 cout << "result = " << scenery.cur_elev << endl;
364 // cout << "abs view pos = " << current_view.abs_view_pos
365 // << " view pos = " << current_view.view_pos << endl;
367 if ( fgCurrentElev(globals->get_current_view()->get_abs_view_pos(),
369 &tmp_elev, &scenery.cur_radius, scenery.cur_normal) )
371 scenery.cur_elev = tmp_elev;
373 scenery.cur_elev = 0.0;
381 void FGTileMgr::prep_ssg_nodes() {
384 #ifndef FG_OLD_WEATHER
385 if ( WeatherDatabase ) {
386 vis = WeatherDatabase->getWeatherVisibility();
391 vis = current_weather.get_visibility();
393 // cout << "visibility = " << vis << endl;
395 // traverse the potentially viewable tile list and update range
396 // selector and transform
399 tile_cache.reset_traversal();
401 while ( ! tile_cache.at_end() ) {
402 // cout << "processing a tile" << endl;
403 if ( (e = tile_cache.get_current()) ) {
404 e->prep_ssg_node( scenery.center, vis);
406 cout << "warning ... empty tile in cache" << endl;