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);
394 inline void sgdSetVec3 ( sgdVec3 dst, sgVec3 src )
396 dst [ 0 ] = src [ 0 ] ;
397 dst [ 1 ] = src [ 1 ] ;
398 dst [ 2 ] = src [ 2 ] ;
401 inline void sgdSetMat4 ( sgdMat4 dst, sgMat4 src )
403 for ( int i = 0; i < 4; ++i ) {
404 for ( int j = 0; j < 4; ++j ) {
405 dst [ i ] [ j ] = src [ i ] [ j ];
410 // check for an instersection with the individual triangles of a leaf
411 static bool my_ssg_instersect_leaf( string s, ssgLeaf *leaf, sgdMat4 m,
412 const sgdVec3 p, const sgdVec3 dir,
417 double x, y, z; // temporary holding spot for result
419 double x0, y0, z0, x1, y1, z1, a1, b1, c1;
421 double xmin, xmax, ymin, ymax, zmin, zmax;
422 double dx, dy, dz, min_dim, x2, y2, x3, y3, rx, ry;
428 // cout << s << "Intersecting" << endl;
430 // traverse the triangle list for this leaf
431 for ( int i = 0; i < leaf->getNumTriangles(); ++i ) {
432 // cout << s << "testing triangle = " << i << endl;
434 leaf->getTriangle( i, &i1, &i2, &i3 );
436 // get triangle vertex coordinates
438 ftmp = leaf->getVertex( i1 );
439 sgdSetVec3( tmp, ftmp );
440 // cout << s << "orig point 1 = " << tmp[0] << " " << tmp[1]
441 // << " " << tmp[2] << endl;
442 sgdXformPnt3( p1, tmp, m ) ;
444 ftmp = leaf->getVertex( i2 );
445 sgdSetVec3( tmp, ftmp );
446 // cout << s << "orig point 2 = " << tmp[0] << " " << tmp[1]
447 // << " " << tmp[2] << endl;
448 sgdXformPnt3( p2, tmp, m ) ;
450 ftmp = leaf->getVertex( i3 );
451 sgdSetVec3( tmp, ftmp );
452 // cout << s << "orig point 3 = " << tmp[0] << " " << tmp[1]
453 // << " " << tmp[2] << endl;
454 sgdXformPnt3( p3, tmp, m ) ;
456 // cout << s << "point 1 = " << p1[0] << " " << p1[1] << " " << p1[2]
458 // cout << s << "point 2 = " << p2[0] << " " << p2[1] << " " << p2[2]
460 // cout << s << "point 3 = " << p3[0] << " " << p3[1] << " " << p3[2]
463 // calculate two edge vectors, and the face normal
464 sgdSubVec3(v1, p2, p1);
465 sgdSubVec3(v2, p3, p1);
466 sgdVectorProductVec3(n, v1, v2);
468 // calculate the plane coefficients for the plane defined by
469 // this face. If n is the normal vector, n = (a, b, c) and p1
470 // is a point on the plane, p1 = (x0, y0, z0), then the
471 // equation of the line is a(x-x0) + b(y-y0) + c(z-z0) = 0
475 d = a * p1[0] + b * p1[1] + c * p1[2];
476 // printf("a, b, c, d = %.2f %.2f %.2f %.2f\n", a, b, c, d);
478 // printf("p1(d) = %.2f\n", a * p1[0] + b * p1[1] + c * p1[2]);
479 // printf("p2(d) = %.2f\n", a * p2[0] + b * p2[1] + c * p2[2]);
480 // printf("p3(d) = %.2f\n", a * p3[0] + b * p3[1] + c * p3[2]);
482 // calculate the line coefficients for the specified line
483 x0 = p[0]; x1 = p[0] + dir[0];
484 y0 = p[1]; y1 = p[1] + dir[1];
485 z0 = p[2]; z1 = p[2] + dir[2];
487 if ( fabs(x1 - x0) > FG_EPSILON ) {
488 a1 = 1.0 / (x1 - x0);
490 // we got a big divide by zero problem here
496 // intersect the specified line with this plane
500 // printf("a = %.2f t1 = %.2f t2 = %.2f\n", a, t1, t2);
502 if ( fabs(a + t1 + t2) > FG_EPSILON ) {
503 x = (t1*x0 - b*y0 + t2*x0 - c*z0 + d) / (a + t1 + t2);
507 // printf("result(d) = %.2f\n", a * x + b * y + c * z);
509 // no intersection point
515 // check to see if end0 and end1 are on opposite sides of
517 if ( (x - x0) > FG_EPSILON ) {
521 } else if ( (y - y0) > FG_EPSILON ) {
525 } else if ( (z - z0) > FG_EPSILON ) {
530 // everything is too close together to tell the difference
531 // so the current intersection point should work as good
533 sgdSetVec3( result, x, y, z );
536 side1 = fg_sign (t1 - t2);
537 side2 = fg_sign (t1 - t3);
538 if ( side1 == side2 ) {
545 // check to see if intersection point is in the bounding
547 #ifdef XTRA_DEBUG_STUFF
548 xmin = fg_min3 (p1[0], p2[0], p3[0]);
549 xmax = fg_max3 (p1[0], p2[0], p3[0]);
550 ymin = fg_min3 (p1[1], p2[1], p3[1]);
551 ymax = fg_max3 (p1[1], p2[1], p3[1]);
552 zmin = fg_min3 (p1[2], p2[2], p3[2]);
553 zmax = fg_max3 (p1[2], p2[2], p3[2]);
554 printf("bounding cube = %.2f,%.2f,%.2f %.2f,%.2f,%.2f\n",
555 xmin, ymin, zmin, xmax, ymax, zmax);
557 // punt if outside bouding cube
558 if ( x < (xmin = fg_min3 (p1[0], p2[0], p3[0])) ) {
560 } else if ( x > (xmax = fg_max3 (p1[0], p2[0], p3[0])) ) {
562 } else if ( y < (ymin = fg_min3 (p1[1], p2[1], p3[1])) ) {
564 } else if ( y > (ymax = fg_max3 (p1[1], p2[1], p3[1])) ) {
566 } else if ( z < (zmin = fg_min3 (p1[2], p2[2], p3[2])) ) {
568 } else if ( z > (zmax = fg_max3 (p1[2], p2[2], p3[2])) ) {
572 // (finally) check to see if the intersection point is
573 // actually inside this face
575 //first, drop the smallest dimension so we only have to work
580 min_dim = fg_min3 (dx, dy, dz);
581 if ( fabs(min_dim - dx) <= FG_EPSILON ) {
582 // x is the smallest dimension
591 } else if ( fabs(min_dim - dy) <= FG_EPSILON ) {
592 // y is the smallest dimension
601 } else if ( fabs(min_dim - dz) <= FG_EPSILON ) {
602 // z is the smallest dimension
612 // all dimensions are really small so lets call it close
613 // enough and return a successful match
614 sgdSetVec3( result, x, y, z );
618 // check if intersection point is on the same side of p1 <-> p2 as p3
619 t1 = (y1 - y2) / (x1 - x2);
620 side1 = fg_sign (t1 * ((x3) - x2) + y2 - (y3));
621 side2 = fg_sign (t1 * ((rx) - x2) + y2 - (ry));
622 if ( side1 != side2 ) {
623 // printf("failed side 1 check\n");
627 // check if intersection point is on correct side of p2 <-> p3 as p1
628 t1 = (y2 - y3) / (x2 - x3);
629 side1 = fg_sign (t1 * ((x1) - x3) + y3 - (y1));
630 side2 = fg_sign (t1 * ((rx) - x3) + y3 - (ry));
631 if ( side1 != side2 ) {
632 // printf("failed side 2 check\n");
636 // check if intersection point is on correct side of p1 <-> p3 as p2
637 t1 = (y1 - y3) / (x1 - x3);
638 side1 = fg_sign (t1 * ((x2) - x3) + y3 - (y2));
639 side2 = fg_sign (t1 * ((rx) - x3) + y3 - (ry));
640 if ( side1 != side2 ) {
641 // printf("failed side 3 check\n");
645 // printf( "intersection point = %.2f %.2f %.2f\n", x, y, z);
646 sgdSetVec3( result, x, y, z );
656 void FGTileMgr::my_ssg_los( string s, ssgBranch *branch, sgdMat4 m,
657 const sgdVec3 p, const sgdVec3 dir )
660 for ( ssgEntity *kid = branch->getKid( 0 );
662 kid = branch->getNextKid() )
664 if ( kid->getTraversalMask() & SSGTRAV_HOT ) {
665 bsphere = kid->getBSphere();
667 sgCopyVec3( fcenter, bsphere->getCenter() );
669 center[0] = fcenter[0];
670 center[1] = fcenter[1];
671 center[2] = fcenter[2];
672 sgdXformPnt3( center, m ) ;
673 // cout << s << "entity bounding sphere:" << endl;
674 // cout << s << "center = " << center[0] << " "
675 // << center[1] << " " << center[2] << endl;
676 // cout << s << "radius = " << bsphere->getRadius() << endl;
677 double radius_sqd = bsphere->getRadius() * bsphere->getRadius();
678 if ( sgdPointLineDistSquared( center, p, dir ) < radius_sqd ) {
679 // possible intersections
680 if ( kid->isAKindOf ( ssgTypeBranch() ) ) {
682 sgdCopyMat4(m_new, m);
683 if ( kid->isA( ssgTypeTransform() ) ) {
685 ((ssgTransform *)kid)->getTransform( fxform );
687 sgdSetMat4( xform, fxform );
688 sgdPreMultMat4( m_new, xform );
690 my_ssg_los( s + " ", (ssgBranch *)kid, m_new, p, dir );
691 } else if ( kid->isAKindOf ( ssgTypeLeaf() ) ) {
693 if ( my_ssg_instersect_leaf( s, (ssgLeaf *)kid, m, p, dir,
696 // cout << "sgLOS hit: " << result[0] << ","
697 // << result[1] << "," << result[2] << endl;
698 hit_pts[hitcount] = result;
703 // end of the line for this branch
706 // branch requested not to be traversed
712 // Determine scenery altitude via ssg. Normally this just happens
713 // when we render the scene, but we'd also like to be able to do this
714 // explicitely. lat & lon are in radians. view_pos in current world
715 // coordinate translated near (0,0,0) (in meters.) Returns result in
718 FGTileMgr::current_elev_ssg( const Point3D& abs_view_pos,
719 const Point3D& view_pos )
724 sgdMakeIdentMat4 ( m ) ;
727 sgdSetVec3(sgavp, abs_view_pos.x(), abs_view_pos.y(), abs_view_pos.z() );
728 sgdSetVec3(sgvp, view_pos.x(), view_pos.y(), view_pos.z() );
730 // cout << "starting ssg_los, abs view pos = " << abs_view_pos[0] << " "
731 // << abs_view_pos[1] << " " << abs_view_pos[2] << endl;
732 // cout << "starting ssg_los, view pos = " << view_pos[0] << " "
733 // << view_pos[1] << " " << view_pos[2] << endl;
734 my_ssg_los( "", scene, m, sgvp, sgavp );
736 double result = -9999;
738 for ( int i = 0; i < hitcount; ++i ) {
739 Point3D rel_cart( hit_pts[i][0], hit_pts[i][1], hit_pts[i][2] );
740 Point3D abs_cart = rel_cart + scenery.center;
741 Point3D pp = fgCartToPolar3d( abs_cart );
742 FG_LOG( FG_TERRAIN, FG_DEBUG, " polar form = " << pp );
743 // convert to geodetic coordinates
744 double lat_geod, alt, sea_level_r;
745 fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod,
748 // printf("alt = %.2f\n", alt);
749 // exit since we found an intersection
750 if ( alt > result && alt < 10000 ) {
751 // printf("returning alt\n");
756 if ( result > -9000 ) {
759 FG_LOG( FG_TERRAIN, FG_INFO, "no terrain intersection" );
765 // given the current lon/lat, fill in the array of local chunks. If
766 // the chunk isn't already in the cache, then read it from disk.
767 int FGTileMgr::update( void ) {
772 static FGBucket p_last(false);
773 static double last_lon = -1000.0; // in degrees
774 static double last_lat = -1000.0; // in degrees
778 c = &global_tile_cache;
779 f = current_aircraft.fdm_state;
781 tile_diameter = current_options.get_tile_diameter();
783 FGBucket p1( f->get_Longitude() * RAD_TO_DEG,
784 f->get_Latitude() * RAD_TO_DEG );
786 long int index = c->exists(p1);
788 t = c->get_tile(index);
789 scenery.next_center = t->center;
791 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
794 dw = tile_diameter / 2;
795 dh = tile_diameter / 2;
797 if ( (p1 == p_last) && (state == Running) ) {
798 // same bucket as last time
799 FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
800 } else if ( (state == Start) || (state == Inited) ) {
803 // First time through or we have teleported, initialize the
804 // system and load all relavant tiles
806 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
807 FG_LOG( FG_TERRAIN, FG_INFO, " First time through ... " );
808 FG_LOG( FG_TERRAIN, FG_INFO, " Updating Tile list for " << p1 );
809 FG_LOG( FG_TERRAIN, FG_INFO, " Loading "
810 << tile_diameter * tile_diameter << " tiles" );
812 // wipe/initialize tile cache
816 // build the local area list and schedule tiles for loading
818 // start with the center tile and work out in concentric
821 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
822 f->get_Latitude() * RAD_TO_DEG,
826 // prime scenery center calculations
827 Point3D geod_view_center( p2.get_center_lon(),
829 cur_fdm_state->get_Altitude()*FEET_TO_METER +
831 current_view.abs_view_pos = fgGeodToCart( geod_view_center );
832 current_view.view_pos = current_view.abs_view_pos - scenery.next_center;
834 for ( i = 3; i <= tile_diameter; i = i + 2 ) {
838 for ( j = -span; j <= span; ++j ) {
839 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
840 f->get_Latitude() * RAD_TO_DEG,
846 for ( j = -span; j <= span; ++j ) {
847 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
848 f->get_Latitude() * RAD_TO_DEG,
854 for ( j = -span + 1; j <= span - 1; ++j ) {
855 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
856 f->get_Latitude() * RAD_TO_DEG,
859 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
860 f->get_Latitude() * RAD_TO_DEG,
867 /* for ( j = 0; j < tile_diameter; j++ ) {
868 for ( i = 0; i < tile_diameter; i++ ) {
869 // fgBucketOffset(&p1, &p2, i - dw, j - dh);
870 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
871 f->get_Latitude() * RAD_TO_DEG,
877 // Now force a load of the center tile and inner ring so we
878 // have something to see in our first frame.
879 for ( i = 0; i < 9; ++i ) {
880 if ( load_queue.size() ) {
881 FG_LOG( FG_TERRAIN, FG_DEBUG,
882 "Load queue not empty, loading a tile" );
884 FGLoadRec pending = load_queue.front();
885 load_queue.pop_front();
886 load_tile( pending.b, pending.cache_index );
891 // We've moved to a new bucket, we need to scroll our
892 // structures, and load in the new tiles
895 // make sure load queue is flushed before doing shift
896 while ( load_queue.size() ) {
897 FG_LOG( FG_TERRAIN, FG_DEBUG,
898 "Load queue not empty, flushing queue before tile shift." );
900 FGLoadRec pending = load_queue.front();
901 load_queue.pop_front();
902 load_tile( pending.b, pending.index );
906 // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
907 // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
908 // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
910 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
912 if ( (p1.get_lon() > p_last.get_lon()) ||
913 ( (p1.get_lon() == p_last.get_lon()) &&
914 (p1.get_x() > p_last.get_x()) ) ) {
915 FG_LOG( FG_TERRAIN, FG_INFO,
916 " (East) Loading " << tile_diameter << " tiles" );
917 for ( j = 0; j < tile_diameter; j++ ) {
919 // schedule new column
920 p2 = fgBucketOffset( last_lon, last_lat, dw + 1, j - dh );
923 } else if ( (p1.get_lon() < p_last.get_lon()) ||
924 ( (p1.get_lon() == p_last.get_lon()) &&
925 (p1.get_x() < p_last.get_x()) ) ) {
926 FG_LOG( FG_TERRAIN, FG_INFO,
927 " (West) Loading " << tile_diameter << " tiles" );
928 for ( j = 0; j < tile_diameter; j++ ) {
930 // schedule new column
931 p2 = fgBucketOffset( last_lon, last_lat, -dw - 1, j - dh );
936 if ( (p1.get_lat() > p_last.get_lat()) ||
937 ( (p1.get_lat() == p_last.get_lat()) &&
938 (p1.get_y() > p_last.get_y()) ) ) {
939 FG_LOG( FG_TERRAIN, FG_INFO,
940 " (North) Loading " << tile_diameter << " tiles" );
941 for ( i = 0; i < tile_diameter; i++ ) {
944 p2 = fgBucketOffset( last_lon, last_lat, i - dw, dh + 1);
947 } else if ( (p1.get_lat() < p_last.get_lat()) ||
948 ( (p1.get_lat() == p_last.get_lat()) &&
949 (p1.get_y() < p_last.get_y()) ) ) {
950 FG_LOG( FG_TERRAIN, FG_INFO,
951 " (South) Loading " << tile_diameter << " tiles" );
952 for ( i = 0; i < tile_diameter; i++ ) {
955 p2 = fgBucketOffset( last_lon, last_lat, i - dw, -dh - 1);
961 if ( load_queue.size() ) {
962 FG_LOG( FG_TERRAIN, FG_DEBUG, "Load queue not empty, loading a tile" );
964 FGLoadRec pending = load_queue.front();
965 load_queue.pop_front();
966 load_tile( pending.b, pending.cache_index );
969 // find our current elevation (feed in the current bucket to save work)
970 Point3D geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
971 Point3D tmp_abs_view_pos = fgGeodToCart(geod_pos);
973 // cout << "current elevation (old) == "
974 // << current_elev( f->get_Longitude(), f->get_Latitude(),
975 // tmp_abs_view_pos )
977 scenery.cur_elev = current_elev_ssg( current_view.abs_view_pos,
978 current_view.view_pos );
979 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
982 last_lon = f->get_Longitude() * RAD_TO_DEG;
983 last_lat = f->get_Latitude() * RAD_TO_DEG;
991 // inrange() IS THIS POINT WITHIN POSSIBLE VIEWING RANGE ?
992 // calculate distance from vertical tangent line at
993 // current position to center of object.
994 // this is equivalent to
995 // dist = point_line_dist_squared( &(t->center), &(v->abs_view_pos),
997 // if ( dist < FG_SQUARE(t->bounding_radius) ) {
999 // the compiler should inline this for us
1002 inrange( const double radius, const Point3D& center, const Point3D& vp,
1009 u[0] = center.x() - vp.x();
1010 u[1] = center.y() - vp.y();
1011 u[2] = center.z() - vp.z();
1013 // calculate the projection, u1, of u along d.
1014 // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
1016 MAT3_SCALE_VEC(u1, up,
1017 (MAT3_DOT_PRODUCT(u, up) / MAT3_DOT_PRODUCT(up, up)) );
1019 // v = u - u1 = vector from closest point on line, p1, to the
1020 // original point, p.
1021 MAT3_SUB_VEC(v, u, u1);
1023 return( FG_SQUARE(radius) >= MAT3_DOT_PRODUCT(v, v));
1027 // NEW for legibility
1029 // update this tile's geometry for current view
1030 // The Compiler should inline this
1032 update_tile_geometry( FGTileEntry *t, GLdouble *MODEL_VIEW)
1037 // calculate tile offset
1038 t->offset = t->center - scenery.center;
1046 // Calculate the model_view transformation matrix for this tile
1047 FG_MEM_COPY( m, MODEL_VIEW, 16*sizeof(GLdouble) );
1049 // This is equivalent to doing a glTranslatef(x, y, z);
1050 m[12] += (m[0]*x + m[4]*y + m[8] *z);
1051 m[13] += (m[1]*x + m[5]*y + m[9] *z);
1052 m[14] += (m[2]*x + m[6]*y + m[10]*z);
1053 // m[15] += (m[3]*x + m[7]*y + m[11]*z);
1054 // m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
1055 // so m[15] is unchanged
1059 // Prepare the ssg nodes ... for each tile, set it's proper
1060 // transform and update it's range selector based on current
1062 void FGTileMgr::prep_ssg_nodes( void ) {
1068 // traverse the potentially viewable tile list and update range
1069 // selector and transform
1070 for ( int i = 0; i < (int)global_tile_cache.get_size(); i++ ) {
1071 t = global_tile_cache.get_tile( i );
1073 if ( t->is_loaded() ) {
1074 // set range selector (LOD trick) to be distance to center
1075 // of tile + bounding radius
1076 #ifndef FG_OLD_WEATHER
1077 ranges[1] = WeatherDatabase->getWeatherVisibility()
1078 + t->bounding_radius;
1080 ranges[1] = current_weather.get_visibility()+t->bounding_radius;
1082 t->range_ptr->setRanges( ranges, 2 );
1084 // calculate tile offset
1085 t->SetOffset( scenery.center );
1087 // calculate ssg transform
1089 sgSetCoord( &sgcoord,
1090 t->offset.x(), t->offset.y(), t->offset.z(),
1092 t->transform_ptr->setTransform( &sgcoord );