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;
62 FGTileMgr global_tile_mgr;
65 // a temporary hack until we get everything rewritten with sgdVec3
66 static inline Point3D operator + (const Point3D& a, const sgdVec3 b)
68 return Point3D(a.x()+b[0], a.y()+b[1], a.z()+b[2]);
73 FGTileMgr::FGTileMgr():
81 FGTileMgr::~FGTileMgr() {
85 // Initialize the Tile Manager subsystem
86 int FGTileMgr::init() {
87 SG_LOG( SG_TERRAIN, SG_INFO, "Initializing Tile Manager subsystem." );
89 if ( state != Start ) {
90 SG_LOG( SG_TERRAIN, SG_INFO,
91 "... Reinitializing." );
94 SG_LOG( SG_TERRAIN, SG_INFO,
95 "... First time through." );
103 previous_bucket.make_bad();
104 current_bucket.make_bad();
106 longitude = latitude = -1000.0;
107 last_longitude = last_latitude = -1000.0;
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 = tile_cache.get_tile( b );
119 // create a new entry
120 FGTileEntry *e = new FGTileEntry( b );
122 // insert the tile into the cache
123 tile_cache.insert_tile( e );
125 // Schedule tile for loading
131 // depricated for threading
134 void FGTileMgr::load_tile( const SGBucket& b ) {
135 // see if tile already exists in the cache
136 FGTileEntry *t = tile_cache.get_tile( b );
139 SG_LOG( SG_TERRAIN, SG_DEBUG, "Loading tile " << b );
140 tile_cache.fill_in( b );
141 t = tile_cache.get_tile( b );
142 t->prep_ssg_node( scenery.center, vis);
144 SG_LOG( SG_TERRAIN, SG_DEBUG, "Tile already in cache " << b );
150 static void CurrentNormalInLocalPlane(sgVec3 dst, sgVec3 src) {
152 sgSetVec3(tmp, src[0], src[1], src[2] );
154 sgTransposeNegateMat4 ( TMP, globals->get_current_view()->get_UP() ) ;
155 sgXformVec3(tmp, tmp, TMP);
156 sgSetVec3(dst, tmp[2], tmp[1], tmp[0] );
160 // Determine scenery altitude via ssg. Normally this just happens
161 // when we render the scene, but we'd also like to be able to do this
162 // explicitely. lat & lon are in radians. view_pos in current world
163 // coordinate translated near (0,0,0) (in meters.) Returns result in
165 bool FGTileMgr::current_elev_ssg( sgdVec3 abs_view_pos, double *terrain_elev ) {
168 sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
169 sgdSubVec3( view_pos, abs_view_pos, sc );
172 sgdCopyVec3(orig, view_pos );
173 sgdCopyVec3(dir, abs_view_pos );
175 hit_list.Intersect( terrain, orig, dir );
179 double result = -9999;
181 int hitcount = hit_list.num_hits();
182 for ( int i = 0; i < hitcount; ++i ) {
183 geoc = sgCartToPolar3d( scenery.center + hit_list.get_point(i) );
184 double lat_geod, alt, sea_level_r;
185 sgGeocToGeod(geoc.lat(), geoc.radius(), &lat_geod,
187 if ( alt > result && alt < 10000 ) {
193 if ( result > -9000 ) {
194 *terrain_elev = result;
195 scenery.cur_radius = geoc.radius();
197 sgSetVec3(tmp, hit_list.get_normal(this_hit));
198 // cout << "cur_normal: " << tmp[0] << " " << tmp[1] << " "
199 // << tmp[2] << endl;
200 ssgState *IntersectedLeafState =
201 ((ssgLeaf*)hit_list.get_entity(this_hit))->getState();
202 CurrentNormalInLocalPlane(tmp, tmp);
203 sgdSetVec3( scenery.cur_normal, tmp );
204 // cout << "NED: " << tmp[0] << " " << tmp[1] << " " << tmp[2] << endl;
207 SG_LOG( SG_TERRAIN, SG_INFO, "no terrain intersection" );
209 float *up = globals->get_current_view()->get_world_up();
210 sgdSetVec3(scenery.cur_normal, up[0], up[1], up[2]);
216 // schedule a needed buckets for loading
217 void FGTileMgr::schedule_needed() {
218 #ifndef FG_OLD_WEATHER
219 if ( WeatherDatabase != NULL ) {
220 vis = WeatherDatabase->getWeatherVisibility();
225 vis = current_weather.get_visibility();
227 // cout << "visibility = " << vis << endl;
229 double tile_width = current_bucket.get_width_m();
230 double tile_height = current_bucket.get_height_m();
231 // cout << "tile width = " << tile_width << " tile_height = "
232 // << tile_height !<< endl;
234 xrange = (int)(vis / tile_width) + 1;
235 yrange = (int)(vis / tile_height) + 1;
236 if ( xrange < 1 ) { xrange = 1; }
237 if ( yrange < 1 ) { yrange = 1; }
238 // cout << "xrange = " << xrange << " yrange = " << yrange << endl;
240 tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
244 // schedule center tile first so it can be loaded first
245 b = sgBucketOffset( longitude, latitude, 0, 0 );
250 // schedule next ring of 8 tiles
251 for ( x = -1; x <= 1; ++x ) {
252 for ( y = -1; y <= 1; ++y ) {
253 if ( x != 0 || y != 0 ) {
254 b = sgBucketOffset( longitude, latitude, x, y );
260 // schedule remaining tiles
261 for ( x = -xrange; x <= xrange; ++x ) {
262 for ( y = -yrange; y <= yrange; ++y ) {
263 if ( x < -1 || x > 1 || y < -1 || y > 1 ) {
264 SGBucket b = sgBucketOffset( longitude, latitude, x, y );
272 void FGTileMgr::initialize_queue()
274 // First time through or we have teleported, initialize the
275 // system and load all relavant tiles
277 SG_LOG( SG_TERRAIN, SG_INFO, "Updating Tile list for " << current_bucket );
278 // cout << "tile cache size = " << tile_cache.get_size() << endl;
280 // wipe/initialize tile cache
281 // tile_cache.init();
282 previous_bucket.make_bad();
284 // build the local area list and schedule tiles for loading
286 // start with the center tile and work out in concentric
291 // do we really want to lose this? CLO
293 // Now force a load of the center tile and inner ring so we
294 // have something to see in our first frame.
296 for ( i = 0; i < 9; ++i ) {
297 if ( load_queue.size() ) {
298 SG_LOG( SG_TERRAIN, SG_DEBUG,
299 "Load queue not empty, loading a tile" );
301 SGBucket pending = load_queue.front();
302 load_queue.pop_front();
303 load_tile( pending );
310 // forced emptying of the queue
311 // This is necessay to keep bookeeping straight for the
312 // tile_cache -- which actually handles all the
314 void FGTileMgr::destroy_queue() {
315 // load_queue.clear();
319 // given the current lon/lat (in degrees), fill in the array of local
320 // chunks. If the chunk isn't already in the cache, then read it from
322 int FGTileMgr::update( double lon, double lat ) {
323 SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update()" );
325 // FGInterface *f = current_aircraft.fdm_state;
327 // lonlat for this update
328 // longitude = f->get_Longitude() * SGD_RADIANS_TO_DEGREES;
329 // latitude = f->get_Latitude() * SGD_RADIANS_TO_DEGREES;
332 // SG_LOG( SG_TERRAIN, SG_DEBUG, "lon "<< lonlat[LON] <<
333 // " lat " << lonlat[LAT] );
335 current_bucket.set_bucket( longitude, latitude );
336 // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating Tile list for " << current_bucket );
338 if ( tile_cache.exists( current_bucket ) ) {
339 current_tile = tile_cache.get_tile( current_bucket );
340 scenery.next_center = current_tile->center;
342 SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
345 if ( state == Running ) {
346 if ( !(current_bucket == previous_bucket) ) {
347 // We've moved to a new bucket, we need to schedule any
348 // needed tiles for loading.
351 } else if ( state == Start || state == Inited ) {
356 // now handled by threaded tile pager
358 if ( load_queue.size() ) {
359 SG_LOG( SG_TERRAIN, SG_INFO, "Load queue size = " << load_queue.size()
360 << " loading a tile" );
362 SGBucket pending = load_queue.front();
363 load_queue.pop_front();
364 load_tile( pending );
368 if ( scenery.center == Point3D(0.0) ) {
370 cout << "initializing scenery current elevation ... " << endl;
371 sgdVec3 tmp_abs_view_pos;
374 Point3D geod_pos = Point3D( longitude * SGD_DEGREES_TO_RADIANS,
375 latitude * SGD_DEGREES_TO_RADIANS,
377 Point3D tmp = sgGeodToCart( geod_pos );
378 scenery.center = tmp;
379 sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
381 // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
383 sgSetVec3( tmp_view_pos, 0.0, 0.0, 0.0 );
385 if ( current_elev_ssg(tmp_abs_view_pos, &tmp_elev) ) {
386 scenery.cur_elev = tmp_elev;
388 scenery.cur_elev = 0.0;
390 cout << "result = " << scenery.cur_elev << endl;
392 // cout << "abs view pos = " << current_view.abs_view_pos
393 // << " view pos = " << current_view.view_pos << endl;
395 if ( current_elev_ssg(globals->get_current_view()->get_abs_view_pos(),
398 scenery.cur_elev = tmp_elev;
400 scenery.cur_elev = 0.0;
404 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
406 previous_bucket = current_bucket;
407 last_longitude = longitude;
408 last_latitude = latitude;
410 // activate loader thread one out of every 5 frames
411 counter_hack = (counter_hack + 1) % 5;
412 if ( !counter_hack ) {
413 // Notify the tile loader that it can load another tile
421 void FGTileMgr::prep_ssg_nodes() {
424 #ifndef FG_OLD_WEATHER
425 if ( WeatherDatabase ) {
426 vis = WeatherDatabase->getWeatherVisibility();
431 vis = current_weather.get_visibility();
433 // cout << "visibility = " << vis << endl;
435 // traverse the potentially viewable tile list and update range
436 // selector and transform
439 tile_cache.reset_traversal();
441 while ( ! tile_cache.at_end() ) {
442 // cout << "processing a tile" << endl;
443 if ( (e = tile_cache.get_current()) ) {
444 e->prep_ssg_node( scenery.center, vis);
446 cout << "warning ... empty tile in cache" << endl;