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.
35 #include <Aircraft/aircraft.hxx>
37 #include <Debug/logstream.hxx>
38 // #include <Bucket/bucketutils.hxx>
39 #include <Include/fg_constants.h>
40 #include <Main/options.hxx>
41 #include <Main/views.hxx>
42 #include <Math/fg_geodesy.hxx>
43 #include <Math/mat3.h>
44 #include <Math/point3d.hxx>
45 #include <Math/polar3d.hxx>
46 #include <Math/vector.hxx>
47 #include <Objects/materialmgr.hxx>
48 #include <Objects/obj.hxx>
49 #include <Weather/weather.hxx>
51 #include "scenery.hxx"
52 #include "tilecache.hxx"
53 #include "tileentry.hxx"
54 #include "tilemgr.hxx"
57 // to test clipping speedup in fgTileMgrRender()
58 #if defined ( USE_FAST_FOV_CLIP )
59 // #define TEST_FOV_CLIP
64 extern ssgRoot *scene;
68 FGTileMgr global_tile_mgr;
72 FGTileMgr::FGTileMgr ( void ):
79 FGTileMgr::~FGTileMgr ( void ) {
83 // Initialize the Tile Manager subsystem
84 int FGTileMgr::init( void ) {
85 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
87 // load default material library
88 if ( ! material_mgr.loaded() ) {
89 material_mgr.load_lib();
98 // schedule a tile for loading
99 static void disable_tile( int cache_index ) {
100 // see if tile already exists in the cache
101 cout << "DISABLING CACHE ENTRY = " << cache_index << endl;
102 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
107 // schedule a tile for loading
108 int FGTileMgr::sched_tile( const FGBucket& b ) {
109 // see if tile already exists in the cache
110 int cache_index = global_tile_cache.exists( b );
112 if ( cache_index >= 0 ) {
113 // tile exists in cache, reenable it.
114 cout << "REENABLING DISABLED TILE" << endl;
115 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
116 t->select_ptr->select( 1 );
119 // find the next available cache entry and mark it as
121 cache_index = global_tile_cache.next_avail();
122 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
123 t->mark_scheduled_for_use();
125 // register a load request
128 request.cache_index = cache_index;
129 load_queue.push_back( request );
137 void FGTileMgr::load_tile( const FGBucket& b, int cache_index) {
139 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loading tile " << b );
141 global_tile_cache.fill_in(cache_index, b);
143 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loaded for cache index: " << cache_index );
147 // Calculate shortest distance from point to line
148 static double point_line_dist_squared( const Point3D& tc, const Point3D& vp,
153 p[0] = tc.x(); p[1] = tc.y(); p[2] = tc.z();
154 p0[0] = vp.x(); p0[1] = vp.y(); p0[2] = vp.z();
156 return fgPointLineSquared(p, p0, d);
160 // Determine scenery altitude. Normally this just happens when we
161 // render the scene, but we'd also like to be able to do this
162 // explicitely. lat & lon are in radians. abs_view_pos in meters.
163 // Returns result in meters.
165 FGTileMgr::current_elev_new( const FGBucket& p ) {
167 fgFRAGMENT *frag_ptr;
168 Point3D abs_view_pos = current_view.get_abs_view_pos();
169 Point3D earth_center(0.0);
172 double dist, lat_geod, alt, sea_level_r;
175 local_up[0] = abs_view_pos.x();
176 local_up[1] = abs_view_pos.y();
177 local_up[2] = abs_view_pos.z();
179 // Find current translation offset
180 // fgBucketFind(lon * RAD_TO_DEG, lat * RAD_TO_DEG, &p);
181 index = global_tile_cache.exists(p);
183 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
187 t = global_tile_cache.get_tile(index);
189 scenery.next_center = t->center;
191 FG_LOG( FG_TERRAIN, FG_DEBUG,
192 "Current bucket = " << p << " Index = " << p.gen_index_str() );
193 FG_LOG( FG_TERRAIN, FG_DEBUG,
194 "abs_view_pos = " << abs_view_pos );
196 // calculate tile offset
197 // x = (t->offset.x = t->center.x - scenery.center.x);
198 // y = (t->offset.y = t->center.y - scenery.center.y);
199 // z = (t->offset.z = t->center.z - scenery.center.z);
201 // calc current terrain elevation calculate distance from
202 // vertical tangent line at current position to center of
205 /* printf("distance squared = %.2f, bounding radius = %.2f\n",
206 point_line_dist_squared(&(t->offset), &(v->view_pos),
207 v->local_up), t->bounding_radius); */
209 dist = point_line_dist_squared( t->center, abs_view_pos, local_up );
210 if ( dist < FG_SQUARE(t->bounding_radius) ) {
212 // traverse fragment list for tile
213 FGTileEntry::FragmentIterator current = t->begin();
214 FGTileEntry::FragmentIterator last = t->end();
216 for ( ; current != last; ++current ) {
217 frag_ptr = &(*current);
218 /* printf("distance squared = %.2f, bounding radius = %.2f\n",
219 point_line_dist_squared( &(frag_ptr->center),
220 &abs_view_pos), local_up),
221 frag_ptr->bounding_radius); */
223 dist = point_line_dist_squared( frag_ptr->center,
226 if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
227 if ( frag_ptr->intersect( abs_view_pos,
228 earth_center, 0, result ) ) {
229 FG_LOG( FG_TERRAIN, FG_DEBUG, "intersection point " <<
231 // compute geocentric coordinates of tile center
232 Point3D pp = fgCartToPolar3d(result);
233 FG_LOG( FG_TERRAIN, FG_DEBUG, " polar form = " << pp );
234 // convert to geodetic coordinates
235 fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod,
238 // printf("alt = %.2f\n", alt);
239 // exit since we found an intersection
240 if ( alt > -9999.0 ) {
241 // printf("returning alt\n");
244 // printf("returning 0\n");
252 FG_LOG( FG_TERRAIN, FG_INFO, "(new) no terrain intersection found" );
258 // Determine scenery altitude. Normally this just happens when we
259 // render the scene, but we'd also like to be able to do this
260 // explicitely. lat & lon are in radians. abs_view_pos in meters.
261 // Returns result in meters.
263 FGTileMgr::current_elev( double lon, double lat, const Point3D& abs_view_pos ) {
266 fgFRAGMENT *frag_ptr;
267 Point3D earth_center(0.0);
270 double dist, lat_geod, alt, sea_level_r;
273 c = &global_tile_cache;
275 local_up[0] = abs_view_pos.x();
276 local_up[1] = abs_view_pos.y();
277 local_up[2] = abs_view_pos.z();
279 FG_LOG( FG_TERRAIN, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
281 // Find current translation offset
282 FGBucket p( lon * RAD_TO_DEG, lat * RAD_TO_DEG );
283 index = c->exists(p);
285 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
289 t = c->get_tile(index);
291 scenery.next_center = t->center;
293 FG_LOG( FG_TERRAIN, FG_DEBUG,
294 "Pos = (" << lon * RAD_TO_DEG << ", " << lat * RAD_TO_DEG
295 << ") Current bucket = " << p
296 << " Index = " << p.gen_index_str() );
298 FG_LOG( FG_TERRAIN, FG_DEBUG, "Tile center " << t->center
299 << " bounding radius = " << t->bounding_radius );
301 // calculate tile offset
302 // x = (t->offset.x = t->center.x - scenery.center.x);
303 // y = (t->offset.y = t->center.y - scenery.center.y);
304 // z = (t->offset.z = t->center.z - scenery.center.z);
306 // calc current terrain elevation calculate distance from
307 // vertical tangent line at current position to center of
310 /* printf("distance squared = %.2f, bounding radius = %.2f\n",
311 point_line_dist_squared(&(t->offset), &(v->view_pos),
312 v->local_up), t->bounding_radius); */
314 dist = point_line_dist_squared( t->center, abs_view_pos, local_up );
315 FG_LOG( FG_TERRAIN, FG_DEBUG, "(gross check) dist squared = " << dist );
317 if ( dist < FG_SQUARE(t->bounding_radius) ) {
319 // traverse fragment list for tile
320 FGTileEntry::FragmentIterator current = t->begin();
321 FGTileEntry::FragmentIterator last = t->end();
323 for ( ; current != last; ++current ) {
324 frag_ptr = &(*current);
325 /* printf("distance squared = %.2f, bounding radius = %.2f\n",
326 point_line_dist_squared( &(frag_ptr->center),
327 &abs_view_pos), local_up),
328 frag_ptr->bounding_radius); */
330 dist = point_line_dist_squared( frag_ptr->center,
333 if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
334 if ( frag_ptr->intersect( abs_view_pos,
335 earth_center, 0, result ) ) {
336 FG_LOG( FG_TERRAIN, FG_DEBUG, "intersection point " <<
338 // compute geocentric coordinates of tile center
339 Point3D pp = fgCartToPolar3d(result);
340 FG_LOG( FG_TERRAIN, FG_DEBUG, " polar form = " << pp );
341 // convert to geodetic coordinates
342 fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod,
345 // printf("alt = %.2f\n", alt);
346 // exit since we found an intersection
347 if ( alt > -9999.0 ) {
348 // printf("returning alt\n");
351 // printf("returning 0\n");
359 FG_LOG( FG_TERRAIN, FG_INFO, "(old) no terrain intersection found" );
365 // Determine scenery altitude via ssg. Normally this just happens
366 // when we render the scene, but we'd also like to be able to do this
367 // explicitely. lat & lon are in radians. view_pos in current world
368 // coordinate translated near (0,0,0) (in meters.) Returns result in
371 FGTileMgr::current_elev_ssg( const Point3D& abs_view_pos,
372 const Point3D& view_pos )
376 // cout << "view pos = " << view_pos << endl;
377 // cout << "abs view pos = " << abs_view_pos << endl;
380 sgMakeTransMat4( m, view_pos.x(), view_pos.y(), view_pos.z() );
383 sgSetVec3(s, -abs_view_pos.x(), -abs_view_pos.y(), -abs_view_pos.z() );
385 int num_hits = ssgLOS ( scene, s, m, &results ) ;
387 for ( int i = 0 ; i < num_hits ; i++ ) {
388 ssgHit *h = &(results [ i ]) ;
389 cout << "got a hit!" << endl;
390 /* Do something with 'h' */
393 FG_LOG( FG_TERRAIN, FG_INFO, "(ssg) no terrain intersection found" );
399 // given the current lon/lat, fill in the array of local chunks. If
400 // the chunk isn't already in the cache, then read it from disk.
401 int FGTileMgr::update( void ) {
405 static FGBucket p_last(false);
406 static double last_lon = -1000.0; // in degrees
407 static double last_lat = -1000.0; // in degrees
411 c = &global_tile_cache;
412 f = current_aircraft.fdm_state;
414 tile_diameter = current_options.get_tile_diameter();
416 FGBucket p1( f->get_Longitude() * RAD_TO_DEG,
417 f->get_Latitude() * RAD_TO_DEG );
418 dw = tile_diameter / 2;
419 dh = tile_diameter / 2;
421 if ( (p1 == p_last) && (state == Running) ) {
422 // same bucket as last time
423 FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
424 } else if ( (state == Start) || (state == Inited) ) {
427 // First time through or we have teleported, initialize the
428 // system and load all relavant tiles
430 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
431 FG_LOG( FG_TERRAIN, FG_INFO, " First time through ... " );
432 FG_LOG( FG_TERRAIN, FG_INFO, " Updating Tile list for " << p1 );
433 FG_LOG( FG_TERRAIN, FG_INFO, " Loading "
434 << tile_diameter * tile_diameter << " tiles" );
436 // wipe/initialize tile cache
440 // build the local area list and schedule tiles for loading
442 // start with the center tile and work out in concentric
445 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
446 f->get_Latitude() * RAD_TO_DEG,
450 for ( i = 3; i <= tile_diameter; i = i + 2 ) {
454 for ( j = -span; j <= span; ++j ) {
455 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
456 f->get_Latitude() * RAD_TO_DEG,
462 for ( j = -span; j <= span; ++j ) {
463 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
464 f->get_Latitude() * RAD_TO_DEG,
470 for ( j = -span + 1; j <= span - 1; ++j ) {
471 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
472 f->get_Latitude() * RAD_TO_DEG,
475 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
476 f->get_Latitude() * RAD_TO_DEG,
483 /* for ( j = 0; j < tile_diameter; j++ ) {
484 for ( i = 0; i < tile_diameter; i++ ) {
485 // fgBucketOffset(&p1, &p2, i - dw, j - dh);
486 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
487 f->get_Latitude() * RAD_TO_DEG,
493 // Now force a load of the center tile and inner ring so we
494 // have something to see in our first frame.
495 for ( i = 0; i < 9; ++i ) {
496 if ( load_queue.size() ) {
497 FG_LOG( FG_TERRAIN, FG_INFO,
498 "Load queue not empty, loading a tile" );
500 FGLoadRec pending = load_queue.front();
501 load_queue.pop_front();
502 load_tile( pending.b, pending.cache_index );
507 // We've moved to a new bucket, we need to scroll our
508 // structures, and load in the new tiles
511 // make sure load queue is flushed before doing shift
512 while ( load_queue.size() ) {
513 FG_LOG( FG_TERRAIN, FG_INFO,
514 "Load queue not empty, flushing queue before tile shift." );
516 FGLoadRec pending = load_queue.front();
517 load_queue.pop_front();
518 load_tile( pending.b, pending.index );
522 // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
523 // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
524 // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
526 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
528 if ( (p1.get_lon() > p_last.get_lon()) ||
529 ( (p1.get_lon() == p_last.get_lon()) &&
530 (p1.get_x() > p_last.get_x()) ) ) {
531 FG_LOG( FG_TERRAIN, FG_INFO,
532 " (East) Loading " << tile_diameter << " tiles" );
533 for ( j = 0; j < tile_diameter; j++ ) {
535 // schedule new column
536 p2 = fgBucketOffset( last_lon, last_lat, dw + 1, j - dh );
539 } else if ( (p1.get_lon() < p_last.get_lon()) ||
540 ( (p1.get_lon() == p_last.get_lon()) &&
541 (p1.get_x() < p_last.get_x()) ) ) {
542 FG_LOG( FG_TERRAIN, FG_INFO,
543 " (West) Loading " << tile_diameter << " tiles" );
544 for ( j = 0; j < tile_diameter; j++ ) {
546 // schedule new column
547 p2 = fgBucketOffset( last_lon, last_lat, -dw - 1, j - dh );
552 if ( (p1.get_lat() > p_last.get_lat()) ||
553 ( (p1.get_lat() == p_last.get_lat()) &&
554 (p1.get_y() > p_last.get_y()) ) ) {
555 FG_LOG( FG_TERRAIN, FG_INFO,
556 " (North) Loading " << tile_diameter << " tiles" );
557 for ( i = 0; i < tile_diameter; i++ ) {
560 p2 = fgBucketOffset( last_lon, last_lat, i - dw, dh + 1);
563 } else if ( (p1.get_lat() < p_last.get_lat()) ||
564 ( (p1.get_lat() == p_last.get_lat()) &&
565 (p1.get_y() < p_last.get_y()) ) ) {
566 FG_LOG( FG_TERRAIN, FG_INFO,
567 " (South) Loading " << tile_diameter << " tiles" );
568 for ( i = 0; i < tile_diameter; i++ ) {
571 p2 = fgBucketOffset( last_lon, last_lat, i - dw, -dh - 1);
577 if ( load_queue.size() ) {
578 FG_LOG( FG_TERRAIN, FG_INFO, "Load queue not empty, loading a tile" );
580 FGLoadRec pending = load_queue.front();
581 load_queue.pop_front();
582 load_tile( pending.b, pending.cache_index );
585 // find our current elevation (feed in the current bucket to save work)
586 Point3D geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
587 Point3D tmp_abs_view_pos = fgGeodToCart(geod_pos);
590 current_elev( f->get_Longitude(), f->get_Latitude(), tmp_abs_view_pos );
591 // cout << "current elevation == " << scenery.cur_elev << endl;
592 // double junk = current_elev_ssg( current_view.abs_view_pos,
593 // current_view.view_pos );
594 // cout << "current elevation (ssg) == " <<
597 last_lon = f->get_Longitude() * RAD_TO_DEG;
598 last_lat = f->get_Latitude() * RAD_TO_DEG;
606 // inrange() IS THIS POINT WITHIN POSSIBLE VIEWING RANGE ?
607 // calculate distance from vertical tangent line at
608 // current position to center of object.
609 // this is equivalent to
610 // dist = point_line_dist_squared( &(t->center), &(v->abs_view_pos),
612 // if ( dist < FG_SQUARE(t->bounding_radius) ) {
614 // the compiler should inline this for us
617 inrange( const double radius, const Point3D& center, const Point3D& vp,
624 u[0] = center.x() - vp.x();
625 u[1] = center.y() - vp.y();
626 u[2] = center.z() - vp.z();
628 // calculate the projection, u1, of u along d.
629 // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
631 MAT3_SCALE_VEC(u1, up,
632 (MAT3_DOT_PRODUCT(u, up) / MAT3_DOT_PRODUCT(up, up)) );
634 // v = u - u1 = vector from closest point on line, p1, to the
635 // original point, p.
636 MAT3_SUB_VEC(v, u, u1);
638 return( FG_SQUARE(radius) >= MAT3_DOT_PRODUCT(v, v));
642 // NEW for legibility
644 // update this tile's geometry for current view
645 // The Compiler should inline this
647 update_tile_geometry( FGTileEntry *t, GLdouble *MODEL_VIEW)
652 // calculate tile offset
653 t->offset = t->center - scenery.center;
661 // Calculate the model_view transformation matrix for this tile
662 FG_MEM_COPY( m, MODEL_VIEW, 16*sizeof(GLdouble) );
664 // This is equivalent to doing a glTranslatef(x, y, z);
665 m[12] += (m[0]*x + m[4]*y + m[8] *z);
666 m[13] += (m[1]*x + m[5]*y + m[9] *z);
667 m[14] += (m[2]*x + m[6]*y + m[10]*z);
668 // m[15] += (m[3]*x + m[7]*y + m[11]*z);
669 // m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
670 // so m[15] is unchanged
674 // Prepare the ssg nodes ... for each tile, set it's proper
675 // transform and update it's range selector based on current
677 void FGTileMgr::prep_ssg_nodes( void ) {
683 // traverse the potentially viewable tile list and update range
684 // selector and transform
685 for ( int i = 0; i < (int)global_tile_cache.get_size(); i++ ) {
686 t = global_tile_cache.get_tile( i );
688 if ( t->is_loaded() ) {
689 // set range selector (LOD trick) to be distance to center
690 // of tile + bounding radius
691 ranges[1] = current_weather.get_visibility() + t->bounding_radius;
692 t->range_ptr->setRanges( ranges, 2 );
694 // calculate tile offset
695 t->SetOffset( scenery.center );
697 // calculate ssg transform
699 sgSetCoord( &sgcoord,
700 t->offset.x(), t->offset.y(), t->offset.z(),
702 t->transform_ptr->setTransform( &sgcoord );