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,
448 tiles[(dh*tile_diameter) + dw] = sched_tile( p2 );
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,
458 tiles[((dh-span)*tile_diameter) + dw+j] = sched_tile( p2 );
462 for ( j = -span; j <= span; ++j ) {
463 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
464 f->get_Latitude() * RAD_TO_DEG,
466 tiles[((dh+span)*tile_diameter) + dw+j] = sched_tile( p2 );
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,
474 tiles[((dh+j)*tile_diameter) + dw-span] = sched_tile( p2 );
475 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
476 f->get_Latitude() * RAD_TO_DEG,
478 tiles[((dh+j)*tile_diameter) + dw+span] = sched_tile( p2 );
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,
489 tiles[(j*tile_diameter) + i] = sched_tile( p2 );
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()) && (p1.get_x() > p_last.get_x()) ) ) {
530 FG_LOG( FG_TERRAIN, FG_INFO,
531 " (East) Loading " << tile_diameter << " tiles" );
532 for ( j = 0; j < tile_diameter; j++ ) {
534 disable_tile( tiles[(j*tile_diameter) + 0] );
535 for ( i = 0; i < tile_diameter - 1; i++ ) {
536 tiles[(j*tile_diameter) + i] =
537 tiles[(j*tile_diameter) + i + 1];
539 // load in new column
540 p2 = fgBucketOffset( last_lon, last_lat, dw + 1, j - dh );
541 tiles[(j*tile_diameter) + tile_diameter - 1] = sched_tile( p2 );
543 } else if ( (p1.get_lon() < p_last.get_lon()) ||
544 ( (p1.get_lon() == p_last.get_lon()) &&
545 (p1.get_x() < p_last.get_x()) ) ) {
546 FG_LOG( FG_TERRAIN, FG_INFO,
547 " (West) Loading " << tile_diameter << " tiles" );
548 for ( j = 0; j < tile_diameter; j++ ) {
550 disable_tile( tiles[(j*tile_diameter) + tile_diameter - 1] );
551 for ( i = tile_diameter - 1; i > 0; i-- ) {
552 tiles[(j*tile_diameter) + i] =
553 tiles[(j*tile_diameter) + i - 1];
555 // load in new column
556 p2 = fgBucketOffset( last_lon, last_lat, -dw - 1, j - dh );
557 tiles[(j*tile_diameter) + 0] = sched_tile( p2 );
561 if ( (p1.get_lat() > p_last.get_lat()) ||
562 ( (p1.get_lat() == p_last.get_lat()) && (p1.get_y() > p_last.get_y()) ) ) {
563 FG_LOG( FG_TERRAIN, FG_INFO,
564 " (North) Loading " << tile_diameter << " tiles" );
565 for ( i = 0; i < tile_diameter; i++ ) {
567 disable_tile( tiles[0 + i] );
568 for ( j = 0; j < tile_diameter - 1; j++ ) {
569 tiles[(j * tile_diameter) + i] =
570 tiles[((j+1) * tile_diameter) + i];
572 // load in new column
573 p2 = fgBucketOffset( last_lon, last_lat, i - dw, dh + 1);
574 tiles[((tile_diameter-1) * tile_diameter) + i] =
577 } else if ( (p1.get_lat() < p_last.get_lat()) ||
578 ( (p1.get_lat() == p_last.get_lat()) && (p1.get_y() < p_last.get_y()) ) ) {
579 FG_LOG( FG_TERRAIN, FG_INFO,
580 " (South) Loading " << tile_diameter << " tiles" );
581 for ( i = 0; i < tile_diameter; i++ ) {
583 disable_tile( tiles[((tile_diameter-1) * tile_diameter) + i] );
584 for ( j = tile_diameter - 1; j > 0; j-- ) {
585 tiles[(j * tile_diameter) + i] =
586 tiles[((j-1) * tile_diameter) + i];
588 // load in new column
589 p2 = fgBucketOffset( last_lon, last_lat, i - dw, -dh - 1);
590 tiles[0 + i] = sched_tile( p2 );
595 if ( load_queue.size() ) {
596 FG_LOG( FG_TERRAIN, FG_INFO, "Load queue not empty, loading a tile" );
598 FGLoadRec pending = load_queue.front();
599 load_queue.pop_front();
600 load_tile( pending.b, pending.cache_index );
603 // find our current elevation (feed in the current bucket to save work)
604 Point3D geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
605 Point3D tmp_abs_view_pos = fgGeodToCart(geod_pos);
608 current_elev( f->get_Longitude(), f->get_Latitude(), tmp_abs_view_pos );
609 // cout << "current elevation == " << scenery.cur_elev << endl;
610 // double junk = current_elev_ssg( current_view.abs_view_pos,
611 // current_view.view_pos );
612 // cout << "current elevation (ssg) == " <<
615 last_lon = f->get_Longitude() * RAD_TO_DEG;
616 last_lat = f->get_Latitude() * RAD_TO_DEG;
624 // inrange() IS THIS POINT WITHIN POSSIBLE VIEWING RANGE ?
625 // calculate distance from vertical tangent line at
626 // current position to center of object.
627 // this is equivalent to
628 // dist = point_line_dist_squared( &(t->center), &(v->abs_view_pos),
630 // if ( dist < FG_SQUARE(t->bounding_radius) ) {
632 // the compiler should inline this for us
635 inrange( const double radius, const Point3D& center, const Point3D& vp,
642 u[0] = center.x() - vp.x();
643 u[1] = center.y() - vp.y();
644 u[2] = center.z() - vp.z();
646 // calculate the projection, u1, of u along d.
647 // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
649 MAT3_SCALE_VEC(u1, up,
650 (MAT3_DOT_PRODUCT(u, up) / MAT3_DOT_PRODUCT(up, up)) );
652 // v = u - u1 = vector from closest point on line, p1, to the
653 // original point, p.
654 MAT3_SUB_VEC(v, u, u1);
656 return( FG_SQUARE(radius) >= MAT3_DOT_PRODUCT(v, v));
660 // NEW for legibility
662 // update this tile's geometry for current view
663 // The Compiler should inline this
665 update_tile_geometry( FGTileEntry *t, GLdouble *MODEL_VIEW)
670 // calculate tile offset
671 t->offset = t->center - scenery.center;
679 // Calculate the model_view transformation matrix for this tile
680 FG_MEM_COPY( m, MODEL_VIEW, 16*sizeof(GLdouble) );
682 // This is equivalent to doing a glTranslatef(x, y, z);
683 m[12] += (m[0]*x + m[4]*y + m[8] *z);
684 m[13] += (m[1]*x + m[5]*y + m[9] *z);
685 m[14] += (m[2]*x + m[6]*y + m[10]*z);
686 // m[15] += (m[3]*x + m[7]*y + m[11]*z);
687 // m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
688 // so m[15] is unchanged
692 // Prepare the ssg nodes ... for each tile, set it's proper
693 // transform and update it's range selector based on current
695 void FGTileMgr::prep_ssg_nodes( void ) {
698 int tile_diameter = current_options.get_tile_diameter();
703 // traverse the potentially viewable tile list and update range
704 // selector and transform
705 for ( int i = 0; i < (tile_diameter * tile_diameter); i++ ) {
706 int index = tiles[i];
707 t = global_tile_cache.get_tile(index);
709 if ( t->is_loaded() ) {
710 // set range selector (LOD trick) to be distance to center
711 // of tile + bounding radius
712 ranges[1] = current_weather.get_visibility() + t->bounding_radius;
713 t->range_ptr->setRanges( ranges, 2 );
715 // calculate tile offset
716 t->SetOffset( scenery.center );
718 // calculate ssg transform
720 sgSetCoord( &sgcoord,
721 t->offset.x(), t->offset.y(), t->offset.z(),
723 t->transform_ptr->setTransform( &sgcoord );