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