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/point3d.hxx>
38 #include <simgear/math/polar3d.hxx>
39 #include <simgear/math/sg_geodesy.hxx>
40 #include <simgear/math/vector.hxx>
42 // #include <Aircraft/aircraft.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 "scenery.hxx"
53 #include "tilecache.hxx"
54 #include "tilemgr.hxx"
56 #define TEST_LAST_HIT_CACHE
58 extern ssgRoot *scene;
59 extern ssgBranch *terrain;
62 FGTileMgr global_tile_mgr;
65 // a temporary hack until we get everything rewritten with sgdVec3
66 static inline Point3D operator + (const Point3D& a, const sgdVec3 b)
68 return Point3D(a.x()+b[0], a.y()+b[1], a.z()+b[2]);
73 FGTileMgr::FGTileMgr ( void ):
80 FGTileMgr::~FGTileMgr ( void ) {
84 // Initialize the Tile Manager subsystem
85 int FGTileMgr::init( void ) {
86 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
88 if ( state != Start ) {
89 FG_LOG( FG_TERRAIN, FG_INFO,
90 "... Reinitializing." );
93 FG_LOG( FG_TERRAIN, FG_INFO,
94 "... First time through." );
97 global_tile_cache.init();
102 tile_diameter = globals->get_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 int FGTileMgr::sched_tile( const FGBucket& b ) {
120 // see if tile already exists in the cache
121 int cache_index = global_tile_cache.exists( b );
123 if ( cache_index >= 0 ) {
124 // tile exists in cache, reenable it.
125 // cout << "REENABLING DISABLED TILE" << endl;
126 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
127 t->select_ptr->select( 1 );
130 // find the next available cache entry and mark it as
132 cache_index = global_tile_cache.next_avail();
133 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
134 t->mark_scheduled_for_use();
136 // register a load request
139 request.cache_index = cache_index;
140 load_queue.push_back( request );
148 void FGTileMgr::load_tile( const FGBucket& b, int cache_index) {
150 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loading tile " << b );
151 global_tile_cache.fill_in(cache_index, b);
152 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loaded for cache index: " << cache_index );
156 // Determine scenery altitude via ssg. Normally this just happens
157 // when we render the scene, but we'd also like to be able to do this
158 // explicitely. lat & lon are in radians. view_pos in current world
159 // coordinate translated near (0,0,0) (in meters.) Returns result in
163 FGTileMgr::current_elev_ssg( sgdVec3 abs_view_pos,
168 sgdSetVec3(orig, view_pos );
169 sgdCopyVec3(dir, abs_view_pos );
171 hit_list.Intersect( terrain, orig, dir );
175 double result = -9999;
177 int hitcount = hit_list.num_hits();
178 for ( int i = 0; i < hitcount; ++i ) {
179 geoc = sgCartToPolar3d( scenery.center + hit_list.get_point(i) );
180 double lat_geod, alt, sea_level_r;
181 sgGeocToGeod(geoc.lat(), geoc.radius(), &lat_geod,
183 if ( alt > result && alt < 10000 ) {
189 if ( result > -9000 ) {
190 scenery.cur_elev = result;
191 scenery.cur_radius = geoc.radius();
193 sgSetVec3(tmp, hit_list.get_normal(this_hit));
194 ssgState *IntersectedLeafState =
195 ((ssgLeaf*)hit_list.get_entity(this_hit))->getState();
196 globals->get_current_view()->CurrentNormalInLocalPlane(tmp, tmp);
197 sgdSetVec3( scenery.cur_normal, tmp );
198 // cout << "NED: " << tmp[0] << " " << tmp[1] << " " << tmp[2] << endl;
201 FG_LOG( FG_TERRAIN, FG_INFO, "no terrain intersection" );
202 scenery.cur_elev = 0.0;
203 float *up = globals->get_current_view()->get_local_up();
204 sgdSetVec3(scenery.cur_normal, up[0], up[1], up[2]);
210 FGBucket FGTileMgr::BucketOffset( int dx, int dy )
212 double clat, clon, span;
213 if( scroll_direction == SCROLL_INIT ) {
214 // use current latitude and longitude
215 // walk dy units in the lat direction
216 clat = current_bucket.get_center_lat() + dy * FG_BUCKET_SPAN;
218 // find the lon span for the new latitude
219 span = bucket_span( clat );
221 // walk dx units in the lon direction
222 clon = longitude + dx * span;
224 // use previous latitude and longitude
225 // walk dy units in the lat direction
226 clat = previous_bucket.get_center_lat() + dy * FG_BUCKET_SPAN;
228 // find the lon span for the new latitude
229 span = bucket_span( clat );
231 // walk dx units in the lon direction
232 clon = last_longitude + dx * span;
235 while ( clon < -180.0 ) clon += 360.0;
236 while ( clon >= 180.0 ) clon -= 360.0;
237 pending.set_bucket( clon, clat );
239 FG_LOG( FG_TERRAIN, FG_DEBUG, " fgBucketOffset " << pending );
244 // schedule a tile row(column) for loading
245 void FGTileMgr::scroll( void )
247 FG_LOG( FG_TERRAIN, FG_DEBUG, "schedule_row: Scrolling" );
251 switch( scroll_direction ) {
253 FG_LOG( FG_TERRAIN, FG_DEBUG,
254 " (North) Loading " << tile_diameter << " tiles" );
255 dw = tile_diameter / 2;
257 for ( i = 0; i < tile_diameter; i++ ) {
258 sched_tile( BucketOffset( i - dw, dh ) );
262 FG_LOG( FG_TERRAIN, FG_DEBUG,
263 " (East) Loading " << tile_diameter << " tiles" );
264 dh = tile_diameter / 2;
266 for ( i = 0; i < tile_diameter; i++ ) {
267 sched_tile( BucketOffset( dw, i - dh ) );
271 FG_LOG( FG_TERRAIN, FG_DEBUG,
272 " (South) Loading " << tile_diameter << " tiles" );
273 dw = tile_diameter / 2;
275 for ( i = 0; i < tile_diameter; i++ ) {
276 sched_tile( BucketOffset( i - dw, dh ) );
280 FG_LOG( FG_TERRAIN, FG_DEBUG,
281 " (West) Loading " << tile_diameter << " tiles" );
282 dh = tile_diameter / 2;
284 for ( i = 0; i < tile_diameter; i++ ) {
285 sched_tile( BucketOffset( dw, i - dh ) );
289 FG_LOG( FG_TERRAIN, FG_WARN, "UNKNOWN SCROLL DIRECTION in schedule_row" );
292 FG_LOG( FG_TERRAIN, FG_DEBUG, "\tschedule_row returns" );
296 void FGTileMgr::initialize_queue()
298 // First time through or we have teleported, initialize the
299 // system and load all relavant tiles
301 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << current_bucket );
302 FG_LOG( FG_TERRAIN, FG_INFO, " Updating Tile list for " << current_bucket );
303 FG_LOG( FG_TERRAIN, FG_INFO, " Loading "
304 << tile_diameter * tile_diameter << " tiles" );
307 scroll_direction = SCROLL_INIT;
309 // wipe/initialize tile cache
310 global_tile_cache.init();
311 previous_bucket.make_bad();
313 // build the local area list and schedule tiles for loading
315 // start with the center tile and work out in concentric
318 sched_tile( current_bucket );
320 for ( i = 3; i <= tile_diameter; i = i + 2 ) {
325 for ( j = -span; j <= span; ++j ) {
326 sched_tile( BucketOffset( j, -span ) );
330 for ( j = -span; j <= span; ++j ) {
331 sched_tile( BucketOffset( j, span ) );
335 for ( j = -span + 1; j <= span - 1; ++j ) {
336 sched_tile( BucketOffset( -span, j ) );
337 sched_tile( BucketOffset( span, j ) );
342 // Now force a load of the center tile and inner ring so we
343 // have something to see in our first frame.
344 for ( i = 0; i < 9; ++i ) {
345 if ( load_queue.size() ) {
346 FG_LOG( FG_TERRAIN, FG_DEBUG,
347 "Load queue not empty, loading a tile" );
349 FGLoadRec pending = load_queue.front();
350 load_queue.pop_front();
351 load_tile( pending.b, pending.cache_index );
357 // forced emptying of the queue
358 // This is necessay to keep bookeeping straight for the
359 // tile_cache -- which actually handles all the
361 void FGTileMgr::destroy_queue() {
362 while( load_queue.size() ) {
363 FG_LOG( FG_TERRAIN, FG_INFO,
364 "Load queue not empty, popping a tile" );
365 FGLoadRec pending = load_queue.front();
366 load_queue.pop_front();
367 FGTileEntry *t = global_tile_cache.get_tile( pending.cache_index );
368 // just t->mark_unused() should be enough
369 // but a little paranoia doesn't hurt us here
370 if(t->is_scheduled_for_use())
373 load_tile( pending.b, pending.cache_index );
378 // given the current lon/lat (in degrees), fill in the array of local
379 // chunks. If the chunk isn't already in the cache, then read it from
381 int FGTileMgr::update( double lon, double lat ) {
382 // FG_LOG( FG_TERRAIN, FG_DEBUG, "FGTileMgr::update()" );
384 // FGInterface *f = current_aircraft.fdm_state;
386 // lonlat for this update
387 // longitude = f->get_Longitude() * RAD_TO_DEG;
388 // latitude = f->get_Latitude() * RAD_TO_DEG;
391 // FG_LOG( FG_TERRAIN, FG_DEBUG, "lon "<< lonlat[LON] <<
392 // " lat " << lonlat[LAT] );
394 current_bucket.set_bucket( longitude, latitude );
395 // FG_LOG( FG_TERRAIN, FG_DEBUG, "Updating Tile list for " << current_bucket );
397 tile_index = global_tile_cache.exists(current_bucket);
398 // FG_LOG( FG_TERRAIN, FG_DEBUG, "tile index " << tile_index );
400 if ( tile_index >= 0 ) {
401 current_tile = global_tile_cache.get_tile(tile_index);
402 scenery.next_center = current_tile->center;
404 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found (Ok if initializing)" );
407 if ( state == Running ) {
408 if( current_bucket == previous_bucket) {
409 FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
410 scroll_direction = SCROLL_NONE;
412 // We've moved to a new bucket, we need to scroll our
413 // structures, and load in the new tiles
414 // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
415 // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
416 // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
418 if ( (current_bucket.get_lon() > previous_bucket.get_lon()) ||
419 ( (current_bucket.get_lon() == previous_bucket.get_lon()) &&
420 (current_bucket.get_x() > previous_bucket.get_x()) ) )
422 scroll_direction = SCROLL_EAST;
424 else if ( (current_bucket.get_lon() < previous_bucket.get_lon()) ||
425 ( (current_bucket.get_lon() == previous_bucket.get_lon()) &&
426 (current_bucket.get_x() < previous_bucket.get_x()) ) )
428 scroll_direction = SCROLL_WEST;
431 if ( (current_bucket.get_lat() > previous_bucket.get_lat()) ||
432 ( (current_bucket.get_lat() == previous_bucket.get_lat()) &&
433 (current_bucket.get_y() > previous_bucket.get_y()) ) )
435 scroll_direction = SCROLL_NORTH;
437 else if ( (current_bucket.get_lat() < previous_bucket.get_lat()) ||
438 ( (current_bucket.get_lat() == previous_bucket.get_lat()) &&
439 (current_bucket.get_y() < previous_bucket.get_y()) ) )
441 scroll_direction = SCROLL_SOUTH;
447 } else if ( state == Start || state == Inited ) {
452 if ( load_queue.size() ) {
453 FG_LOG( FG_TERRAIN, FG_DEBUG, "Load queue not empty, loading a tile" );
455 FGLoadRec pending = load_queue.front();
456 load_queue.pop_front();
457 load_tile( pending.b, pending.cache_index );
460 if ( scenery.center == Point3D(0.0) ) {
462 // cout << "initializing ... " << endl;
463 sgdVec3 tmp_abs_view_pos;
466 Point3D geod_pos = Point3D( longitude * DEG_TO_RAD,
467 latitude * DEG_TO_RAD,
469 Point3D tmp = sgGeodToCart( geod_pos );
470 scenery.center = tmp;
471 sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
473 // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
475 sgSetVec3( tmp_view_pos, 0.0, 0.0, 0.0 );
476 current_elev_ssg( tmp_abs_view_pos, tmp_view_pos );
478 // cout << "abs view pos = " << current_view.abs_view_pos
479 // << " view pos = " << current_view.view_pos << endl;
480 current_elev_ssg( globals->get_current_view()->get_abs_view_pos(),
481 globals->get_current_view()->get_view_pos() );
484 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
486 previous_bucket = current_bucket;
487 last_longitude = longitude;
488 last_latitude = latitude;
493 // Prepare the ssg nodes ... for each tile, set it's proper
494 // transform and update it's range selector based on current
496 void FGTileMgr::prep_ssg_node( int idx ) {
499 void FGTileMgr::prep_ssg_nodes( void ) {
505 #ifndef FG_OLD_WEATHER
506 if ( WeatherDatabase != NULL ) {
507 vis = WeatherDatabase->getWeatherVisibility();
512 vis = current_weather.get_visibility();
514 // cout << "visibility = " << vis << endl;
516 // traverse the potentially viewable tile list and update range
517 // selector and transform
518 for ( int i = 0; i < (int)global_tile_cache.get_size(); i++ ) {
519 t = global_tile_cache.get_tile( i );
521 if ( t->is_loaded() ) {
522 // set range selector (LOD trick) to be distance to center
523 // of tile + bounding radius
525 ranges[1] = vis + t->bounding_radius;
526 t->range_ptr->setRanges( ranges, 2 );
528 // calculate tile offset
529 t->SetOffset( scenery.center );
531 // calculate ssg transform
533 sgSetCoord( &sgcoord,
534 t->offset.x(), t->offset.y(), t->offset.z(),
536 t->transform_ptr->setTransform( &sgcoord );