]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilemgr.cxx
Tweaks to the tile pager so it waits for a signal from the main thread before
[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
34 #include <plib/ssg.h>
35
36 #include <simgear/constants.h>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/math/point3d.hxx>
39 #include <simgear/math/polar3d.hxx>
40 #include <simgear/math/sg_geodesy.hxx>
41 #include <simgear/math/vector.hxx>
42
43 #include <Main/globals.hxx>
44 #include <Objects/obj.hxx>
45
46 #ifndef FG_OLD_WEATHER
47 #  include <WeatherCM/FGLocalWeatherDatabase.h>
48 #else
49 #  include <Weather/weather.hxx>
50 #endif
51
52 #include "newcache.hxx"
53 #include "scenery.hxx"
54 #include "tilemgr.hxx"
55
56 #define TEST_LAST_HIT_CACHE
57
58 extern ssgRoot *scene;
59 extern ssgBranch *terrain;
60
61 // the tile manager
62 FGTileMgr global_tile_mgr;
63
64
65 // a temporary hack until we get everything rewritten with sgdVec3
66 static inline Point3D operator + (const Point3D& a, const sgdVec3 b)
67 {
68     return Point3D(a.x()+b[0], a.y()+b[1], a.z()+b[2]);
69 }
70
71
72 // Constructor
73 FGTileMgr::FGTileMgr():
74     state( Start ),
75     vis( 16000 )
76 {
77 }
78
79
80 // Destructor
81 FGTileMgr::~FGTileMgr() {
82 }
83
84
85 // Initialize the Tile Manager subsystem
86 int FGTileMgr::init() {
87     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing Tile Manager subsystem." );
88
89     if ( state != Start ) {
90         SG_LOG( SG_TERRAIN, SG_INFO,
91                 "... Reinitializing." );
92         destroy_queue();
93     } else {
94         SG_LOG( SG_TERRAIN, SG_INFO,
95                 "... First time through." );
96         tile_cache.init();
97     }
98
99     hit_list.clear();
100
101     state = Inited;
102
103     previous_bucket.make_bad();
104     current_bucket.make_bad();
105
106     longitude = latitude = -1000.0;
107     last_longitude = last_latitude = -1000.0;
108         
109     return 1;
110 }
111
112
113 // schedule a tile for loading
114 void FGTileMgr::sched_tile( const SGBucket& b ) {
115     // see if tile already exists in the cache
116     FGTileEntry *t = tile_cache.get_tile( b );
117
118     if ( t == NULL ) {
119         // create a new entry
120         FGTileEntry *e = new FGTileEntry( b );
121
122         // insert the tile into the cache
123         tile_cache.insert_tile( e );
124
125         // Schedule tile for loading
126         loader.add( e );
127     }
128 }
129
130
131 // depricated for threading
132 #if 0
133 // load a tile
134 void FGTileMgr::load_tile( const SGBucket& b ) {
135     // see if tile already exists in the cache
136     FGTileEntry *t = tile_cache.get_tile( b );
137
138     if ( t == NULL ) {
139         SG_LOG( SG_TERRAIN, SG_DEBUG, "Loading tile " << b );
140         tile_cache.fill_in( b );
141         t = tile_cache.get_tile( b );
142         t->prep_ssg_node( scenery.center, vis);
143     } else {
144         SG_LOG( SG_TERRAIN, SG_DEBUG, "Tile already in cache " << b );
145     }
146 }
147 #endif
148
149
150 static void CurrentNormalInLocalPlane(sgVec3 dst, sgVec3 src) {
151     sgVec3 tmp;
152     sgSetVec3(tmp, src[0], src[1], src[2] );
153     sgMat4 TMP;
154     sgTransposeNegateMat4 ( TMP, globals->get_current_view()->get_UP() ) ;
155     sgXformVec3(tmp, tmp, TMP);
156     sgSetVec3(dst, tmp[2], tmp[1], tmp[0] );
157 }
158
159
160 // Determine scenery altitude via ssg.  Normally this just happens
161 // when we render the scene, but we'd also like to be able to do this
162 // explicitely.  lat & lon are in radians.  view_pos in current world
163 // coordinate translated near (0,0,0) (in meters.)  Returns result in
164 // meters.
165 bool FGTileMgr::current_elev_ssg( sgdVec3 abs_view_pos, double *terrain_elev ) {
166     sgdVec3 view_pos;
167     sgdVec3 sc;
168     sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
169     sgdSubVec3( view_pos, abs_view_pos, sc );
170
171     sgdVec3 orig, dir;
172     sgdCopyVec3(orig, view_pos );
173     sgdCopyVec3(dir, abs_view_pos );
174
175     hit_list.Intersect( terrain, orig, dir );
176
177     int this_hit=0;
178     Point3D geoc;
179     double result = -9999;
180
181     int hitcount = hit_list.num_hits();
182     for ( int i = 0; i < hitcount; ++i ) {
183         geoc = sgCartToPolar3d( scenery.center + hit_list.get_point(i) );      
184         double lat_geod, alt, sea_level_r;
185         sgGeocToGeod(geoc.lat(), geoc.radius(), &lat_geod, 
186                      &alt, &sea_level_r);
187         if ( alt > result && alt < 10000 ) {
188             result = alt;
189             this_hit = i;
190         }
191     }
192
193     if ( result > -9000 ) {
194         *terrain_elev = result;
195         scenery.cur_radius = geoc.radius();
196         sgVec3 tmp;
197         sgSetVec3(tmp, hit_list.get_normal(this_hit));
198         // cout << "cur_normal: " << tmp[0] << " " << tmp[1] << " "
199         //      << tmp[2] << endl;
200         ssgState *IntersectedLeafState =
201             ((ssgLeaf*)hit_list.get_entity(this_hit))->getState();
202         CurrentNormalInLocalPlane(tmp, tmp);
203         sgdSetVec3( scenery.cur_normal, tmp );
204         // cout << "NED: " << tmp[0] << " " << tmp[1] << " " << tmp[2] << endl;
205         return true;
206     } else {
207         SG_LOG( SG_TERRAIN, SG_INFO, "no terrain intersection" );
208         *terrain_elev = 0.0;
209         float *up = globals->get_current_view()->get_world_up();
210         sgdSetVec3(scenery.cur_normal, up[0], up[1], up[2]);
211         return false;
212     }
213 }
214
215
216 // schedule a needed buckets for loading
217 void FGTileMgr::schedule_needed() {
218 #ifndef FG_OLD_WEATHER
219     if ( WeatherDatabase != NULL ) {
220         vis = WeatherDatabase->getWeatherVisibility();
221     } else {
222         vis = 16000;
223     }
224 #else
225     vis = current_weather.get_visibility();
226 #endif
227     // cout << "visibility = " << vis << endl;
228
229     double tile_width = current_bucket.get_width_m();
230     double tile_height = current_bucket.get_height_m();
231     // cout << "tile width = " << tile_width << "  tile_height = "
232     //      << tile_height !<< endl;
233
234     xrange = (int)(vis / tile_width) + 1;
235     yrange = (int)(vis / tile_height) + 1;
236     if ( xrange < 1 ) { xrange = 1; }
237     if ( yrange < 1 ) { yrange = 1; }
238     // cout << "xrange = " << xrange << "  yrange = " << yrange << endl;
239
240     tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
241
242     SGBucket b;
243
244     // schedule center tile first so it can be loaded first
245     b = sgBucketOffset( longitude, latitude, 0, 0 );
246     sched_tile( b );
247
248     int x, y;
249
250     // schedule next ring of 8 tiles
251     for ( x = -1; x <= 1; ++x ) {
252         for ( y = -1; y <= 1; ++y ) {
253             if ( x != 0 || y != 0 ) {
254                 b = sgBucketOffset( longitude, latitude, x, y );
255                 sched_tile( b );
256             }
257         }
258     }
259     
260     // schedule remaining tiles
261     for ( x = -xrange; x <= xrange; ++x ) {
262         for ( y = -yrange; y <= yrange; ++y ) {
263             if ( x < -1 || x > 1 || y < -1 || y > 1 ) {
264                 SGBucket b = sgBucketOffset( longitude, latitude, x, y );
265                 sched_tile( b );
266             }
267         }
268     }
269 }
270
271
272 void FGTileMgr::initialize_queue()
273 {
274     // First time through or we have teleported, initialize the
275     // system and load all relavant tiles
276
277     SG_LOG( SG_TERRAIN, SG_INFO, "Updating Tile list for " << current_bucket );
278     // cout << "tile cache size = " << tile_cache.get_size() << endl;
279
280     // wipe/initialize tile cache
281     // tile_cache.init();
282     previous_bucket.make_bad();
283
284     // build the local area list and schedule tiles for loading
285
286     // start with the center tile and work out in concentric
287     // "rings"
288
289     schedule_needed();
290
291     // do we really want to lose this? CLO
292 #if 0
293     // Now force a load of the center tile and inner ring so we
294     // have something to see in our first frame.
295     int i;
296     for ( i = 0; i < 9; ++i ) {
297         if ( load_queue.size() ) {
298             SG_LOG( SG_TERRAIN, SG_DEBUG, 
299                     "Load queue not empty, loading a tile" );
300
301             SGBucket pending = load_queue.front();
302             load_queue.pop_front();
303             load_tile( pending );
304         }
305     }
306 #endif
307 }
308
309
310 // forced emptying of the queue
311 // This is necessay to keep bookeeping straight for the
312 // tile_cache   -- which actually handles all the
313 // (de)allocations  
314 void FGTileMgr::destroy_queue() {
315     // load_queue.clear();
316 }
317
318
319 // given the current lon/lat (in degrees), fill in the array of local
320 // chunks.  If the chunk isn't already in the cache, then read it from
321 // disk.
322 int FGTileMgr::update( double lon, double lat ) {
323     SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update()" );
324
325     // FGInterface *f = current_aircraft.fdm_state;
326
327     // lonlat for this update 
328     // longitude = f->get_Longitude() * SGD_RADIANS_TO_DEGREES;
329     // latitude = f->get_Latitude() * SGD_RADIANS_TO_DEGREES;
330     longitude = lon;
331     latitude = lat;
332     // SG_LOG( SG_TERRAIN, SG_DEBUG, "lon "<< lonlat[LON] <<
333     //      " lat " << lonlat[LAT] );
334
335     current_bucket.set_bucket( longitude, latitude );
336     // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating Tile list for " << current_bucket );
337
338     if ( tile_cache.exists( current_bucket ) ) {
339         current_tile = tile_cache.get_tile( current_bucket );
340         scenery.next_center = current_tile->center;
341     } else {
342         SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
343     }
344
345     if ( state == Running ) {
346         if ( !(current_bucket == previous_bucket) ) {
347             // We've moved to a new bucket, we need to schedule any
348             // needed tiles for loading.
349             schedule_needed();
350         }
351     } else if ( state == Start || state == Inited ) {
352         initialize_queue();
353         state = Running;
354     }
355
356     // now handled by threaded tile pager
357 #if 0
358     if ( load_queue.size() ) {
359         SG_LOG( SG_TERRAIN, SG_INFO, "Load queue size = " << load_queue.size()
360                 << " loading a tile" );
361
362         SGBucket pending = load_queue.front();
363         load_queue.pop_front();
364         load_tile( pending );
365     }
366 #endif
367
368     if ( scenery.center == Point3D(0.0) ) {
369         // initializing
370         cout << "initializing scenery current elevation  ... " << endl;
371         sgdVec3 tmp_abs_view_pos;
372         sgVec3 tmp_view_pos;
373
374         Point3D geod_pos = Point3D( longitude * SGD_DEGREES_TO_RADIANS,
375                                     latitude * SGD_DEGREES_TO_RADIANS,
376                                     0.0);
377         Point3D tmp = sgGeodToCart( geod_pos );
378         scenery.center = tmp;
379         sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
380
381         // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
382         prep_ssg_nodes();
383         sgSetVec3( tmp_view_pos, 0.0, 0.0, 0.0 );
384         double tmp_elev;
385         if ( current_elev_ssg(tmp_abs_view_pos, &tmp_elev) ) {
386             scenery.cur_elev = tmp_elev;
387         } else {
388             scenery.cur_elev = 0.0;
389         }
390         cout << "result = " << scenery.cur_elev << endl;
391     } else {
392         // cout << "abs view pos = " << current_view.abs_view_pos
393         //      << " view pos = " << current_view.view_pos << endl;
394         double tmp_elev;
395         if ( current_elev_ssg(globals->get_current_view()->get_abs_view_pos(),
396                               &tmp_elev) )
397         {
398             scenery.cur_elev = tmp_elev;
399         } else {
400             scenery.cur_elev = 0.0;
401         }  
402     }
403
404     // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
405
406     previous_bucket = current_bucket;
407     last_longitude = longitude;
408     last_latitude  = latitude;
409
410     // activate loader thread one out of every 5 frames
411     counter_hack = (counter_hack + 1) % 5;
412     if ( !counter_hack ) {
413         // Notify the tile loader that it can load another tile
414         loader.update();
415     }
416
417     return 1;
418 }
419
420
421 void FGTileMgr::prep_ssg_nodes() {
422     float vis = 0.0;
423
424 #ifndef FG_OLD_WEATHER
425     if ( WeatherDatabase ) {
426         vis = WeatherDatabase->getWeatherVisibility();
427     } else {
428         vis = 16000;
429     }
430 #else
431     vis = current_weather.get_visibility();
432 #endif
433     // cout << "visibility = " << vis << endl;
434
435     // traverse the potentially viewable tile list and update range
436     // selector and transform
437
438     FGTileEntry *e;
439     tile_cache.reset_traversal();
440
441     while ( ! tile_cache.at_end() ) {
442         // cout << "processing a tile" << endl;
443         if ( (e = tile_cache.get_current()) ) {
444             e->prep_ssg_node( scenery.center, vis);
445         } else {
446             cout << "warning ... empty tile in cache" << endl;
447         }
448         tile_cache.next();
449    }
450 }