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::attach_queue;
74 SGLockedQueue<FGDeferredModel *> FGTileMgr::model_queue;
76 queue<FGTileEntry *> FGTileMgr::attach_queue;
77 queue<FGDeferredModel *> FGTileMgr::model_queue;
78 #endif // ENABLE_THREADS
82 FGTileMgr::FGTileMgr():
91 FGTileMgr::~FGTileMgr() {
95 // Initialize the Tile Manager subsystem
96 int FGTileMgr::init() {
97 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing Tile Manager subsystem." );
102 while ( ! attach_queue.empty() ) {
106 while ( ! model_queue.empty() ) {
107 #ifdef ENABLE_THREADS
108 FGDeferredModel* dm = model_queue.pop();
110 FGDeferredModel* dm = model_queue.front();
118 if ( state != Start ) {
119 SG_LOG( SG_TERRAIN, SG_INFO,
120 "... Reinitializing." );
123 SG_LOG( SG_TERRAIN, SG_INFO,
124 "... First time through." );
133 previous_bucket.make_bad();
134 current_bucket.make_bad();
136 longitude = latitude = -1000.0;
137 last_longitude = last_latitude = -1000.0;
143 // schedule a tile for loading
144 void FGTileMgr::sched_tile( const SGBucket& b ) {
145 // see if tile already exists in the cache
146 FGTileEntry *t = tile_cache.get_tile( b );
149 // create a new entry
150 FGTileEntry *e = new FGTileEntry( b );
152 // insert the tile into the cache
153 tile_cache.insert_tile( e );
155 // Schedule tile for loading
161 static void CurrentNormalInLocalPlane(sgVec3 dst, sgVec3 src) {
163 sgSetVec3(tmp, src[0], src[1], src[2] );
165 sgTransposeNegateMat4 ( TMP, globals->get_current_view()->get_UP() ) ;
166 sgXformVec3(tmp, tmp, TMP);
167 sgSetVec3(dst, tmp[2], tmp[1], tmp[0] );
171 // Determine scenery altitude via ssg. Normally this just happens
172 // when we render the scene, but we'd also like to be able to do this
173 // explicitely. lat & lon are in radians. view_pos in current world
174 // coordinate translated near (0,0,0) (in meters.) Returns result in
176 bool FGTileMgr::current_elev_ssg( sgdVec3 abs_view_pos, double *terrain_elev ) {
179 sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
180 sgdSubVec3( view_pos, abs_view_pos, sc );
183 sgdCopyVec3(orig, view_pos );
184 sgdCopyVec3(dir, abs_view_pos );
186 hit_list.Intersect( terrain, orig, dir );
190 double result = -9999;
192 int hitcount = hit_list.num_hits();
193 for ( int i = 0; i < hitcount; ++i ) {
194 geoc = sgCartToPolar3d( scenery.center + hit_list.get_point(i) );
195 double lat_geod, alt, sea_level_r;
196 sgGeocToGeod(geoc.lat(), geoc.radius(), &lat_geod,
198 if ( alt > result && alt < 10000 ) {
204 if ( result > -9000 ) {
205 *terrain_elev = result;
206 scenery.cur_radius = geoc.radius();
208 sgSetVec3(tmp, hit_list.get_normal(this_hit));
209 // cout << "cur_normal: " << tmp[0] << " " << tmp[1] << " "
210 // << tmp[2] << endl;
211 ssgState *IntersectedLeafState =
212 ((ssgLeaf*)hit_list.get_entity(this_hit))->getState();
213 CurrentNormalInLocalPlane(tmp, tmp);
214 sgdSetVec3( scenery.cur_normal, tmp );
215 // cout << "NED: " << tmp[0] << " " << tmp[1] << " " << tmp[2] << endl;
218 SG_LOG( SG_TERRAIN, SG_INFO, "no terrain intersection" );
220 float *up = globals->get_current_view()->get_world_up();
221 sgdSetVec3(scenery.cur_normal, up[0], up[1], up[2]);
227 // schedule a needed buckets for loading
228 void FGTileMgr::schedule_needed() {
229 #ifndef FG_OLD_WEATHER
230 if ( WeatherDatabase != NULL ) {
231 vis = WeatherDatabase->getWeatherVisibility();
236 vis = current_weather.get_visibility();
238 // cout << "visibility = " << vis << endl;
240 double tile_width = current_bucket.get_width_m();
241 double tile_height = current_bucket.get_height_m();
242 // cout << "tile width = " << tile_width << " tile_height = "
243 // << tile_height !<< endl;
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;
251 tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
255 // schedule center tile first so it can be loaded first
256 b = sgBucketOffset( longitude, latitude, 0, 0 );
261 // schedule next ring of 8 tiles
262 for ( x = -1; x <= 1; ++x ) {
263 for ( y = -1; y <= 1; ++y ) {
264 if ( x != 0 || y != 0 ) {
265 b = sgBucketOffset( longitude, latitude, x, y );
271 // schedule remaining tiles
272 for ( x = -xrange; x <= xrange; ++x ) {
273 for ( y = -yrange; y <= yrange; ++y ) {
274 if ( x < -1 || x > 1 || y < -1 || y > 1 ) {
275 SGBucket b = sgBucketOffset( longitude, latitude, x, y );
283 void FGTileMgr::initialize_queue()
285 // First time through or we have teleported, initialize the
286 // system and load all relavant tiles
288 SG_LOG( SG_TERRAIN, SG_INFO, "Updating Tile list for " << current_bucket );
289 // cout << "tile cache size = " << tile_cache.get_size() << endl;
291 // wipe/initialize tile cache
292 // tile_cache.init();
293 previous_bucket.make_bad();
295 // build the local area list and schedule tiles for loading
297 // start with the center tile and work out in concentric
302 // do we really want to lose this? CLO
304 // Now force a load of the center tile and inner ring so we
305 // have something to see in our first frame.
307 for ( i = 0; i < 9; ++i ) {
308 if ( load_queue.size() ) {
309 SG_LOG( SG_TERRAIN, SG_DEBUG,
310 "Load queue not empty, loading a tile" );
312 SGBucket pending = load_queue.front();
313 load_queue.pop_front();
314 load_tile( pending );
321 // forced emptying of the queue
322 // This is necessay to keep bookeeping straight for the
323 // tile_cache -- which actually handles all the
325 void FGTileMgr::destroy_queue() {
326 // load_queue.clear();
330 // given the current lon/lat (in degrees), fill in the array of local
331 // chunks. If the chunk isn't already in the cache, then read it from
333 int FGTileMgr::update( double lon, double lat ) {
334 SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update()" );
336 // FGInterface *f = current_aircraft.fdm_state;
338 // lonlat for this update
339 // longitude = f->get_Longitude() * SGD_RADIANS_TO_DEGREES;
340 // latitude = f->get_Latitude() * SGD_RADIANS_TO_DEGREES;
343 // SG_LOG( SG_TERRAIN, SG_DEBUG, "lon "<< lonlat[LON] <<
344 // " lat " << lonlat[LAT] );
346 current_bucket.set_bucket( longitude, latitude );
347 // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating Tile list for " << current_bucket );
349 if ( tile_cache.exists( current_bucket ) ) {
350 current_tile = tile_cache.get_tile( current_bucket );
351 scenery.next_center = current_tile->center;
353 SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
356 if ( state == Running ) {
357 if ( !(current_bucket == previous_bucket) ) {
358 // We've moved to a new bucket, we need to schedule any
359 // needed tiles for loading.
362 } else if ( state == Start || state == Inited ) {
367 // load the next tile in the load queue (or authorize the next
368 // load in the case of the threaded tile pager)
371 // load the next model in the load queue. Currently this must
372 // happen in the render thread because model loading can trigger
373 // texture loading which involves use of the opengl api.
374 if ( !model_queue.empty() ) {
375 cout << "loading next model ..." << endl;
376 // load the next tile in the queue
377 #ifdef ENABLE_THREADS
378 FGDeferredModel* dm = model_queue.pop();
380 FGDeferredModel* dm = model_queue.front();
384 ssgTexturePath( (char *)(dm->get_texture_path().c_str()) );
386 = ssgLoad( (char *)(dm->get_model_path().c_str()) );
387 dm->get_obj_trans()->addKid( obj_model );
388 dm->get_tile()->dec_pending_models();
393 if ( scenery.center == Point3D(0.0) ) {
395 cout << "initializing scenery current elevation ... " << endl;
396 sgdVec3 tmp_abs_view_pos;
399 Point3D geod_pos = Point3D( longitude * SGD_DEGREES_TO_RADIANS,
400 latitude * SGD_DEGREES_TO_RADIANS,
402 Point3D tmp = sgGeodToCart( geod_pos );
403 scenery.center = tmp;
404 sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
406 // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
408 sgSetVec3( tmp_view_pos, 0.0, 0.0, 0.0 );
410 if ( current_elev_ssg(tmp_abs_view_pos, &tmp_elev) ) {
411 scenery.cur_elev = tmp_elev;
413 scenery.cur_elev = 0.0;
415 cout << "result = " << scenery.cur_elev << endl;
417 // cout << "abs view pos = " << current_view.abs_view_pos
418 // << " view pos = " << current_view.view_pos << endl;
420 if ( current_elev_ssg(globals->get_current_view()->get_abs_view_pos(),
423 scenery.cur_elev = tmp_elev;
425 scenery.cur_elev = 0.0;
429 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
431 previous_bucket = current_bucket;
432 last_longitude = longitude;
433 last_latitude = latitude;
435 // activate loader thread one out of every 5 frames
436 counter_hack = (counter_hack + 1) % 5;
437 if ( !counter_hack ) {
438 // Notify the tile loader that it can load another tile
441 if ( !attach_queue.empty() ) {
442 #ifdef ENABLE_THREADS
443 FGTileEntry* e = attach_queue.pop();
445 FGTileEntry* e = attach_queue.front();
448 e->add_ssg_nodes( terrain, ground );
449 // cout << "Adding ssg nodes for "
457 void FGTileMgr::prep_ssg_nodes() {
460 #ifndef FG_OLD_WEATHER
461 if ( WeatherDatabase ) {
462 vis = WeatherDatabase->getWeatherVisibility();
467 vis = current_weather.get_visibility();
469 // cout << "visibility = " << vis << endl;
471 // traverse the potentially viewable tile list and update range
472 // selector and transform
475 tile_cache.reset_traversal();
477 while ( ! tile_cache.at_end() ) {
478 // cout << "processing a tile" << endl;
479 if ( (e = tile_cache.get_current()) ) {
480 e->prep_ssg_node( scenery.center, vis);
482 cout << "warning ... empty tile in cache" << endl;