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 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
89 if ( state != Start ) {
90 FG_LOG( FG_TERRAIN, FG_INFO,
91 "... Reinitializing." );
94 FG_LOG( FG_TERRAIN, FG_INFO,
95 "... First time through." );
96 global_tile_cache.init();
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 = global_tile_cache.get_tile( b );
119 // register a load request
120 load_queue.push_back( b );
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 );
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);
136 FG_LOG( FG_TERRAIN, FG_DEBUG, "Tile already in cache " << b );
141 static void CurrentNormalInLocalPlane(sgVec3 dst, sgVec3 src) {
143 sgSetVec3(tmp, src[0], src[1], src[2] );
145 sgTransposeNegateMat4 ( TMP, globals->get_current_view()->get_UP() ) ;
146 sgXformVec3(tmp, tmp, TMP);
147 sgSetVec3(dst, tmp[2], tmp[1], tmp[0] );
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
156 bool FGTileMgr::current_elev_ssg( sgdVec3 abs_view_pos, double *terrain_elev ) {
159 sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
160 sgdSubVec3( view_pos, abs_view_pos, sc );
163 sgdCopyVec3(orig, view_pos );
164 sgdCopyVec3(dir, abs_view_pos );
166 hit_list.Intersect( terrain, orig, dir );
170 double result = -9999;
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,
178 if ( alt > result && alt < 10000 ) {
184 if ( result > -9000 ) {
185 *terrain_elev = result;
186 scenery.cur_radius = geoc.radius();
188 sgSetVec3(tmp, hit_list.get_normal(this_hit));
189 // cout << "cur_normal: " << tmp[0] << " " << tmp[1] << " "
190 // << tmp[2] << endl;
191 ssgState *IntersectedLeafState =
192 ((ssgLeaf*)hit_list.get_entity(this_hit))->getState();
193 CurrentNormalInLocalPlane(tmp, tmp);
194 sgdSetVec3( scenery.cur_normal, tmp );
195 // cout << "NED: " << tmp[0] << " " << tmp[1] << " " << tmp[2] << endl;
198 FG_LOG( FG_TERRAIN, FG_INFO, "no terrain intersection" );
200 float *up = globals->get_current_view()->get_world_up();
201 sgdSetVec3(scenery.cur_normal, up[0], up[1], up[2]);
207 // schedule a needed buckets for loading
208 void FGTileMgr::schedule_needed() {
209 #ifndef FG_OLD_WEATHER
210 if ( WeatherDatabase != NULL ) {
211 vis = WeatherDatabase->getWeatherVisibility();
216 vis = current_weather.get_visibility();
218 cout << "visibility = " << vis << endl;
220 double tile_width = current_bucket.get_width_m();
221 double tile_height = current_bucket.get_height_m();
222 cout << "tile width = " << tile_width << " tile_height = " << tile_height
225 xrange = (int)(vis / tile_width) + 1;
226 yrange = (int)(vis / tile_height) + 1;
227 if ( xrange < 1 ) { xrange = 1; }
228 if ( yrange < 1 ) { yrange = 1; }
229 cout << "xrange = " << xrange << " yrange = " << yrange << endl;
231 global_tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
235 // schedule center tile first so it can be loaded first
236 b = sgBucketOffset( longitude, latitude, 0, 0 );
241 // schedule next ring of 8 tiles
242 for ( x = -1; x <= 1; ++x ) {
243 for ( y = -1; y <= 1; ++y ) {
244 if ( x != 0 || y != 0 ) {
245 b = sgBucketOffset( longitude, latitude, x, y );
246 if ( ! global_tile_cache.exists( b ) ) {
253 // schedule remaining tiles
254 for ( x = -xrange; x <= xrange; ++x ) {
255 for ( y = -yrange; y <= yrange; ++y ) {
256 if ( x < -1 || x > 1 || y < -1 || y > 1 ) {
257 SGBucket b = sgBucketOffset( longitude, latitude, x, y );
258 if ( ! global_tile_cache.exists( b ) ) {
267 void FGTileMgr::initialize_queue()
269 // First time through or we have teleported, initialize the
270 // system and load all relavant tiles
272 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << current_bucket );
273 cout << "tile cache size = " << global_tile_cache.get_size() << endl;
277 // wipe/initialize tile cache
278 // global_tile_cache.init();
279 previous_bucket.make_bad();
281 // build the local area list and schedule tiles for loading
283 // start with the center tile and work out in concentric
288 // Now force a load of the center tile and inner ring so we
289 // have something to see in our first frame.
290 for ( i = 0; i < 9; ++i ) {
291 if ( load_queue.size() ) {
292 FG_LOG( FG_TERRAIN, FG_DEBUG,
293 "Load queue not empty, loading a tile" );
295 SGBucket pending = load_queue.front();
296 load_queue.pop_front();
297 load_tile( pending );
303 // forced emptying of the queue
304 // This is necessay to keep bookeeping straight for the
305 // tile_cache -- which actually handles all the
307 void FGTileMgr::destroy_queue() {
312 // given the current lon/lat (in degrees), fill in the array of local
313 // chunks. If the chunk isn't already in the cache, then read it from
315 int FGTileMgr::update( double lon, double lat ) {
316 FG_LOG( FG_TERRAIN, FG_DEBUG, "FGTileMgr::update()" );
318 // FGInterface *f = current_aircraft.fdm_state;
320 // lonlat for this update
321 // longitude = f->get_Longitude() * RAD_TO_DEG;
322 // latitude = f->get_Latitude() * RAD_TO_DEG;
325 // FG_LOG( FG_TERRAIN, FG_DEBUG, "lon "<< lonlat[LON] <<
326 // " lat " << lonlat[LAT] );
328 current_bucket.set_bucket( longitude, latitude );
329 // FG_LOG( FG_TERRAIN, FG_DEBUG, "Updating Tile list for " << current_bucket );
331 if ( global_tile_cache.exists( current_bucket ) ) {
332 current_tile = global_tile_cache.get_tile( current_bucket );
333 scenery.next_center = current_tile->center;
335 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found (Ok if initializing)" );
338 if ( state == Running ) {
339 if ( !(current_bucket == previous_bucket) ) {
340 // We've moved to a new bucket, we need to schedule any
341 // needed tiles for loading.
344 } else if ( state == Start || state == Inited ) {
349 if ( load_queue.size() ) {
350 FG_LOG( FG_TERRAIN, FG_INFO, "Load queue size = " << load_queue.size()
351 << " loading a tile" );
353 SGBucket pending = load_queue.front();
354 load_queue.pop_front();
355 load_tile( pending );
358 if ( scenery.center == Point3D(0.0) ) {
360 cout << "initializing scenery current elevation ... " << endl;
361 sgdVec3 tmp_abs_view_pos;
364 Point3D geod_pos = Point3D( longitude * DEG_TO_RAD,
365 latitude * DEG_TO_RAD,
367 Point3D tmp = sgGeodToCart( geod_pos );
368 scenery.center = tmp;
369 sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
371 // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
373 sgSetVec3( tmp_view_pos, 0.0, 0.0, 0.0 );
375 if ( current_elev_ssg(tmp_abs_view_pos, &tmp_elev) ) {
376 scenery.cur_elev = tmp_elev;
378 scenery.cur_elev = 0.0;
380 cout << "result = " << scenery.cur_elev << endl;
382 // cout << "abs view pos = " << current_view.abs_view_pos
383 // << " view pos = " << current_view.view_pos << endl;
385 if ( current_elev_ssg(globals->get_current_view()->get_abs_view_pos(),
388 scenery.cur_elev = tmp_elev;
390 scenery.cur_elev = 0.0;
394 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
396 previous_bucket = current_bucket;
397 last_longitude = longitude;
398 last_latitude = latitude;
404 void FGTileMgr::prep_ssg_nodes() {
407 #ifndef FG_OLD_WEATHER
408 if ( WeatherDatabase ) {
409 vis = WeatherDatabase->getWeatherVisibility();
414 vis = current_weather.get_visibility();
416 // cout << "visibility = " << vis << endl;
418 // traverse the potentially viewable tile list and update range
419 // selector and transform
422 global_tile_cache.reset_traversal();
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( scenery.center, vis);
429 cout << "warning ... empty tile in cache" << endl;
431 global_tile_cache.next();