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 // check for an instersection with the individual triangles of a leaf
395 static bool my_ssg_instersect_leaf( string s, ssgLeaf *leaf, sgdMat4 m,
396 const sgdVec3 p, const sgdVec3 dir,
401 double x, y, z; // temporary holding spot for result
403 double x0, y0, z0, x1, y1, z1, a1, b1, c1;
405 double xmin, xmax, ymin, ymax, zmin, zmax;
406 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 ftmp = leaf->getVertex( i1 );
423 sgdSetVec3( tmp, ftmp );
424 // cout << s << "orig point 1 = " << tmp[0] << " " << tmp[1]
425 // << " " << tmp[2] << endl;
426 sgdXformPnt3( p1, tmp, m ) ;
428 ftmp = leaf->getVertex( i2 );
429 sgdSetVec3( tmp, ftmp );
430 // cout << s << "orig point 2 = " << tmp[0] << " " << tmp[1]
431 // << " " << tmp[2] << endl;
432 sgdXformPnt3( p2, tmp, m ) ;
434 ftmp = leaf->getVertex( i3 );
435 sgdSetVec3( tmp, ftmp );
436 // cout << s << "orig point 3 = " << tmp[0] << " " << tmp[1]
437 // << " " << tmp[2] << endl;
438 sgdXformPnt3( p3, tmp, m ) ;
440 // cout << s << "point 1 = " << p1[0] << " " << p1[1] << " " << p1[2]
442 // cout << s << "point 2 = " << p2[0] << " " << p2[1] << " " << p2[2]
444 // cout << s << "point 3 = " << p3[0] << " " << p3[1] << " " << p3[2]
447 // calculate two edge vectors, and the face normal
448 sgdSubVec3(v1, p2, p1);
449 sgdSubVec3(v2, p3, p1);
450 sgdVectorProductVec3(n, v1, v2);
452 // calculate the plane coefficients for the plane defined by
453 // this face. If n is the normal vector, n = (a, b, c) and p1
454 // is a point on the plane, p1 = (x0, y0, z0), then the
455 // equation of the line is a(x-x0) + b(y-y0) + c(z-z0) = 0
459 d = a * p1[0] + b * p1[1] + c * p1[2];
460 // printf("a, b, c, d = %.2f %.2f %.2f %.2f\n", a, b, c, d);
462 // printf("p1(d) = %.2f\n", a * p1[0] + b * p1[1] + c * p1[2]);
463 // printf("p2(d) = %.2f\n", a * p2[0] + b * p2[1] + c * p2[2]);
464 // printf("p3(d) = %.2f\n", a * p3[0] + b * p3[1] + c * p3[2]);
466 // calculate the line coefficients for the specified line
467 x0 = p[0]; x1 = p[0] + dir[0];
468 y0 = p[1]; y1 = p[1] + dir[1];
469 z0 = p[2]; z1 = p[2] + dir[2];
471 if ( fabs(x1 - x0) > FG_EPSILON ) {
472 a1 = 1.0 / (x1 - x0);
474 // we got a big divide by zero problem here
480 // intersect the specified line with this plane
484 // printf("a = %.2f t1 = %.2f t2 = %.2f\n", a, t1, t2);
486 if ( fabs(a + t1 + t2) > FG_EPSILON ) {
487 x = (t1*x0 - b*y0 + t2*x0 - c*z0 + d) / (a + t1 + t2);
491 // printf("result(d) = %.2f\n", a * x + b * y + c * z);
493 // no intersection point
499 // check to see if end0 and end1 are on opposite sides of
501 if ( (x - x0) > FG_EPSILON ) {
505 } else if ( (y - y0) > FG_EPSILON ) {
509 } else if ( (z - z0) > FG_EPSILON ) {
514 // everything is too close together to tell the difference
515 // so the current intersection point should work as good
517 sgdSetVec3( result, x, y, z );
520 side1 = fg_sign (t1 - t2);
521 side2 = fg_sign (t1 - t3);
522 if ( side1 == side2 ) {
529 // check to see if intersection point is in the bounding
531 #ifdef XTRA_DEBUG_STUFF
532 xmin = fg_min3 (p1[0], p2[0], p3[0]);
533 xmax = fg_max3 (p1[0], p2[0], p3[0]);
534 ymin = fg_min3 (p1[1], p2[1], p3[1]);
535 ymax = fg_max3 (p1[1], p2[1], p3[1]);
536 zmin = fg_min3 (p1[2], p2[2], p3[2]);
537 zmax = fg_max3 (p1[2], p2[2], p3[2]);
538 printf("bounding cube = %.2f,%.2f,%.2f %.2f,%.2f,%.2f\n",
539 xmin, ymin, zmin, xmax, ymax, zmax);
541 // punt if outside bouding cube
542 if ( x < (xmin = fg_min3 (p1[0], p2[0], p3[0])) ) {
544 } else if ( x > (xmax = fg_max3 (p1[0], p2[0], p3[0])) ) {
546 } else if ( y < (ymin = fg_min3 (p1[1], p2[1], p3[1])) ) {
548 } else if ( y > (ymax = fg_max3 (p1[1], p2[1], p3[1])) ) {
550 } else if ( z < (zmin = fg_min3 (p1[2], p2[2], p3[2])) ) {
552 } else if ( z > (zmax = fg_max3 (p1[2], p2[2], p3[2])) ) {
556 // (finally) check to see if the intersection point is
557 // actually inside this face
559 //first, drop the smallest dimension so we only have to work
564 min_dim = fg_min3 (dx, dy, dz);
565 if ( fabs(min_dim - dx) <= FG_EPSILON ) {
566 // x is the smallest dimension
575 } else if ( fabs(min_dim - dy) <= FG_EPSILON ) {
576 // y is the smallest dimension
585 } else if ( fabs(min_dim - dz) <= FG_EPSILON ) {
586 // z is the smallest dimension
596 // all dimensions are really small so lets call it close
597 // enough and return a successful match
598 sgdSetVec3( result, x, y, z );
602 // check if intersection point is on the same side of p1 <-> p2 as p3
603 t1 = (y1 - y2) / (x1 - x2);
604 side1 = fg_sign (t1 * ((x3) - x2) + y2 - (y3));
605 side2 = fg_sign (t1 * ((rx) - x2) + y2 - (ry));
606 if ( side1 != side2 ) {
607 // printf("failed side 1 check\n");
611 // check if intersection point is on correct side of p2 <-> p3 as p1
612 t1 = (y2 - y3) / (x2 - x3);
613 side1 = fg_sign (t1 * ((x1) - x3) + y3 - (y1));
614 side2 = fg_sign (t1 * ((rx) - x3) + y3 - (ry));
615 if ( side1 != side2 ) {
616 // printf("failed side 2 check\n");
620 // check if intersection point is on correct side of p1 <-> p3 as p2
621 t1 = (y1 - y3) / (x1 - x3);
622 side1 = fg_sign (t1 * ((x2) - x3) + y3 - (y2));
623 side2 = fg_sign (t1 * ((rx) - x3) + y3 - (ry));
624 if ( side1 != side2 ) {
625 // printf("failed side 3 check\n");
629 // printf( "intersection point = %.2f %.2f %.2f\n", x, y, z);
630 sgdSetVec3( result, x, y, z );
640 void FGTileMgr::my_ssg_los( string s, ssgBranch *branch, sgdMat4 m,
641 const sgdVec3 p, const sgdVec3 dir )
644 for ( ssgEntity *kid = branch->getKid( 0 );
646 kid = branch->getNextKid() )
648 if ( kid->getTraversalMask() & SSGTRAV_HOT ) {
649 bsphere = kid->getBSphere();
651 sgCopyVec3( fcenter, bsphere->getCenter() );
653 center[0] = fcenter[0];
654 center[1] = fcenter[1];
655 center[2] = fcenter[2];
656 sgdXformPnt3( center, m ) ;
657 // cout << s << "entity bounding sphere:" << endl;
658 // cout << s << "center = " << center[0] << " "
659 // << center[1] << " " << center[2] << endl;
660 // cout << s << "radius = " << bsphere->getRadius() << endl;
661 double radius_sqd = bsphere->getRadius() * bsphere->getRadius();
662 if ( sgdPointLineDistSquared( center, p, dir ) < radius_sqd ) {
663 // possible intersections
664 if ( kid->isAKindOf ( ssgTypeBranch() ) ) {
666 sgdCopyMat4(m_new, m);
667 if ( kid->isA( ssgTypeTransform() ) ) {
669 ((ssgTransform *)kid)->getTransform( fxform );
671 sgdSetMat4( xform, fxform );
672 sgdPreMultMat4( m_new, xform );
674 my_ssg_los( s + " ", (ssgBranch *)kid, m_new, p, dir );
675 } else if ( kid->isAKindOf ( ssgTypeLeaf() ) ) {
677 if ( my_ssg_instersect_leaf( s, (ssgLeaf *)kid, m, p, dir,
680 // cout << "sgLOS hit: " << result[0] << ","
681 // << result[1] << "," << result[2] << endl;
682 for (int i=0; i < 3; i++) {
683 hit_pts[hitcount][i] = result[i];
689 // end of the line for this branch
692 // branch requested not to be traversed
698 // Determine scenery altitude via ssg. Normally this just happens
699 // when we render the scene, but we'd also like to be able to do this
700 // explicitely. lat & lon are in radians. view_pos in current world
701 // coordinate translated near (0,0,0) (in meters.) Returns result in
704 FGTileMgr::current_elev_ssg( const Point3D& abs_view_pos,
705 const Point3D& view_pos )
710 sgdMakeIdentMat4 ( m ) ;
713 sgdSetVec3(sgavp, abs_view_pos.x(), abs_view_pos.y(), abs_view_pos.z() );
714 sgdSetVec3(sgvp, view_pos.x(), view_pos.y(), view_pos.z() );
716 // cout << "starting ssg_los, abs view pos = " << abs_view_pos[0] << " "
717 // << abs_view_pos[1] << " " << abs_view_pos[2] << endl;
718 // cout << "starting ssg_los, view pos = " << view_pos[0] << " "
719 // << view_pos[1] << " " << view_pos[2] << endl;
720 my_ssg_los( "", scene, m, sgvp, sgavp );
722 double result = -9999;
724 for ( int i = 0; i < hitcount; ++i ) {
725 Point3D rel_cart( hit_pts[i][0], hit_pts[i][1], hit_pts[i][2] );
726 Point3D abs_cart = rel_cart + scenery.center;
727 Point3D pp = fgCartToPolar3d( abs_cart );
728 FG_LOG( FG_TERRAIN, FG_DEBUG, " polar form = " << pp );
729 // convert to geodetic coordinates
730 double lat_geod, alt, sea_level_r;
731 fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod,
734 // printf("alt = %.2f\n", alt);
735 // exit since we found an intersection
736 if ( alt > result && alt < 10000 ) {
737 // printf("returning alt\n");
742 if ( result > -9000 ) {
745 FG_LOG( FG_TERRAIN, FG_INFO, "no terrain intersection" );
751 // given the current lon/lat, fill in the array of local chunks. If
752 // the chunk isn't already in the cache, then read it from disk.
753 int FGTileMgr::update( void ) {
758 static FGBucket p_last(false);
759 static double last_lon = -1000.0; // in degrees
760 static double last_lat = -1000.0; // in degrees
764 c = &global_tile_cache;
765 f = current_aircraft.fdm_state;
767 tile_diameter = current_options.get_tile_diameter();
769 FGBucket p1( f->get_Longitude() * RAD_TO_DEG,
770 f->get_Latitude() * RAD_TO_DEG );
772 long int index = c->exists(p1);
774 t = c->get_tile(index);
775 scenery.next_center = t->center;
777 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
780 dw = tile_diameter / 2;
781 dh = tile_diameter / 2;
783 if ( (p1 == p_last) && (state == Running) ) {
784 // same bucket as last time
785 FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
786 } else if ( (state == Start) || (state == Inited) ) {
789 // First time through or we have teleported, initialize the
790 // system and load all relavant tiles
792 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
793 FG_LOG( FG_TERRAIN, FG_INFO, " First time through ... " );
794 FG_LOG( FG_TERRAIN, FG_INFO, " Updating Tile list for " << p1 );
795 FG_LOG( FG_TERRAIN, FG_INFO, " Loading "
796 << tile_diameter * tile_diameter << " tiles" );
798 // wipe/initialize tile cache
802 // build the local area list and schedule tiles for loading
804 // start with the center tile and work out in concentric
807 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
808 f->get_Latitude() * RAD_TO_DEG,
812 // prime scenery center calculations
813 Point3D geod_view_center( p2.get_center_lon(),
815 cur_fdm_state->get_Altitude()*FEET_TO_METER +
817 current_view.abs_view_pos = fgGeodToCart( geod_view_center );
818 current_view.view_pos = current_view.abs_view_pos - scenery.next_center;
820 for ( i = 3; i <= tile_diameter; i = i + 2 ) {
824 for ( j = -span; j <= span; ++j ) {
825 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
826 f->get_Latitude() * RAD_TO_DEG,
832 for ( j = -span; j <= span; ++j ) {
833 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
834 f->get_Latitude() * RAD_TO_DEG,
840 for ( j = -span + 1; j <= span - 1; ++j ) {
841 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
842 f->get_Latitude() * RAD_TO_DEG,
845 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
846 f->get_Latitude() * RAD_TO_DEG,
853 /* for ( j = 0; j < tile_diameter; j++ ) {
854 for ( i = 0; i < tile_diameter; i++ ) {
855 // fgBucketOffset(&p1, &p2, i - dw, j - dh);
856 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
857 f->get_Latitude() * RAD_TO_DEG,
863 // Now force a load of the center tile and inner ring so we
864 // have something to see in our first frame.
865 for ( i = 0; i < 9; ++i ) {
866 if ( load_queue.size() ) {
867 FG_LOG( FG_TERRAIN, FG_DEBUG,
868 "Load queue not empty, loading a tile" );
870 FGLoadRec pending = load_queue.front();
871 load_queue.pop_front();
872 load_tile( pending.b, pending.cache_index );
877 // We've moved to a new bucket, we need to scroll our
878 // structures, and load in the new tiles
881 // make sure load queue is flushed before doing shift
882 while ( load_queue.size() ) {
883 FG_LOG( FG_TERRAIN, FG_DEBUG,
884 "Load queue not empty, flushing queue before tile shift." );
886 FGLoadRec pending = load_queue.front();
887 load_queue.pop_front();
888 load_tile( pending.b, pending.index );
892 // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
893 // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
894 // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
896 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
898 if ( (p1.get_lon() > p_last.get_lon()) ||
899 ( (p1.get_lon() == p_last.get_lon()) &&
900 (p1.get_x() > p_last.get_x()) ) ) {
901 FG_LOG( FG_TERRAIN, FG_INFO,
902 " (East) Loading " << tile_diameter << " tiles" );
903 for ( j = 0; j < tile_diameter; j++ ) {
905 // schedule new column
906 p2 = fgBucketOffset( last_lon, last_lat, dw + 1, j - dh );
909 } else if ( (p1.get_lon() < p_last.get_lon()) ||
910 ( (p1.get_lon() == p_last.get_lon()) &&
911 (p1.get_x() < p_last.get_x()) ) ) {
912 FG_LOG( FG_TERRAIN, FG_INFO,
913 " (West) Loading " << tile_diameter << " tiles" );
914 for ( j = 0; j < tile_diameter; j++ ) {
916 // schedule new column
917 p2 = fgBucketOffset( last_lon, last_lat, -dw - 1, j - dh );
922 if ( (p1.get_lat() > p_last.get_lat()) ||
923 ( (p1.get_lat() == p_last.get_lat()) &&
924 (p1.get_y() > p_last.get_y()) ) ) {
925 FG_LOG( FG_TERRAIN, FG_INFO,
926 " (North) Loading " << tile_diameter << " tiles" );
927 for ( i = 0; i < tile_diameter; i++ ) {
930 p2 = fgBucketOffset( last_lon, last_lat, i - dw, dh + 1);
933 } else if ( (p1.get_lat() < p_last.get_lat()) ||
934 ( (p1.get_lat() == p_last.get_lat()) &&
935 (p1.get_y() < p_last.get_y()) ) ) {
936 FG_LOG( FG_TERRAIN, FG_INFO,
937 " (South) Loading " << tile_diameter << " tiles" );
938 for ( i = 0; i < tile_diameter; i++ ) {
941 p2 = fgBucketOffset( last_lon, last_lat, i - dw, -dh - 1);
947 if ( load_queue.size() ) {
948 FG_LOG( FG_TERRAIN, FG_DEBUG, "Load queue not empty, loading a tile" );
950 FGLoadRec pending = load_queue.front();
951 load_queue.pop_front();
952 load_tile( pending.b, pending.cache_index );
955 // find our current elevation (feed in the current bucket to save work)
956 Point3D geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
957 Point3D tmp_abs_view_pos = fgGeodToCart(geod_pos);
959 // cout << "current elevation (old) == "
960 // << current_elev( f->get_Longitude(), f->get_Latitude(),
961 // tmp_abs_view_pos )
963 scenery.cur_elev = current_elev_ssg( current_view.abs_view_pos,
964 current_view.view_pos );
965 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
968 last_lon = f->get_Longitude() * RAD_TO_DEG;
969 last_lat = f->get_Latitude() * RAD_TO_DEG;
977 // inrange() IS THIS POINT WITHIN POSSIBLE VIEWING RANGE ?
978 // calculate distance from vertical tangent line at
979 // current position to center of object.
980 // this is equivalent to
981 // dist = point_line_dist_squared( &(t->center), &(v->abs_view_pos),
983 // if ( dist < FG_SQUARE(t->bounding_radius) ) {
985 // the compiler should inline this for us
988 inrange( const double radius, const Point3D& center, const Point3D& vp,
995 u[0] = center.x() - vp.x();
996 u[1] = center.y() - vp.y();
997 u[2] = center.z() - vp.z();
999 // calculate the projection, u1, of u along d.
1000 // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
1002 MAT3_SCALE_VEC(u1, up,
1003 (MAT3_DOT_PRODUCT(u, up) / MAT3_DOT_PRODUCT(up, up)) );
1005 // v = u - u1 = vector from closest point on line, p1, to the
1006 // original point, p.
1007 MAT3_SUB_VEC(v, u, u1);
1009 return( FG_SQUARE(radius) >= MAT3_DOT_PRODUCT(v, v));
1013 // NEW for legibility
1015 // update this tile's geometry for current view
1016 // The Compiler should inline this
1018 update_tile_geometry( FGTileEntry *t, GLdouble *MODEL_VIEW)
1023 // calculate tile offset
1024 t->offset = t->center - scenery.center;
1032 // Calculate the model_view transformation matrix for this tile
1033 FG_MEM_COPY( m, MODEL_VIEW, 16*sizeof(GLdouble) );
1035 // This is equivalent to doing a glTranslatef(x, y, z);
1036 m[12] += (m[0]*x + m[4]*y + m[8] *z);
1037 m[13] += (m[1]*x + m[5]*y + m[9] *z);
1038 m[14] += (m[2]*x + m[6]*y + m[10]*z);
1039 // m[15] += (m[3]*x + m[7]*y + m[11]*z);
1040 // m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
1041 // so m[15] is unchanged
1045 // Prepare the ssg nodes ... for each tile, set it's proper
1046 // transform and update it's range selector based on current
1048 void FGTileMgr::prep_ssg_nodes( void ) {
1054 // traverse the potentially viewable tile list and update range
1055 // selector and transform
1056 for ( int i = 0; i < (int)global_tile_cache.get_size(); i++ ) {
1057 t = global_tile_cache.get_tile( i );
1059 if ( t->is_loaded() ) {
1060 // set range selector (LOD trick) to be distance to center
1061 // of tile + bounding radius
1062 #ifndef FG_OLD_WEATHER
1063 ranges[1] = WeatherDatabase->getWeatherVisibility()
1064 + t->bounding_radius;
1066 ranges[1] = current_weather.get_visibility()+t->bounding_radius;
1068 t->range_ptr->setRanges( ranges, 2 );
1070 // calculate tile offset
1071 t->SetOffset( scenery.center );
1073 // calculate ssg transform
1075 sgSetCoord( &sgcoord,
1076 t->offset.x(), t->offset.y(), t->offset.z(),
1078 t->transform_ptr->setTransform( &sgcoord );