]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilemgr.cxx
2f71d6bff73ae270499d85c233f7c0b1356bea68
[flightgear.git] / src / Scenery / tilemgr.cxx
1 // tilemgr.cxx -- routines to handle dynamic management of scenery tiles
2 //
3 // Written by Curtis Olson, started January 1998.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>
30 #endif
31
32 #include <GL/glut.h>
33 #include <simgear/xgl/xgl.h>
34
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>
41
42 #include <Aircraft/aircraft.hxx>
43 #include <Main/options.hxx>
44 #include <Main/views.hxx>
45 #include <Objects/obj.hxx>
46
47 #ifndef FG_OLD_WEATHER
48 #  include <WeatherCM/FGLocalWeatherDatabase.h>
49 #else
50 #  include <Weather/weather.hxx>
51 #endif
52
53 #include "scenery.hxx"
54 #include "tilecache.hxx"
55 #include "tilemgr.hxx"
56
57 #define TEST_LAST_HIT_CACHE
58
59 extern ssgRoot *scene;
60 extern ssgBranch *terrain;
61
62 // the tile manager
63 FGTileMgr global_tile_mgr;
64
65
66 // a temporary hack until we get everything rewritten with sgdVec3
67 static inline Point3D operator + (const Point3D& a, const sgdVec3 b)
68 {
69     return Point3D(a.x()+b[0], a.y()+b[1], a.z()+b[2]);
70 }
71
72
73 // Constructor
74 FGTileMgr::FGTileMgr ( void ):
75     state( Start )
76 {
77 }
78
79
80 // Destructor
81 FGTileMgr::~FGTileMgr ( void ) {
82 }
83
84
85 // Initialize the Tile Manager subsystem
86 int FGTileMgr::init( void ) {
87     FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
88
89     global_tile_cache.init();
90     hit_list.clear();
91
92     state = Inited;
93
94     // last_hit = 0;
95
96     tile_diameter = current_options.get_tile_diameter();
97     FG_LOG( FG_TERRAIN, FG_INFO, "Tile Diameter = " << tile_diameter);
98     
99     previous_bucket.make_bad();
100     current_bucket.make_bad();
101
102     scroll_direction = SCROLL_INIT;
103     tile_index = -9999;
104
105     longitude = latitude = -1000.0;
106     last_longitude = last_latitude = -1000.0;
107         
108     return 1;
109 }
110
111 #if 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 );
117     t->ssg_disable();
118 }
119 #endif
120
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 );
125
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 );
131         t->mark_loaded();
132     } else {
133         // find the next available cache entry and mark it as
134         // scheduled
135         cache_index = global_tile_cache.next_avail();
136         FGTileEntry *t = global_tile_cache.get_tile( cache_index );
137         t->mark_scheduled_for_use();
138
139         // register a load request
140         FGLoadRec request;
141         request.b = b;
142         request.cache_index = cache_index;
143         load_queue.push_back( request );
144     }
145
146     return cache_index;
147 }
148
149
150 // load a tile
151 void FGTileMgr::load_tile( const FGBucket& b, int cache_index) {
152
153     FG_LOG( FG_TERRAIN, FG_DEBUG, "Loading tile " << b );
154
155     global_tile_cache.fill_in(cache_index, b);
156
157     FG_LOG( FG_TERRAIN, FG_DEBUG, "Loaded for cache index: " << cache_index );
158 }
159
160
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
165 // meters.
166
167 bool
168 FGTileMgr::current_elev_ssg( const Point3D& abs_view_pos, 
169                              const Point3D& view_pos )
170 {
171     sgdVec3 orig, dir;
172
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() );
175
176     hit_list.Intersect( terrain, orig, dir );
177
178     int this_hit=0;
179     Point3D geoc;
180     double result = -9999;
181
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, 
187                      &alt, &sea_level_r);
188         if ( alt > result && alt < 10000 ) {
189             result = alt;
190             this_hit = i;
191         }
192     }
193
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));
198         return true;
199     } else {
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]);
204         return false;
205     }
206 }
207
208
209 FGBucket FGTileMgr::BucketOffset( int dx, int dy )
210 {
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;
215
216         // walk dy units in the lat direction
217         pending.set_bucket( longitude, clat );
218
219         // find the lon span for the new latitude
220         span = bucket_span( clat );
221         
222         // walk dx units in the lon direction
223         clon = longitude + dx * span;
224     } else      {
225         pending.set_bucket( last_longitude, last_latitude );
226         clat = pending.get_center_lat() + dy * FG_BUCKET_SPAN;
227
228         // walk dy units in the lat direction
229         pending.set_bucket( last_longitude, clat );
230
231         // find the lon span for the new latitude
232         span = bucket_span( clat );
233
234         // walk dx units in the lon direction
235         clon = last_longitude + dx * span;
236     }    
237         
238     while ( clon < -180.0 ) clon += 360.0;
239     while ( clon >= 180.0 ) clon -= 360.0;
240     pending.set_bucket( clon, clat );
241
242     FG_LOG( FG_TERRAIN, FG_DEBUG, "    fgBucketOffset " << pending );
243     return pending;
244 }
245
246
247 // schedule a tile row(column) for loading
248 void FGTileMgr::scroll( void )
249 {
250     FG_LOG( FG_TERRAIN, FG_DEBUG, "schedule_row: Scrolling" );
251
252     int i, dw, dh;
253         
254     switch( scroll_direction ) {
255     case SCROLL_NORTH:
256         FG_LOG( FG_TERRAIN, FG_DEBUG, 
257                 "  (North) Loading " << tile_diameter << " tiles" );
258         dw = tile_diameter / 2;
259         dh = dw + 1;
260         for ( i = 0; i < tile_diameter; i++ ) {
261             sched_tile( BucketOffset( i - dw, dh) );
262         }
263         break;
264     case SCROLL_EAST:
265         FG_LOG( FG_TERRAIN, FG_DEBUG, 
266                 "  (East) Loading " << tile_diameter << " tiles" );
267         dh = tile_diameter / 2;
268         dw = dh + 1;
269         for ( i = 0; i < tile_diameter; i++ ) {
270             sched_tile( BucketOffset( dw, i - dh ) );
271         }
272         break;
273     case SCROLL_SOUTH:
274         dw = tile_diameter / 2;
275         dh = -dw - 1;
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) );
280         }
281         break;
282     case SCROLL_WEST:
283         dh = tile_diameter / 2;
284         dw = -dh - 1;
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 ) );
289         }
290         break;
291     default:
292         FG_LOG( FG_TERRAIN, FG_WARN, "UNKNOWN SCROLL DIRECTION in schedule_row" );
293         ;
294     }
295     FG_LOG( FG_TERRAIN, FG_DEBUG, "\tschedule_row returns" );
296 }
297
298
299 void FGTileMgr::initialize_queue( void )
300 {
301     // First time through or we have teleported, initialize the
302     // system and load all relavant tiles
303
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" );
309
310     int i;
311     scroll_direction = SCROLL_INIT;
312
313     // wipe/initialize tile cache
314     global_tile_cache.init();
315     previous_bucket.make_bad();
316
317     // build the local area list and schedule tiles for loading
318
319     // start with the center tile and work out in concentric
320     // "rings"
321
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 );
326
327     current_view.abs_view_pos = fgGeodToCart( geod_view_center );
328     current_view.view_pos = current_view.abs_view_pos - scenery.next_center;
329
330     for ( i = 3; i <= tile_diameter; i = i + 2 ) {
331         int j;
332         int span = i / 2;
333
334         // bottom row
335         for ( j = -span; j <= span; ++j ) {
336             sched_tile( BucketOffset( j, -span ) );
337         }
338
339         // top row
340         for ( j = -span; j <= span; ++j ) {
341             sched_tile( BucketOffset( j, span ) );
342         }
343
344         // middle rows
345         for ( j = -span + 1; j <= span - 1; ++j ) {
346             sched_tile( BucketOffset( -span, j ) );
347             sched_tile( BucketOffset( span, j ) );
348         }
349
350     }
351
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" );
358
359             FGLoadRec pending = load_queue.front();
360             load_queue.pop_front();
361             load_tile( pending.b, pending.cache_index );
362         }
363     }
364 }
365
366
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( double junk1, double junk2 ) {
370     // FG_LOG( FG_TERRAIN, FG_DEBUG, "FGTileMgr::update()" );
371
372     FGInterface *f = current_aircraft.fdm_state;
373
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] );
379
380     current_bucket.set_bucket( longitude, latitude );
381     // FG_LOG( FG_TERRAIN, FG_DEBUG, "Updating Tile list for " << current_bucket );
382
383     tile_index = global_tile_cache.exists(current_bucket);
384     // FG_LOG( FG_TERRAIN, FG_DEBUG, "tile index " << tile_index );
385
386     if ( tile_index >= 0 ) {
387         current_tile = global_tile_cache.get_tile(tile_index);
388         scenery.next_center = current_tile->center;
389     } else {
390         FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
391     }
392
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;
397         } else {
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.
403
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()) ) )
407                 {
408                     scroll_direction = SCROLL_EAST;
409                 }
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()) ) )
413                 {   
414                     scroll_direction = SCROLL_WEST;
415                 }   
416
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()) ) )
420                 {   
421                     scroll_direction = SCROLL_NORTH;
422                 }
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()) ) )
426                 {
427                     scroll_direction = SCROLL_SOUTH;
428                 }
429
430             scroll();
431         }
432
433     } else if ( (state == Start) || (state == Inited) ) {
434         initialize_queue();
435         state = Running;
436     }
437
438     if ( load_queue.size() ) {
439         FG_LOG( FG_TERRAIN, FG_DEBUG, "Load queue not empty, loading a tile" );
440
441         FGLoadRec pending = load_queue.front();
442         load_queue.pop_front();
443         load_tile( pending.b, pending.cache_index );
444     }
445
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);
449
450     // cout << "current elevation (old) == " 
451     //      << current_elev( f->get_Longitude(), f->get_Latitude(), 
452     //                       tmp_abs_view_pos ) 
453     //      << endl;
454
455     // set scenery.cur_elev and scenery.cur_radius
456
457     current_elev_ssg( current_view.abs_view_pos,
458                       current_view.view_pos );
459     // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
460
461     previous_bucket = current_bucket;
462     last_longitude = longitude;
463     last_latitude  = latitude;
464
465     return 1;
466 }
467
468 // Prepare the ssg nodes ... for each tile, set it's proper
469 // transform and update it's range selector based on current
470 // visibilty
471 void FGTileMgr::prep_ssg_node( int idx ) {
472 }
473
474 void FGTileMgr::prep_ssg_nodes( void ) {
475     FGTileEntry *t;
476     float ranges[2];
477     ranges[0] = 0.0f;
478
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 );
483
484         if ( t->is_loaded() ) {
485             // set range selector (LOD trick) to be distance to center
486             // of tile + bounding radius
487
488 #ifndef FG_OLD_WEATHER
489             ranges[1] = WeatherDatabase->getWeatherVisibility()
490                 + t->bounding_radius;
491 #else
492             ranges[1] = current_weather.get_visibility()+t->bounding_radius;
493 #endif
494             t->range_ptr->setRanges( ranges, 2 );
495
496             // calculate tile offset
497             t->SetOffset( scenery.center );
498
499             // calculate ssg transform
500             sgCoord sgcoord;
501             sgSetCoord( &sgcoord,
502                         t->offset.x(), t->offset.y(), t->offset.z(),
503                         0.0, 0.0, 0.0 );
504             t->transform_ptr->setTransform( &sgcoord );
505         }
506     }
507 }