]> git.mxchange.org Git - flightgear.git/blob - Simulator/Scenery/tilemgr.cxx
Removed in-src cvs logs.
[flightgear.git] / Simulator / 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/material.hxx>
48 #include <Objects/obj.hxx>
49 #include <Weather/weather.hxx>
50
51 #include "scenery.hxx"
52 #include "tile.hxx"
53 #include "tilecache.hxx"
54 #include "tilemgr.hxx"
55
56
57 // to test clipping speedup in fgTileMgrRender()
58 #if defined ( USE_FAST_FOV_CLIP )
59   // #define TEST_FOV_CLIP
60   // #define TEST_ELEV
61 #endif
62
63
64 #define FG_LOCAL_X_Y         81  // max(o->tile_diameter) ** 2
65
66 #define FG_SQUARE( X ) ( (X) * (X) )
67
68 #if defined(USE_MEM) || defined(WIN32)
69 #  define FG_MEM_COPY(to,from,n)        memcpy(to, from, n)
70 #else
71 #  define FG_MEM_COPY(to,from,n)        bcopy(from, to, n)
72 #endif
73
74 // closest (potentially viewable) tiles, centered on current tile.
75 // This is an array of pointers to cache indexes.
76 int tiles[FG_LOCAL_X_Y];
77
78
79 // Initialize the Tile Manager subsystem
80 int fgTileMgrInit( void ) {
81     FG_LOG( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem." );
82
83     // load default material library
84     material_mgr.load_lib();
85
86     return 1;
87 }
88
89
90 // load a tile
91 void fgTileMgrLoadTile( const FGBucket& p, int *index) {
92     fgTILECACHE *c;
93
94     c = &global_tile_cache;
95
96     FG_LOG( FG_TERRAIN, FG_DEBUG, "Updating for bucket " << p );
97     
98     // if not in cache, load tile into the next available slot
99     *index = c->exists(p);
100     if ( *index < 0 ) {
101         *index = c->next_avail();
102         c->fill_in(*index, p);
103     }
104
105     FG_LOG( FG_TERRAIN, FG_DEBUG, "Selected cache index: " << *index );
106 }
107
108
109 // Calculate shortest distance from point to line
110 static double point_line_dist_squared( const Point3D& tc, const Point3D& vp, 
111                                        MAT3vec d )
112 {
113     MAT3vec p, p0;
114     double dist;
115
116     p[0] = tc.x(); p[1] = tc.y(); p[2] = tc.z();
117     p0[0] = vp.x(); p0[1] = vp.y(); p0[2] = vp.z();
118
119     return fgPointLineSquared(p, p0, d);
120 }
121
122
123 // Determine scenery altitude.  Normally this just happens when we
124 // render the scene, but we'd also like to be able to do this
125 // explicitely.  lat & lon are in radians.  abs_view_pos in meters.
126 // Returns result in meters.
127 double
128 fgTileMgrCurElevNEW( const FGBucket& p ) {
129     fgTILE *t;
130     fgFRAGMENT *frag_ptr;
131     Point3D abs_view_pos = current_view.get_abs_view_pos();
132     Point3D earth_center(0.0);
133     Point3D result;
134     MAT3vec local_up;
135     double dist, lat_geod, alt, sea_level_r;
136     int index;
137
138     local_up[0] = abs_view_pos.x();
139     local_up[1] = abs_view_pos.y();
140     local_up[2] = abs_view_pos.z();
141
142     // Find current translation offset
143     // fgBucketFind(lon * RAD_TO_DEG, lat * RAD_TO_DEG, &p);
144     index = global_tile_cache.exists(p);
145     if ( index < 0 ) {
146         FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
147         return 0.0;
148     }
149
150     t = global_tile_cache.get_tile(index);
151
152     scenery.next_center = t->center;
153     
154     FG_LOG( FG_TERRAIN, FG_DEBUG, 
155             "Current bucket = " << p << "  Index = " << p.gen_index_str() );
156     FG_LOG( FG_TERRAIN, FG_DEBUG,
157             "abs_view_pos = " << abs_view_pos );
158
159     // calculate tile offset
160     // x = (t->offset.x = t->center.x - scenery.center.x);
161     // y = (t->offset.y = t->center.y - scenery.center.y);
162     // z = (t->offset.z = t->center.z - scenery.center.z);
163     
164     // calc current terrain elevation calculate distance from
165     // vertical tangent line at current position to center of
166     // tile.
167         
168     /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
169        point_line_dist_squared(&(t->offset), &(v->view_pos), 
170        v->local_up), t->bounding_radius); */
171
172     dist = point_line_dist_squared( t->center, abs_view_pos, local_up );
173     if ( dist < FG_SQUARE(t->bounding_radius) ) {
174
175         // traverse fragment list for tile
176         fgTILE::FragmentIterator current = t->begin();
177         fgTILE::FragmentIterator last = t->end();
178
179         for ( ; current != last; ++current ) {
180             frag_ptr = &(*current);
181             /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
182                point_line_dist_squared( &(frag_ptr->center), 
183                &abs_view_pos), local_up),
184                frag_ptr->bounding_radius); */
185
186             dist = point_line_dist_squared( frag_ptr->center,
187                                             abs_view_pos,
188                                             local_up);
189             if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
190                 if ( frag_ptr->intersect( abs_view_pos, 
191                                           earth_center, 0, result ) ) {
192                     FG_LOG( FG_TERRAIN, FG_DEBUG, "intersection point " <<
193                             result );
194                     // compute geocentric coordinates of tile center
195                     Point3D pp = fgCartToPolar3d(result);
196                     FG_LOG( FG_TERRAIN, FG_DEBUG, "  polar form = " << pp );
197                     // convert to geodetic coordinates
198                     fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod, 
199                                  &alt, &sea_level_r);
200
201                     // printf("alt = %.2f\n", alt);
202                     // exit since we found an intersection
203                     if ( alt > -9999.0 ) {
204                         // printf("returning alt\n");
205                         return alt;
206                     } else {
207                         // printf("returning 0\n");
208                         return 0.0;
209                     }
210                 }
211             }
212         }
213     }
214
215     FG_LOG( FG_TERRAIN, FG_INFO, "(new) no terrain intersection found" );
216
217     return 0.0;
218 }
219
220
221 // Determine scenery altitude.  Normally this just happens when we
222 // render the scene, but we'd also like to be able to do this
223 // explicitely.  lat & lon are in radians.  abs_view_pos in meters.
224 // Returns result in meters.
225 double
226 fgTileMgrCurElev( double lon, double lat, const Point3D& abs_view_pos ) {
227     fgTILECACHE *c;
228     fgTILE *t;
229     fgFRAGMENT *frag_ptr;
230     Point3D earth_center(0.0);
231     Point3D result;
232     MAT3vec local_up;
233     double dist, lat_geod, alt, sea_level_r;
234     int index;
235
236     c = &global_tile_cache;
237
238     local_up[0] = abs_view_pos.x();
239     local_up[1] = abs_view_pos.y();
240     local_up[2] = abs_view_pos.z();
241
242     FG_LOG( FG_TERRAIN, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
243
244     // Find current translation offset
245     FGBucket p( lon * RAD_TO_DEG, lat * RAD_TO_DEG );
246     index = c->exists(p);
247     if ( index < 0 ) {
248         FG_LOG( FG_TERRAIN, FG_WARN, "Tile not found" );
249         return 0.0;
250     }
251
252     t = c->get_tile(index);
253
254     scenery.next_center = t->center;
255     
256     FG_LOG( FG_TERRAIN, FG_DEBUG, 
257             "Pos = (" << lon * RAD_TO_DEG << ", " << lat * RAD_TO_DEG
258             << ")  Current bucket = " << p 
259             << "  Index = " << p.gen_index_str() );
260
261     FG_LOG( FG_TERRAIN, FG_DEBUG, "Tile center " << t->center 
262             << "  bounding radius = " << t->bounding_radius );
263
264     // calculate tile offset
265     // x = (t->offset.x = t->center.x - scenery.center.x);
266     // y = (t->offset.y = t->center.y - scenery.center.y);
267     // z = (t->offset.z = t->center.z - scenery.center.z);
268     
269     // calc current terrain elevation calculate distance from
270     // vertical tangent line at current position to center of
271     // tile.
272         
273     /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
274        point_line_dist_squared(&(t->offset), &(v->view_pos), 
275        v->local_up), t->bounding_radius); */
276
277     dist = point_line_dist_squared( t->center, abs_view_pos, local_up );
278     FG_LOG( FG_TERRAIN, FG_DEBUG, "(gross check) dist squared = " << dist );
279
280     if ( dist < FG_SQUARE(t->bounding_radius) ) {
281
282         // traverse fragment list for tile
283         fgTILE::FragmentIterator current = t->begin();
284         fgTILE::FragmentIterator last = t->end();
285
286         for ( ; current != last; ++current ) {
287             frag_ptr = &(*current);
288             /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
289                point_line_dist_squared( &(frag_ptr->center), 
290                &abs_view_pos), local_up),
291                frag_ptr->bounding_radius); */
292
293             dist = point_line_dist_squared( frag_ptr->center,
294                                             abs_view_pos,
295                                             local_up);
296             if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
297                 if ( frag_ptr->intersect( abs_view_pos, 
298                                           earth_center, 0, result ) ) {
299                     FG_LOG( FG_TERRAIN, FG_DEBUG, "intersection point " <<
300                             result );
301                     // compute geocentric coordinates of tile center
302                     Point3D pp = fgCartToPolar3d(result);
303                     FG_LOG( FG_TERRAIN, FG_DEBUG, "  polar form = " << pp );
304                     // convert to geodetic coordinates
305                     fgGeocToGeod(pp.lat(), pp.radius(), &lat_geod, 
306                                  &alt, &sea_level_r);
307
308                     // printf("alt = %.2f\n", alt);
309                     // exit since we found an intersection
310                     if ( alt > -9999.0 ) {
311                         // printf("returning alt\n");
312                         return alt;
313                     } else {
314                         // printf("returning 0\n");
315                         return 0.0;
316                     }
317                 }
318             }
319         }
320     }
321
322     FG_LOG( FG_TERRAIN, FG_INFO, "(old) no terrain intersection found" );
323
324     return 0.0;
325 }
326
327
328 // given the current lon/lat, fill in the array of local chunks.  If
329 // the chunk isn't already in the cache, then read it from disk.
330 int fgTileMgrUpdate( void ) {
331     fgTILECACHE *c;
332     FGInterface *f;
333     FGBucket p2;
334     static FGBucket p_last(false);
335     static double last_lon = -1000.0;  // in degrees
336     static double last_lat = -1000.0;  // in degrees
337     int tile_diameter;
338     int i, j, dw, dh;
339
340     c = &global_tile_cache;
341     f = current_aircraft.fdm_state;
342
343     tile_diameter = current_options.get_tile_diameter();
344
345     FGBucket p1( f->get_Longitude() * RAD_TO_DEG,
346                  f->get_Latitude() * RAD_TO_DEG );
347     dw = tile_diameter / 2;
348     dh = tile_diameter / 2;
349
350     if ( p1 == p_last ) {
351         // same bucket as last time
352         FG_LOG( FG_TERRAIN, FG_DEBUG, "Same bucket as last time" );
353     } else if ( p_last.get_lon() == -1000 ) {
354         // First time through, initialize the system and load all
355         // relavant tiles
356
357         FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
358         FG_LOG( FG_TERRAIN, FG_INFO, "  First time through ... " );
359         FG_LOG( FG_TERRAIN, FG_INFO, "  Updating Tile list for " << p1 );
360         FG_LOG( FG_TERRAIN, FG_INFO, "  Loading " 
361                 << tile_diameter * tile_diameter << " tiles" );
362
363         // wipe/initialize tile cache
364         c->init();
365
366         // build the local area list and update cache
367         for ( j = 0; j < tile_diameter; j++ ) {
368             for ( i = 0; i < tile_diameter; i++ ) {
369                 // fgBucketOffset(&p1, &p2, i - dw, j - dh);
370                 p2 = fgBucketOffset( f->get_Longitude() * RAD_TO_DEG,
371                                      f->get_Latitude() * RAD_TO_DEG,
372                                      i - dw, j -dh );
373                 fgTileMgrLoadTile( p2, &tiles[(j*tile_diameter) + i]);
374             }
375         }
376     } else {
377         // We've moved to a new bucket, we need to scroll our
378         // structures, and load in the new tiles
379
380         // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
381         // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
382         // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
383
384         FG_LOG( FG_TERRAIN, FG_INFO, "Updating Tile list for " << p1 );
385
386         if ( (p1.get_lon() > p_last.get_lon()) ||
387              ( (p1.get_lon() == p_last.get_lon()) && (p1.get_x() > p_last.get_x()) ) ) {
388             FG_LOG( FG_TERRAIN, FG_INFO, 
389                     "  Loading " << tile_diameter << "tiles" );
390             for ( j = 0; j < tile_diameter; j++ ) {
391                 // scrolling East
392                 for ( i = 0; i < tile_diameter - 1; i++ ) {
393                     tiles[(j*tile_diameter) + i] = 
394                         tiles[(j*tile_diameter) + i + 1];
395                 }
396                 // load in new column
397                 // fgBucketOffset(&p_last, &p2, dw + 1, j - dh);
398                 p2 = fgBucketOffset( last_lon, last_lat, dw + 1, j - dh );
399                 fgTileMgrLoadTile( p2, &tiles[(j*tile_diameter) + 
400                                              tile_diameter - 1]);
401             }
402         } else if ( (p1.get_lon() < p_last.get_lon()) ||
403                     ( (p1.get_lon() == p_last.get_lon()) && (p1.get_x() < p_last.get_x()) ) ) {
404             FG_LOG( FG_TERRAIN, FG_INFO, 
405                     "  Loading " << tile_diameter << "tiles" );
406             for ( j = 0; j < tile_diameter; j++ ) {
407                 // scrolling West
408                 for ( i = tile_diameter - 1; i > 0; i-- ) {
409                     tiles[(j*tile_diameter) + i] = 
410                         tiles[(j*tile_diameter) + i - 1];
411                 }
412                 // load in new column
413                 // fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
414                 p2 = fgBucketOffset( last_lon, last_lat, -dw - 1, j - dh );
415                 fgTileMgrLoadTile( p2, &tiles[(j*tile_diameter) + 0]);
416             }
417         }
418
419         if ( (p1.get_lat() > p_last.get_lat()) ||
420              ( (p1.get_lat() == p_last.get_lat()) && (p1.get_y() > p_last.get_y()) ) ) {
421             FG_LOG( FG_TERRAIN, FG_INFO, 
422                     "  Loading " << tile_diameter << "tiles" );
423             for ( i = 0; i < tile_diameter; i++ ) {
424                 // scrolling North
425                 for ( j = 0; j < tile_diameter - 1; j++ ) {
426                     tiles[(j * tile_diameter) + i] =
427                         tiles[((j+1) * tile_diameter) + i];
428                 }
429                 // load in new column
430                 // fgBucketOffset(&p_last, &p2, i - dw, dh + 1);
431                 p2 = fgBucketOffset( last_lon, last_lat, i - dw, dh + 1);
432                 fgTileMgrLoadTile( p2, &tiles[((tile_diameter-1) * 
433                                                tile_diameter) + i]);
434             }
435         } else if ( (p1.get_lat() < p_last.get_lat()) ||
436                     ( (p1.get_lat() == p_last.get_lat()) && (p1.get_y() < p_last.get_y()) ) ) {
437             FG_LOG( FG_TERRAIN, FG_INFO, 
438                     "  Loading " << tile_diameter << "tiles" );
439             for ( i = 0; i < tile_diameter; i++ ) {
440                 // scrolling South
441                 for ( j = tile_diameter - 1; j > 0; j-- ) {
442                     tiles[(j * tile_diameter) + i] = 
443                         tiles[((j-1) * tile_diameter) + i];
444                 }
445                 // load in new column
446                 // fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
447                 p2 = fgBucketOffset( last_lon, last_lat, i - dw, -dh - 1);
448                 fgTileMgrLoadTile( p2, &tiles[0 + i]);
449             }
450         }
451     }
452
453     // find our current elevation (feed in the current bucket to save work)
454     Point3D geod_pos = Point3D( f->get_Longitude(), f->get_Latitude(), 0.0);
455     Point3D tmp_abs_view_pos = fgGeodToCart(geod_pos);
456
457     scenery.cur_elev = 
458         fgTileMgrCurElev( f->get_Longitude(), f->get_Latitude(), 
459                           tmp_abs_view_pos );
460
461     p_last = p1;
462     last_lon = f->get_Longitude() * RAD_TO_DEG;
463     last_lat = f->get_Latitude() * RAD_TO_DEG;
464
465     return 1;
466 }
467
468
469 // Calculate if point/radius is inside view frustum
470 static int viewable( const Point3D& cp, double radius ) {
471     int viewable = 1; // start by assuming it's viewable
472     double x1, y1;
473
474 /********************************/
475 #if defined( USE_FAST_FOV_CLIP ) // views.hxx
476 /********************************/
477         
478     MAT3vec eye;        
479     double *mat;
480     double x, y, z;
481
482     x = cp.x();
483     y = cp.y();
484     z = cp.z();
485         
486     mat = (double *)(current_view.get_WORLD_TO_EYE());
487         
488     eye[2] =  x*mat[2] + y*mat[6] + z*mat[10] + mat[14];
489         
490     // Check near and far clip plane
491     if( ( eye[2] > radius ) ||
492         ( eye[2] + radius + current_weather.get_visibility() < 0) )
493     {
494         return(0);
495     }
496         
497     eye[0] = (x*mat[0] + y*mat[4] + z*mat[8] + mat[12])
498         * current_view.get_slope_x();
499
500     // check right and left clip plane (from eye perspective)
501     x1 = radius * current_view.get_fov_x_clip();
502     if( (eye[2] > -(eye[0]+x1)) || (eye[2] > (eye[0]-x1)) )
503     {
504         return(0);
505     }
506         
507     eye[1] = (x*mat[1] + y*mat[5] + z*mat[9] + mat[13]) 
508         * current_view.get_slope_y();
509
510     // check bottom and top clip plane (from eye perspective)
511     y1 = radius * current_view.get_fov_y_clip();
512     if( (eye[2] > -(eye[1]+y1)) || (eye[2] > (eye[1]-y1)) )
513     {
514         return(0);
515     }
516
517 /********************************/      
518 #else // DO NOT USE_FAST_FOV_CLIP
519 /********************************/      
520
521     fgVIEW *v;
522     MAT3hvec world, eye;
523     double x0, slope;
524
525     v = &current_view;
526
527     MAT3_SET_HVEC(world, cp->x, cp->y, cp->z, 1.0);
528     // MAT3mult_vec(eye, world, v->WORLD_TO_EYE);
529     // printf( "\nworld -> eye = %.2f %.2f %.2f  radius = %.2f\n", 
530     //         eye[0], eye[1], eye[2], radius);
531
532     // Use lazy evaluation for calculating eye hvec.
533 #define vec world
534 #define mat v->WORLD_TO_EYE
535     eye[2] = vec[0]*mat[0][2]+vec[1]*mat[1][2]+vec[2]*mat[2][2]+mat[3][2];
536
537     // Check near clip plane
538     if ( eye[2] > radius ) {
539         return(0);
540     }
541
542     // Check far clip plane
543     if ( eye[2] + radius < -current_weather.get_visibility() ) {
544         return(0);
545     }
546
547     // check right clip plane (from eye perspective)
548     // y = m * (x - x0) = equation of a line intercepting X axis at x0
549     x1 = v->cos_fov_x * radius;
550     y1 = v->sin_fov_x * radius;
551     slope = v->slope_x;
552     eye[0] = vec[0]*mat[0][0]+vec[1]*mat[1][0]+vec[2]*mat[2][0]+mat[3][0];
553
554     if ( eye[2] > ((slope * (eye[0] - x1)) + y1) ) {
555         return( false );
556     }
557
558     // check left clip plane (from eye perspective)
559     if ( eye[2] > -((slope * (eye[0] + x1)) - y1) ) {
560         return( false );
561     }
562
563     // check bottom clip plane (from eye perspective)
564     x1 = -(v->cos_fov_y) * radius;
565     y1 = v->sin_fov_y * radius;
566     slope = v->slope_y;
567     eye[1] = vec[0]*mat[0][1]+vec[1]*mat[1][1]+vec[2]*mat[2][1]+mat[3][1];
568 #undef vec
569 #undef mat
570
571     if ( eye[2] > ((slope * (eye[1] - x1)) + y1) ) {
572         return( false );
573     }
574
575     // check top clip plane (from eye perspective)
576     if ( eye[2] > -((slope * (eye[1] + x1)) - y1) ) {
577         return( false );
578     }
579
580 #endif // defined( USE_FAST_FOV_CLIP )
581         
582     return(viewable);
583 }
584
585
586 // NEW 
587
588 // inrange() IS THIS POINT WITHIN POSSIBLE VIEWING RANGE ?
589 //      calculate distance from vertical tangent line at
590 //      current position to center of object.
591 //      this is equivalent to
592 //      dist = point_line_dist_squared( &(t->center), &(v->abs_view_pos), 
593 //                                      v->local_up );
594 //      if ( dist < FG_SQUARE(t->bounding_radius) ) {
595 //
596 // the compiler should inline this for us
597
598 static int
599 inrange( const double radius, const Point3D& center, const Point3D& vp,
600          const MAT3vec up)
601 {
602     MAT3vec u, u1, v;
603     //  double tmp;
604         
605     // u = p - p0
606     u[0] = center.x() - vp.x();
607     u[1] = center.y() - vp.y();
608     u[2] = center.z() - vp.z();
609         
610     // calculate the projection, u1, of u along d.
611     // u1 = ( dot_prod(u, d) / dot_prod(d, d) ) * d;
612         
613     MAT3_SCALE_VEC(u1, up,
614                    (MAT3_DOT_PRODUCT(u, up) / MAT3_DOT_PRODUCT(up, up)) );
615     
616     // v = u - u1 = vector from closest point on line, p1, to the
617     // original point, p.
618     MAT3_SUB_VEC(v, u, u1);
619         
620     return( FG_SQUARE(radius) >= MAT3_DOT_PRODUCT(v, v));
621 }
622
623
624 // NEW for legibility
625
626 // update this tile's geometry for current view
627 // The Compiler should inline this
628 static void
629 update_tile_geometry( fgTILE *t, GLdouble *MODEL_VIEW)
630 {
631     GLdouble *m;
632     double x, y, z;
633         
634     // calculate tile offset
635     t->offset = t->center - scenery.center;
636
637     x = t->offset.x();
638     y = t->offset.y();
639     z = t->offset.z();
640         
641     m = t->model_view;
642         
643     // Calculate the model_view transformation matrix for this tile
644     FG_MEM_COPY( m, MODEL_VIEW, 16*sizeof(GLdouble) );
645     
646     // This is equivalent to doing a glTranslatef(x, y, z);
647     m[12] += (m[0]*x + m[4]*y + m[8] *z);
648     m[13] += (m[1]*x + m[5]*y + m[9] *z);
649     m[14] += (m[2]*x + m[6]*y + m[10]*z);
650     // m[15] += (m[3]*x + m[7]*y + m[11]*z);
651     // m[3] m7[] m[11] are 0.0 see LookAt() in views.cxx
652     // so m[15] is unchanged
653 }
654
655
656 // Render the local tiles
657 void fgTileMgrRender( void ) {
658     FGInterface *f;
659     fgTILECACHE *c;
660     fgTILE *t;
661     FGView *v;
662     Point3D frag_offset;
663     fgFRAGMENT *frag_ptr;
664     fgMATERIAL *mtl_ptr;
665     int i;
666     int tile_diameter;
667     int index;
668     int culled = 0;
669     int drawn = 0;
670
671     c = &global_tile_cache;
672     f = current_aircraft.fdm_state;
673     v = &current_view;
674
675     tile_diameter = current_options.get_tile_diameter();
676
677     // moved to fgTileMgrUpdate, right after we check if we need to
678     // load additional tiles:
679     // scenery.cur_elev = fgTileMgrCurElev( FG_Longitude, FG_Latitude, 
680     //                                      v->abs_view_pos );
681  
682     // initialize the transient per-material fragment lists
683     material_mgr.init_transient_material_lists();
684    
685     // Pass 1
686     // traverse the potentially viewable tile list
687     for ( i = 0; i < (tile_diameter * tile_diameter); i++ ) {
688         index = tiles[i];
689         // fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index);
690         t = c->get_tile(index);
691
692         // calculate tile offset
693         t->SetOffset( scenery.center );
694
695         // Course (tile based) culling
696         if ( viewable(t->offset, t->bounding_radius) ) {
697             // at least a portion of this tile could be viewable
698             
699             // Calculate the model_view transformation matrix for this tile
700             // This is equivalent to doing a glTranslatef(x, y, z);
701             t->UpdateViewMatrix( v->get_MODEL_VIEW() );
702
703             // xglPushMatrix();
704             // xglTranslatef(t->offset.x, t->offset.y, t->offset.z);
705
706             // traverse fragment list for tile
707             fgTILE::FragmentIterator current = t->begin();
708             fgTILE::FragmentIterator last = t->end();
709
710             for ( ; current != last; ++current ) {
711                 frag_ptr = &(*current);
712                 
713                 if ( frag_ptr->display_list >= 0 ) {
714                     // Fine (fragment based) culling
715                     frag_offset = frag_ptr->center - scenery.center;
716
717                     if ( viewable(frag_offset, frag_ptr->bounding_radius*2) ) {
718                         // add to transient per-material property fragment list
719                         // frag_ptr->tile_offset.x = t->offset.x;
720                         // frag_ptr->tile_offset.y = t->offset.y;
721                         // frag_ptr->tile_offset.z = t->offset.z;
722
723                         mtl_ptr = frag_ptr->material_ptr;
724                         // printf(" lookup = %s\n", mtl_ptr->texture_name);
725                         if ( ! mtl_ptr->append_sort_list( frag_ptr ) ) {
726                             FG_LOG( FG_TERRAIN, FG_ALERT,
727                                     "Overran material sorting array" );
728                         }
729
730                         // xglCallList(frag_ptr->display_list);
731                         drawn++;
732                     } else {
733                         // printf("Culled a fragment %.2f %.2f %.2f %.2f\n",
734                         //        frag_ptr->center.x, frag_ptr->center.y,
735                         //        frag_ptr->center.z, frag_ptr->bounding_radius);
736                         culled++;
737                     }
738                 }
739             }
740
741             // xglPopMatrix();
742         } else {
743             culled += t->fragment_list.size();
744         }
745     }
746
747     if ( (drawn + culled) > 0 ) {
748         v->set_vfc_ratio( (double)culled / (double)(drawn + culled) );
749     } else {
750         v->set_vfc_ratio( 0.0 );
751     }
752     // printf("drawn = %d  culled = %d  saved = %.2f\n", drawn, culled, 
753     //        v->vfc_ratio);
754
755     // Pass 2
756     // traverse the transient per-material fragment lists and render
757     // out all fragments for each material property.
758     xglPushMatrix();
759     material_mgr.render_fragments();
760     xglPopMatrix();
761 }
762
763