]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilemgr.cxx
a8a078a09c579b81743785d2d6e92b01b98d429f
[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 extern ssgBranch *ground;
61
62 // the tile manager
63 FGTileMgr global_tile_mgr;
64
65
66 #ifdef ENABLE_THREADS
67 SGLockedQueue<FGTileEntry *> FGTileMgr::attach_queue;
68 SGLockedQueue<FGDeferredModel *> FGTileMgr::model_queue;
69 #else
70 queue<FGTileEntry *> FGTileMgr::attach_queue;
71 queue<FGDeferredModel *> FGTileMgr::model_queue;
72 #endif // ENABLE_THREADS
73
74
75 // Constructor
76 FGTileMgr::FGTileMgr():
77     state( Start ),
78     vis( 16000 ),
79     counter_hack(0)
80 {
81 }
82
83
84 // Destructor
85 FGTileMgr::~FGTileMgr() {
86 }
87
88
89 // Initialize the Tile Manager subsystem
90 int FGTileMgr::init() {
91     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing Tile Manager subsystem." );
92
93     tile_cache.init();
94
95 #if 0
96
97     // instead it's just a lot easier to let any pending work flush
98     // through, rather than trying to arrest the queue and nuke all
99     // the various work at all the various stages and get everything
100     // cleaned up properly.
101
102     while ( ! attach_queue.empty() ) {
103         attach_queue.pop();
104     }
105
106     while ( ! model_queue.empty() ) {
107 #ifdef ENABLE_THREADS
108         FGDeferredModel* dm = model_queue.pop();
109 #else
110         FGDeferredModel* dm = model_queue.front();
111         model_queue.pop();
112 #endif
113         delete dm;
114     }
115     loader.reinit();
116 #endif
117
118     hit_list.clear();
119
120     state = Inited;
121
122     previous_bucket.make_bad();
123     current_bucket.make_bad();
124
125     longitude = latitude = -1000.0;
126     last_longitude = last_latitude = -1000.0;
127         
128     return 1;
129 }
130
131
132 // schedule a tile for loading
133 void FGTileMgr::sched_tile( const SGBucket& b ) {
134     // see if tile already exists in the cache
135     FGTileEntry *t = tile_cache.get_tile( b );
136
137     if ( t == NULL ) {
138         // create a new entry
139         FGTileEntry *e = new FGTileEntry( b );
140
141         // insert the tile into the cache
142         if ( tile_cache.insert_tile( e ) ) {
143           // Schedule tile for loading
144           loader.add( e );
145         } else {
146           // insert failed (cache full with no available entries to
147           // delete.)  Try again later
148           delete e;
149         }
150     }
151 }
152
153
154 // schedule a needed buckets for loading
155 void FGTileMgr::schedule_needed() {
156 #ifndef FG_OLD_WEATHER
157     if ( WeatherDatabase != NULL ) {
158         vis = WeatherDatabase->getWeatherVisibility();
159     } else {
160         vis = 16000;
161     }
162 #else
163     vis = current_weather.get_visibility();
164 #endif
165     // cout << "visibility = " << vis << endl;
166
167     double tile_width = current_bucket.get_width_m();
168     double tile_height = current_bucket.get_height_m();
169     // cout << "tile width = " << tile_width << "  tile_height = "
170     //      << tile_height !<< endl;
171
172     xrange = (int)(vis / tile_width) + 1;
173     yrange = (int)(vis / tile_height) + 1;
174     if ( xrange < 1 ) { xrange = 1; }
175     if ( yrange < 1 ) { yrange = 1; }
176     // cout << "xrange = " << xrange << "  yrange = " << yrange << endl;
177
178     tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
179
180     SGBucket b;
181
182     // schedule center tile first so it can be loaded first
183     b = sgBucketOffset( longitude, latitude, 0, 0 );
184     sched_tile( b );
185
186     int x, y;
187
188     // schedule next ring of 8 tiles
189     for ( x = -1; x <= 1; ++x ) {
190         for ( y = -1; y <= 1; ++y ) {
191             if ( x != 0 || y != 0 ) {
192                 b = sgBucketOffset( longitude, latitude, x, y );
193                 sched_tile( b );
194             }
195         }
196     }
197     
198     // schedule remaining tiles
199     for ( x = -xrange; x <= xrange; ++x ) {
200         for ( y = -yrange; y <= yrange; ++y ) {
201             if ( x < -1 || x > 1 || y < -1 || y > 1 ) {
202                 SGBucket b = sgBucketOffset( longitude, latitude, x, y );
203                 sched_tile( b );
204             }
205         }
206     }
207 }
208
209
210 void FGTileMgr::initialize_queue()
211 {
212     // First time through or we have teleported, initialize the
213     // system and load all relavant tiles
214
215     SG_LOG( SG_TERRAIN, SG_INFO, "Updating Tile list for " << current_bucket );
216     // cout << "tile cache size = " << tile_cache.get_size() << endl;
217
218     // wipe/initialize tile cache
219     // tile_cache.init();
220     previous_bucket.make_bad();
221
222     // build the local area list and schedule tiles for loading
223
224     // start with the center tile and work out in concentric
225     // "rings"
226
227     schedule_needed();
228
229     // do we really want to lose this? CLO
230 #if 0
231     // Now force a load of the center tile and inner ring so we
232     // have something to see in our first frame.
233     int i;
234     for ( i = 0; i < 9; ++i ) {
235         if ( load_queue.size() ) {
236             SG_LOG( SG_TERRAIN, SG_DEBUG, 
237                     "Load queue not empty, loading a tile" );
238
239             SGBucket pending = load_queue.front();
240             load_queue.pop_front();
241             load_tile( pending );
242         }
243     }
244 #endif
245 }
246
247
248 // given the current lon/lat (in degrees), fill in the array of local
249 // chunks.  If the chunk isn't already in the cache, then read it from
250 // disk.
251 int FGTileMgr::update( double lon, double lat ) {
252     SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update()" );
253
254     // FGInterface *f = current_aircraft.fdm_state;
255
256     // lonlat for this update 
257     // longitude = f->get_Longitude() * SGD_RADIANS_TO_DEGREES;
258     // latitude = f->get_Latitude() * SGD_RADIANS_TO_DEGREES;
259     longitude = lon;
260     latitude = lat;
261     // SG_LOG( SG_TERRAIN, SG_DEBUG, "lon "<< lonlat[LON] <<
262     //      " lat " << lonlat[LAT] );
263
264     current_bucket.set_bucket( longitude, latitude );
265     // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating Tile list for " << current_bucket );
266
267     if ( tile_cache.exists( current_bucket ) ) {
268         current_tile = tile_cache.get_tile( current_bucket );
269         scenery.next_center = current_tile->center;
270     } else {
271         SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
272     }
273
274     if ( state == Running ) {
275         if ( !(current_bucket == previous_bucket) ) {
276             // We've moved to a new bucket, we need to schedule any
277             // needed tiles for loading.
278             schedule_needed();
279         }
280     } else if ( state == Start || state == Inited ) {
281         initialize_queue();
282         state = Running;
283     }
284
285     // load the next tile in the load queue (or authorize the next
286     // load in the case of the threaded tile pager)
287     loader.update();
288
289     // load the next model in the load queue.  Currently this must
290     // happen in the render thread because model loading can trigger
291     // texture loading which involves use of the opengl api.
292     if ( !model_queue.empty() ) {
293         cout << "loading next model ..." << endl;
294         // load the next tile in the queue
295 #ifdef ENABLE_THREADS
296         FGDeferredModel* dm = model_queue.pop();
297 #else
298         FGDeferredModel* dm = model_queue.front();
299         model_queue.pop();
300 #endif
301         
302         ssgTexturePath( (char *)(dm->get_texture_path().c_str()) );
303         ssgEntity *obj_model
304             = ssgLoad( (char *)(dm->get_model_path().c_str()) );
305         if ( obj_model != NULL ) {
306             dm->get_obj_trans()->addKid( obj_model );
307         }
308         dm->get_tile()->dec_pending_models();
309
310         delete dm;
311     }
312
313     // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
314
315     previous_bucket = current_bucket;
316     last_longitude = longitude;
317     last_latitude  = latitude;
318
319     // activate loader thread one out of every 5 frames
320     if ( counter_hack == 0 ) {
321         // Notify the tile loader that it can load another tile
322         // loader.update();
323
324         if ( !attach_queue.empty() ) {
325 #ifdef ENABLE_THREADS
326             FGTileEntry* e = attach_queue.pop();
327 #else
328             FGTileEntry* e = attach_queue.front();
329             attach_queue.pop();
330 #endif
331             e->add_ssg_nodes( terrain, ground );
332             // cout << "Adding ssg nodes for "
333         }
334     }
335     counter_hack = (counter_hack + 1) % 5;
336     sgdVec3 sc;
337     sgdSetVec3( sc, scenery.center[0], scenery.center[1], scenery.center[2] );
338
339     if ( scenery.center == Point3D(0.0) ) {
340         // initializing
341         cout << "initializing scenery current elevation ... " << endl;
342         sgdVec3 tmp_abs_view_pos;
343
344         Point3D geod_pos = Point3D( longitude * SGD_DEGREES_TO_RADIANS,
345                                     latitude * SGD_DEGREES_TO_RADIANS,
346                                     0.0);
347         Point3D tmp = sgGeodToCart( geod_pos );
348         scenery.center = tmp;
349         sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
350
351         // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
352         prep_ssg_nodes();
353
354         double tmp_elev;
355         if ( fgCurrentElev(tmp_abs_view_pos, sc, &hit_list,
356                            &tmp_elev, &scenery.cur_radius, scenery.cur_normal) )
357         {
358             scenery.cur_elev = tmp_elev;
359         } else {
360             scenery.cur_elev = 0.0;
361         }
362         cout << "result = " << scenery.cur_elev << endl;
363     } else {
364         // cout << "abs view pos = " << current_view.abs_view_pos
365         //      << " view pos = " << current_view.view_pos << endl;
366         double tmp_elev;
367         if ( fgCurrentElev(globals->get_current_view()->get_abs_view_pos(),
368                            sc, &hit_list,
369                            &tmp_elev, &scenery.cur_radius, scenery.cur_normal) )
370         {
371             scenery.cur_elev = tmp_elev;
372         } else {
373             scenery.cur_elev = 0.0;
374         }  
375     }
376
377     return 1;
378 }
379
380
381 void FGTileMgr::prep_ssg_nodes() {
382     float vis = 0.0;
383
384 #ifndef FG_OLD_WEATHER
385     if ( WeatherDatabase ) {
386         vis = WeatherDatabase->getWeatherVisibility();
387     } else {
388         vis = 16000;
389     }
390 #else
391     vis = current_weather.get_visibility();
392 #endif
393     // cout << "visibility = " << vis << endl;
394
395     // traverse the potentially viewable tile list and update range
396     // selector and transform
397
398     FGTileEntry *e;
399     tile_cache.reset_traversal();
400
401     while ( ! tile_cache.at_end() ) {
402         // cout << "processing a tile" << endl;
403         if ( (e = tile_cache.get_current()) ) {
404             e->prep_ssg_node( scenery.center, vis);
405         } else {
406             cout << "warning ... empty tile in cache" << endl;
407         }
408         tile_cache.next();
409    }
410 }