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