]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilemgr.cxx
Updates to JSBsim from Jon's CVS.
[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     if ( state != Start ) {
90         FG_LOG( FG_TERRAIN, FG_INFO,
91                 "ReInitializing the Tile Manager subsystem." );
92
93         // This is necessay to keep bookeeping straight for the
94         // tile_cache   -- which actually handles all the
95         // (de)allocations  
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 );
102         }
103     } else {
104         FG_LOG( FG_TERRAIN, FG_INFO,
105                 "Initializing Tile Manager subsystem." );
106     }
107
108     global_tile_cache.init();
109     hit_list.clear();
110
111     state = Inited;
112
113     tile_diameter = current_options.get_tile_diameter();
114     FG_LOG( FG_TERRAIN, FG_INFO, "Tile Diameter = " << tile_diameter);
115     
116     previous_bucket.make_bad();
117     current_bucket.make_bad();
118
119     scroll_direction = SCROLL_INIT;
120     tile_index = -9999;
121
122     longitude = latitude = -1000.0;
123     last_longitude = last_latitude = -1000.0;
124         
125     return 1;
126 }
127
128
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 );
133
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 );
139         t->mark_loaded();
140     } else {
141         // find the next available cache entry and mark it as
142         // scheduled
143         cache_index = global_tile_cache.next_avail();
144         FGTileEntry *t = global_tile_cache.get_tile( cache_index );
145         t->mark_scheduled_for_use();
146
147         // register a load request
148         FGLoadRec request;
149         request.b = b;
150         request.cache_index = cache_index;
151         load_queue.push_back( request );
152     }
153
154     return cache_index;
155 }
156
157
158 // load a tile
159 void FGTileMgr::load_tile( const FGBucket& b, int cache_index) {
160
161     FG_LOG( FG_TERRAIN, FG_DEBUG, "Loading tile " << b );
162
163     global_tile_cache.fill_in(cache_index, b);
164
165     FG_LOG( FG_TERRAIN, FG_DEBUG, "Loaded for cache index: " << cache_index );
166 }
167
168
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
173 // meters.
174
175 bool
176 FGTileMgr::current_elev_ssg( const Point3D& abs_view_pos, 
177                              const Point3D& view_pos )
178 {
179     sgdVec3 orig, dir;
180
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() );
183
184     hit_list.Intersect( terrain, orig, dir );
185
186     int this_hit=0;
187     Point3D geoc;
188     double result = -9999;
189
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, 
195                      &alt, &sea_level_r);
196         if ( alt > result && alt < 10000 ) {
197             result = alt;
198             this_hit = i;
199         }
200     }
201
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));
206         return true;
207     } else {
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]);
212         return false;
213     }
214 }
215
216
217 FGBucket FGTileMgr::BucketOffset( int dx, int dy )
218 {
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;
223
224         // walk dy units in the lat direction
225         pending.set_bucket( longitude, clat );
226
227         // find the lon span for the new latitude
228         span = bucket_span( clat );
229         
230         // walk dx units in the lon direction
231         clon = longitude + dx * span;
232     } else      {
233         pending.set_bucket( last_longitude, last_latitude );
234         clat = pending.get_center_lat() + dy * FG_BUCKET_SPAN;
235
236         // walk dy units in the lat direction
237         pending.set_bucket( last_longitude, clat );
238
239         // find the lon span for the new latitude
240         span = bucket_span( clat );
241
242         // walk dx units in the lon direction
243         clon = last_longitude + dx * span;
244     }    
245         
246     while ( clon < -180.0 ) clon += 360.0;
247     while ( clon >= 180.0 ) clon -= 360.0;
248     pending.set_bucket( clon, clat );
249
250     FG_LOG( FG_TERRAIN, FG_DEBUG, "    fgBucketOffset " << pending );
251     return pending;
252 }
253
254
255 // schedule a tile row(column) for loading
256 void FGTileMgr::scroll( void )
257 {
258     FG_LOG( FG_TERRAIN, FG_DEBUG, "schedule_row: Scrolling" );
259
260     int i, dw, dh;
261         
262     switch( scroll_direction ) {
263     case SCROLL_NORTH:
264         FG_LOG( FG_TERRAIN, FG_DEBUG, 
265                 "  (North) Loading " << tile_diameter << " tiles" );
266         dw = tile_diameter / 2;
267         dh = dw + 1;
268         for ( i = 0; i < tile_diameter; i++ ) {
269             sched_tile( BucketOffset( i - dw, dh) );
270         }
271         break;
272     case SCROLL_EAST:
273         FG_LOG( FG_TERRAIN, FG_DEBUG, 
274                 "  (East) Loading " << tile_diameter << " tiles" );
275         dh = tile_diameter / 2;
276         dw = dh + 1;
277         for ( i = 0; i < tile_diameter; i++ ) {
278             sched_tile( BucketOffset( dw, i - dh ) );
279         }
280         break;
281     case SCROLL_SOUTH:
282         dw = tile_diameter / 2;
283         dh = -dw - 1;
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) );
288         }
289         break;
290     case SCROLL_WEST:
291         dh = tile_diameter / 2;
292         dw = -dh - 1;
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 ) );
297         }
298         break;
299     default:
300         FG_LOG( FG_TERRAIN, FG_WARN, "UNKNOWN SCROLL DIRECTION in schedule_row" );
301         ;
302     }
303     FG_LOG( FG_TERRAIN, FG_DEBUG, "\tschedule_row returns" );
304 }
305
306
307 void FGTileMgr::initialize_queue( void )
308 {
309     // First time through or we have teleported, initialize the
310     // system and load all relavant tiles
311
312     FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << current_bucket );
313     FG_LOG( FG_TERRAIN, FG_INFO, "  First time through ... " );
314     FG_LOG( FG_TERRAIN, FG_INFO, "  Updating Tile list for " << current_bucket );
315     FG_LOG( FG_TERRAIN, FG_INFO, "  Loading " 
316             << tile_diameter * tile_diameter << " tiles" );
317
318     int i;
319     scroll_direction = SCROLL_INIT;
320
321     // wipe/initialize tile cache
322     global_tile_cache.init();
323     previous_bucket.make_bad();
324
325     // build the local area list and schedule tiles for loading
326
327     // start with the center tile and work out in concentric
328     // "rings"
329
330     sched_tile( current_bucket );
331     Point3D geod_view_center( current_bucket.get_center_lon(), 
332                               current_bucket.get_center_lat(), 
333                               cur_fdm_state->get_Altitude()*FEET_TO_METER + 3 );
334
335     current_view.abs_view_pos = fgGeodToCart( geod_view_center );
336     current_view.view_pos = current_view.abs_view_pos - scenery.next_center;
337
338     for ( i = 3; i <= tile_diameter; i = i + 2 ) {
339         int j;
340         int span = i / 2;
341
342         // bottom row
343         for ( j = -span; j <= span; ++j ) {
344             sched_tile( BucketOffset( j, -span ) );
345         }
346
347         // top row
348         for ( j = -span; j <= span; ++j ) {
349             sched_tile( BucketOffset( j, span ) );
350         }
351
352         // middle rows
353         for ( j = -span + 1; j <= span - 1; ++j ) {
354             sched_tile( BucketOffset( -span, j ) );
355             sched_tile( BucketOffset( span, j ) );
356         }
357
358     }
359
360     // Now force a load of the center tile and inner ring so we
361     // have something to see in our first frame.
362     for ( i = 0; i < 9; ++i ) {
363         if ( load_queue.size() ) {
364             FG_LOG( FG_TERRAIN, FG_DEBUG, 
365                     "Load queue not empty, loading a tile" );
366
367             FGLoadRec pending = load_queue.front();
368             load_queue.pop_front();
369             load_tile( pending.b, pending.cache_index );
370         }
371     }
372 }
373
374
375 // given the current lon/lat, fill in the array of local chunks.  If
376 // the chunk isn't already in the cache, then read it from disk.
377 int FGTileMgr::update( double junk1, double junk2 ) {
378     // FG_LOG( FG_TERRAIN, FG_DEBUG, "FGTileMgr::update()" );
379
380     FGInterface *f = current_aircraft.fdm_state;
381
382     // lonlat for this update 
383     longitude = f->get_Longitude() * RAD_TO_DEG;
384     latitude = f->get_Latitude() * RAD_TO_DEG;
385     // FG_LOG( FG_TERRAIN, FG_DEBUG, "lon "<< lonlat[LON] <<
386     //      " lat " << lonlat[LAT] );
387
388     current_bucket.set_bucket( longitude, latitude );
389     // FG_LOG( FG_TERRAIN, FG_DEBUG, "Updating Tile list for " << current_bucket );
390
391     tile_index = global_tile_cache.exists(current_bucket);
392     // FG_LOG( FG_TERRAIN, FG_DEBUG, "tile index " << tile_index );
393
394     if ( tile_index >= 0 ) {
395         current_tile = global_tile_cache.get_tile(tile_index);
396         scenery.next_center = current_tile->center;
397     } else {
398         FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
399     }
400
401     if ( state == Running ) {
402         if( current_bucket == previous_bucket) {
403             FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
404             scroll_direction = SCROLL_NONE;
405         } else {
406             // We've moved to a new bucket, we need to scroll our
407             // structures, and load in the new tiles
408             // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
409             // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
410             // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
411
412             if ( (current_bucket.get_lon() > previous_bucket.get_lon()) ||
413                  ( (current_bucket.get_lon() == previous_bucket.get_lon()) && 
414                    (current_bucket.get_x() > previous_bucket.get_x()) ) )
415                 {
416                     scroll_direction = SCROLL_EAST;
417                 }
418             else 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()) ) )
421                 {   
422                     scroll_direction = SCROLL_WEST;
423                 }   
424
425             if ( (current_bucket.get_lat() > previous_bucket.get_lat()) ||
426                  ( (current_bucket.get_lat() == previous_bucket.get_lat()) && 
427                    (current_bucket.get_y() > previous_bucket.get_y()) ) )
428                 {   
429                     scroll_direction = SCROLL_NORTH;
430                 }
431             else 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()) ) )
434                 {
435                     scroll_direction = SCROLL_SOUTH;
436                 }
437
438             scroll();
439         }
440
441     } else if ( (state == Start) || (state == Inited) ) {
442         initialize_queue();
443         state = Running;
444     }
445
446     if ( load_queue.size() ) {
447         FG_LOG( FG_TERRAIN, FG_DEBUG, "Load queue not empty, loading a tile" );
448
449         FGLoadRec pending = load_queue.front();
450         load_queue.pop_front();
451         load_tile( pending.b, pending.cache_index );
452     }
453
454     // find our current elevation (feed in the current bucket to save work)
455     // Point3D geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
456     // Point3D tmp_abs_view_pos = fgGeodToCart(geod_pos);
457
458     // cout << "current elevation (old) == " 
459     //      << current_elev( f->get_Longitude(), f->get_Latitude(), 
460     //                       tmp_abs_view_pos ) 
461     //      << endl;
462
463     // set scenery.cur_elev and scenery.cur_radius
464
465     current_elev_ssg( current_view.abs_view_pos,
466                       current_view.view_pos );
467     // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
468
469     previous_bucket = current_bucket;
470     last_longitude = longitude;
471     last_latitude  = latitude;
472
473     return 1;
474 }
475
476 // Prepare the ssg nodes ... for each tile, set it's proper
477 // transform and update it's range selector based on current
478 // visibilty
479 void FGTileMgr::prep_ssg_node( int idx ) {
480 }
481
482 void FGTileMgr::prep_ssg_nodes( void ) {
483     FGTileEntry *t;
484     float ranges[2];
485     ranges[0] = 0.0f;
486
487     // traverse the potentially viewable tile list and update range
488     // selector and transform
489     for ( int i = 0; i < (int)global_tile_cache.get_size(); i++ ) {
490         t = global_tile_cache.get_tile( i );
491
492         if ( t->is_loaded() ) {
493             // set range selector (LOD trick) to be distance to center
494             // of tile + bounding radius
495
496 #ifndef FG_OLD_WEATHER
497             ranges[1] = WeatherDatabase->getWeatherVisibility()
498                 + t->bounding_radius;
499 #else
500             ranges[1] = current_weather.get_visibility()+t->bounding_radius;
501 #endif
502             t->range_ptr->setRanges( ranges, 2 );
503
504             // calculate tile offset
505             t->SetOffset( scenery.center );
506
507             // calculate ssg transform
508             sgCoord sgcoord;
509             sgSetCoord( &sgcoord,
510                         t->offset.x(), t->offset.y(), t->offset.z(),
511                         0.0, 0.0, 0.0 );
512             t->transform_ptr->setTransform( &sgcoord );
513         }
514     }
515 }