]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilemgr.cxx
4bdcfda59c7ca9d16dd6c5b6188e4cb8b9007211
[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         // register a load request
120         tile_cache.load_tile( b );
121     }
122 }
123
124
125 // depricated for threading
126 #if 0
127 // load a tile
128 void FGTileMgr::load_tile( const SGBucket& b ) {
129     // see if tile already exists in the cache
130     FGTileEntry *t = tile_cache.get_tile( b );
131
132     if ( t == NULL ) {
133         SG_LOG( SG_TERRAIN, SG_DEBUG, "Loading tile " << b );
134         tile_cache.fill_in( b );
135         t = tile_cache.get_tile( b );
136         t->prep_ssg_node( scenery.center, vis);
137     } else {
138         SG_LOG( SG_TERRAIN, SG_DEBUG, "Tile already in cache " << b );
139     }
140 }
141 #endif
142
143
144 static void CurrentNormalInLocalPlane(sgVec3 dst, sgVec3 src) {
145     sgVec3 tmp;
146     sgSetVec3(tmp, src[0], src[1], src[2] );
147     sgMat4 TMP;
148     sgTransposeNegateMat4 ( TMP, globals->get_current_view()->get_UP() ) ;
149     sgXformVec3(tmp, tmp, TMP);
150     sgSetVec3(dst, tmp[2], tmp[1], tmp[0] );
151 }
152
153
154 // Determine scenery altitude via ssg.  Normally this just happens
155 // when we render the scene, but we'd also like to be able to do this
156 // explicitely.  lat & lon are in radians.  view_pos in current world
157 // coordinate translated near (0,0,0) (in meters.)  Returns result in
158 // meters.
159 bool FGTileMgr::current_elev_ssg( sgdVec3 abs_view_pos, double *terrain_elev ) {
160     sgdVec3 view_pos;
161     sgdVec3 sc;
162     sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
163     sgdSubVec3( view_pos, abs_view_pos, sc );
164
165     sgdVec3 orig, dir;
166     sgdCopyVec3(orig, view_pos );
167     sgdCopyVec3(dir, abs_view_pos );
168
169     hit_list.Intersect( terrain, orig, dir );
170
171     int this_hit=0;
172     Point3D geoc;
173     double result = -9999;
174
175     int hitcount = hit_list.num_hits();
176     for ( int i = 0; i < hitcount; ++i ) {
177         geoc = sgCartToPolar3d( scenery.center + hit_list.get_point(i) );      
178         double lat_geod, alt, sea_level_r;
179         sgGeocToGeod(geoc.lat(), geoc.radius(), &lat_geod, 
180                      &alt, &sea_level_r);
181         if ( alt > result && alt < 10000 ) {
182             result = alt;
183             this_hit = i;
184         }
185     }
186
187     if ( result > -9000 ) {
188         *terrain_elev = result;
189         scenery.cur_radius = geoc.radius();
190         sgVec3 tmp;
191         sgSetVec3(tmp, hit_list.get_normal(this_hit));
192         // cout << "cur_normal: " << tmp[0] << " " << tmp[1] << " "
193         //      << tmp[2] << endl;
194         ssgState *IntersectedLeafState =
195             ((ssgLeaf*)hit_list.get_entity(this_hit))->getState();
196         CurrentNormalInLocalPlane(tmp, tmp);
197         sgdSetVec3( scenery.cur_normal, tmp );
198         // cout << "NED: " << tmp[0] << " " << tmp[1] << " " << tmp[2] << endl;
199         return true;
200     } else {
201         SG_LOG( SG_TERRAIN, SG_INFO, "no terrain intersection" );
202         *terrain_elev = 0.0;
203         float *up = globals->get_current_view()->get_world_up();
204         sgdSetVec3(scenery.cur_normal, up[0], up[1], up[2]);
205         return false;
206     }
207 }
208
209
210 // schedule a needed buckets for loading
211 void FGTileMgr::schedule_needed() {
212 #ifndef FG_OLD_WEATHER
213     if ( WeatherDatabase != NULL ) {
214         vis = WeatherDatabase->getWeatherVisibility();
215     } else {
216         vis = 16000;
217     }
218 #else
219     vis = current_weather.get_visibility();
220 #endif
221     // cout << "visibility = " << vis << endl;
222
223     double tile_width = current_bucket.get_width_m();
224     double tile_height = current_bucket.get_height_m();
225     // cout << "tile width = " << tile_width << "  tile_height = "
226     //      << tile_height !<< endl;
227
228     xrange = (int)(vis / tile_width) + 1;
229     yrange = (int)(vis / tile_height) + 1;
230     if ( xrange < 1 ) { xrange = 1; }
231     if ( yrange < 1 ) { yrange = 1; }
232     // cout << "xrange = " << xrange << "  yrange = " << yrange << endl;
233
234     tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
235
236     SGBucket b;
237
238     // schedule center tile first so it can be loaded first
239     b = sgBucketOffset( longitude, latitude, 0, 0 );
240     sched_tile( b );
241
242     int x, y;
243
244     // schedule next ring of 8 tiles
245     for ( x = -1; x <= 1; ++x ) {
246         for ( y = -1; y <= 1; ++y ) {
247             if ( x != 0 || y != 0 ) {
248                 b = sgBucketOffset( longitude, latitude, x, y );
249                 sched_tile( b );
250             }
251         }
252     }
253     
254     // schedule remaining tiles
255     for ( x = -xrange; x <= xrange; ++x ) {
256         for ( y = -yrange; y <= yrange; ++y ) {
257             if ( x < -1 || x > 1 || y < -1 || y > 1 ) {
258                 SGBucket b = sgBucketOffset( longitude, latitude, x, y );
259                 sched_tile( b );
260             }
261         }
262     }
263 }
264
265
266 void FGTileMgr::initialize_queue()
267 {
268     // First time through or we have teleported, initialize the
269     // system and load all relavant tiles
270
271     SG_LOG( SG_TERRAIN, SG_INFO, "Updating Tile list for " << current_bucket );
272     // cout << "tile cache size = " << tile_cache.get_size() << endl;
273
274     int i;
275
276     // wipe/initialize tile cache
277     // tile_cache.init();
278     previous_bucket.make_bad();
279
280     // build the local area list and schedule tiles for loading
281
282     // start with the center tile and work out in concentric
283     // "rings"
284
285     schedule_needed();
286
287     // do we really want to lose this? CLO
288 #if 0
289     // Now force a load of the center tile and inner ring so we
290     // have something to see in our first frame.
291     for ( i = 0; i < 9; ++i ) {
292         if ( load_queue.size() ) {
293             SG_LOG( SG_TERRAIN, SG_DEBUG, 
294                     "Load queue not empty, loading a tile" );
295
296             SGBucket pending = load_queue.front();
297             load_queue.pop_front();
298             load_tile( pending );
299         }
300     }
301 #endif
302 }
303
304
305 // forced emptying of the queue
306 // This is necessay to keep bookeeping straight for the
307 // tile_cache   -- which actually handles all the
308 // (de)allocations  
309 void FGTileMgr::destroy_queue() {
310     // load_queue.clear();
311 }
312
313
314 // given the current lon/lat (in degrees), fill in the array of local
315 // chunks.  If the chunk isn't already in the cache, then read it from
316 // disk.
317 int FGTileMgr::update( double lon, double lat ) {
318     SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update()" );
319
320     // FGInterface *f = current_aircraft.fdm_state;
321
322     // lonlat for this update 
323     // longitude = f->get_Longitude() * SGD_RADIANS_TO_DEGREES;
324     // latitude = f->get_Latitude() * SGD_RADIANS_TO_DEGREES;
325     longitude = lon;
326     latitude = lat;
327     // SG_LOG( SG_TERRAIN, SG_DEBUG, "lon "<< lonlat[LON] <<
328     //      " lat " << lonlat[LAT] );
329
330     current_bucket.set_bucket( longitude, latitude );
331     // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating Tile list for " << current_bucket );
332
333     if ( tile_cache.exists( current_bucket ) ) {
334         current_tile = tile_cache.get_tile( current_bucket );
335         scenery.next_center = current_tile->center;
336     } else {
337         SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
338     }
339
340     if ( state == Running ) {
341         if ( !(current_bucket == previous_bucket) ) {
342             // We've moved to a new bucket, we need to schedule any
343             // needed tiles for loading.
344             schedule_needed();
345         }
346     } else if ( state == Start || state == Inited ) {
347         initialize_queue();
348         state = Running;
349     }
350
351     // now handled by threaded tile pager
352 #if 0
353     if ( load_queue.size() ) {
354         SG_LOG( SG_TERRAIN, SG_INFO, "Load queue size = " << load_queue.size()
355                 << " loading a tile" );
356
357         SGBucket pending = load_queue.front();
358         load_queue.pop_front();
359         load_tile( pending );
360     }
361 #endif
362
363     if ( scenery.center == Point3D(0.0) ) {
364         // initializing
365         cout << "initializing scenery current elevation  ... " << endl;
366         sgdVec3 tmp_abs_view_pos;
367         sgVec3 tmp_view_pos;
368
369         Point3D geod_pos = Point3D( longitude * SGD_DEGREES_TO_RADIANS,
370                                     latitude * SGD_DEGREES_TO_RADIANS,
371                                     0.0);
372         Point3D tmp = sgGeodToCart( geod_pos );
373         scenery.center = tmp;
374         sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
375
376         // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
377         prep_ssg_nodes();
378         sgSetVec3( tmp_view_pos, 0.0, 0.0, 0.0 );
379         double tmp_elev;
380         if ( current_elev_ssg(tmp_abs_view_pos, &tmp_elev) ) {
381             scenery.cur_elev = tmp_elev;
382         } else {
383             scenery.cur_elev = 0.0;
384         }
385         cout << "result = " << scenery.cur_elev << endl;
386     } else {
387         // cout << "abs view pos = " << current_view.abs_view_pos
388         //      << " view pos = " << current_view.view_pos << endl;
389         double tmp_elev;
390         if ( current_elev_ssg(globals->get_current_view()->get_abs_view_pos(),
391                               &tmp_elev) )
392         {
393             scenery.cur_elev = tmp_elev;
394         } else {
395             scenery.cur_elev = 0.0;
396         }  
397     }
398
399     // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
400
401     previous_bucket = current_bucket;
402     last_longitude = longitude;
403     last_latitude  = latitude;
404
405     return 1;
406 }
407
408
409 void FGTileMgr::prep_ssg_nodes() {
410     float vis = 0.0;
411
412 #ifndef FG_OLD_WEATHER
413     if ( WeatherDatabase ) {
414         vis = WeatherDatabase->getWeatherVisibility();
415     } else {
416         vis = 16000;
417     }
418 #else
419     vis = current_weather.get_visibility();
420 #endif
421     // cout << "visibility = " << vis << endl;
422
423     // traverse the potentially viewable tile list and update range
424     // selector and transform
425
426     FGTileEntry *e;
427     tile_cache.reset_traversal();
428
429     while ( ! tile_cache.at_end() ) {
430         // cout << "processing a tile" << endl;
431         if ( (e = tile_cache.get_current()) ) {
432             e->prep_ssg_node( scenery.center, vis);
433         } else {
434             cout << "warning ... empty tile in cache" << endl;
435         }
436         tile_cache.next();
437    }
438 }