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.
33 #include <simgear/xgl/xgl.h>
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>
42 #include <Main/globals.hxx>
43 #include <Objects/obj.hxx>
45 #ifndef FG_OLD_WEATHER
46 # include <WeatherCM/FGLocalWeatherDatabase.h>
48 # include <Weather/weather.hxx>
51 #include "newcache.hxx"
52 #include "scenery.hxx"
53 #include "tilemgr.hxx"
55 #define TEST_LAST_HIT_CACHE
57 extern ssgRoot *scene;
58 extern ssgBranch *terrain;
61 FGTileMgr global_tile_mgr;
64 // a temporary hack until we get everything rewritten with sgdVec3
65 static inline Point3D operator + (const Point3D& a, const sgdVec3 b)
67 return Point3D(a.x()+b[0], a.y()+b[1], a.z()+b[2]);
72 FGTileMgr::FGTileMgr():
79 FGTileMgr::~FGTileMgr() {
83 // Initialize the Tile Manager subsystem
84 int FGTileMgr::init() {
85 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
87 if ( state != Start ) {
88 FG_LOG( FG_TERRAIN, FG_INFO,
89 "... Reinitializing." );
92 FG_LOG( FG_TERRAIN, FG_INFO,
93 "... First time through." );
94 global_tile_cache.init();
101 previous_bucket.make_bad();
102 current_bucket.make_bad();
104 longitude = latitude = -1000.0;
105 last_longitude = last_latitude = -1000.0;
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 );
117 // tile exists in cache, reenable it.
118 // cout << "REENABLING DISABLED TILE" << endl;
119 t->select_ptr->select( 1 );
121 // register a load request
122 load_queue.push_back( b );
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 );
133 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loading tile " << b );
134 global_tile_cache.fill_in( b );
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, sgVec3 view_pos,
157 double *terrain_elev ) {
160 sgdSetVec3(orig, view_pos );
161 sgdCopyVec3(dir, abs_view_pos );
163 hit_list.Intersect( terrain, orig, dir );
167 double result = -9999;
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,
175 if ( alt > result && alt < 10000 ) {
181 if ( result > -9000 ) {
182 *terrain_elev = result;
183 scenery.cur_radius = geoc.radius();
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;
193 FG_LOG( FG_TERRAIN, FG_INFO, "no terrain intersection" );
195 float *up = globals->get_current_view()->get_world_up();
196 sgdSetVec3(scenery.cur_normal, up[0], up[1], up[2]);
202 // schedule a needed buckets for loading
203 void FGTileMgr::schedule_needed() {
206 #ifndef FG_OLD_WEATHER
207 if ( WeatherDatabase != NULL ) {
208 vis = WeatherDatabase->getWeatherVisibility();
213 vis = current_weather.get_visibility();
215 cout << "visibility = " << vis << endl;
217 double clat = (int)current_bucket.get_center_lat();
219 clat = (int)clat + 0.5;
221 clat = (int)clat - 0.5;
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;
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;
236 double perimeter = 2.0 * EQUATORIAL_RADIUS_M * FG_PI;
237 double degree_height = perimeter / 360.0;
238 cout << "degree_height = " << degree_height << endl;
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
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 global_tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) );
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 ) ) {
264 void FGTileMgr::initialize_queue()
266 // First time through or we have teleported, initialize the
267 // system and load all relavant tiles
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;
276 // wipe/initialize tile cache
277 // global_tile_cache.init();
278 previous_bucket.make_bad();
280 // build the local area list and schedule tiles for loading
282 // start with the center tile and work out in concentric
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" );
294 FGBucket pending = load_queue.front();
295 load_queue.pop_front();
296 load_tile( pending );
302 // forced emptying of the queue
303 // This is necessay to keep bookeeping straight for the
304 // tile_cache -- which actually handles all the
306 void FGTileMgr::destroy_queue() {
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
314 int FGTileMgr::update( double lon, double lat ) {
315 FG_LOG( FG_TERRAIN, FG_DEBUG, "FGTileMgr::update()" );
317 // FGInterface *f = current_aircraft.fdm_state;
319 // lonlat for this update
320 // longitude = f->get_Longitude() * RAD_TO_DEG;
321 // latitude = f->get_Latitude() * RAD_TO_DEG;
324 // FG_LOG( FG_TERRAIN, FG_DEBUG, "lon "<< lonlat[LON] <<
325 // " lat " << lonlat[LAT] );
327 current_bucket.set_bucket( longitude, latitude );
328 // FG_LOG( FG_TERRAIN, FG_DEBUG, "Updating Tile list for " << current_bucket );
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;
334 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found (Ok if initializing)" );
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.
343 } else if ( state == Start || state == Inited ) {
348 if ( load_queue.size() ) {
349 FG_LOG( FG_TERRAIN, FG_INFO, "Load queue size = " << load_queue.size()
350 << " loading a tile" );
352 FGBucket pending = load_queue.front();
353 load_queue.pop_front();
354 load_tile( pending );
357 if ( scenery.center == Point3D(0.0) ) {
359 // cout << "initializing ... " << endl;
360 sgdVec3 tmp_abs_view_pos;
363 Point3D geod_pos = Point3D( longitude * DEG_TO_RAD,
364 latitude * DEG_TO_RAD,
366 Point3D tmp = sgGeodToCart( geod_pos );
367 scenery.center = tmp;
368 sgdSetVec3( tmp_abs_view_pos, tmp.x(), tmp.y(), tmp.z() );
370 // cout << "abs_view_pos = " << tmp_abs_view_pos << endl;
372 sgSetVec3( tmp_view_pos, 0.0, 0.0, 0.0 );
374 if ( current_elev_ssg(tmp_abs_view_pos, tmp_view_pos, &tmp_elev) ) {
375 scenery.cur_elev = tmp_elev;
377 scenery.cur_elev = 0.0;
380 // cout << "abs view pos = " << current_view.abs_view_pos
381 // << " view pos = " << current_view.view_pos << endl;
383 if ( current_elev_ssg(globals->get_current_view()->get_abs_view_pos(),
384 globals->get_current_view()->get_view_pos(),
387 scenery.cur_elev = tmp_elev;
389 scenery.cur_elev = 0.0;
393 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
395 previous_bucket = current_bucket;
396 last_longitude = longitude;
397 last_latitude = latitude;
403 void FGTileMgr::prep_ssg_nodes() {
406 #ifndef FG_OLD_WEATHER
407 if ( WeatherDatabase ) {
408 vis = WeatherDatabase->getWeatherVisibility();
413 vis = current_weather.get_visibility();
415 // cout << "visibility = " << vis << endl;
417 // traverse the potentially viewable tile list and update range
418 // selector and transform
421 Point3D p = scenery.center;
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( p, vis);
429 cout << "warning ... empty tile in cache" << endl;
431 global_tile_cache.next();