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
68 FGTileMgr::FGTileMgr():
78 FGTileMgr::~FGTileMgr() {
82 // Initialize the Tile Manager subsystem
83 int FGTileMgr::init() {
84 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing Tile Manager subsystem." );
90 // instead it's just a lot easier to let any pending work flush
91 // through, rather than trying to arrest the queue and nuke all
92 // the various work at all the various stages and get everything
93 // cleaned up properly.
95 while ( ! attach_queue.empty() ) {
99 while ( ! model_queue.empty() ) {
100 #ifdef ENABLE_THREADS
101 FGDeferredModel* dm = model_queue.pop();
103 FGDeferredModel* dm = model_queue.front();
115 previous_bucket.make_bad();
116 current_bucket.make_bad();
118 longitude = latitude = -1000.0;
119 last_longitude = last_latitude = -1000.0;
125 // schedule a tile for loading
126 void FGTileMgr::sched_tile( const SGBucket& b ) {
127 // see if tile already exists in the cache
128 FGTileEntry *t = tile_cache.get_tile( b );
131 // create a new entry
132 FGTileEntry *e = new FGTileEntry( b );
134 // insert the tile into the cache
135 if ( tile_cache.insert_tile( e ) ) {
136 // Schedule tile for loading
139 // insert failed (cache full with no available entries to
140 // delete.) Try again later
147 // schedule a needed buckets for loading
148 void FGTileMgr::schedule_needed( double vis) {
149 // sanity check (unfortunately needed!)
150 if ( longitude < -180.0 || longitude > 180.0
151 || latitude < -90.0 || latitude > 90.0 )
153 SG_LOG( SG_TERRAIN, SG_ALERT,
154 "Attempting to schedule tiles for bogus latitude and" );
155 SG_LOG( SG_TERRAIN, SG_ALERT,
156 "longitude. This is a FATAL error. Exiting!" );
160 SG_LOG( SG_TERRAIN, SG_INFO,
161 "scheduling needed tiles for " << longitude << " " << latitude );
163 // vis = fgGetDouble("/environment/visibility-m");
165 double tile_width = current_bucket.get_width_m();
166 double tile_height = current_bucket.get_height_m();
167 // cout << "tile width = " << tile_width << " tile_height = "
168 // << tile_height !<< endl;
170 xrange = (int)(vis / tile_width) + 1;
171 yrange = (int)(vis / tile_height) + 1;
172 if ( xrange < 1 ) { xrange = 1; }
173 if ( yrange < 1 ) { yrange = 1; }
174 // cout << "xrange = " << xrange << " yrange = " << yrange << endl;
176 tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
180 // schedule center tile first so it can be loaded first
181 b = sgBucketOffset( longitude, latitude, 0, 0 );
186 // schedule next ring of 8 tiles
187 for ( x = -1; x <= 1; ++x ) {
188 for ( y = -1; y <= 1; ++y ) {
189 if ( x != 0 || y != 0 ) {
190 b = sgBucketOffset( longitude, latitude, x, y );
196 // schedule remaining tiles
197 for ( x = -xrange; x <= xrange; ++x ) {
198 for ( y = -yrange; y <= yrange; ++y ) {
199 if ( x < -1 || x > 1 || y < -1 || y > 1 ) {
200 SGBucket b = sgBucketOffset( longitude, latitude, x, y );
208 void FGTileMgr::initialize_queue()
210 // First time through or we have teleported, initialize the
211 // system and load all relavant tiles
213 SG_LOG( SG_TERRAIN, SG_INFO, "Updating Tile list for " << current_bucket );
214 // cout << "tile cache size = " << tile_cache.get_size() << endl;
216 // wipe/initialize tile cache
217 // tile_cache.init();
218 previous_bucket.make_bad();
220 // build the local area list and schedule tiles for loading
222 // start with the center tile and work out in concentric
225 double visibility_meters = fgGetDouble("/environment/visibility-m");
226 schedule_needed(visibility_meters);
228 // do we really want to lose this? CLO
230 // Now force a load of the center tile and inner ring so we
231 // have something to see in our first frame.
233 for ( i = 0; i < 9; ++i ) {
234 if ( load_queue.size() ) {
235 SG_LOG( SG_TERRAIN, SG_DEBUG,
236 "Load queue not empty, loading a tile" );
238 SGBucket pending = load_queue.front();
239 load_queue.pop_front();
240 load_tile( pending );
247 // given the current lon/lat (in degrees), fill in the array of local
248 // chunks. If the chunk isn't already in the cache, then read it from
250 int FGTileMgr::update( double lon, double lat, double visibility_meters ) {
251 // SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update() for "
252 // << lon << " " << lat );
256 // SG_LOG( SG_TERRAIN, SG_DEBUG, "lon "<< lonlat[LON] <<
257 // " lat " << lonlat[LAT] );
259 current_bucket.set_bucket( longitude, latitude );
260 // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating Tile list for " << current_bucket );
262 if ( tile_cache.exists( current_bucket ) ) {
263 current_tile = tile_cache.get_tile( current_bucket );
264 globals->get_scenery()->set_next_center( current_tile->center );
266 SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
267 globals->get_scenery()->set_next_center( Point3D(0.0) );
270 if ( state == Running ) {
271 SG_LOG( SG_TERRAIN, SG_DEBUG, "State == Running" );
272 if ( !(current_bucket == previous_bucket) ) {
273 // We've moved to a new bucket, we need to schedule any
274 // needed tiles for loading.
275 schedule_needed(visibility_meters);
277 } else if ( state == Start || state == Inited ) {
278 SG_LOG( SG_TERRAIN, SG_INFO, "State == Start || Inited" );
282 // load the next tile in the load queue (or authorize the next
283 // load in the case of the threaded tile pager)
287 // load the next model in the load queue. Currently this must
288 // happen in the render thread because model loading can trigger
289 // texture loading which involves use of the opengl api.
290 if ( !model_queue.empty() ) {
291 // cout << "loading next model ..." << endl;
292 // load the next tile in the queue
293 #ifdef ENABLE_THREADS
294 FGDeferredModel* dm = model_queue.pop();
296 FGDeferredModel* dm = model_queue.front();
300 ssgTexturePath( (char *)(dm->get_texture_path().c_str()) );
302 = ssgLoad( (char *)(dm->get_model_path().c_str()) );
303 if ( obj_model != NULL ) {
304 dm->get_obj_trans()->addKid( obj_model );
306 dm->get_tile()->dec_pending_models();
311 // cout << "current elevation (ssg) == " << scenery.get_cur_elev() << endl;
313 previous_bucket = current_bucket;
314 last_longitude = longitude;
315 last_latitude = latitude;
317 // activate loader thread one out of every 5 frames
318 if ( counter_hack == 0 ) {
319 // Notify the tile loader that it can load another tile
324 counter_hack = (counter_hack + 1) % 5;
326 if ( !attach_queue.empty() ) {
327 #ifdef ENABLE_THREADS
328 FGTileEntry* e = attach_queue.pop();
330 FGTileEntry* e = attach_queue.front();
333 e->add_ssg_nodes( globals->get_terrain_branch(),
334 globals->get_gnd_lights_branch(),
335 globals->get_rwy_lights_branch() );
336 // cout << "Adding ssg nodes for "
341 globals->get_scenery()->get_center()[0],
342 globals->get_scenery()->get_center()[1],
343 globals->get_scenery()->get_center()[2] );
346 cout << "abs view pos = "
347 << globals->get_current_view()->get_abs_view_pos()[0] << ","
348 << globals->get_current_view()->get_abs_view_pos()[1] << ","
349 << globals->get_current_view()->get_abs_view_pos()[2]
351 << globals->get_current_view()->get_view_pos()[0] << ","
352 << globals->get_current_view()->get_view_pos()[1] << ","
353 << globals->get_current_view()->get_view_pos()[2]
355 cout << "current_tile = " << current_tile << endl;
356 cout << "Scenery center = " << sc[0] << "," << sc[1] << "," << sc[2]
360 // overridden with actual values if a terrain intersection is
362 double hit_elev = -9999.0;
363 double hit_radius = 0.0;
364 sgdVec3 hit_normal = { 0.0, 0.0, 0.0 };
367 if ( fabs(sc[0]) > 1.0 || fabs(sc[1]) > 1.0 || fabs(sc[2]) > 1.0 ) {
368 // scenery center has been properly defined so any hit
369 // should be valid (and not just luck)
371 globals->get_scenery()->get_center()[0],
372 globals->get_scenery()->get_center()[1],
373 globals->get_scenery()->get_center()[2] );
374 hit = fgCurrentElev(globals->get_current_view()->get_absolute_view_pos(),
376 current_tile->get_terra_transform(),
384 globals->get_scenery()->set_cur_elev( hit_elev );
385 globals->get_scenery()->set_cur_radius( hit_radius );
386 globals->get_scenery()->set_cur_normal( hit_normal );
388 globals->get_scenery()->set_cur_elev( -9999.0 );
389 globals->get_scenery()->set_cur_radius( 0.0 );
390 globals->get_scenery()->set_cur_normal( hit_normal );
392 // cout << "Current elevation = " << scenery.get_cur_elev() << endl;
402 void FGTileMgr::prep_ssg_nodes(float vis) {
405 // vis = fgGetDouble("/environment/visibility-m");
407 // traverse the potentially viewable tile list and update range
408 // selector and transform
411 tile_cache.reset_traversal();
414 sgCopyVec3( up, globals->get_current_view()->get_world_up() );
416 while ( ! tile_cache.at_end() ) {
417 // cout << "processing a tile" << endl;
418 if ( (e = tile_cache.get_current()) ) {
419 e->prep_ssg_node( globals->get_scenery()->get_center(), up, vis);
421 SG_LOG(SG_INPUT, SG_ALERT, "warning ... empty tile in cache");