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 tile_cache.insert_tile( e );
144 // Schedule tile for loading
150 // schedule a needed buckets for loading
151 void FGTileMgr::schedule_needed() {
152 #ifndef FG_OLD_WEATHER
153 if ( WeatherDatabase != NULL ) {
154 vis = WeatherDatabase->getWeatherVisibility();
159 vis = current_weather.get_visibility();
161 // cout << "visibility = " << vis << endl;
163 double tile_width = current_bucket.get_width_m();
164 double tile_height = current_bucket.get_height_m();
165 // cout << "tile width = " << tile_width << " tile_height = "
166 // << tile_height !<< endl;
168 xrange = (int)(vis / tile_width) + 1;
169 yrange = (int)(vis / tile_height) + 1;
170 if ( xrange < 1 ) { xrange = 1; }
171 if ( yrange < 1 ) { yrange = 1; }
172 // cout << "xrange = " << xrange << " yrange = " << yrange << endl;
174 tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
178 // schedule center tile first so it can be loaded first
179 b = sgBucketOffset( longitude, latitude, 0, 0 );
184 // schedule next ring of 8 tiles
185 for ( x = -1; x <= 1; ++x ) {
186 for ( y = -1; y <= 1; ++y ) {
187 if ( x != 0 || y != 0 ) {
188 b = sgBucketOffset( longitude, latitude, x, y );
194 // schedule remaining tiles
195 for ( x = -xrange; x <= xrange; ++x ) {
196 for ( y = -yrange; y <= yrange; ++y ) {
197 if ( x < -1 || x > 1 || y < -1 || y > 1 ) {
198 SGBucket b = sgBucketOffset( longitude, latitude, x, y );
206 void FGTileMgr::initialize_queue()
208 // First time through or we have teleported, initialize the
209 // system and load all relavant tiles
211 SG_LOG( SG_TERRAIN, SG_INFO, "Updating Tile list for " << current_bucket );
212 // cout << "tile cache size = " << tile_cache.get_size() << endl;
214 // wipe/initialize tile cache
215 // tile_cache.init();
216 previous_bucket.make_bad();
218 // build the local area list and schedule tiles for loading
220 // start with the center tile and work out in concentric
225 // do we really want to lose this? CLO
227 // Now force a load of the center tile and inner ring so we
228 // have something to see in our first frame.
230 for ( i = 0; i < 9; ++i ) {
231 if ( load_queue.size() ) {
232 SG_LOG( SG_TERRAIN, SG_DEBUG,
233 "Load queue not empty, loading a tile" );
235 SGBucket pending = load_queue.front();
236 load_queue.pop_front();
237 load_tile( pending );
244 // given the current lon/lat (in degrees), fill in the array of local
245 // chunks. If the chunk isn't already in the cache, then read it from
247 int FGTileMgr::update( double lon, double lat ) {
248 SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update()" );
250 // FGInterface *f = current_aircraft.fdm_state;
252 // lonlat for this update
253 // longitude = f->get_Longitude() * SGD_RADIANS_TO_DEGREES;
254 // latitude = f->get_Latitude() * SGD_RADIANS_TO_DEGREES;
257 // SG_LOG( SG_TERRAIN, SG_DEBUG, "lon "<< lonlat[LON] <<
258 // " lat " << lonlat[LAT] );
260 current_bucket.set_bucket( longitude, latitude );
261 // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating Tile list for " << current_bucket );
263 if ( tile_cache.exists( current_bucket ) ) {
264 current_tile = tile_cache.get_tile( current_bucket );
265 scenery.next_center = current_tile->center;
267 SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
270 if ( state == Running ) {
271 if ( !(current_bucket == previous_bucket) ) {
272 // We've moved to a new bucket, we need to schedule any
273 // needed tiles for loading.
276 } else if ( state == Start || state == Inited ) {
281 // load the next tile in the load queue (or authorize the next
282 // load in the case of the threaded tile pager)
285 // load the next model in the load queue. Currently this must
286 // happen in the render thread because model loading can trigger
287 // texture loading which involves use of the opengl api.
288 if ( !model_queue.empty() ) {
289 cout << "loading next model ..." << endl;
290 // load the next tile in the queue
291 #ifdef ENABLE_THREADS
292 FGDeferredModel* dm = model_queue.pop();
294 FGDeferredModel* dm = model_queue.front();
298 ssgTexturePath( (char *)(dm->get_texture_path().c_str()) );
300 = ssgLoad( (char *)(dm->get_model_path().c_str()) );
301 if ( obj_model != NULL ) {
302 dm->get_obj_trans()->addKid( obj_model );
304 dm->get_tile()->dec_pending_models();
309 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
311 previous_bucket = current_bucket;
312 last_longitude = longitude;
313 last_latitude = latitude;
315 // activate loader thread one out of every 5 frames
316 if ( counter_hack == 0 ) {
317 // Notify the tile loader that it can load another tile
320 if ( !attach_queue.empty() ) {
321 #ifdef ENABLE_THREADS
322 FGTileEntry* e = attach_queue.pop();
324 FGTileEntry* e = attach_queue.front();
327 e->add_ssg_nodes( terrain, ground );
328 // cout << "Adding ssg nodes for "
331 counter_hack = (counter_hack + 1) % 5;
333 sgdSetVec3( sc, scenery.center[0], scenery.center[1], scenery.center[2] );
335 if ( scenery.center == Point3D(0.0) ) {
337 cout << "initializing scenery current elevation ... " << endl;
338 sgdVec3 tmp_abs_view_pos;
340 Point3D geod_pos = Point3D( longitude * SGD_DEGREES_TO_RADIANS,
341 latitude * SGD_DEGREES_TO_RADIANS,
343 Point3D tmp = sgGeodToCart( geod_pos );
344 scenery.center = tmp;
345 sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
347 // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
351 if ( fgCurrentElev(tmp_abs_view_pos, sc, &hit_list,
352 &tmp_elev, &scenery.cur_radius, scenery.cur_normal) )
354 scenery.cur_elev = tmp_elev;
356 scenery.cur_elev = 0.0;
358 cout << "result = " << scenery.cur_elev << endl;
360 // cout << "abs view pos = " << current_view.abs_view_pos
361 // << " view pos = " << current_view.view_pos << endl;
363 if ( fgCurrentElev(globals->get_current_view()->get_abs_view_pos(),
365 &tmp_elev, &scenery.cur_radius, scenery.cur_normal) )
367 scenery.cur_elev = tmp_elev;
369 scenery.cur_elev = 0.0;
377 void FGTileMgr::prep_ssg_nodes() {
380 #ifndef FG_OLD_WEATHER
381 if ( WeatherDatabase ) {
382 vis = WeatherDatabase->getWeatherVisibility();
387 vis = current_weather.get_visibility();
389 // cout << "visibility = " << vis << endl;
391 // traverse the potentially viewable tile list and update range
392 // selector and transform
395 tile_cache.reset_traversal();
397 while ( ! tile_cache.at_end() ) {
398 // cout << "processing a tile" << endl;
399 if ( (e = tile_cache.get_current()) ) {
400 e->prep_ssg_node( scenery.center, vis);
402 cout << "warning ... empty tile in cache" << endl;