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.
33 #include <simgear/xgl/xgl.h>
35 #include <simgear/constants.h>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/math/fg_geodesy.hxx>
38 #include <simgear/math/point3d.hxx>
39 #include <simgear/math/polar3d.hxx>
40 #include <simgear/math/vector.hxx>
42 #include <Aircraft/aircraft.hxx>
43 #include <Main/options.hxx>
44 #include <Main/views.hxx>
45 #include <Objects/materialmgr.hxx>
46 #include <Objects/obj.hxx>
48 #ifndef FG_OLD_WEATHER
49 # include <WeatherCM/FGLocalWeatherDatabase.h>
51 # include <Weather/weather.hxx>
54 #include "scenery.hxx"
55 #include "tilecache.hxx"
56 #include "tilemgr.hxx"
58 #define TEST_LAST_HIT_CACHE
60 extern ssgRoot *scene;
61 extern ssgBranch *terrain;
64 FGTileMgr global_tile_mgr;
67 // a temporary hack until we get everything rewritten with sgdVec3
68 static inline Point3D operator + (const Point3D& a, const sgdVec3 b)
70 return Point3D(a.x()+b[0], a.y()+b[1], a.z()+b[2]);
75 FGTileMgr::FGTileMgr ( void ):
82 FGTileMgr::~FGTileMgr ( void ) {
86 // Initialize the Tile Manager subsystem
87 int FGTileMgr::init( void ) {
88 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
90 // load default material library
91 if ( ! material_mgr.loaded() ) {
92 material_mgr.load_lib();
95 global_tile_cache.init();
102 tile_diameter = current_options.get_tile_diameter();
103 FG_LOG( FG_TERRAIN, FG_INFO, "Tile Diameter = " << tile_diameter);
105 previous_bucket.make_bad();
106 current_bucket.make_bad();
108 scroll_direction = SCROLL_INIT;
111 longitude = latitude = -1000.0;
112 last_longitude = last_latitude = -1000.0;
118 // schedule a tile for loading
119 static void disable_tile( int cache_index ) {
120 // see if tile already exists in the cache
121 // cout << "DISABLING CACHE ENTRY = " << cache_index << endl;
122 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
127 // schedule a tile for loading
128 int FGTileMgr::sched_tile( const FGBucket& b ) {
129 // see if tile already exists in the cache
130 int cache_index = global_tile_cache.exists( b );
132 if ( cache_index >= 0 ) {
133 // tile exists in cache, reenable it.
134 // cout << "REENABLING DISABLED TILE" << endl;
135 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
136 t->select_ptr->select( 1 );
139 // find the next available cache entry and mark it as
141 cache_index = global_tile_cache.next_avail();
142 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
143 t->mark_scheduled_for_use();
145 // register a load request
148 request.cache_index = cache_index;
149 load_queue.push_back( request );
157 void FGTileMgr::load_tile( const FGBucket& b, int cache_index) {
159 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loading tile " << b );
161 global_tile_cache.fill_in(cache_index, b);
163 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loaded for cache index: " << cache_index );
167 // Determine scenery altitude via ssg. Normally this just happens
168 // when we render the scene, but we'd also like to be able to do this
169 // explicitely. lat & lon are in radians. view_pos in current world
170 // coordinate translated near (0,0,0) (in meters.) Returns result in
174 FGTileMgr::current_elev_ssg( const Point3D& abs_view_pos,
175 const Point3D& view_pos )
179 sgdSetVec3(orig, view_pos.x(), view_pos.y(), view_pos.z() );
180 sgdSetVec3(dir, abs_view_pos.x(), abs_view_pos.y(), abs_view_pos.z() );
182 hit_list.Intersect( terrain, orig, dir );
186 double result = -9999;
188 int hitcount = hit_list.num_hits();
189 for ( int i = 0; i < hitcount; ++i ) {
190 geoc = fgCartToPolar3d( scenery.center + hit_list.get_point(i) );
191 double lat_geod, alt, sea_level_r;
192 fgGeocToGeod(geoc.lat(), geoc.radius(), &lat_geod,
194 if ( alt > result && alt < 10000 ) {
200 if ( result > -9000 ) {
201 scenery.cur_elev = result;
202 scenery.cur_radius = geoc.radius();
203 sgdCopyVec3(scenery.cur_normal, hit_list.get_normal(this_hit));
206 FG_LOG( FG_TERRAIN, FG_INFO, "no terrain intersection" );
207 scenery.cur_elev = 0.0;
208 float *up = current_view.local_up;
209 sgdSetVec3(scenery.cur_normal, up[0], up[1], up[2]);
215 FGBucket FGTileMgr::BucketOffset( int dx, int dy )
217 double clat, clon, span;
218 if( scroll_direction == SCROLL_INIT ) {
219 pending.set_bucket( longitude, latitude );
220 clat = pending.get_center_lat() + dy * FG_BUCKET_SPAN;
222 // walk dy units in the lat direction
223 pending.set_bucket( longitude, clat );
225 // find the lon span for the new latitude
226 span = bucket_span( clat );
228 // walk dx units in the lon direction
229 clon = longitude + dx * span;
231 pending.set_bucket( last_longitude, last_latitude );
232 clat = pending.get_center_lat() + dy * FG_BUCKET_SPAN;
234 // walk dy units in the lat direction
235 pending.set_bucket( last_longitude, clat );
237 // find the lon span for the new latitude
238 span = bucket_span( clat );
240 // walk dx units in the lon direction
241 clon = last_longitude + dx * span;
244 while ( clon < -180.0 ) clon += 360.0;
245 while ( clon >= 180.0 ) clon -= 360.0;
246 pending.set_bucket( clon, clat );
248 FG_LOG( FG_TERRAIN, FG_DEBUG, " fgBucketOffset " << pending );
253 // schedule a tile row(column) for loading
254 void FGTileMgr::scroll( void )
256 FG_LOG( FG_TERRAIN, FG_DEBUG, "schedule_row: Scrolling" );
260 switch( scroll_direction ) {
262 FG_LOG( FG_TERRAIN, FG_DEBUG,
263 " (North) Loading " << tile_diameter << " tiles" );
264 dw = tile_diameter / 2;
266 for ( i = 0; i < tile_diameter; i++ ) {
267 sched_tile( BucketOffset( i - dw, dh) );
271 FG_LOG( FG_TERRAIN, FG_DEBUG,
272 " (East) Loading " << tile_diameter << " tiles" );
273 dh = tile_diameter / 2;
275 for ( i = 0; i < tile_diameter; i++ ) {
276 sched_tile( BucketOffset( dw, i - dh ) );
280 dw = tile_diameter / 2;
282 FG_LOG( FG_TERRAIN, FG_DEBUG,
283 " (South) Loading " << tile_diameter << " tiles" );
284 for ( i = 0; i < tile_diameter; i++ ) {
285 sched_tile( BucketOffset( i - dw, dh) );
289 dh = tile_diameter / 2;
291 FG_LOG( FG_TERRAIN, FG_DEBUG,
292 " (West) Loading " << tile_diameter << " tiles" );
293 for ( i = 0; i < tile_diameter; i++ ) {
294 sched_tile( BucketOffset( dw, i - dh ) );
298 FG_LOG( FG_TERRAIN, FG_WARN, "UNKNOWN SCROLL DIRECTION in schedule_row" );
301 FG_LOG( FG_TERRAIN, FG_DEBUG, "\tschedule_row returns" );
305 void FGTileMgr::initialize_queue( void )
307 // First time through or we have teleported, initialize the
308 // system and load all relavant tiles
310 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << current_bucket );
311 FG_LOG( FG_TERRAIN, FG_INFO, " First time through ... " );
312 FG_LOG( FG_TERRAIN, FG_INFO, " Updating Tile list for " << current_bucket );
313 FG_LOG( FG_TERRAIN, FG_INFO, " Loading "
314 << tile_diameter * tile_diameter << " tiles" );
317 scroll_direction = SCROLL_INIT;
319 // wipe/initialize tile cache
320 global_tile_cache.init();
321 previous_bucket.make_bad();
323 // build the local area list and schedule tiles for loading
325 // start with the center tile and work out in concentric
328 sched_tile( current_bucket );
329 Point3D geod_view_center( current_bucket.get_center_lon(),
330 current_bucket.get_center_lat(),
331 cur_fdm_state->get_Altitude()*FEET_TO_METER + 3 );
333 current_view.abs_view_pos = fgGeodToCart( geod_view_center );
334 current_view.view_pos = current_view.abs_view_pos - scenery.next_center;
336 for ( i = 3; i <= tile_diameter; i = i + 2 ) {
341 for ( j = -span; j <= span; ++j ) {
342 sched_tile( BucketOffset( j, -span ) );
346 for ( j = -span; j <= span; ++j ) {
347 sched_tile( BucketOffset( j, span ) );
351 for ( j = -span + 1; j <= span - 1; ++j ) {
352 sched_tile( BucketOffset( -span, j ) );
353 sched_tile( BucketOffset( span, j ) );
358 // Now force a load of the center tile and inner ring so we
359 // have something to see in our first frame.
360 for ( i = 0; i < 9; ++i ) {
361 if ( load_queue.size() ) {
362 FG_LOG( FG_TERRAIN, FG_DEBUG,
363 "Load queue not empty, loading a tile" );
365 FGLoadRec pending = load_queue.front();
366 load_queue.pop_front();
367 load_tile( pending.b, pending.cache_index );
373 // given the current lon/lat, fill in the array of local chunks. If
374 // the chunk isn't already in the cache, then read it from disk.
375 int FGTileMgr::update( void ) {
376 // FG_LOG( FG_TERRAIN, FG_DEBUG, "FGTileMgr::update()" );
378 FGInterface *f = current_aircraft.fdm_state;
380 // lonlat for this update
381 longitude = f->get_Longitude() * RAD_TO_DEG;
382 latitude = f->get_Latitude() * RAD_TO_DEG;
383 // FG_LOG( FG_TERRAIN, FG_DEBUG, "lon "<< lonlat[LON] <<
384 // " lat " << lonlat[LAT] );
386 current_bucket.set_bucket( longitude, latitude );
387 // FG_LOG( FG_TERRAIN, FG_DEBUG, "Updating Tile list for " << current_bucket );
389 tile_index = global_tile_cache.exists(current_bucket);
390 // FG_LOG( FG_TERRAIN, FG_DEBUG, "tile index " << tile_index );
392 if ( tile_index >= 0 ) {
393 current_tile = global_tile_cache.get_tile(tile_index);
394 scenery.next_center = current_tile->center;
396 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
399 if ( state == Running ) {
400 if( current_bucket == previous_bucket) {
401 FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
402 scroll_direction = SCROLL_NONE;
404 // We've moved to a new bucket, we need to scroll our
405 // structures, and load in the new tiles
406 // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
407 // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
408 // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
410 if ( (current_bucket.get_lon() > previous_bucket.get_lon()) ||
411 ( (current_bucket.get_lon() == previous_bucket.get_lon()) &&
412 (current_bucket.get_x() > previous_bucket.get_x()) ) )
414 scroll_direction = SCROLL_EAST;
416 else if ( (current_bucket.get_lon() < previous_bucket.get_lon()) ||
417 ( (current_bucket.get_lon() == previous_bucket.get_lon()) &&
418 (current_bucket.get_x() < previous_bucket.get_x()) ) )
420 scroll_direction = SCROLL_WEST;
423 if ( (current_bucket.get_lat() > previous_bucket.get_lat()) ||
424 ( (current_bucket.get_lat() == previous_bucket.get_lat()) &&
425 (current_bucket.get_y() > previous_bucket.get_y()) ) )
427 scroll_direction = SCROLL_NORTH;
429 else if ( (current_bucket.get_lat() < previous_bucket.get_lat()) ||
430 ( (current_bucket.get_lat() == previous_bucket.get_lat()) &&
431 (current_bucket.get_y() < previous_bucket.get_y()) ) )
433 scroll_direction = SCROLL_SOUTH;
439 } else if ( (state == Start) || (state == Inited) ) {
444 if ( load_queue.size() ) {
445 FG_LOG( FG_TERRAIN, FG_DEBUG, "Load queue not empty, loading a tile" );
447 FGLoadRec pending = load_queue.front();
448 load_queue.pop_front();
449 load_tile( pending.b, pending.cache_index );
452 // find our current elevation (feed in the current bucket to save work)
453 // Point3D geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
454 // Point3D tmp_abs_view_pos = fgGeodToCart(geod_pos);
456 // cout << "current elevation (old) == "
457 // << current_elev( f->get_Longitude(), f->get_Latitude(),
458 // tmp_abs_view_pos )
461 // set scenery.cur_elev and scenery.cur_radius
463 current_elev_ssg( current_view.abs_view_pos,
464 current_view.view_pos );
465 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
467 previous_bucket = current_bucket;
468 last_longitude = longitude;
469 last_latitude = latitude;
474 // Prepare the ssg nodes ... for each tile, set it's proper
475 // transform and update it's range selector based on current
477 void FGTileMgr::prep_ssg_node( int idx ) {
480 void FGTileMgr::prep_ssg_nodes( void ) {
485 // traverse the potentially viewable tile list and update range
486 // selector and transform
487 for ( int i = 0; i < (int)global_tile_cache.get_size(); i++ ) {
488 t = global_tile_cache.get_tile( i );
490 if ( t->is_loaded() ) {
491 // set range selector (LOD trick) to be distance to center
492 // of tile + bounding radius
494 #ifndef FG_OLD_WEATHER
495 ranges[1] = WeatherDatabase->getWeatherVisibility()
496 + t->bounding_radius;
498 ranges[1] = current_weather.get_visibility()+t->bounding_radius;
500 t->range_ptr->setRanges( ranges, 2 );
502 // calculate tile offset
503 t->SetOffset( scenery.center );
505 // calculate ssg transform
507 sgSetCoord( &sgcoord,
508 t->offset.x(), t->offset.y(), t->offset.z(),
510 t->transform_ptr->setTransform( &sgcoord );