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