]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilemgr.cxx
8cf2b6c98d3480b01a9e95f4a8d5cf249f25f20d
[flightgear.git] / src / Scenery / tilemgr.cxx
1 // tilemgr.cxx -- routines to handle dynamic management of scenery tiles
2 //
3 // Written by Curtis Olson, started January 1998.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>
30 #endif
31
32 #include <GL/glut.h>
33 #include <XGL/xgl.h>
34
35 #include <Aircraft/aircraft.hxx>
36
37 #include <Debug/logstream.hxx>
38 // #include <Bucket/bucketutils.hxx>
39 #include <Include/fg_constants.h>
40 #include <Main/options.hxx>
41 #include <Main/views.hxx>
42 #include <Math/fg_geodesy.hxx>
43 #include <Math/mat3.h>
44 #include <Math/point3d.hxx>
45 #include <Math/polar3d.hxx>
46 #include <Math/vector.hxx>
47 #include <Objects/materialmgr.hxx>
48 #include <Objects/obj.hxx>
49
50 #ifdef FG_NEW_WEATHER
51 #  include <WeatherCM/FGLocalWeatherDatabase.h>
52 #else
53 #  include <Weather/weather.hxx>
54 #endif
55
56 #include "scenery.hxx"
57 #include "tilecache.hxx"
58 #include "tileentry.hxx"
59 #include "tilemgr.hxx"
60
61
62 // to test clipping speedup in fgTileMgrRender()
63 #if defined ( USE_FAST_FOV_CLIP )
64 // #define TEST_FOV_CLIP
65 // #define TEST_ELEV
66 #endif
67
68
69 extern ssgRoot *scene;
70
71
72 // the tile manager
73 FGTileMgr global_tile_mgr;
74
75
76 // Constructor
77 FGTileMgr::FGTileMgr ( void ):
78     state( Start )
79 {
80 }
81
82
83 // Destructor
84 FGTileMgr::~FGTileMgr ( void ) {
85 }
86
87
88 // Initialize the Tile Manager subsystem
89 int FGTileMgr::init( void ) {
90     FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
91
92     // load default material library
93     if ( ! material_mgr.loaded() ) {
94         material_mgr.load_lib();
95     }
96
97     global_tile_cache.init();
98
99     state = Inited;
100
101     return 1;
102 }
103
104
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 );
110     t->ssg_disable();
111 }
112
113
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 );
118
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 );
124         t->mark_loaded();
125     } else {
126         // find the next available cache entry and mark it as
127         // scheduled
128         cache_index = global_tile_cache.next_avail();
129         FGTileEntry *t = global_tile_cache.get_tile( cache_index );
130         t->mark_scheduled_for_use();
131
132         // register a load request
133         FGLoadRec request;
134         request.b = b;
135         request.cache_index = cache_index;
136         load_queue.push_back( request );
137     }
138
139     return cache_index;
140 }
141
142
143 // load a tile
144 void FGTileMgr::load_tile( const FGBucket& b, int cache_index) {
145
146     FG_LOG( FG_TERRAIN, FG_DEBUG, "Loading tile " << b );
147     
148     global_tile_cache.fill_in(cache_index, b);
149
150     FG_LOG( FG_TERRAIN, FG_DEBUG, "Loaded for cache index: " << cache_index );
151 }
152
153
154 // Calculate shortest distance from point to line
155 static double point_line_dist_squared( const Point3D& tc, const Point3D& vp, 
156                                        MAT3vec d )
157 {
158     MAT3vec p, p0;
159
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();
162
163     return fgPointLineSquared(p, p0, d);
164 }
165
166
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.
171 double
172 FGTileMgr::current_elev_new( const FGBucket& p ) {
173     FGTileEntry *t;
174     fgFRAGMENT *frag_ptr;
175     Point3D abs_view_pos = current_view.get_abs_view_pos();
176     Point3D earth_center(0.0);
177     Point3D result;
178     MAT3vec local_up;
179     double dist, lat_geod, alt, sea_level_r;
180     int index;
181
182     local_up[0] = abs_view_pos.x();
183     local_up[1] = abs_view_pos.y();
184     local_up[2] = abs_view_pos.z();
185
186     // Find current translation offset
187     // fgBucketFind(lon * RAD_TO_DEG, lat * RAD_TO_DEG, &p);
188     index = global_tile_cache.exists(p);
189     if ( index < 0 ) {
190         FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
191         return 0.0;
192     }
193
194     t = global_tile_cache.get_tile(index);
195
196     scenery.next_center = t->center;
197     
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 );
202
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);
207     
208     // calc current terrain elevation calculate distance from
209     // vertical tangent line at current position to center of
210     // tile.
211         
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); */
215
216     dist = point_line_dist_squared( t->center, abs_view_pos, local_up );
217     if ( dist < FG_SQUARE(t->bounding_radius) ) {
218
219         // traverse fragment list for tile
220         FGTileEntry::FragmentIterator current = t->begin();
221         FGTileEntry::FragmentIterator last = t->end();
222
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); */
229
230             dist = point_line_dist_squared( frag_ptr->center,
231                                             abs_view_pos,
232                                             local_up);
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 " <<
237                             result );
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, 
243                                  &alt, &sea_level_r);
244
245                     // printf("alt = %.2f\n", alt);
246                     // exit since we found an intersection
247                     if ( alt > -9999.0 ) {
248                         // printf("returning alt\n");
249                         return alt;
250                     } else {
251                         // printf("returning 0\n");
252                         return 0.0;
253                     }
254                 }
255             }
256         }
257     }
258
259     FG_LOG( FG_TERRAIN, FG_INFO, "(new) no terrain intersection found" );
260
261     return 0.0;
262 }
263
264
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.
269 double
270 FGTileMgr::current_elev( double lon, double lat, const Point3D& abs_view_pos ) {
271     FGTileCache *c;
272     FGTileEntry *t;
273     fgFRAGMENT *frag_ptr;
274     Point3D earth_center(0.0);
275     Point3D result;
276     MAT3vec local_up;
277     double dist, lat_geod, alt, sea_level_r;
278     int index;
279
280     c = &global_tile_cache;
281
282     local_up[0] = abs_view_pos.x();
283     local_up[1] = abs_view_pos.y();
284     local_up[2] = abs_view_pos.z();
285
286     FG_LOG( FG_TERRAIN, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
287
288     // Find current translation offset
289     FGBucket p( lon * RAD_TO_DEG, lat * RAD_TO_DEG );
290     index = c->exists(p);
291     if ( index < 0 ) {
292         FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
293         return 0.0;
294     }
295
296     t = c->get_tile(index);
297
298     scenery.next_center = t->center;
299     
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() );
304
305     FG_LOG( FG_TERRAIN, FG_DEBUG, "Tile center " << t->center 
306             << "  bounding radius = " << t->bounding_radius );
307
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);
312     
313     // calc current terrain elevation calculate distance from
314     // vertical tangent line at current position to center of
315     // tile.
316         
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); */
320
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 );
323
324     if ( dist < FG_SQUARE(t->bounding_radius) ) {
325
326         // traverse fragment list for tile
327         FGTileEntry::FragmentIterator current = t->begin();
328         FGTileEntry::FragmentIterator last = t->end();
329
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); */
336
337             dist = point_line_dist_squared( frag_ptr->center,
338                                             abs_view_pos,
339                                             local_up);
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 " <<
344                             result );
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, 
350                                  &alt, &sea_level_r);
351
352                     // printf("alt = %.2f\n", alt);
353                     // exit since we found an intersection
354                     if ( alt > -9999.0 ) {
355                         // printf("returning alt\n");
356                         return alt;
357                     } else {
358                         // printf("returning 0\n");
359                         return 0.0;
360                     }
361                 }
362             }
363         }
364     }
365
366     FG_LOG( FG_TERRAIN, FG_INFO, "(old) no terrain intersection found" );
367
368     return 0.0;
369 }
370
371
372 // Determine scenery altitude via ssg.  Normally this just happens
373 // when we render the scene, but we'd also like to be able to do this
374 // explicitely.  lat & lon are in radians.  view_pos in current world
375 // coordinate translated near (0,0,0) (in meters.)  Returns result in
376 // meters.
377 double
378 FGTileMgr::current_elev_ssg( const Point3D& abs_view_pos, 
379                              const Point3D& view_pos )
380 {
381     ssgHit *results ;
382
383     // cout << "view pos = " << view_pos << endl;
384     // cout << "abs view pos = " << abs_view_pos << endl;
385
386     sgMat4 m;
387     sgMakeTransMat4( m, view_pos.x(), view_pos.y(), view_pos.z() );
388
389     sgVec3 s;
390     sgSetVec3(s, -abs_view_pos.x(), -abs_view_pos.y(), -abs_view_pos.z() );
391
392     int num_hits = ssgLOS ( scene, s, m, &results ) ;
393
394     for ( int i = 0 ; i < num_hits ; i++ ) {
395         ssgHit *h = &(results [ i ]) ;
396         // cout << "got a hit!" << endl;
397         /* Do something with 'h' */
398     }
399
400     FG_LOG( FG_TERRAIN, FG_INFO, "(ssg) no terrain intersection found" );
401
402     return 0.0;
403 }
404
405
406 // given the current lon/lat, fill in the array of local chunks.  If
407 // the chunk isn't already in the cache, then read it from disk.
408 int FGTileMgr::update( void ) {
409     FGTileCache *c;
410     FGInterface *f;
411     FGBucket p2;
412     static FGBucket p_last(false);
413     static double last_lon = -1000.0;  // in degrees
414     static double last_lat = -1000.0;  // in degrees
415     int tile_diameter;
416     int i, j, dw, dh;
417
418     c = &global_tile_cache;
419     f = current_aircraft.fdm_state;
420
421     tile_diameter = current_options.get_tile_diameter();
422
423     FGBucket p1( f->get_Longitude() * RAD_TO_DEG,
424                  f->get_Latitude() * RAD_TO_DEG );
425     dw = tile_diameter / 2;
426     dh = tile_diameter / 2;
427
428     if ( (p1 == p_last) && (state == Running) ) {
429         // same bucket as last time
430         FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
431     } else if ( (state == Start) || (state == Inited) ) {
432         state = Running;
433
434         // First time through or we have teleported, initialize the
435         // system and load all relavant tiles
436
437         FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
438         FG_LOG( FG_TERRAIN, FG_INFO, "  First time through ... " );
439         FG_LOG( FG_TERRAIN, FG_INFO, "  Updating Tile list for " << p1 );
440         FG_LOG( FG_TERRAIN, FG_INFO, "  Loading " 
441                 << tile_diameter * tile_diameter << " tiles" );
442
443         // wipe/initialize tile cache
444         c->init();
445         p_last.make_bad();
446
447         // build the local area list and schedule tiles for loading
448
449         // start with the center tile and work out in concentric
450         // "rings"
451
452         p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
453                              f->get_Latitude() * RAD_TO_DEG,
454                              0, 0 );
455         sched_tile( p2 );
456
457         for ( i = 3; i <= tile_diameter; i = i + 2 ) {
458             int span = i / 2;
459
460             // bottom row
461             for ( j = -span; j <= span; ++j ) {
462                 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
463                                      f->get_Latitude() * RAD_TO_DEG,
464                                      j, -span );
465                 sched_tile( p2 );
466             }
467
468             // top row
469             for ( j = -span; j <= span; ++j ) {
470                 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
471                                      f->get_Latitude() * RAD_TO_DEG,
472                                      j, span );
473                 sched_tile( p2 );
474             }
475
476             // middle rows
477             for ( j = -span + 1; j <= span - 1; ++j ) {
478                 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
479                                      f->get_Latitude() * RAD_TO_DEG,
480                                      -span, j );
481                 sched_tile( p2 );
482                 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
483                                      f->get_Latitude() * RAD_TO_DEG,
484                                      span, j );
485                 sched_tile( p2 );
486             }
487
488         }
489
490         /* for ( j = 0; j < tile_diameter; j++ ) {
491             for ( i = 0; i < tile_diameter; i++ ) {
492                 // fgBucketOffset(&p1, &p2, i - dw, j - dh);
493                 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
494                                      f->get_Latitude() * RAD_TO_DEG,
495                                      i - dw, j -dh );
496                 sched_tile( p2 );
497             }
498         } */
499
500         // Now force a load of the center tile and inner ring so we
501         // have something to see in our first frame.
502         for ( i = 0; i < 9; ++i ) {
503             if ( load_queue.size() ) {
504                 FG_LOG( FG_TERRAIN, FG_DEBUG, 
505                         "Load queue not empty, loading a tile" );
506             
507                 FGLoadRec pending = load_queue.front();
508                 load_queue.pop_front();
509                 load_tile( pending.b, pending.cache_index );
510             }
511         }
512
513     } else {
514         // We've moved to a new bucket, we need to scroll our
515         // structures, and load in the new tiles
516
517 #if 0 
518         // make sure load queue is flushed before doing shift
519         while ( load_queue.size() ) {
520             FG_LOG( FG_TERRAIN, FG_DEBUG, 
521                     "Load queue not empty, flushing queue before tile shift." );
522             
523             FGLoadRec pending = load_queue.front();
524             load_queue.pop_front();
525             load_tile( pending.b, pending.index );
526         }
527 #endif
528
529         // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
530         // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
531         // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
532
533         FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
534
535         if ( (p1.get_lon() > p_last.get_lon()) ||
536              ( (p1.get_lon() == p_last.get_lon()) && 
537                (p1.get_x() > p_last.get_x()) ) ) {
538             FG_LOG( FG_TERRAIN, FG_INFO, 
539                     "  (East) Loading " << tile_diameter << " tiles" );
540             for ( j = 0; j < tile_diameter; j++ ) {
541                 // scrolling East
542                 // schedule new column
543                 p2 = fgBucketOffset( last_lon, last_lat, dw + 1, j - dh );
544                 sched_tile( p2 );
545             }
546         } else if ( (p1.get_lon() < p_last.get_lon()) ||
547                     ( (p1.get_lon() == p_last.get_lon()) && 
548                       (p1.get_x() < p_last.get_x()) ) ) {
549             FG_LOG( FG_TERRAIN, FG_INFO, 
550                     "  (West) Loading " << tile_diameter << " tiles" );
551             for ( j = 0; j < tile_diameter; j++ ) {
552                 // scrolling West
553                 // schedule new column
554                 p2 = fgBucketOffset( last_lon, last_lat, -dw - 1, j - dh );
555                 sched_tile( p2 );
556             }
557         }
558
559         if ( (p1.get_lat() > p_last.get_lat()) ||
560              ( (p1.get_lat() == p_last.get_lat()) && 
561                (p1.get_y() > p_last.get_y()) ) ) {
562             FG_LOG( FG_TERRAIN, FG_INFO, 
563                     "  (North) Loading " << tile_diameter << " tiles" );
564             for ( i = 0; i < tile_diameter; i++ ) {
565                 // scrolling North
566                 // schedule new row
567                 p2 = fgBucketOffset( last_lon, last_lat, i - dw, dh + 1);
568                 sched_tile( p2 );
569             }
570         } else if ( (p1.get_lat() < p_last.get_lat()) ||
571                     ( (p1.get_lat() == p_last.get_lat()) && 
572                       (p1.get_y() < p_last.get_y()) ) ) {
573             FG_LOG( FG_TERRAIN, FG_INFO, 
574                     "  (South) Loading " << tile_diameter << " tiles" );
575             for ( i = 0; i < tile_diameter; i++ ) {
576                 // scrolling South
577                 // schedule new row
578                 p2 = fgBucketOffset( last_lon, last_lat, i - dw, -dh - 1);
579                 sched_tile( p2 );
580             }
581         }
582     }
583
584     if ( load_queue.size() ) {
585         FG_LOG( FG_TERRAIN, FG_DEBUG, "Load queue not empty, loading a tile" );
586
587         FGLoadRec pending = load_queue.front();
588         load_queue.pop_front();
589         load_tile( pending.b, pending.cache_index );
590     }
591
592     // find our current elevation (feed in the current bucket to save work)
593     Point3D geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
594     Point3D tmp_abs_view_pos = fgGeodToCart(geod_pos);
595
596     scenery.cur_elev = 
597         current_elev( f->get_Longitude(), f->get_Latitude(), tmp_abs_view_pos );
598     // cout << "current elevation == " << scenery.cur_elev << endl;
599     // double junk = current_elev_ssg( current_view.abs_view_pos,
600     //                                 current_view.view_pos );
601     // cout << "current elevation (ssg) == " << 
602         
603     p_last = p1;
604     last_lon = f->get_Longitude() * RAD_TO_DEG;
605     last_lat = f->get_Latitude() * RAD_TO_DEG;
606
607     return 1;
608 }
609
610
611 // NEW 
612
613 // inrange() IS THIS POINT WITHIN POSSIBLE VIEWING RANGE ?
614 //      calculate distance from vertical tangent line at
615 //      current position to center of object.
616 //      this is equivalent to
617 //      dist = point_line_dist_squared( &(t->center), &(v->abs_view_pos), 
618 //                                      v->local_up );
619 //      if ( dist < FG_SQUARE(t->bounding_radius) ) {
620 //
621 // the compiler should inline this for us
622
623 static int
624 inrange( const double radius, const Point3D& center, const Point3D& vp,
625          const MAT3vec up)
626 {
627     MAT3vec u, u1, v;
628     //  double tmp;
629         
630     // u = p - p0
631     u[0] = center.x() - vp.x();
632     u[1] = center.y() - vp.y();
633     u[2] = center.z() - vp.z();
634         
635     // calculate the projection, u1, of u along d.
636     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
637         
638     MAT3_SCALE_VEC(u1, up,
639                    (MAT3_DOT_PRODUCT(u, up) / MAT3_DOT_PRODUCT(up, up)) );
640     
641     // v = u - u1 = vector from closest point on line, p1, to the
642     // original point, p.
643     MAT3_SUB_VEC(v, u, u1);
644         
645     return( FG_SQUARE(radius) >= MAT3_DOT_PRODUCT(v, v));
646 }
647
648
649 // NEW for legibility
650
651 // update this tile's geometry for current view
652 // The Compiler should inline this
653 static void
654 update_tile_geometry( FGTileEntry *t, GLdouble *MODEL_VIEW)
655 {
656     GLfloat *m;
657     double x, y, z;
658         
659     // calculate tile offset
660     t->offset = t->center - scenery.center;
661
662     x = t->offset.x();
663     y = t->offset.y();
664     z = t->offset.z();
665         
666     m = t->model_view;
667         
668     // Calculate the model_view transformation matrix for this tile
669     FG_MEM_COPY( m, MODEL_VIEW, 16*sizeof(GLdouble) );
670     
671     // This is equivalent to doing a glTranslatef(x, y, z);
672     m[12] += (m[0]*x + m[4]*y + m[8] *z);
673     m[13] += (m[1]*x + m[5]*y + m[9] *z);
674     m[14] += (m[2]*x + m[6]*y + m[10]*z);
675     // m[15] += (m[3]*x + m[7]*y + m[11]*z);
676     // m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
677     // so m[15] is unchanged
678 }
679
680
681 // Prepare the ssg nodes ... for each tile, set it's proper
682 // transform and update it's range selector based on current
683 // visibilty
684 void FGTileMgr::prep_ssg_nodes( void ) {
685     FGTileEntry *t;
686
687     float ranges[2];
688     ranges[0] = 0.0f;
689
690     // traverse the potentially viewable tile list and update range
691     // selector and transform
692     for ( int i = 0; i < (int)global_tile_cache.get_size(); i++ ) {
693         t = global_tile_cache.get_tile( i );
694
695         if ( t->is_loaded() ) {
696             // set range selector (LOD trick) to be distance to center
697             // of tile + bounding radius
698 #ifdef FG_NEW_WEATHER
699             ranges[1] = WeatherDatabase->getWeatherVisibility()
700                 + t->bounding_radius;
701 #else
702             ranges[1] = current_weather.get_visibility()+t->bounding_radius;
703 #endif
704             t->range_ptr->setRanges( ranges, 2 );
705
706             // calculate tile offset
707             t->SetOffset( scenery.center );
708
709             // calculate ssg transform
710             sgCoord sgcoord;
711             sgSetCoord( &sgcoord,
712                         t->offset.x(), t->offset.y(), t->offset.z(),
713                         0.0, 0.0, 0.0 );
714             t->transform_ptr->setTransform( &sgcoord );
715         }
716     }
717 }