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>
50 #ifndef FG_OLD_WEATHER
51 # include <WeatherCM/FGLocalWeatherDatabase.h>
53 # include <Weather/weather.hxx>
56 #include "scenery.hxx"
57 #include "tilecache.hxx"
58 #include "tileentry.hxx"
59 #include "tilemgr.hxx"
62 // to test clipping speedup in fgTileMgrRender()
63 #if defined ( USE_FAST_FOV_CLIP )
64 // #define TEST_FOV_CLIP
69 extern ssgRoot *scene;
73 FGTileMgr global_tile_mgr;
77 FGTileMgr::FGTileMgr ( void ):
84 FGTileMgr::~FGTileMgr ( void ) {
88 // Initialize the Tile Manager subsystem
89 int FGTileMgr::init( void ) {
90 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
92 // load default material library
93 if ( ! material_mgr.loaded() ) {
94 material_mgr.load_lib();
97 global_tile_cache.init();
105 // schedule a tile for loading
106 static void disable_tile( int cache_index ) {
107 // see if tile already exists in the cache
108 // cout << "DISABLING CACHE ENTRY = " << cache_index << endl;
109 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
114 // schedule a tile for loading
115 int FGTileMgr::sched_tile( const FGBucket& b ) {
116 // see if tile already exists in the cache
117 int cache_index = global_tile_cache.exists( b );
119 if ( cache_index >= 0 ) {
120 // tile exists in cache, reenable it.
121 // cout << "REENABLING DISABLED TILE" << endl;
122 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
123 t->select_ptr->select( 1 );
126 // find the next available cache entry and mark it as
128 cache_index = global_tile_cache.next_avail();
129 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
130 t->mark_scheduled_for_use();
132 // register a load request
135 request.cache_index = cache_index;
136 load_queue.push_back( request );
144 void FGTileMgr::load_tile( const FGBucket& b, int cache_index) {
146 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loading tile " << b );
148 global_tile_cache.fill_in(cache_index, b);
150 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loaded for cache index: " << cache_index );
154 // Calculate shortest distance from point to line
155 static double point_line_dist_squared( const Point3D& tc, const Point3D& vp,
160 p[0] = tc.x(); p[1] = tc.y(); p[2] = tc.z();
161 p0[0] = vp.x(); p0[1] = vp.y(); p0[2] = vp.z();
163 return fgPointLineSquared(p, p0, d);
167 // Determine scenery altitude. Normally this just happens when we
168 // render the scene, but we'd also like to be able to do this
169 // explicitely. lat & lon are in radians. abs_view_pos in meters.
170 // Returns result in meters.
172 FGTileMgr::current_elev_new( const FGBucket& p ) {
174 fgFRAGMENT *frag_ptr;
175 Point3D abs_view_pos = current_view.get_abs_view_pos();
176 Point3D earth_center(0.0);
179 double dist, lat_geod, alt, sea_level_r;
182 local_up[0] = abs_view_pos.x();
183 local_up[1] = abs_view_pos.y();
184 local_up[2] = abs_view_pos.z();
186 // Find current translation offset
187 // fgBucketFind(lon * RAD_TO_DEG, lat * RAD_TO_DEG, &p);
188 index = global_tile_cache.exists(p);
190 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
194 t = global_tile_cache.get_tile(index);
196 scenery.next_center = t->center;
198 FG_LOG( FG_TERRAIN, FG_DEBUG,
199 "Current bucket = " << p << " Index = " << p.gen_index_str() );
200 FG_LOG( FG_TERRAIN, FG_DEBUG,
201 "abs_view_pos = " << abs_view_pos );
203 // calculate tile offset
204 // x = (t->offset.x = t->center.x - scenery.center.x);
205 // y = (t->offset.y = t->center.y - scenery.center.y);
206 // z = (t->offset.z = t->center.z - scenery.center.z);
208 // calc current terrain elevation calculate distance from
209 // vertical tangent line at current position to center of
212 /* printf("distance squared = %.2f, bounding radius = %.2f\n",
213 point_line_dist_squared(&(t->offset), &(v->view_pos),
214 v->local_up), t->bounding_radius); */
216 dist = point_line_dist_squared( t->center, abs_view_pos, local_up );
217 if ( dist < FG_SQUARE(t->bounding_radius) ) {
219 // traverse fragment list for tile
220 FGTileEntry::FragmentIterator current = t->begin();
221 FGTileEntry::FragmentIterator last = t->end();
223 for ( ; current != last; ++current ) {
224 frag_ptr = &(*current);
225 /* printf("distance squared = %.2f, bounding radius = %.2f\n",
226 point_line_dist_squared( &(frag_ptr->center),
227 &abs_view_pos), local_up),
228 frag_ptr->bounding_radius); */
230 dist = point_line_dist_squared( frag_ptr->center,
233 if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
234 if ( frag_ptr->intersect( abs_view_pos,
235 earth_center, 0, result ) ) {
236 FG_LOG( FG_TERRAIN, FG_DEBUG, "intersection point " <<
238 // compute geocentric coordinates of tile center
239 Point3D pp = fgCartToPolar3d(result);
240 FG_LOG( FG_TERRAIN, FG_DEBUG, " polar form = " << pp );
241 // convert to geodetic coordinates
242 fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod,
245 // printf("alt = %.2f\n", alt);
246 // exit since we found an intersection
247 if ( alt > -9999.0 ) {
248 // printf("returning alt\n");
251 // printf("returning 0\n");
259 FG_LOG( FG_TERRAIN, FG_INFO, "(new) no terrain intersection found" );
265 // Determine scenery altitude. Normally this just happens when we
266 // render the scene, but we'd also like to be able to do this
267 // explicitely. lat & lon are in radians. abs_view_pos in meters.
268 // Returns result in meters.
270 FGTileMgr::current_elev( double lon, double lat, const Point3D& abs_view_pos ) {
273 fgFRAGMENT *frag_ptr;
274 Point3D earth_center(0.0);
277 double dist, lat_geod, alt, sea_level_r;
280 c = &global_tile_cache;
282 local_up[0] = abs_view_pos.x();
283 local_up[1] = abs_view_pos.y();
284 local_up[2] = abs_view_pos.z();
286 FG_LOG( FG_TERRAIN, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
288 // Find current translation offset
289 FGBucket p( lon * RAD_TO_DEG, lat * RAD_TO_DEG );
290 index = c->exists(p);
292 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
296 t = c->get_tile(index);
298 scenery.next_center = t->center;
300 FG_LOG( FG_TERRAIN, FG_DEBUG,
301 "Pos = (" << lon * RAD_TO_DEG << ", " << lat * RAD_TO_DEG
302 << ") Current bucket = " << p
303 << " Index = " << p.gen_index_str() );
305 FG_LOG( FG_TERRAIN, FG_DEBUG, "Tile center " << t->center
306 << " bounding radius = " << t->bounding_radius );
308 // calculate tile offset
309 // x = (t->offset.x = t->center.x - scenery.center.x);
310 // y = (t->offset.y = t->center.y - scenery.center.y);
311 // z = (t->offset.z = t->center.z - scenery.center.z);
313 // calc current terrain elevation calculate distance from
314 // vertical tangent line at current position to center of
317 /* printf("distance squared = %.2f, bounding radius = %.2f\n",
318 point_line_dist_squared(&(t->offset), &(v->view_pos),
319 v->local_up), t->bounding_radius); */
321 dist = point_line_dist_squared( t->center, abs_view_pos, local_up );
322 FG_LOG( FG_TERRAIN, FG_DEBUG, "(gross check) dist squared = " << dist );
324 if ( dist < FG_SQUARE(t->bounding_radius) ) {
326 // traverse fragment list for tile
327 FGTileEntry::FragmentIterator current = t->begin();
328 FGTileEntry::FragmentIterator last = t->end();
330 for ( ; current != last; ++current ) {
331 frag_ptr = &(*current);
332 /* printf("distance squared = %.2f, bounding radius = %.2f\n",
333 point_line_dist_squared( &(frag_ptr->center),
334 &abs_view_pos), local_up),
335 frag_ptr->bounding_radius); */
337 dist = point_line_dist_squared( frag_ptr->center,
340 if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
341 if ( frag_ptr->intersect( abs_view_pos,
342 earth_center, 0, result ) ) {
343 FG_LOG( FG_TERRAIN, FG_DEBUG, "intersection point " <<
345 // compute geocentric coordinates of tile center
346 Point3D pp = fgCartToPolar3d(result);
347 FG_LOG( FG_TERRAIN, FG_DEBUG, " polar form = " << pp );
348 // convert to geodetic coordinates
349 fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod,
352 // printf("alt = %.2f\n", alt);
353 // exit since we found an intersection
354 if ( alt > -9999.0 ) {
355 // printf("returning alt\n");
358 // printf("returning 0\n");
366 FG_LOG( FG_TERRAIN, FG_INFO, "(old) no terrain intersection found" );
372 inline int fg_sign( const double x ) {
373 return x < 0 ? -1 : 1;
376 inline double fg_min( const double a, const double b ) {
377 return b < a ? b : a;
380 inline double fg_max( const double a, const double b ) {
381 return a < b ? b : a;
384 // return the minimum of the three values
385 inline double fg_min3( const double a, const double b, const double c ) {
386 return a > b ? fg_min(b, c) : fg_min(a, c);
389 // return the maximum of the three values
390 inline double fg_max3 (const double a, const double b, const double c ) {
391 return a < b ? fg_max(b, c) : fg_max(a, c);
395 // check for an instersection with the individual triangles of a leaf
396 static bool my_ssg_instersect_leaf( string s, ssgLeaf *leaf, sgMat4 m,
397 const sgVec3 p, const sgVec3 dir,
402 double x, y, z; // temporary holding spot for result
404 double x0, y0, z0, x1, y1, z1, a1, b1, c1;
406 double xmin, xmax, ymin, ymax, zmin, zmax;
407 double dx, dy, dz, min_dim, x2, y2, x3, y3, rx, ry;
412 cout << s << "Intersecting" << endl;
414 // traverse the triangle list for this leaf
415 for ( int i = 0; i < leaf->getNumTriangles(); ++i ) {
416 cout << s << "testing triangle = " << i << endl;
418 leaf->getTriangle( i, &i1, &i2, &i3 );
420 // get triangle vertex coordinates
422 tmp = leaf->getVertex( i1 );
423 cout << s << "orig point 1 = " << tmp[0] << " " << tmp[1]
424 << " " << tmp[2] << endl;
425 sgXformPnt3( p1, tmp, m ) ;
427 tmp = leaf->getVertex( i2 );
428 cout << s << "orig point 2 = " << tmp[0] << " " << tmp[1]
429 << " " << tmp[2] << endl;
430 sgXformPnt3( p2, tmp, m ) ;
432 tmp = leaf->getVertex( i3 );
433 cout << s << "orig point 3 = " << tmp[0] << " " << tmp[1]
434 << " " << tmp[2] << endl;
435 sgXformPnt3( p3, tmp, m ) ;
437 cout << s << "point 1 = " << p1[0] << " " << p1[1] << " " << p1[2]
439 cout << s << "point 2 = " << p2[0] << " " << p2[1] << " " << p2[2]
441 cout << s << "point 3 = " << p3[0] << " " << p3[1] << " " << p3[2]
444 // calculate two edge vectors, and the face normal
445 sgSubVec3(v1, p2, p1);
446 sgSubVec3(v2, p3, p1);
447 sgVectorProductVec3(n, v1, v2);
449 // calculate the plane coefficients for the plane defined by
450 // this face. If n is the normal vector, n = (a, b, c) and p1
451 // is a point on the plane, p1 = (x0, y0, z0), then the
452 // equation of the line is a(x-x0) + b(y-y0) + c(z-z0) = 0
456 d = a * p1[0] + b * p1[1] + c * p1[2];
457 // printf("a, b, c, d = %.2f %.2f %.2f %.2f\n", a, b, c, d);
459 // printf("p1(d) = %.2f\n", a * p1[0] + b * p1[1] + c * p1[2]);
460 // printf("p2(d) = %.2f\n", a * p2[0] + b * p2[1] + c * p2[2]);
461 // printf("p3(d) = %.2f\n", a * p3[0] + b * p3[1] + c * p3[2]);
463 // calculate the line coefficients for the specified line
464 x0 = p[0]; x1 = p[0] + dir[0];
465 y0 = p[1]; y1 = p[1] + dir[1];
466 z0 = p[2]; z1 = p[2] + dir[2];
468 if ( fabs(x1 - x0) > FG_EPSILON ) {
469 a1 = 1.0 / (x1 - x0);
471 // we got a big divide by zero problem here
477 // intersect the specified line with this plane
481 // printf("a = %.2f t1 = %.2f t2 = %.2f\n", a, t1, t2);
483 if ( fabs(a + t1 + t2) > FG_EPSILON ) {
484 x = (t1*x0 - b*y0 + t2*x0 - c*z0 + d) / (a + t1 + t2);
488 // printf("result(d) = %.2f\n", a * x + b * y + c * z);
490 // no intersection point
496 // check to see if end0 and end1 are on opposite sides of
498 if ( (x - x0) > FG_EPSILON ) {
502 } else if ( (y - y0) > FG_EPSILON ) {
506 } else if ( (z - z0) > FG_EPSILON ) {
511 // everything is too close together to tell the difference
512 // so the current intersection point should work as good
514 sgSetVec3( result, x, y, z );
517 side1 = fg_sign (t1 - t2);
518 side2 = fg_sign (t1 - t3);
519 if ( side1 == side2 ) {
526 // check to see if intersection point is in the bounding
528 #ifdef XTRA_DEBUG_STUFF
529 xmin = fg_min3 (p1[0], p2[0], p3[0]);
530 xmax = fg_max3 (p1[0], p2[0], p3[0]);
531 ymin = fg_min3 (p1[1], p2[1], p3[1]);
532 ymax = fg_max3 (p1[1], p2[1], p3[1]);
533 zmin = fg_min3 (p1[2], p2[2], p3[2]);
534 zmax = fg_max3 (p1[2], p2[2], p3[2]);
535 printf("bounding cube = %.2f,%.2f,%.2f %.2f,%.2f,%.2f\n",
536 xmin, ymin, zmin, xmax, ymax, zmax);
538 // punt if outside bouding cube
539 if ( x < (xmin = fg_min3 (p1[0], p2[0], p3[0])) ) {
541 } else if ( x > (xmax = fg_max3 (p1[0], p2[0], p3[0])) ) {
543 } else if ( y < (ymin = fg_min3 (p1[1], p2[1], p3[1])) ) {
545 } else if ( y > (ymax = fg_max3 (p1[1], p2[1], p3[1])) ) {
547 } else if ( z < (zmin = fg_min3 (p1[2], p2[2], p3[2])) ) {
549 } else if ( z > (zmax = fg_max3 (p1[2], p2[2], p3[2])) ) {
553 // (finally) check to see if the intersection point is
554 // actually inside this face
556 //first, drop the smallest dimension so we only have to work
561 min_dim = fg_min3 (dx, dy, dz);
562 if ( fabs(min_dim - dx) <= FG_EPSILON ) {
563 // x is the smallest dimension
572 } else if ( fabs(min_dim - dy) <= FG_EPSILON ) {
573 // y is the smallest dimension
582 } else if ( fabs(min_dim - dz) <= FG_EPSILON ) {
583 // z is the smallest dimension
593 // all dimensions are really small so lets call it close
594 // enough and return a successful match
595 sgSetVec3( result, x, y, z );
599 // check if intersection point is on the same side of p1 <-> p2 as p3
600 t1 = (y1 - y2) / (x1 - x2);
601 side1 = fg_sign (t1 * ((x3) - x2) + y2 - (y3));
602 side2 = fg_sign (t1 * ((rx) - x2) + y2 - (ry));
603 if ( side1 != side2 ) {
604 // printf("failed side 1 check\n");
608 // check if intersection point is on correct side of p2 <-> p3 as p1
609 t1 = (y2 - y3) / (x2 - x3);
610 side1 = fg_sign (t1 * ((x1) - x3) + y3 - (y1));
611 side2 = fg_sign (t1 * ((rx) - x3) + y3 - (ry));
612 if ( side1 != side2 ) {
613 // printf("failed side 2 check\n");
617 // check if intersection point is on correct side of p1 <-> p3 as p2
618 t1 = (y1 - y3) / (x1 - x3);
619 side1 = fg_sign (t1 * ((x2) - x3) + y3 - (y2));
620 side2 = fg_sign (t1 * ((rx) - x3) + y3 - (ry));
621 if ( side1 != side2 ) {
622 // printf("failed side 3 check\n");
626 // printf( "intersection point = %.2f %.2f %.2f\n", x, y, z);
627 sgSetVec3( result, x, y, z );
637 void FGTileMgr::my_ssg_los( string s, ssgBranch *branch, sgMat4 m,
638 const sgVec3 p, const sgVec3 dir )
641 for ( ssgEntity *kid = branch->getKid( 0 );
643 kid = branch->getNextKid() )
645 if ( kid->getTraversalMask() & SSGTRAV_HOT ) {
646 bsphere = kid->getBSphere();
648 sgCopyVec3( center, bsphere->getCenter() );
649 sgXformPnt3( center, m ) ;
650 cout << s << "entity bounding sphere:" << endl;
651 cout << s << "center = " << center[0] << " "
652 << center[1] << " " << center[2] << endl;
653 cout << s << "radius = " << bsphere->getRadius() << endl;
654 double radius_sqd = bsphere->getRadius() * bsphere->getRadius();
655 if ( sgPointLineDistSquared( center, p, dir ) < radius_sqd ) {
656 // possible intersections
657 if ( kid->isAKindOf ( ssgTypeBranch() ) ) {
659 sgCopyMat4(m_new, m);
660 if ( kid->isA( ssgTypeTransform() ) ) {
662 ((ssgTransform *)kid)->getTransform( xform );
663 sgPreMultMat4( m_new, xform );
665 my_ssg_los( s + " ", (ssgBranch *)kid, m_new, p, dir );
666 } else if ( kid->isAKindOf ( ssgTypeLeaf() ) ) {
668 if ( my_ssg_instersect_leaf( s, (ssgLeaf *)kid, m, p, dir,
671 cout << "sgLOS hit: " << result[0] << ","
672 << result[1] << "," << result[2] << endl;
676 // end of the line for this branch
679 // branch requested not to be traversed
685 // Determine scenery altitude via ssg. Normally this just happens
686 // when we render the scene, but we'd also like to be able to do this
687 // explicitely. lat & lon are in radians. view_pos in current world
688 // coordinate translated near (0,0,0) (in meters.) Returns result in
691 FGTileMgr::current_elev_ssg( const Point3D& abs_view_pos,
692 const Point3D& view_pos )
697 sgMakeIdentMat4 ( m ) ;
700 sgSetVec3(sgavp, abs_view_pos.x(), abs_view_pos.y(), abs_view_pos.z() );
701 sgSetVec3(sgvp, view_pos.x(), view_pos.y(), view_pos.z() );
703 cout << "starting ssg_los, view pos = " << view_pos[0] << " "
704 << view_pos[1] << " " << view_pos[2] << endl;
705 my_ssg_los( "", scene, m, sgvp, sgavp );
709 // given the current lon/lat, fill in the array of local chunks. If
710 // the chunk isn't already in the cache, then read it from disk.
711 int FGTileMgr::update( void ) {
715 static FGBucket p_last(false);
716 static double last_lon = -1000.0; // in degrees
717 static double last_lat = -1000.0; // in degrees
721 c = &global_tile_cache;
722 f = current_aircraft.fdm_state;
724 tile_diameter = current_options.get_tile_diameter();
726 FGBucket p1( f->get_Longitude() * RAD_TO_DEG,
727 f->get_Latitude() * RAD_TO_DEG );
728 dw = tile_diameter / 2;
729 dh = tile_diameter / 2;
731 if ( (p1 == p_last) && (state == Running) ) {
732 // same bucket as last time
733 FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
734 } else if ( (state == Start) || (state == Inited) ) {
737 // First time through or we have teleported, initialize the
738 // system and load all relavant tiles
740 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
741 FG_LOG( FG_TERRAIN, FG_INFO, " First time through ... " );
742 FG_LOG( FG_TERRAIN, FG_INFO, " Updating Tile list for " << p1 );
743 FG_LOG( FG_TERRAIN, FG_INFO, " Loading "
744 << tile_diameter * tile_diameter << " tiles" );
746 // wipe/initialize tile cache
750 // build the local area list and schedule tiles for loading
752 // start with the center tile and work out in concentric
755 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
756 f->get_Latitude() * RAD_TO_DEG,
760 for ( i = 3; i <= tile_diameter; i = i + 2 ) {
764 for ( j = -span; j <= span; ++j ) {
765 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
766 f->get_Latitude() * RAD_TO_DEG,
772 for ( j = -span; j <= span; ++j ) {
773 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
774 f->get_Latitude() * RAD_TO_DEG,
780 for ( j = -span + 1; j <= span - 1; ++j ) {
781 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
782 f->get_Latitude() * RAD_TO_DEG,
785 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
786 f->get_Latitude() * RAD_TO_DEG,
793 /* for ( j = 0; j < tile_diameter; j++ ) {
794 for ( i = 0; i < tile_diameter; i++ ) {
795 // fgBucketOffset(&p1, &p2, i - dw, j - dh);
796 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
797 f->get_Latitude() * RAD_TO_DEG,
803 // Now force a load of the center tile and inner ring so we
804 // have something to see in our first frame.
805 for ( i = 0; i < 9; ++i ) {
806 if ( load_queue.size() ) {
807 FG_LOG( FG_TERRAIN, FG_DEBUG,
808 "Load queue not empty, loading a tile" );
810 FGLoadRec pending = load_queue.front();
811 load_queue.pop_front();
812 load_tile( pending.b, pending.cache_index );
817 // We've moved to a new bucket, we need to scroll our
818 // structures, and load in the new tiles
821 // make sure load queue is flushed before doing shift
822 while ( load_queue.size() ) {
823 FG_LOG( FG_TERRAIN, FG_DEBUG,
824 "Load queue not empty, flushing queue before tile shift." );
826 FGLoadRec pending = load_queue.front();
827 load_queue.pop_front();
828 load_tile( pending.b, pending.index );
832 // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
833 // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
834 // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
836 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
838 if ( (p1.get_lon() > p_last.get_lon()) ||
839 ( (p1.get_lon() == p_last.get_lon()) &&
840 (p1.get_x() > p_last.get_x()) ) ) {
841 FG_LOG( FG_TERRAIN, FG_INFO,
842 " (East) Loading " << tile_diameter << " tiles" );
843 for ( j = 0; j < tile_diameter; j++ ) {
845 // schedule new column
846 p2 = fgBucketOffset( last_lon, last_lat, dw + 1, j - dh );
849 } else if ( (p1.get_lon() < p_last.get_lon()) ||
850 ( (p1.get_lon() == p_last.get_lon()) &&
851 (p1.get_x() < p_last.get_x()) ) ) {
852 FG_LOG( FG_TERRAIN, FG_INFO,
853 " (West) Loading " << tile_diameter << " tiles" );
854 for ( j = 0; j < tile_diameter; j++ ) {
856 // schedule new column
857 p2 = fgBucketOffset( last_lon, last_lat, -dw - 1, j - dh );
862 if ( (p1.get_lat() > p_last.get_lat()) ||
863 ( (p1.get_lat() == p_last.get_lat()) &&
864 (p1.get_y() > p_last.get_y()) ) ) {
865 FG_LOG( FG_TERRAIN, FG_INFO,
866 " (North) Loading " << tile_diameter << " tiles" );
867 for ( i = 0; i < tile_diameter; i++ ) {
870 p2 = fgBucketOffset( last_lon, last_lat, i - dw, dh + 1);
873 } else if ( (p1.get_lat() < p_last.get_lat()) ||
874 ( (p1.get_lat() == p_last.get_lat()) &&
875 (p1.get_y() < p_last.get_y()) ) ) {
876 FG_LOG( FG_TERRAIN, FG_INFO,
877 " (South) Loading " << tile_diameter << " tiles" );
878 for ( i = 0; i < tile_diameter; i++ ) {
881 p2 = fgBucketOffset( last_lon, last_lat, i - dw, -dh - 1);
887 if ( load_queue.size() ) {
888 FG_LOG( FG_TERRAIN, FG_DEBUG, "Load queue not empty, loading a tile" );
890 FGLoadRec pending = load_queue.front();
891 load_queue.pop_front();
892 load_tile( pending.b, pending.cache_index );
895 // find our current elevation (feed in the current bucket to save work)
896 Point3D geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
897 Point3D tmp_abs_view_pos = fgGeodToCart(geod_pos);
900 current_elev( f->get_Longitude(), f->get_Latitude(), tmp_abs_view_pos );
901 cout << "current elevation == " << scenery.cur_elev << endl;
902 double junk = current_elev_ssg( current_view.abs_view_pos,
903 current_view.view_pos );
904 cout << "current elevation (ssg) == " << junk << endl;
907 last_lon = f->get_Longitude() * RAD_TO_DEG;
908 last_lat = f->get_Latitude() * RAD_TO_DEG;
916 // inrange() IS THIS POINT WITHIN POSSIBLE VIEWING RANGE ?
917 // calculate distance from vertical tangent line at
918 // current position to center of object.
919 // this is equivalent to
920 // dist = point_line_dist_squared( &(t->center), &(v->abs_view_pos),
922 // if ( dist < FG_SQUARE(t->bounding_radius) ) {
924 // the compiler should inline this for us
927 inrange( const double radius, const Point3D& center, const Point3D& vp,
934 u[0] = center.x() - vp.x();
935 u[1] = center.y() - vp.y();
936 u[2] = center.z() - vp.z();
938 // calculate the projection, u1, of u along d.
939 // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
941 MAT3_SCALE_VEC(u1, up,
942 (MAT3_DOT_PRODUCT(u, up) / MAT3_DOT_PRODUCT(up, up)) );
944 // v = u - u1 = vector from closest point on line, p1, to the
945 // original point, p.
946 MAT3_SUB_VEC(v, u, u1);
948 return( FG_SQUARE(radius) >= MAT3_DOT_PRODUCT(v, v));
952 // NEW for legibility
954 // update this tile's geometry for current view
955 // The Compiler should inline this
957 update_tile_geometry( FGTileEntry *t, GLdouble *MODEL_VIEW)
962 // calculate tile offset
963 t->offset = t->center - scenery.center;
971 // Calculate the model_view transformation matrix for this tile
972 FG_MEM_COPY( m, MODEL_VIEW, 16*sizeof(GLdouble) );
974 // This is equivalent to doing a glTranslatef(x, y, z);
975 m[12] += (m[0]*x + m[4]*y + m[8] *z);
976 m[13] += (m[1]*x + m[5]*y + m[9] *z);
977 m[14] += (m[2]*x + m[6]*y + m[10]*z);
978 // m[15] += (m[3]*x + m[7]*y + m[11]*z);
979 // m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
980 // so m[15] is unchanged
984 // Prepare the ssg nodes ... for each tile, set it's proper
985 // transform and update it's range selector based on current
987 void FGTileMgr::prep_ssg_nodes( void ) {
993 // traverse the potentially viewable tile list and update range
994 // selector and transform
995 for ( int i = 0; i < (int)global_tile_cache.get_size(); i++ ) {
996 t = global_tile_cache.get_tile( i );
998 if ( t->is_loaded() ) {
999 // set range selector (LOD trick) to be distance to center
1000 // of tile + bounding radius
1001 #ifndef FG_OLD_WEATHER
1002 ranges[1] = WeatherDatabase->getWeatherVisibility()
1003 + t->bounding_radius;
1005 ranges[1] = current_weather.get_visibility()+t->bounding_radius;
1007 t->range_ptr->setRanges( ranges, 2 );
1009 // calculate tile offset
1010 t->SetOffset( scenery.center );
1012 // calculate ssg transform
1014 sgSetCoord( &sgcoord,
1015 t->offset.x(), t->offset.y(), t->offset.z(),
1017 t->transform_ptr->setTransform( &sgcoord );