]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilemgr.cxx
Rewrote the tile scheme to use a "map" structure rather than "vector"
[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 #include <simgear/xgl/xgl.h>
34
35 #include <simgear/constants.h>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/math/point3d.hxx>
38 #include <simgear/math/polar3d.hxx>
39 #include <simgear/math/sg_geodesy.hxx>
40 #include <simgear/math/vector.hxx>
41
42 #include <Main/globals.hxx>
43 #include <Objects/obj.hxx>
44
45 #ifndef FG_OLD_WEATHER
46 #  include <WeatherCM/FGLocalWeatherDatabase.h>
47 #else
48 #  include <Weather/weather.hxx>
49 #endif
50
51 #include "newcache.hxx"
52 #include "scenery.hxx"
53 #include "tilemgr.hxx"
54
55 #define TEST_LAST_HIT_CACHE
56
57 extern ssgRoot *scene;
58 extern ssgBranch *terrain;
59
60 // the tile manager
61 FGTileMgr global_tile_mgr;
62
63
64 // a temporary hack until we get everything rewritten with sgdVec3
65 static inline Point3D operator + (const Point3D& a, const sgdVec3 b)
66 {
67     return Point3D(a.x()+b[0], a.y()+b[1], a.z()+b[2]);
68 }
69
70
71 // Constructor
72 FGTileMgr::FGTileMgr():
73     state( Start )
74 {
75 }
76
77
78 // Destructor
79 FGTileMgr::~FGTileMgr() {
80 }
81
82
83 // Initialize the Tile Manager subsystem
84 int FGTileMgr::init() {
85     FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
86
87     if ( state != Start ) {
88         FG_LOG( FG_TERRAIN, FG_INFO,
89                 "... Reinitializing." );
90         destroy_queue();
91     } else {
92         FG_LOG( FG_TERRAIN, FG_INFO,
93                 "... First time through." );
94         global_tile_cache.init();
95     }
96
97     hit_list.clear();
98
99     state = Inited;
100
101     previous_bucket.make_bad();
102     current_bucket.make_bad();
103
104     longitude = latitude = -1000.0;
105     last_longitude = last_latitude = -1000.0;
106         
107     return 1;
108 }
109
110
111 // schedule a tile for loading
112 void FGTileMgr::sched_tile( const FGBucket& b ) {
113     // see if tile already exists in the cache
114     FGTileEntry *t = global_tile_cache.get_tile( b );
115
116     if ( t != NULL ) {
117         // tile exists in cache, reenable it.
118         // cout << "REENABLING DISABLED TILE" << endl;
119         t->select_ptr->select( 1 );
120     } else {
121         // register a load request
122         load_queue.push_back( b );
123     }
124 }
125
126
127 // load a tile
128 void FGTileMgr::load_tile( const FGBucket& b ) {
129     // see if tile already exists in the cache
130     FGTileEntry *t = global_tile_cache.get_tile( b );
131
132     if ( t == NULL ) {
133         FG_LOG( FG_TERRAIN, FG_DEBUG, "Loading tile " << b );
134         global_tile_cache.fill_in( b );
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, sgVec3 view_pos,
157                                   double *terrain_elev ) {
158     sgdVec3 orig, dir;
159
160     sgdSetVec3(orig, view_pos );
161     sgdCopyVec3(dir, abs_view_pos );
162
163     hit_list.Intersect( terrain, orig, dir );
164
165     int this_hit=0;
166     Point3D geoc;
167     double result = -9999;
168
169     int hitcount = hit_list.num_hits();
170     for ( int i = 0; i < hitcount; ++i ) {
171         geoc = sgCartToPolar3d( scenery.center + hit_list.get_point(i) );      
172         double lat_geod, alt, sea_level_r;
173         sgGeocToGeod(geoc.lat(), geoc.radius(), &lat_geod, 
174                      &alt, &sea_level_r);
175         if ( alt > result && alt < 10000 ) {
176             result = alt;
177             this_hit = i;
178         }
179     }
180
181     if ( result > -9000 ) {
182         *terrain_elev = result;
183         scenery.cur_radius = geoc.radius();
184         sgVec3 tmp;
185         sgSetVec3(tmp, hit_list.get_normal(this_hit));
186         ssgState *IntersectedLeafState =
187             ((ssgLeaf*)hit_list.get_entity(this_hit))->getState();
188         CurrentNormalInLocalPlane(tmp, tmp);
189         sgdSetVec3( scenery.cur_normal, tmp );
190         // cout << "NED: " << tmp[0] << " " << tmp[1] << " " << tmp[2] << endl;
191         return true;
192     } else {
193         FG_LOG( FG_TERRAIN, FG_INFO, "no terrain intersection" );
194         *terrain_elev = 0.0;
195         float *up = globals->get_current_view()->get_world_up();
196         sgdSetVec3(scenery.cur_normal, up[0], up[1], up[2]);
197         return false;
198     }
199 }
200
201
202 // schedule a needed buckets for loading
203 void FGTileMgr::schedule_needed() {
204     double vis;
205
206 #ifndef FG_OLD_WEATHER
207     if ( WeatherDatabase != NULL ) {
208         vis = WeatherDatabase->getWeatherVisibility();
209     } else {
210         vis = 16000;
211     }
212 #else
213     vis = current_weather.get_visibility();
214 #endif
215     cout << "visibility = " << vis << endl;
216
217     double clat = (int)current_bucket.get_center_lat();
218     if ( clat > 0 ) {
219         clat = (int)clat + 0.5;
220     } else {
221         clat = (int)clat - 0.5;
222     }
223     double clat_rad = clat * DEG_TO_RAD;
224     double cos_lat = cos( clat_rad );
225     double local_radius = cos_lat * EQUATORIAL_RADIUS_M;
226     double local_perimeter = 2.0 * local_radius * FG_PI;
227     double degree_width = local_perimeter / 360.0;
228
229     // cout << "clat = " << clat << endl;
230     // cout << "clat (radians) = " << clat_rad << endl;
231     // cout << "cos(lat) = " << cos_lat << endl;
232     // cout << "local_radius = " << local_radius << endl;
233     // cout << "local_perimeter = " << local_perimeter << endl;
234     cout << "degree_width = " << degree_width << endl;
235
236     double perimeter = 2.0 * EQUATORIAL_RADIUS_M * FG_PI;
237     double degree_height = perimeter / 360.0;
238     cout << "degree_height = " << degree_height << endl;
239
240     double tile_width = current_bucket.get_width() * degree_width;
241     double tile_height = current_bucket.get_height() * degree_height;
242     cout << "tile width = " << tile_width << "  tile_height = " << tile_height
243          << endl;
244
245     xrange = (int)(vis / tile_width) + 1;
246     yrange = (int)(vis / tile_height) + 1;
247     if ( xrange < 1 ) { xrange = 1; }
248     if ( yrange < 1 ) { yrange = 1; }
249     cout << "xrange = " << xrange << "  yrange = " << yrange << endl;
250
251     global_tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
252
253     for ( int x = -xrange; x <= xrange; ++x ) {
254         for ( int y = -yrange; y <= yrange; ++y ) {
255             FGBucket b = fgBucketOffset( longitude, latitude, x, y );
256             if ( ! global_tile_cache.exists( b ) ) {
257                 sched_tile( b );
258             }
259         }
260     }
261 }
262
263
264 void FGTileMgr::initialize_queue()
265 {
266     // First time through or we have teleported, initialize the
267     // system and load all relavant tiles
268
269     FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << current_bucket );
270     FG_LOG( FG_TERRAIN, FG_INFO, "  Loading " 
271             << xrange * yrange << " tiles" );
272     cout << "tile cache size = " << global_tile_cache.get_size() << endl;
273
274     int i;
275
276     // wipe/initialize tile cache
277     // global_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     // Now force a load of the center tile and inner ring so we
288     // have something to see in our first frame.
289     for ( i = 0; i < 9; ++i ) {
290         if ( load_queue.size() ) {
291             FG_LOG( FG_TERRAIN, FG_DEBUG, 
292                     "Load queue not empty, loading a tile" );
293
294             FGBucket pending = load_queue.front();
295             load_queue.pop_front();
296             load_tile( pending );
297         }
298     }
299 }
300
301
302 // forced emptying of the queue
303 // This is necessay to keep bookeeping straight for the
304 // tile_cache   -- which actually handles all the
305 // (de)allocations  
306 void FGTileMgr::destroy_queue() {
307     load_queue.clear();
308 }
309
310
311 // given the current lon/lat (in degrees), fill in the array of local
312 // chunks.  If the chunk isn't already in the cache, then read it from
313 // disk.
314 int FGTileMgr::update( double lon, double lat ) {
315     FG_LOG( FG_TERRAIN, FG_DEBUG, "FGTileMgr::update()" );
316
317     // FGInterface *f = current_aircraft.fdm_state;
318
319     // lonlat for this update 
320     // longitude = f->get_Longitude() * RAD_TO_DEG;
321     // latitude = f->get_Latitude() * RAD_TO_DEG;
322     longitude = lon;
323     latitude = lat;
324     // FG_LOG( FG_TERRAIN, FG_DEBUG, "lon "<< lonlat[LON] <<
325     //      " lat " << lonlat[LAT] );
326
327     current_bucket.set_bucket( longitude, latitude );
328     // FG_LOG( FG_TERRAIN, FG_DEBUG, "Updating Tile list for " << current_bucket );
329
330     if ( global_tile_cache.exists( current_bucket ) ) {
331         current_tile = global_tile_cache.get_tile( current_bucket );
332         scenery.next_center = current_tile->center;
333     } else {
334         FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found (Ok if initializing)" );
335     }
336
337     if ( state == Running ) {
338         if( current_bucket != previous_bucket) {
339             // We've moved to a new bucket, we need to schedule any
340             // needed tiles for loading.
341             schedule_needed();
342         }
343     } else if ( state == Start || state == Inited ) {
344         initialize_queue();
345         state = Running;
346     }
347
348     if ( load_queue.size() ) {
349         FG_LOG( FG_TERRAIN, FG_INFO, "Load queue size = " << load_queue.size()
350                 << " loading a tile" );
351
352         FGBucket pending = load_queue.front();
353         load_queue.pop_front();
354         load_tile( pending );
355     }
356
357     if ( scenery.center == Point3D(0.0) ) {
358         // initializing
359         // cout << "initializing ... " << endl;
360         sgdVec3 tmp_abs_view_pos;
361         sgVec3 tmp_view_pos;
362
363         Point3D geod_pos = Point3D( longitude * DEG_TO_RAD,
364                                     latitude * DEG_TO_RAD,
365                                     0.0);
366         Point3D tmp = sgGeodToCart( geod_pos );
367         scenery.center = tmp;
368         sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
369
370         // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
371         prep_ssg_nodes();
372         sgSetVec3( tmp_view_pos, 0.0, 0.0, 0.0 );
373         double tmp_elev;
374         if ( current_elev_ssg(tmp_abs_view_pos, tmp_view_pos, &tmp_elev) ) {
375             scenery.cur_elev = tmp_elev;
376         } else {
377             scenery.cur_elev = 0.0;
378         }
379     } else {
380         // cout << "abs view pos = " << current_view.abs_view_pos
381         //      << " view pos = " << current_view.view_pos << endl;
382         double tmp_elev;
383         if ( current_elev_ssg(globals->get_current_view()->get_abs_view_pos(),
384                               globals->get_current_view()->get_view_pos(),
385                               &tmp_elev) )
386         {
387             scenery.cur_elev = tmp_elev;
388         } else {
389             scenery.cur_elev = 0.0;
390         }  
391     }
392
393     // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
394
395     previous_bucket = current_bucket;
396     last_longitude = longitude;
397     last_latitude  = latitude;
398
399     return 1;
400 }
401
402
403 void FGTileMgr::prep_ssg_nodes() {
404     float vis = 0.0;
405
406 #ifndef FG_OLD_WEATHER
407     if ( WeatherDatabase ) {
408         vis = WeatherDatabase->getWeatherVisibility();
409     } else {
410         vis = 16000;
411     }
412 #else
413     vis = current_weather.get_visibility();
414 #endif
415     // cout << "visibility = " << vis << endl;
416
417     // traverse the potentially viewable tile list and update range
418     // selector and transform
419
420     FGTileEntry *e;
421     Point3D p = scenery.center;
422     global_tile_cache.reset_traversal();
423
424     while ( ! global_tile_cache.at_end() ) {
425         // cout << "processing a tile" << endl;
426         if ( (e = global_tile_cache.get_current()) ) {
427             e->prep_ssg_node( p, vis);
428         } else {
429             cout << "warning ... empty tile in cache" << endl;
430         }
431         global_tile_cache.next();
432    }
433 }