1 // tilemgr.cxx -- routines to handle dynamic management of scenery tiles
3 // Written by Curtis Olson, started January 1998.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
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>
43 #include <Main/globals.hxx>
44 #include <Objects/obj.hxx>
46 #ifndef FG_OLD_WEATHER
47 # include <WeatherCM/FGLocalWeatherDatabase.h>
49 # include <Weather/weather.hxx>
52 #include "newcache.hxx"
53 #include "scenery.hxx"
54 #include "tilemgr.hxx"
56 #define TEST_LAST_HIT_CACHE
58 extern ssgRoot *scene;
59 extern ssgBranch *terrain;
60 extern ssgBranch *ground;
63 FGTileMgr global_tile_mgr;
66 // a temporary hack until we get everything rewritten with sgdVec3
67 static inline Point3D operator + (const Point3D& a, const sgdVec3 b)
69 return Point3D(a.x()+b[0], a.y()+b[1], a.z()+b[2]);
73 SGLockedQueue<FGTileEntry*> FGTileMgr::loaded_queue;
74 #endif // ENABLE_THREADS
77 FGTileMgr::FGTileMgr():
86 FGTileMgr::~FGTileMgr() {
90 // Initialize the Tile Manager subsystem
91 int FGTileMgr::init() {
92 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing Tile Manager subsystem." );
94 if ( state != Start ) {
95 SG_LOG( SG_TERRAIN, SG_INFO,
96 "... Reinitializing." );
99 SG_LOG( SG_TERRAIN, SG_INFO,
100 "... First time through." );
108 previous_bucket.make_bad();
109 current_bucket.make_bad();
111 longitude = latitude = -1000.0;
112 last_longitude = last_latitude = -1000.0;
118 // schedule a tile for loading
119 void FGTileMgr::sched_tile( const SGBucket& b ) {
120 // see if tile already exists in the cache
121 FGTileEntry *t = tile_cache.get_tile( b );
124 // create a new entry
125 FGTileEntry *e = new FGTileEntry( b );
127 // insert the tile into the cache
128 tile_cache.insert_tile( e );
130 // Schedule tile for loading
136 // depricated for threading
139 void FGTileMgr::load_tile( const SGBucket& b ) {
140 // see if tile already exists in the cache
141 FGTileEntry *t = tile_cache.get_tile( b );
144 SG_LOG( SG_TERRAIN, SG_DEBUG, "Loading tile " << b );
145 tile_cache.fill_in( b );
146 t = tile_cache.get_tile( b );
147 t->prep_ssg_node( scenery.center, vis);
149 SG_LOG( SG_TERRAIN, SG_DEBUG, "Tile already in cache " << b );
155 static void CurrentNormalInLocalPlane(sgVec3 dst, sgVec3 src) {
157 sgSetVec3(tmp, src[0], src[1], src[2] );
159 sgTransposeNegateMat4 ( TMP, globals->get_current_view()->get_UP() ) ;
160 sgXformVec3(tmp, tmp, TMP);
161 sgSetVec3(dst, tmp[2], tmp[1], tmp[0] );
165 // Determine scenery altitude via ssg. Normally this just happens
166 // when we render the scene, but we'd also like to be able to do this
167 // explicitely. lat & lon are in radians. view_pos in current world
168 // coordinate translated near (0,0,0) (in meters.) Returns result in
170 bool FGTileMgr::current_elev_ssg( sgdVec3 abs_view_pos, double *terrain_elev ) {
173 sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
174 sgdSubVec3( view_pos, abs_view_pos, sc );
177 sgdCopyVec3(orig, view_pos );
178 sgdCopyVec3(dir, abs_view_pos );
180 hit_list.Intersect( terrain, orig, dir );
184 double result = -9999;
186 int hitcount = hit_list.num_hits();
187 for ( int i = 0; i < hitcount; ++i ) {
188 geoc = sgCartToPolar3d( scenery.center + hit_list.get_point(i) );
189 double lat_geod, alt, sea_level_r;
190 sgGeocToGeod(geoc.lat(), geoc.radius(), &lat_geod,
192 if ( alt > result && alt < 10000 ) {
198 if ( result > -9000 ) {
199 *terrain_elev = result;
200 scenery.cur_radius = geoc.radius();
202 sgSetVec3(tmp, hit_list.get_normal(this_hit));
203 // cout << "cur_normal: " << tmp[0] << " " << tmp[1] << " "
204 // << tmp[2] << endl;
205 ssgState *IntersectedLeafState =
206 ((ssgLeaf*)hit_list.get_entity(this_hit))->getState();
207 CurrentNormalInLocalPlane(tmp, tmp);
208 sgdSetVec3( scenery.cur_normal, tmp );
209 // cout << "NED: " << tmp[0] << " " << tmp[1] << " " << tmp[2] << endl;
212 SG_LOG( SG_TERRAIN, SG_INFO, "no terrain intersection" );
214 float *up = globals->get_current_view()->get_world_up();
215 sgdSetVec3(scenery.cur_normal, up[0], up[1], up[2]);
221 // schedule a needed buckets for loading
222 void FGTileMgr::schedule_needed() {
223 #ifndef FG_OLD_WEATHER
224 if ( WeatherDatabase != NULL ) {
225 vis = WeatherDatabase->getWeatherVisibility();
230 vis = current_weather.get_visibility();
232 // cout << "visibility = " << vis << endl;
234 double tile_width = current_bucket.get_width_m();
235 double tile_height = current_bucket.get_height_m();
236 // cout << "tile width = " << tile_width << " tile_height = "
237 // << tile_height !<< endl;
239 xrange = (int)(vis / tile_width) + 1;
240 yrange = (int)(vis / tile_height) + 1;
241 if ( xrange < 1 ) { xrange = 1; }
242 if ( yrange < 1 ) { yrange = 1; }
243 // cout << "xrange = " << xrange << " yrange = " << yrange << endl;
245 tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
249 // schedule center tile first so it can be loaded first
250 b = sgBucketOffset( longitude, latitude, 0, 0 );
255 // schedule next ring of 8 tiles
256 for ( x = -1; x <= 1; ++x ) {
257 for ( y = -1; y <= 1; ++y ) {
258 if ( x != 0 || y != 0 ) {
259 b = sgBucketOffset( longitude, latitude, x, y );
265 // schedule remaining tiles
266 for ( x = -xrange; x <= xrange; ++x ) {
267 for ( y = -yrange; y <= yrange; ++y ) {
268 if ( x < -1 || x > 1 || y < -1 || y > 1 ) {
269 SGBucket b = sgBucketOffset( longitude, latitude, x, y );
277 void FGTileMgr::initialize_queue()
279 // First time through or we have teleported, initialize the
280 // system and load all relavant tiles
282 SG_LOG( SG_TERRAIN, SG_INFO, "Updating Tile list for " << current_bucket );
283 // cout << "tile cache size = " << tile_cache.get_size() << endl;
285 // wipe/initialize tile cache
286 // tile_cache.init();
287 previous_bucket.make_bad();
289 // build the local area list and schedule tiles for loading
291 // start with the center tile and work out in concentric
296 // do we really want to lose this? CLO
298 // Now force a load of the center tile and inner ring so we
299 // have something to see in our first frame.
301 for ( i = 0; i < 9; ++i ) {
302 if ( load_queue.size() ) {
303 SG_LOG( SG_TERRAIN, SG_DEBUG,
304 "Load queue not empty, loading a tile" );
306 SGBucket pending = load_queue.front();
307 load_queue.pop_front();
308 load_tile( pending );
315 // forced emptying of the queue
316 // This is necessay to keep bookeeping straight for the
317 // tile_cache -- which actually handles all the
319 void FGTileMgr::destroy_queue() {
320 // load_queue.clear();
324 // given the current lon/lat (in degrees), fill in the array of local
325 // chunks. If the chunk isn't already in the cache, then read it from
327 int FGTileMgr::update( double lon, double lat ) {
328 SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update()" );
330 // FGInterface *f = current_aircraft.fdm_state;
332 // lonlat for this update
333 // longitude = f->get_Longitude() * SGD_RADIANS_TO_DEGREES;
334 // latitude = f->get_Latitude() * SGD_RADIANS_TO_DEGREES;
337 // SG_LOG( SG_TERRAIN, SG_DEBUG, "lon "<< lonlat[LON] <<
338 // " lat " << lonlat[LAT] );
340 current_bucket.set_bucket( longitude, latitude );
341 // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating Tile list for " << current_bucket );
343 if ( tile_cache.exists( current_bucket ) ) {
344 current_tile = tile_cache.get_tile( current_bucket );
345 scenery.next_center = current_tile->center;
347 SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
350 if ( state == Running ) {
351 if ( !(current_bucket == previous_bucket) ) {
352 // We've moved to a new bucket, we need to schedule any
353 // needed tiles for loading.
356 } else if ( state == Start || state == Inited ) {
361 // now handled by threaded tile pager
363 if ( load_queue.size() ) {
364 SG_LOG( SG_TERRAIN, SG_INFO, "Load queue size = " << load_queue.size()
365 << " loading a tile" );
367 SGBucket pending = load_queue.front();
368 load_queue.pop_front();
369 load_tile( pending );
373 if ( scenery.center == Point3D(0.0) ) {
375 cout << "initializing scenery current elevation ... " << endl;
376 sgdVec3 tmp_abs_view_pos;
379 Point3D geod_pos = Point3D( longitude * SGD_DEGREES_TO_RADIANS,
380 latitude * SGD_DEGREES_TO_RADIANS,
382 Point3D tmp = sgGeodToCart( geod_pos );
383 scenery.center = tmp;
384 sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
386 // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
388 sgSetVec3( tmp_view_pos, 0.0, 0.0, 0.0 );
390 if ( current_elev_ssg(tmp_abs_view_pos, &tmp_elev) ) {
391 scenery.cur_elev = tmp_elev;
393 scenery.cur_elev = 0.0;
395 cout << "result = " << scenery.cur_elev << endl;
397 // cout << "abs view pos = " << current_view.abs_view_pos
398 // << " view pos = " << current_view.view_pos << endl;
400 if ( current_elev_ssg(globals->get_current_view()->get_abs_view_pos(),
403 scenery.cur_elev = tmp_elev;
405 scenery.cur_elev = 0.0;
409 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
411 previous_bucket = current_bucket;
412 last_longitude = longitude;
413 last_latitude = latitude;
415 // activate loader thread one out of every 5 frames
416 counter_hack = (counter_hack + 1) % 5;
417 if ( !counter_hack ) {
418 // Notify the tile loader that it can load another tile
421 #ifdef ENABLE_THREADS
422 if (!loaded_queue.empty())
424 FGTileEntry* e = loaded_queue.pop();
425 e->add_ssg_nodes( terrain, ground );
426 //std::cout << "Adding ssg nodes for "
427 //<< e->get_tile_bucket() << "\n";
429 #endif // ENABLE_THREADS
436 void FGTileMgr::prep_ssg_nodes() {
439 #ifndef FG_OLD_WEATHER
440 if ( WeatherDatabase ) {
441 vis = WeatherDatabase->getWeatherVisibility();
446 vis = current_weather.get_visibility();
448 // cout << "visibility = " << vis << endl;
450 // traverse the potentially viewable tile list and update range
451 // selector and transform
454 tile_cache.reset_traversal();
456 while ( ! tile_cache.at_end() ) {
457 // cout << "processing a tile" << endl;
458 if ( (e = tile_cache.get_current()) ) {
459 e->prep_ssg_node( scenery.center, vis);
461 cout << "warning ... empty tile in cache" << endl;