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 if ( state != Start ) {
90 FG_LOG( FG_TERRAIN, FG_INFO,
91 "... Reinitializing." );
93 // This is necessay to keep bookeeping straight for the
94 // tile_cache -- which actually handles all the
96 while( load_queue.size() ) {
97 FG_LOG( FG_TERRAIN, FG_INFO,
98 "Load queue not empty, popping a tile" );
99 FGLoadRec pending = load_queue.front();
100 load_queue.pop_front();
101 load_tile( pending.b, pending.cache_index );
104 FG_LOG( FG_TERRAIN, FG_INFO,
105 "... First time through." );
108 global_tile_cache.init();
113 tile_diameter = current_options.get_tile_diameter();
114 FG_LOG( FG_TERRAIN, FG_INFO, "Tile Diameter = " << tile_diameter);
116 previous_bucket.make_bad();
117 current_bucket.make_bad();
119 scroll_direction = SCROLL_INIT;
122 longitude = latitude = -1000.0;
123 last_longitude = last_latitude = -1000.0;
129 // schedule a tile for loading
130 int FGTileMgr::sched_tile( const FGBucket& b ) {
131 // see if tile already exists in the cache
132 int cache_index = global_tile_cache.exists( b );
134 if ( cache_index >= 0 ) {
135 // tile exists in cache, reenable it.
136 // cout << "REENABLING DISABLED TILE" << endl;
137 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
138 t->select_ptr->select( 1 );
141 // find the next available cache entry and mark it as
143 cache_index = global_tile_cache.next_avail();
144 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
145 t->mark_scheduled_for_use();
147 // register a load request
150 request.cache_index = cache_index;
151 load_queue.push_back( request );
159 void FGTileMgr::load_tile( const FGBucket& b, int cache_index) {
161 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loading tile " << b );
163 global_tile_cache.fill_in(cache_index, b);
165 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loaded for cache index: " << cache_index );
169 // Determine scenery altitude via ssg. Normally this just happens
170 // when we render the scene, but we'd also like to be able to do this
171 // explicitely. lat & lon are in radians. view_pos in current world
172 // coordinate translated near (0,0,0) (in meters.) Returns result in
176 FGTileMgr::current_elev_ssg( const Point3D& abs_view_pos,
177 const Point3D& view_pos )
181 sgdSetVec3(orig, view_pos.x(), view_pos.y(), view_pos.z() );
182 sgdSetVec3(dir, abs_view_pos.x(), abs_view_pos.y(), abs_view_pos.z() );
184 hit_list.Intersect( terrain, orig, dir );
188 double result = -9999;
190 int hitcount = hit_list.num_hits();
191 for ( int i = 0; i < hitcount; ++i ) {
192 geoc = fgCartToPolar3d( scenery.center + hit_list.get_point(i) );
193 double lat_geod, alt, sea_level_r;
194 fgGeocToGeod(geoc.lat(), geoc.radius(), &lat_geod,
196 if ( alt > result && alt < 10000 ) {
202 if ( result > -9000 ) {
203 scenery.cur_elev = result;
204 scenery.cur_radius = geoc.radius();
205 sgdCopyVec3(scenery.cur_normal, hit_list.get_normal(this_hit));
208 FG_LOG( FG_TERRAIN, FG_INFO, "no terrain intersection" );
209 scenery.cur_elev = 0.0;
210 float *up = current_view.local_up;
211 sgdSetVec3(scenery.cur_normal, up[0], up[1], up[2]);
217 FGBucket FGTileMgr::BucketOffset( int dx, int dy )
219 double clat, clon, span;
220 if( scroll_direction == SCROLL_INIT ) {
221 pending.set_bucket( longitude, latitude );
222 clat = pending.get_center_lat() + dy * FG_BUCKET_SPAN;
224 // walk dy units in the lat direction
225 pending.set_bucket( longitude, clat );
227 // find the lon span for the new latitude
228 span = bucket_span( clat );
230 // walk dx units in the lon direction
231 clon = longitude + dx * span;
233 pending.set_bucket( last_longitude, last_latitude );
234 clat = pending.get_center_lat() + dy * FG_BUCKET_SPAN;
236 // walk dy units in the lat direction
237 pending.set_bucket( last_longitude, clat );
239 // find the lon span for the new latitude
240 span = bucket_span( clat );
242 // walk dx units in the lon direction
243 clon = last_longitude + dx * span;
246 while ( clon < -180.0 ) clon += 360.0;
247 while ( clon >= 180.0 ) clon -= 360.0;
248 pending.set_bucket( clon, clat );
250 FG_LOG( FG_TERRAIN, FG_DEBUG, " fgBucketOffset " << pending );
255 // schedule a tile row(column) for loading
256 void FGTileMgr::scroll( void )
258 FG_LOG( FG_TERRAIN, FG_DEBUG, "schedule_row: Scrolling" );
262 switch( scroll_direction ) {
264 FG_LOG( FG_TERRAIN, FG_DEBUG,
265 " (North) Loading " << tile_diameter << " tiles" );
266 dw = tile_diameter / 2;
268 for ( i = 0; i < tile_diameter; i++ ) {
269 sched_tile( BucketOffset( i - dw, dh) );
273 FG_LOG( FG_TERRAIN, FG_DEBUG,
274 " (East) Loading " << tile_diameter << " tiles" );
275 dh = tile_diameter / 2;
277 for ( i = 0; i < tile_diameter; i++ ) {
278 sched_tile( BucketOffset( dw, i - dh ) );
282 dw = tile_diameter / 2;
284 FG_LOG( FG_TERRAIN, FG_DEBUG,
285 " (South) Loading " << tile_diameter << " tiles" );
286 for ( i = 0; i < tile_diameter; i++ ) {
287 sched_tile( BucketOffset( i - dw, dh) );
291 dh = tile_diameter / 2;
293 FG_LOG( FG_TERRAIN, FG_DEBUG,
294 " (West) Loading " << tile_diameter << " tiles" );
295 for ( i = 0; i < tile_diameter; i++ ) {
296 sched_tile( BucketOffset( dw, i - dh ) );
300 FG_LOG( FG_TERRAIN, FG_WARN, "UNKNOWN SCROLL DIRECTION in schedule_row" );
303 FG_LOG( FG_TERRAIN, FG_DEBUG, "\tschedule_row returns" );
307 void FGTileMgr::initialize_queue()
309 // First time through or we have teleported, initialize the
310 // system and load all relavant tiles
312 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << current_bucket );
313 FG_LOG( FG_TERRAIN, FG_INFO, " Updating Tile list for " << current_bucket );
314 FG_LOG( FG_TERRAIN, FG_INFO, " Loading "
315 << tile_diameter * tile_diameter << " tiles" );
318 scroll_direction = SCROLL_INIT;
320 // wipe/initialize tile cache
321 global_tile_cache.init();
322 previous_bucket.make_bad();
324 // build the local area list and schedule tiles for loading
326 // start with the center tile and work out in concentric
329 sched_tile( current_bucket );
331 for ( i = 3; i <= tile_diameter; i = i + 2 ) {
336 for ( j = -span; j <= span; ++j ) {
337 sched_tile( BucketOffset( j, -span ) );
341 for ( j = -span; j <= span; ++j ) {
342 sched_tile( BucketOffset( j, span ) );
346 for ( j = -span + 1; j <= span - 1; ++j ) {
347 sched_tile( BucketOffset( -span, j ) );
348 sched_tile( BucketOffset( span, j ) );
353 // Now force a load of the center tile and inner ring so we
354 // have something to see in our first frame.
355 for ( i = 0; i < 9; ++i ) {
356 if ( load_queue.size() ) {
357 FG_LOG( FG_TERRAIN, FG_DEBUG,
358 "Load queue not empty, loading a tile" );
360 FGLoadRec pending = load_queue.front();
361 load_queue.pop_front();
362 load_tile( pending.b, pending.cache_index );
368 // given the current lon/lat (in degrees), fill in the array of local
369 // chunks. If the chunk isn't already in the cache, then read it from
371 int FGTileMgr::update( double lon, double lat ) {
372 // FG_LOG( FG_TERRAIN, FG_DEBUG, "FGTileMgr::update()" );
374 // FGInterface *f = current_aircraft.fdm_state;
376 // lonlat for this update
377 // longitude = f->get_Longitude() * RAD_TO_DEG;
378 // latitude = f->get_Latitude() * RAD_TO_DEG;
381 // FG_LOG( FG_TERRAIN, FG_DEBUG, "lon "<< lonlat[LON] <<
382 // " lat " << lonlat[LAT] );
384 current_bucket.set_bucket( longitude, latitude );
385 // FG_LOG( FG_TERRAIN, FG_DEBUG, "Updating Tile list for " << current_bucket );
387 tile_index = global_tile_cache.exists(current_bucket);
388 // FG_LOG( FG_TERRAIN, FG_DEBUG, "tile index " << tile_index );
390 if ( tile_index >= 0 ) {
391 current_tile = global_tile_cache.get_tile(tile_index);
392 scenery.next_center = current_tile->center;
394 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found (Ok if initializing)" );
397 if ( state == Running ) {
398 if( current_bucket == previous_bucket) {
399 FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
400 scroll_direction = SCROLL_NONE;
402 // We've moved to a new bucket, we need to scroll our
403 // structures, and load in the new tiles
404 // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
405 // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
406 // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
408 if ( (current_bucket.get_lon() > previous_bucket.get_lon()) ||
409 ( (current_bucket.get_lon() == previous_bucket.get_lon()) &&
410 (current_bucket.get_x() > previous_bucket.get_x()) ) )
412 scroll_direction = SCROLL_EAST;
414 else if ( (current_bucket.get_lon() < previous_bucket.get_lon()) ||
415 ( (current_bucket.get_lon() == previous_bucket.get_lon()) &&
416 (current_bucket.get_x() < previous_bucket.get_x()) ) )
418 scroll_direction = SCROLL_WEST;
421 if ( (current_bucket.get_lat() > previous_bucket.get_lat()) ||
422 ( (current_bucket.get_lat() == previous_bucket.get_lat()) &&
423 (current_bucket.get_y() > previous_bucket.get_y()) ) )
425 scroll_direction = SCROLL_NORTH;
427 else if ( (current_bucket.get_lat() < previous_bucket.get_lat()) ||
428 ( (current_bucket.get_lat() == previous_bucket.get_lat()) &&
429 (current_bucket.get_y() < previous_bucket.get_y()) ) )
431 scroll_direction = SCROLL_SOUTH;
437 } else if ( state == Start || state == Inited ) {
442 if ( load_queue.size() ) {
443 FG_LOG( FG_TERRAIN, FG_DEBUG, "Load queue not empty, loading a tile" );
445 FGLoadRec pending = load_queue.front();
446 load_queue.pop_front();
447 load_tile( pending.b, pending.cache_index );
450 if ( scenery.center == Point3D(0.0) ) {
452 // cout << "initializing ... " << endl;
453 Point3D geod_pos = Point3D( longitude * DEG_TO_RAD,
454 latitude * DEG_TO_RAD,
456 Point3D tmp_abs_view_pos = fgGeodToCart( geod_pos );
457 scenery.center = tmp_abs_view_pos;
458 // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
460 current_elev_ssg( tmp_abs_view_pos,
463 // cout << "abs view pos = " << current_view.abs_view_pos
464 // << " view pos = " << current_view.view_pos << endl;
465 current_elev_ssg( current_view.abs_view_pos,
466 current_view.view_pos );
469 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
471 previous_bucket = current_bucket;
472 last_longitude = longitude;
473 last_latitude = latitude;
478 // Prepare the ssg nodes ... for each tile, set it's proper
479 // transform and update it's range selector based on current
481 void FGTileMgr::prep_ssg_node( int idx ) {
484 void FGTileMgr::prep_ssg_nodes( void ) {
490 #ifndef FG_OLD_WEATHER
491 if ( WeatherDatabase != NULL ) {
492 vis = WeatherDatabase->getWeatherVisibility();
497 vis = current_weather.get_visibility();
499 // cout << "visibility = " << vis << endl;
501 // traverse the potentially viewable tile list and update range
502 // selector and transform
503 for ( int i = 0; i < (int)global_tile_cache.get_size(); i++ ) {
504 t = global_tile_cache.get_tile( i );
506 if ( t->is_loaded() ) {
507 // set range selector (LOD trick) to be distance to center
508 // of tile + bounding radius
510 ranges[1] = vis + t->bounding_radius;
511 t->range_ptr->setRanges( ranges, 2 );
513 // calculate tile offset
514 t->SetOffset( scenery.center );
516 // calculate ssg transform
518 sgSetCoord( &sgcoord,
519 t->offset.x(), t->offset.y(), t->offset.z(),
521 t->transform_ptr->setTransform( &sgcoord );