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/obj.hxx>
47 #ifndef FG_OLD_WEATHER
48 # include <WeatherCM/FGLocalWeatherDatabase.h>
50 # include <Weather/weather.hxx>
53 #include "scenery.hxx"
54 #include "tilecache.hxx"
55 #include "tilemgr.hxx"
57 #define TEST_LAST_HIT_CACHE
59 extern ssgRoot *scene;
60 extern ssgBranch *terrain;
63 FGTileMgr global_tile_mgr;
66 // a temporary hack until we get everything rewritten with sgdVec3
67 static inline Point3D operator + (const Point3D& a, const sgdVec3 b)
69 return Point3D(a.x()+b[0], a.y()+b[1], a.z()+b[2]);
74 FGTileMgr::FGTileMgr ( void ):
81 FGTileMgr::~FGTileMgr ( void ) {
85 // Initialize the Tile Manager subsystem
86 int FGTileMgr::init( void ) {
87 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
89 global_tile_cache.init();
96 tile_diameter = current_options.get_tile_diameter();
97 FG_LOG( FG_TERRAIN, FG_INFO, "Tile Diameter = " << tile_diameter);
99 previous_bucket.make_bad();
100 current_bucket.make_bad();
102 scroll_direction = SCROLL_INIT;
105 longitude = latitude = -1000.0;
106 last_longitude = last_latitude = -1000.0;
112 // schedule a tile for loading
113 static void disable_tile( int cache_index ) {
114 // see if tile already exists in the cache
115 // cout << "DISABLING CACHE ENTRY = " << cache_index << endl;
116 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
121 // schedule a tile for loading
122 int FGTileMgr::sched_tile( const FGBucket& b ) {
123 // see if tile already exists in the cache
124 int cache_index = global_tile_cache.exists( b );
126 if ( cache_index >= 0 ) {
127 // tile exists in cache, reenable it.
128 // cout << "REENABLING DISABLED TILE" << endl;
129 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
130 t->select_ptr->select( 1 );
133 // find the next available cache entry and mark it as
135 cache_index = global_tile_cache.next_avail();
136 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
137 t->mark_scheduled_for_use();
139 // register a load request
142 request.cache_index = cache_index;
143 load_queue.push_back( request );
151 void FGTileMgr::load_tile( const FGBucket& b, int cache_index) {
153 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loading tile " << b );
155 global_tile_cache.fill_in(cache_index, b);
157 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loaded for cache index: " << cache_index );
161 // Determine scenery altitude via ssg. Normally this just happens
162 // when we render the scene, but we'd also like to be able to do this
163 // explicitely. lat & lon are in radians. view_pos in current world
164 // coordinate translated near (0,0,0) (in meters.) Returns result in
168 FGTileMgr::current_elev_ssg( const Point3D& abs_view_pos,
169 const Point3D& view_pos )
173 sgdSetVec3(orig, view_pos.x(), view_pos.y(), view_pos.z() );
174 sgdSetVec3(dir, abs_view_pos.x(), abs_view_pos.y(), abs_view_pos.z() );
176 hit_list.Intersect( terrain, orig, dir );
180 double result = -9999;
182 int hitcount = hit_list.num_hits();
183 for ( int i = 0; i < hitcount; ++i ) {
184 geoc = fgCartToPolar3d( scenery.center + hit_list.get_point(i) );
185 double lat_geod, alt, sea_level_r;
186 fgGeocToGeod(geoc.lat(), geoc.radius(), &lat_geod,
188 if ( alt > result && alt < 10000 ) {
194 if ( result > -9000 ) {
195 scenery.cur_elev = result;
196 scenery.cur_radius = geoc.radius();
197 sgdCopyVec3(scenery.cur_normal, hit_list.get_normal(this_hit));
200 FG_LOG( FG_TERRAIN, FG_INFO, "no terrain intersection" );
201 scenery.cur_elev = 0.0;
202 float *up = current_view.local_up;
203 sgdSetVec3(scenery.cur_normal, up[0], up[1], up[2]);
209 FGBucket FGTileMgr::BucketOffset( int dx, int dy )
211 double clat, clon, span;
212 if( scroll_direction == SCROLL_INIT ) {
213 pending.set_bucket( longitude, latitude );
214 clat = pending.get_center_lat() + dy * FG_BUCKET_SPAN;
216 // walk dy units in the lat direction
217 pending.set_bucket( longitude, clat );
219 // find the lon span for the new latitude
220 span = bucket_span( clat );
222 // walk dx units in the lon direction
223 clon = longitude + dx * span;
225 pending.set_bucket( last_longitude, last_latitude );
226 clat = pending.get_center_lat() + dy * FG_BUCKET_SPAN;
228 // walk dy units in the lat direction
229 pending.set_bucket( last_longitude, clat );
231 // find the lon span for the new latitude
232 span = bucket_span( clat );
234 // walk dx units in the lon direction
235 clon = last_longitude + dx * span;
238 while ( clon < -180.0 ) clon += 360.0;
239 while ( clon >= 180.0 ) clon -= 360.0;
240 pending.set_bucket( clon, clat );
242 FG_LOG( FG_TERRAIN, FG_DEBUG, " fgBucketOffset " << pending );
247 // schedule a tile row(column) for loading
248 void FGTileMgr::scroll( void )
250 FG_LOG( FG_TERRAIN, FG_DEBUG, "schedule_row: Scrolling" );
254 switch( scroll_direction ) {
256 FG_LOG( FG_TERRAIN, FG_DEBUG,
257 " (North) Loading " << tile_diameter << " tiles" );
258 dw = tile_diameter / 2;
260 for ( i = 0; i < tile_diameter; i++ ) {
261 sched_tile( BucketOffset( i - dw, dh) );
265 FG_LOG( FG_TERRAIN, FG_DEBUG,
266 " (East) Loading " << tile_diameter << " tiles" );
267 dh = tile_diameter / 2;
269 for ( i = 0; i < tile_diameter; i++ ) {
270 sched_tile( BucketOffset( dw, i - dh ) );
274 dw = tile_diameter / 2;
276 FG_LOG( FG_TERRAIN, FG_DEBUG,
277 " (South) Loading " << tile_diameter << " tiles" );
278 for ( i = 0; i < tile_diameter; i++ ) {
279 sched_tile( BucketOffset( i - dw, dh) );
283 dh = tile_diameter / 2;
285 FG_LOG( FG_TERRAIN, FG_DEBUG,
286 " (West) Loading " << tile_diameter << " tiles" );
287 for ( i = 0; i < tile_diameter; i++ ) {
288 sched_tile( BucketOffset( dw, i - dh ) );
292 FG_LOG( FG_TERRAIN, FG_WARN, "UNKNOWN SCROLL DIRECTION in schedule_row" );
295 FG_LOG( FG_TERRAIN, FG_DEBUG, "\tschedule_row returns" );
299 void FGTileMgr::initialize_queue( void )
301 // First time through or we have teleported, initialize the
302 // system and load all relavant tiles
304 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << current_bucket );
305 FG_LOG( FG_TERRAIN, FG_INFO, " First time through ... " );
306 FG_LOG( FG_TERRAIN, FG_INFO, " Updating Tile list for " << current_bucket );
307 FG_LOG( FG_TERRAIN, FG_INFO, " Loading "
308 << tile_diameter * tile_diameter << " tiles" );
311 scroll_direction = SCROLL_INIT;
313 // wipe/initialize tile cache
314 global_tile_cache.init();
315 previous_bucket.make_bad();
317 // build the local area list and schedule tiles for loading
319 // start with the center tile and work out in concentric
322 sched_tile( current_bucket );
323 Point3D geod_view_center( current_bucket.get_center_lon(),
324 current_bucket.get_center_lat(),
325 cur_fdm_state->get_Altitude()*FEET_TO_METER + 3 );
327 current_view.abs_view_pos = fgGeodToCart( geod_view_center );
328 current_view.view_pos = current_view.abs_view_pos - scenery.next_center;
330 for ( i = 3; i <= tile_diameter; i = i + 2 ) {
335 for ( j = -span; j <= span; ++j ) {
336 sched_tile( BucketOffset( j, -span ) );
340 for ( j = -span; j <= span; ++j ) {
341 sched_tile( BucketOffset( j, span ) );
345 for ( j = -span + 1; j <= span - 1; ++j ) {
346 sched_tile( BucketOffset( -span, j ) );
347 sched_tile( BucketOffset( span, j ) );
352 // Now force a load of the center tile and inner ring so we
353 // have something to see in our first frame.
354 for ( i = 0; i < 9; ++i ) {
355 if ( load_queue.size() ) {
356 FG_LOG( FG_TERRAIN, FG_DEBUG,
357 "Load queue not empty, loading a tile" );
359 FGLoadRec pending = load_queue.front();
360 load_queue.pop_front();
361 load_tile( pending.b, pending.cache_index );
367 // given the current lon/lat, fill in the array of local chunks. If
368 // the chunk isn't already in the cache, then read it from disk.
369 int FGTileMgr::update( void ) {
370 // FG_LOG( FG_TERRAIN, FG_DEBUG, "FGTileMgr::update()" );
372 FGInterface *f = current_aircraft.fdm_state;
374 // lonlat for this update
375 longitude = f->get_Longitude() * RAD_TO_DEG;
376 latitude = f->get_Latitude() * RAD_TO_DEG;
377 // FG_LOG( FG_TERRAIN, FG_DEBUG, "lon "<< lonlat[LON] <<
378 // " lat " << lonlat[LAT] );
380 current_bucket.set_bucket( longitude, latitude );
381 // FG_LOG( FG_TERRAIN, FG_DEBUG, "Updating Tile list for " << current_bucket );
383 tile_index = global_tile_cache.exists(current_bucket);
384 // FG_LOG( FG_TERRAIN, FG_DEBUG, "tile index " << tile_index );
386 if ( tile_index >= 0 ) {
387 current_tile = global_tile_cache.get_tile(tile_index);
388 scenery.next_center = current_tile->center;
390 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
393 if ( state == Running ) {
394 if( current_bucket == previous_bucket) {
395 FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
396 scroll_direction = SCROLL_NONE;
398 // We've moved to a new bucket, we need to scroll our
399 // structures, and load in the new tiles
400 // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
401 // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
402 // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
404 if ( (current_bucket.get_lon() > previous_bucket.get_lon()) ||
405 ( (current_bucket.get_lon() == previous_bucket.get_lon()) &&
406 (current_bucket.get_x() > previous_bucket.get_x()) ) )
408 scroll_direction = SCROLL_EAST;
410 else 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_WEST;
417 if ( (current_bucket.get_lat() > previous_bucket.get_lat()) ||
418 ( (current_bucket.get_lat() == previous_bucket.get_lat()) &&
419 (current_bucket.get_y() > previous_bucket.get_y()) ) )
421 scroll_direction = SCROLL_NORTH;
423 else 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_SOUTH;
433 } else if ( (state == Start) || (state == Inited) ) {
438 if ( load_queue.size() ) {
439 FG_LOG( FG_TERRAIN, FG_DEBUG, "Load queue not empty, loading a tile" );
441 FGLoadRec pending = load_queue.front();
442 load_queue.pop_front();
443 load_tile( pending.b, pending.cache_index );
446 // find our current elevation (feed in the current bucket to save work)
447 // Point3D geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
448 // Point3D tmp_abs_view_pos = fgGeodToCart(geod_pos);
450 // cout << "current elevation (old) == "
451 // << current_elev( f->get_Longitude(), f->get_Latitude(),
452 // tmp_abs_view_pos )
455 // set scenery.cur_elev and scenery.cur_radius
457 current_elev_ssg( current_view.abs_view_pos,
458 current_view.view_pos );
459 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
461 previous_bucket = current_bucket;
462 last_longitude = longitude;
463 last_latitude = latitude;
468 // Prepare the ssg nodes ... for each tile, set it's proper
469 // transform and update it's range selector based on current
471 void FGTileMgr::prep_ssg_node( int idx ) {
474 void FGTileMgr::prep_ssg_nodes( void ) {
479 // traverse the potentially viewable tile list and update range
480 // selector and transform
481 for ( int i = 0; i < (int)global_tile_cache.get_size(); i++ ) {
482 t = global_tile_cache.get_tile( i );
484 if ( t->is_loaded() ) {
485 // set range selector (LOD trick) to be distance to center
486 // of tile + bounding radius
488 #ifndef FG_OLD_WEATHER
489 ranges[1] = WeatherDatabase->getWeatherVisibility()
490 + t->bounding_radius;
492 ranges[1] = current_weather.get_visibility()+t->bounding_radius;
494 t->range_ptr->setRanges( ranges, 2 );
496 // calculate tile offset
497 t->SetOffset( scenery.center );
499 // calculate ssg transform
501 sgSetCoord( &sgcoord,
502 t->offset.x(), t->offset.y(), t->offset.z(),
504 t->transform_ptr->setTransform( &sgcoord );