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.
33 #include <simgear/xgl/xgl.h>
35 #include <simgear/constants.h>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/math/fg_geodesy.hxx>
38 #include <simgear/math/mat3.h>
39 #include <simgear/math/point3d.hxx>
40 #include <simgear/math/polar3d.hxx>
41 #include <simgear/math/vector.hxx>
43 #include <Aircraft/aircraft.hxx>
44 #include <Main/options.hxx>
45 #include <Main/views.hxx>
46 #include <Objects/materialmgr.hxx>
47 #include <Objects/obj.hxx>
49 #ifndef FG_OLD_WEATHER
50 # include <WeatherCM/FGLocalWeatherDatabase.h>
52 # include <Weather/weather.hxx>
55 #include "scenery.hxx"
56 #include "tilecache.hxx"
57 #include "tileentry.hxx"
58 #include "tilemgr.hxx"
61 // to test clipping speedup in fgTileMgrRender()
62 #if defined ( USE_FAST_FOV_CLIP )
63 // #define TEST_FOV_CLIP
68 extern ssgRoot *scene;
72 FGTileMgr global_tile_mgr;
76 FGTileMgr::FGTileMgr ( void ):
83 FGTileMgr::~FGTileMgr ( void ) {
87 // Initialize the Tile Manager subsystem
88 int FGTileMgr::init( void ) {
89 FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
91 // load default material library
92 if ( ! material_mgr.loaded() ) {
93 material_mgr.load_lib();
96 global_tile_cache.init();
104 // schedule a tile for loading
105 static void disable_tile( int cache_index ) {
106 // see if tile already exists in the cache
107 // cout << "DISABLING CACHE ENTRY = " << cache_index << endl;
108 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
113 // schedule a tile for loading
114 int FGTileMgr::sched_tile( const FGBucket& b ) {
115 // see if tile already exists in the cache
116 int cache_index = global_tile_cache.exists( b );
118 if ( cache_index >= 0 ) {
119 // tile exists in cache, reenable it.
120 // cout << "REENABLING DISABLED TILE" << endl;
121 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
122 t->select_ptr->select( 1 );
125 // find the next available cache entry and mark it as
127 cache_index = global_tile_cache.next_avail();
128 FGTileEntry *t = global_tile_cache.get_tile( cache_index );
129 t->mark_scheduled_for_use();
131 // register a load request
134 request.cache_index = cache_index;
135 load_queue.push_back( request );
143 void FGTileMgr::load_tile( const FGBucket& b, int cache_index) {
145 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loading tile " << b );
147 global_tile_cache.fill_in(cache_index, b);
149 FG_LOG( FG_TERRAIN, FG_DEBUG, "Loaded for cache index: " << cache_index );
153 // Calculate shortest distance from point to line
154 static double point_line_dist_squared( const Point3D& tc, const Point3D& vp,
159 p[0] = tc.x(); p[1] = tc.y(); p[2] = tc.z();
160 p0[0] = vp.x(); p0[1] = vp.y(); p0[2] = vp.z();
162 return fgPointLineSquared(p, p0, d);
166 // Determine scenery altitude. Normally this just happens when we
167 // render the scene, but we'd also like to be able to do this
168 // explicitely. lat & lon are in radians. abs_view_pos in meters.
169 // Returns result in meters.
171 FGTileMgr::current_elev_new( const FGBucket& p ) {
173 fgFRAGMENT *frag_ptr;
174 Point3D abs_view_pos = current_view.get_abs_view_pos();
175 Point3D earth_center(0.0);
178 double dist, lat_geod, alt, sea_level_r;
181 local_up[0] = abs_view_pos.x();
182 local_up[1] = abs_view_pos.y();
183 local_up[2] = abs_view_pos.z();
185 // Find current translation offset
186 // fgBucketFind(lon * RAD_TO_DEG, lat * RAD_TO_DEG, &p);
187 index = global_tile_cache.exists(p);
189 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
193 t = global_tile_cache.get_tile(index);
195 scenery.next_center = t->center;
197 FG_LOG( FG_TERRAIN, FG_DEBUG,
198 "Current bucket = " << p << " Index = " << p.gen_index_str() );
199 FG_LOG( FG_TERRAIN, FG_DEBUG,
200 "abs_view_pos = " << abs_view_pos );
202 // calculate tile offset
203 // x = (t->offset.x = t->center.x - scenery.center.x);
204 // y = (t->offset.y = t->center.y - scenery.center.y);
205 // z = (t->offset.z = t->center.z - scenery.center.z);
207 // calc current terrain elevation calculate distance from
208 // vertical tangent line at current position to center of
211 /* printf("distance squared = %.2f, bounding radius = %.2f\n",
212 point_line_dist_squared(&(t->offset), &(v->view_pos),
213 v->local_up), t->bounding_radius); */
215 dist = point_line_dist_squared( t->center, abs_view_pos, local_up );
216 if ( dist < FG_SQUARE(t->bounding_radius) ) {
218 // traverse fragment list for tile
219 FGTileEntry::FragmentIterator current = t->begin();
220 FGTileEntry::FragmentIterator last = t->end();
222 for ( ; current != last; ++current ) {
223 frag_ptr = &(*current);
224 /* printf("distance squared = %.2f, bounding radius = %.2f\n",
225 point_line_dist_squared( &(frag_ptr->center),
226 &abs_view_pos), local_up),
227 frag_ptr->bounding_radius); */
229 dist = point_line_dist_squared( frag_ptr->center,
232 if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
233 if ( frag_ptr->intersect( abs_view_pos,
234 earth_center, 0, result ) ) {
235 FG_LOG( FG_TERRAIN, FG_DEBUG, "intersection point " <<
237 // compute geocentric coordinates of tile center
238 Point3D pp = fgCartToPolar3d(result);
239 FG_LOG( FG_TERRAIN, FG_DEBUG, " polar form = " << pp );
240 // convert to geodetic coordinates
241 fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod,
244 // printf("alt = %.2f\n", alt);
245 // exit since we found an intersection
246 if ( alt > -9999.0 ) {
247 // printf("returning alt\n");
250 // printf("returning 0\n");
258 FG_LOG( FG_TERRAIN, FG_INFO, "(new) no terrain intersection found" );
264 // Determine scenery altitude. Normally this just happens when we
265 // render the scene, but we'd also like to be able to do this
266 // explicitely. lat & lon are in radians. abs_view_pos in meters.
267 // Returns result in meters.
269 FGTileMgr::current_elev( double lon, double lat, const Point3D& abs_view_pos ) {
272 fgFRAGMENT *frag_ptr;
273 Point3D earth_center(0.0);
276 double dist, lat_geod, alt, sea_level_r;
279 c = &global_tile_cache;
281 local_up[0] = abs_view_pos.x();
282 local_up[1] = abs_view_pos.y();
283 local_up[2] = abs_view_pos.z();
285 FG_LOG( FG_TERRAIN, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
287 // Find current translation offset
288 FGBucket p( lon * RAD_TO_DEG, lat * RAD_TO_DEG );
289 index = c->exists(p);
291 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
295 t = c->get_tile(index);
297 scenery.next_center = t->center;
299 FG_LOG( FG_TERRAIN, FG_DEBUG,
300 "Pos = (" << lon * RAD_TO_DEG << ", " << lat * RAD_TO_DEG
301 << ") Current bucket = " << p
302 << " Index = " << p.gen_index_str() );
304 FG_LOG( FG_TERRAIN, FG_DEBUG, "Tile center " << t->center
305 << " bounding radius = " << t->bounding_radius );
307 // calculate tile offset
308 // x = (t->offset.x = t->center.x - scenery.center.x);
309 // y = (t->offset.y = t->center.y - scenery.center.y);
310 // z = (t->offset.z = t->center.z - scenery.center.z);
312 // calc current terrain elevation calculate distance from
313 // vertical tangent line at current position to center of
316 /* printf("distance squared = %.2f, bounding radius = %.2f\n",
317 point_line_dist_squared(&(t->offset), &(v->view_pos),
318 v->local_up), t->bounding_radius); */
320 dist = point_line_dist_squared( t->center, abs_view_pos, local_up );
321 FG_LOG( FG_TERRAIN, FG_DEBUG, "(gross check) dist squared = " << dist );
323 if ( dist < FG_SQUARE(t->bounding_radius) ) {
325 // traverse fragment list for tile
326 FGTileEntry::FragmentIterator current = t->begin();
327 FGTileEntry::FragmentIterator last = t->end();
329 for ( ; current != last; ++current ) {
330 frag_ptr = &(*current);
331 /* printf("distance squared = %.2f, bounding radius = %.2f\n",
332 point_line_dist_squared( &(frag_ptr->center),
333 &abs_view_pos), local_up),
334 frag_ptr->bounding_radius); */
336 dist = point_line_dist_squared( frag_ptr->center,
339 if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
340 if ( frag_ptr->intersect( abs_view_pos,
341 earth_center, 0, result ) ) {
342 FG_LOG( FG_TERRAIN, FG_DEBUG, "intersection point " <<
344 // compute geocentric coordinates of tile center
345 Point3D pp = fgCartToPolar3d(result);
346 FG_LOG( FG_TERRAIN, FG_DEBUG, " polar form = " << pp );
347 // convert to geodetic coordinates
348 fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod,
351 // printf("alt = %.2f\n", alt);
352 // exit since we found an intersection
353 if ( alt > -9999.0 ) {
354 // printf("returning alt\n");
357 // printf("returning 0\n");
365 FG_LOG( FG_TERRAIN, FG_INFO, "(old) no terrain intersection found" );
371 inline int fg_sign( const double x ) {
372 return x < 0 ? -1 : 1;
375 inline double fg_min( const double a, const double b ) {
376 return b < a ? b : a;
379 inline double fg_max( const double a, const double b ) {
380 return a < b ? b : a;
383 // return the minimum of the three values
384 inline double fg_min3( const double a, const double b, const double c ) {
385 return a > b ? fg_min(b, c) : fg_min(a, c);
388 // return the maximum of the three values
389 inline double fg_max3 (const double a, const double b, const double c ) {
390 return a < b ? fg_max(b, c) : fg_max(a, c);
393 // check for an instersection with the individual triangles of a leaf
394 static bool my_ssg_instersect_leaf( string s, ssgLeaf *leaf, sgdMat4 m,
395 const sgdVec3 p, const sgdVec3 dir,
400 double x, y, z; // temporary holding spot for result
402 double x0, y0, z0, x1, y1, z1, a1, b1, c1;
404 double xmin, xmax, ymin, ymax, zmin, zmax;
405 double dx, dy, dz, min_dim, x2, y2, x3, y3, rx, ry;
411 // cout << s << "Intersecting" << endl;
413 // traverse the triangle list for this leaf
414 for ( int i = 0; i < leaf->getNumTriangles(); ++i ) {
415 // cout << s << "testing triangle = " << i << endl;
417 leaf->getTriangle( i, &i1, &i2, &i3 );
419 // get triangle vertex coordinates
421 ftmp = leaf->getVertex( i1 );
422 sgdSetVec3( tmp, ftmp );
423 // cout << s << "orig point 1 = " << tmp[0] << " " << tmp[1]
424 // << " " << tmp[2] << endl;
425 sgdXformPnt3( p1, tmp, m ) ;
427 ftmp = leaf->getVertex( i2 );
428 sgdSetVec3( tmp, ftmp );
429 // cout << s << "orig point 2 = " << tmp[0] << " " << tmp[1]
430 // << " " << tmp[2] << endl;
431 sgdXformPnt3( p2, tmp, m ) ;
433 ftmp = leaf->getVertex( i3 );
434 sgdSetVec3( tmp, ftmp );
435 // cout << s << "orig point 3 = " << tmp[0] << " " << tmp[1]
436 // << " " << tmp[2] << endl;
437 sgdXformPnt3( p3, tmp, m ) ;
439 // cout << s << "point 1 = " << p1[0] << " " << p1[1] << " " << p1[2]
441 // cout << s << "point 2 = " << p2[0] << " " << p2[1] << " " << p2[2]
443 // cout << s << "point 3 = " << p3[0] << " " << p3[1] << " " << p3[2]
446 // calculate two edge vectors, and the face normal
447 sgdSubVec3(v1, p2, p1);
448 sgdSubVec3(v2, p3, p1);
449 sgdVectorProductVec3(n, v1, v2);
451 // calculate the plane coefficients for the plane defined by
452 // this face. If n is the normal vector, n = (a, b, c) and p1
453 // is a point on the plane, p1 = (x0, y0, z0), then the
454 // equation of the line is a(x-x0) + b(y-y0) + c(z-z0) = 0
458 d = a * p1[0] + b * p1[1] + c * p1[2];
459 // printf("a, b, c, d = %.2f %.2f %.2f %.2f\n", a, b, c, d);
461 // printf("p1(d) = %.2f\n", a * p1[0] + b * p1[1] + c * p1[2]);
462 // printf("p2(d) = %.2f\n", a * p2[0] + b * p2[1] + c * p2[2]);
463 // printf("p3(d) = %.2f\n", a * p3[0] + b * p3[1] + c * p3[2]);
465 // calculate the line coefficients for the specified line
466 x0 = p[0]; x1 = p[0] + dir[0];
467 y0 = p[1]; y1 = p[1] + dir[1];
468 z0 = p[2]; z1 = p[2] + dir[2];
470 if ( fabs(x1 - x0) > FG_EPSILON ) {
471 a1 = 1.0 / (x1 - x0);
473 // we got a big divide by zero problem here
479 // intersect the specified line with this plane
483 // printf("a = %.2f t1 = %.2f t2 = %.2f\n", a, t1, t2);
485 if ( fabs(a + t1 + t2) > FG_EPSILON ) {
486 x = (t1*x0 - b*y0 + t2*x0 - c*z0 + d) / (a + t1 + t2);
490 // printf("result(d) = %.2f\n", a * x + b * y + c * z);
492 // no intersection point
498 // check to see if end0 and end1 are on opposite sides of
500 if ( (x - x0) > FG_EPSILON ) {
504 } else if ( (y - y0) > FG_EPSILON ) {
508 } else if ( (z - z0) > FG_EPSILON ) {
513 // everything is too close together to tell the difference
514 // so the current intersection point should work as good
516 sgdSetVec3( result, x, y, z );
519 side1 = fg_sign (t1 - t2);
520 side2 = fg_sign (t1 - t3);
521 if ( side1 == side2 ) {
528 // check to see if intersection point is in the bounding
530 #ifdef XTRA_DEBUG_STUFF
531 xmin = fg_min3 (p1[0], p2[0], p3[0]);
532 xmax = fg_max3 (p1[0], p2[0], p3[0]);
533 ymin = fg_min3 (p1[1], p2[1], p3[1]);
534 ymax = fg_max3 (p1[1], p2[1], p3[1]);
535 zmin = fg_min3 (p1[2], p2[2], p3[2]);
536 zmax = fg_max3 (p1[2], p2[2], p3[2]);
537 printf("bounding cube = %.2f,%.2f,%.2f %.2f,%.2f,%.2f\n",
538 xmin, ymin, zmin, xmax, ymax, zmax);
540 // punt if outside bouding cube
541 if ( x < (xmin = fg_min3 (p1[0], p2[0], p3[0])) ) {
543 } else if ( x > (xmax = fg_max3 (p1[0], p2[0], p3[0])) ) {
545 } else if ( y < (ymin = fg_min3 (p1[1], p2[1], p3[1])) ) {
547 } else if ( y > (ymax = fg_max3 (p1[1], p2[1], p3[1])) ) {
549 } else if ( z < (zmin = fg_min3 (p1[2], p2[2], p3[2])) ) {
551 } else if ( z > (zmax = fg_max3 (p1[2], p2[2], p3[2])) ) {
555 // (finally) check to see if the intersection point is
556 // actually inside this face
558 //first, drop the smallest dimension so we only have to work
563 min_dim = fg_min3 (dx, dy, dz);
564 if ( fabs(min_dim - dx) <= FG_EPSILON ) {
565 // x is the smallest dimension
574 } else if ( fabs(min_dim - dy) <= FG_EPSILON ) {
575 // y is the smallest dimension
584 } else if ( fabs(min_dim - dz) <= FG_EPSILON ) {
585 // z is the smallest dimension
595 // all dimensions are really small so lets call it close
596 // enough and return a successful match
597 sgdSetVec3( result, x, y, z );
601 // check if intersection point is on the same side of p1 <-> p2 as p3
602 t1 = (y1 - y2) / (x1 - x2);
603 side1 = fg_sign (t1 * ((x3) - x2) + y2 - (y3));
604 side2 = fg_sign (t1 * ((rx) - x2) + y2 - (ry));
605 if ( side1 != side2 ) {
606 // printf("failed side 1 check\n");
610 // check if intersection point is on correct side of p2 <-> p3 as p1
611 t1 = (y2 - y3) / (x2 - x3);
612 side1 = fg_sign (t1 * ((x1) - x3) + y3 - (y1));
613 side2 = fg_sign (t1 * ((rx) - x3) + y3 - (ry));
614 if ( side1 != side2 ) {
615 // printf("failed side 2 check\n");
619 // check if intersection point is on correct side of p1 <-> p3 as p2
620 t1 = (y1 - y3) / (x1 - x3);
621 side1 = fg_sign (t1 * ((x2) - x3) + y3 - (y2));
622 side2 = fg_sign (t1 * ((rx) - x3) + y3 - (ry));
623 if ( side1 != side2 ) {
624 // printf("failed side 3 check\n");
628 // printf( "intersection point = %.2f %.2f %.2f\n", x, y, z);
629 sgdSetVec3( result, x, y, z );
639 void FGTileMgr::my_ssg_los( string s, ssgBranch *branch, sgdMat4 m,
640 const sgdVec3 p, const sgdVec3 dir )
643 for ( ssgEntity *kid = branch->getKid( 0 );
645 kid = branch->getNextKid() )
647 if ( kid->getTraversalMask() & SSGTRAV_HOT ) {
648 bsphere = kid->getBSphere();
650 sgCopyVec3( fcenter, bsphere->getCenter() );
652 center[0] = fcenter[0];
653 center[1] = fcenter[1];
654 center[2] = fcenter[2];
655 sgdXformPnt3( center, m ) ;
656 // cout << s << "entity bounding sphere:" << endl;
657 // cout << s << "center = " << center[0] << " "
658 // << center[1] << " " << center[2] << endl;
659 // cout << s << "radius = " << bsphere->getRadius() << endl;
660 double radius_sqd = bsphere->getRadius() * bsphere->getRadius();
661 if ( sgdPointLineDistSquared( center, p, dir ) < radius_sqd ) {
662 // possible intersections
663 if ( kid->isAKindOf ( ssgTypeBranch() ) ) {
665 sgdCopyMat4(m_new, m);
666 if ( kid->isA( ssgTypeTransform() ) ) {
668 ((ssgTransform *)kid)->getTransform( fxform );
670 sgdSetMat4( xform, fxform );
671 sgdPreMultMat4( m_new, xform );
673 my_ssg_los( s + " ", (ssgBranch *)kid, m_new, p, dir );
674 } else if ( kid->isAKindOf ( ssgTypeLeaf() ) ) {
676 if ( my_ssg_instersect_leaf( s, (ssgLeaf *)kid, m, p, dir,
679 // cout << "sgLOS hit: " << result[0] << ","
680 // << result[1] << "," << result[2] << endl;
681 for (int i=0; i < 3; i++) {
682 hit_pts[hitcount][i] = result[i];
688 // end of the line for this branch
691 // branch requested not to be traversed
697 // Determine scenery altitude via ssg. Normally this just happens
698 // when we render the scene, but we'd also like to be able to do this
699 // explicitely. lat & lon are in radians. view_pos in current world
700 // coordinate translated near (0,0,0) (in meters.) Returns result in
703 FGTileMgr::current_elev_ssg( const Point3D& abs_view_pos,
704 const Point3D& view_pos )
709 sgdMakeIdentMat4 ( m ) ;
712 sgdSetVec3(sgavp, abs_view_pos.x(), abs_view_pos.y(), abs_view_pos.z() );
713 sgdSetVec3(sgvp, view_pos.x(), view_pos.y(), view_pos.z() );
715 // cout << "starting ssg_los, abs view pos = " << abs_view_pos[0] << " "
716 // << abs_view_pos[1] << " " << abs_view_pos[2] << endl;
717 // cout << "starting ssg_los, view pos = " << view_pos[0] << " "
718 // << view_pos[1] << " " << view_pos[2] << endl;
719 my_ssg_los( "", scene, m, sgvp, sgavp );
721 double result = -9999;
723 for ( int i = 0; i < hitcount; ++i ) {
724 Point3D rel_cart( hit_pts[i][0], hit_pts[i][1], hit_pts[i][2] );
725 Point3D abs_cart = rel_cart + scenery.center;
726 Point3D pp = fgCartToPolar3d( abs_cart );
727 FG_LOG( FG_TERRAIN, FG_DEBUG, " polar form = " << pp );
728 // convert to geodetic coordinates
729 double lat_geod, alt, sea_level_r;
730 fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod,
733 // printf("alt = %.2f\n", alt);
734 // exit since we found an intersection
735 if ( alt > result && alt < 10000 ) {
736 // printf("returning alt\n");
741 if ( result > -9000 ) {
744 FG_LOG( FG_TERRAIN, FG_INFO, "no terrain intersection" );
750 // given the current lon/lat, fill in the array of local chunks. If
751 // the chunk isn't already in the cache, then read it from disk.
752 int FGTileMgr::update( void ) {
757 static FGBucket p_last(false);
758 static double last_lon = -1000.0; // in degrees
759 static double last_lat = -1000.0; // in degrees
763 c = &global_tile_cache;
764 f = current_aircraft.fdm_state;
766 tile_diameter = current_options.get_tile_diameter();
768 FGBucket p1( f->get_Longitude() * RAD_TO_DEG,
769 f->get_Latitude() * RAD_TO_DEG );
771 long int index = c->exists(p1);
773 t = c->get_tile(index);
774 scenery.next_center = t->center;
776 FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
779 dw = tile_diameter / 2;
780 dh = tile_diameter / 2;
782 if ( (p1 == p_last) && (state == Running) ) {
783 // same bucket as last time
784 FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
785 } else if ( (state == Start) || (state == Inited) ) {
788 // First time through or we have teleported, initialize the
789 // system and load all relavant tiles
791 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
792 FG_LOG( FG_TERRAIN, FG_INFO, " First time through ... " );
793 FG_LOG( FG_TERRAIN, FG_INFO, " Updating Tile list for " << p1 );
794 FG_LOG( FG_TERRAIN, FG_INFO, " Loading "
795 << tile_diameter * tile_diameter << " tiles" );
797 // wipe/initialize tile cache
801 // build the local area list and schedule tiles for loading
803 // start with the center tile and work out in concentric
806 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
807 f->get_Latitude() * RAD_TO_DEG,
811 // prime scenery center calculations
812 Point3D geod_view_center( p2.get_center_lon(),
814 cur_fdm_state->get_Altitude()*FEET_TO_METER +
816 current_view.abs_view_pos = fgGeodToCart( geod_view_center );
817 current_view.view_pos = current_view.abs_view_pos - scenery.next_center;
819 for ( i = 3; i <= tile_diameter; i = i + 2 ) {
823 for ( j = -span; j <= span; ++j ) {
824 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
825 f->get_Latitude() * RAD_TO_DEG,
831 for ( j = -span; j <= span; ++j ) {
832 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
833 f->get_Latitude() * RAD_TO_DEG,
839 for ( j = -span + 1; j <= span - 1; ++j ) {
840 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
841 f->get_Latitude() * RAD_TO_DEG,
844 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
845 f->get_Latitude() * RAD_TO_DEG,
852 /* for ( j = 0; j < tile_diameter; j++ ) {
853 for ( i = 0; i < tile_diameter; i++ ) {
854 // fgBucketOffset(&p1, &p2, i - dw, j - dh);
855 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
856 f->get_Latitude() * RAD_TO_DEG,
862 // Now force a load of the center tile and inner ring so we
863 // have something to see in our first frame.
864 for ( i = 0; i < 9; ++i ) {
865 if ( load_queue.size() ) {
866 FG_LOG( FG_TERRAIN, FG_DEBUG,
867 "Load queue not empty, loading a tile" );
869 FGLoadRec pending = load_queue.front();
870 load_queue.pop_front();
871 load_tile( pending.b, pending.cache_index );
876 // We've moved to a new bucket, we need to scroll our
877 // structures, and load in the new tiles
880 // make sure load queue is flushed before doing shift
881 while ( load_queue.size() ) {
882 FG_LOG( FG_TERRAIN, FG_DEBUG,
883 "Load queue not empty, flushing queue before tile shift." );
885 FGLoadRec pending = load_queue.front();
886 load_queue.pop_front();
887 load_tile( pending.b, pending.index );
891 // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
892 // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
893 // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
895 FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
897 if ( (p1.get_lon() > p_last.get_lon()) ||
898 ( (p1.get_lon() == p_last.get_lon()) &&
899 (p1.get_x() > p_last.get_x()) ) ) {
900 FG_LOG( FG_TERRAIN, FG_INFO,
901 " (East) Loading " << tile_diameter << " tiles" );
902 for ( j = 0; j < tile_diameter; j++ ) {
904 // schedule new column
905 p2 = fgBucketOffset( last_lon, last_lat, dw + 1, j - dh );
908 } else if ( (p1.get_lon() < p_last.get_lon()) ||
909 ( (p1.get_lon() == p_last.get_lon()) &&
910 (p1.get_x() < p_last.get_x()) ) ) {
911 FG_LOG( FG_TERRAIN, FG_INFO,
912 " (West) Loading " << tile_diameter << " tiles" );
913 for ( j = 0; j < tile_diameter; j++ ) {
915 // schedule new column
916 p2 = fgBucketOffset( last_lon, last_lat, -dw - 1, j - dh );
921 if ( (p1.get_lat() > p_last.get_lat()) ||
922 ( (p1.get_lat() == p_last.get_lat()) &&
923 (p1.get_y() > p_last.get_y()) ) ) {
924 FG_LOG( FG_TERRAIN, FG_INFO,
925 " (North) Loading " << tile_diameter << " tiles" );
926 for ( i = 0; i < tile_diameter; i++ ) {
929 p2 = fgBucketOffset( last_lon, last_lat, i - dw, dh + 1);
932 } else if ( (p1.get_lat() < p_last.get_lat()) ||
933 ( (p1.get_lat() == p_last.get_lat()) &&
934 (p1.get_y() < p_last.get_y()) ) ) {
935 FG_LOG( FG_TERRAIN, FG_INFO,
936 " (South) Loading " << tile_diameter << " tiles" );
937 for ( i = 0; i < tile_diameter; i++ ) {
940 p2 = fgBucketOffset( last_lon, last_lat, i - dw, -dh - 1);
946 if ( load_queue.size() ) {
947 FG_LOG( FG_TERRAIN, FG_DEBUG, "Load queue not empty, loading a tile" );
949 FGLoadRec pending = load_queue.front();
950 load_queue.pop_front();
951 load_tile( pending.b, pending.cache_index );
954 // find our current elevation (feed in the current bucket to save work)
955 Point3D geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
956 Point3D tmp_abs_view_pos = fgGeodToCart(geod_pos);
958 // cout << "current elevation (old) == "
959 // << current_elev( f->get_Longitude(), f->get_Latitude(),
960 // tmp_abs_view_pos )
962 scenery.cur_elev = current_elev_ssg( current_view.abs_view_pos,
963 current_view.view_pos );
964 // cout << "current elevation (ssg) == " << scenery.cur_elev << endl;
967 last_lon = f->get_Longitude() * RAD_TO_DEG;
968 last_lat = f->get_Latitude() * RAD_TO_DEG;
976 // inrange() IS THIS POINT WITHIN POSSIBLE VIEWING RANGE ?
977 // calculate distance from vertical tangent line at
978 // current position to center of object.
979 // this is equivalent to
980 // dist = point_line_dist_squared( &(t->center), &(v->abs_view_pos),
982 // if ( dist < FG_SQUARE(t->bounding_radius) ) {
984 // the compiler should inline this for us
987 inrange( const double radius, const Point3D& center, const Point3D& vp,
994 u[0] = center.x() - vp.x();
995 u[1] = center.y() - vp.y();
996 u[2] = center.z() - vp.z();
998 // calculate the projection, u1, of u along d.
999 // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
1001 MAT3_SCALE_VEC(u1, up,
1002 (MAT3_DOT_PRODUCT(u, up) / MAT3_DOT_PRODUCT(up, up)) );
1004 // v = u - u1 = vector from closest point on line, p1, to the
1005 // original point, p.
1006 MAT3_SUB_VEC(v, u, u1);
1008 return( FG_SQUARE(radius) >= MAT3_DOT_PRODUCT(v, v));
1012 // NEW for legibility
1014 // update this tile's geometry for current view
1015 // The Compiler should inline this
1017 update_tile_geometry( FGTileEntry *t, GLdouble *MODEL_VIEW)
1022 // calculate tile offset
1023 t->offset = t->center - scenery.center;
1031 // Calculate the model_view transformation matrix for this tile
1032 FG_MEM_COPY( m, MODEL_VIEW, 16*sizeof(GLdouble) );
1034 // This is equivalent to doing a glTranslatef(x, y, z);
1035 m[12] += (m[0]*x + m[4]*y + m[8] *z);
1036 m[13] += (m[1]*x + m[5]*y + m[9] *z);
1037 m[14] += (m[2]*x + m[6]*y + m[10]*z);
1038 // m[15] += (m[3]*x + m[7]*y + m[11]*z);
1039 // m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
1040 // so m[15] is unchanged
1044 // Prepare the ssg nodes ... for each tile, set it's proper
1045 // transform and update it's range selector based on current
1047 void FGTileMgr::prep_ssg_nodes( void ) {
1053 // traverse the potentially viewable tile list and update range
1054 // selector and transform
1055 for ( int i = 0; i < (int)global_tile_cache.get_size(); i++ ) {
1056 t = global_tile_cache.get_tile( i );
1058 if ( t->is_loaded() ) {
1059 // set range selector (LOD trick) to be distance to center
1060 // of tile + bounding radius
1061 #ifndef FG_OLD_WEATHER
1062 ranges[1] = WeatherDatabase->getWeatherVisibility()
1063 + t->bounding_radius;
1065 ranges[1] = current_weather.get_visibility()+t->bounding_radius;
1067 t->range_ptr->setRanges( ranges, 2 );
1069 // calculate tile offset
1070 t->SetOffset( scenery.center );
1072 // calculate ssg transform
1074 sgSetCoord( &sgcoord,
1075 t->offset.x(), t->offset.y(), t->offset.z(),
1077 t->transform_ptr->setTransform( &sgcoord );