]> git.mxchange.org Git - flightgear.git/blobdiff - Scenery/tilemgr.cxx
Attempting to iron out seg faults and crashes.
[flightgear.git] / Scenery / tilemgr.cxx
index 252fd83c13e24c2d1f8bfa2df950c69a48b99ea4..fa70e32c0c15e4d890df07fe71b043be913a9ec1 100644 (file)
@@ -45,6 +45,7 @@
 #include <Math/mat3.h>
 #include <Math/polar3d.hxx>
 #include <Math/vector.hxx>
+#include <Weather/weather.h>
 
 #include "material.hxx"
 #include "obj.hxx"
@@ -54,6 +55,8 @@
 
 #define FG_LOCAL_X_Y         81  // max(o->tile_diameter) ** 2
 
+#define FG_SQUARE( X ) ( (X) * (X) )
+
 
 // closest (potentially viewable) tiles, centered on current tile.
 // This is an array of pointers to cache indexes.
@@ -95,18 +98,19 @@ void fgTileMgrLoadTile( fgBUCKET *p, int *index) {
 int fgTileMgrUpdate( void ) {
     fgTILECACHE *c;
     fgFLIGHT *f;
-    fgOPTIONS *o;
     fgBUCKET p1, p2;
     static fgBUCKET p_last = {-1000, 0, 0, 0};
+    int tile_diameter;
     int i, j, dw, dh;
 
     c = &global_tile_cache;
     f = current_aircraft.flight;
-    o = &current_options;
+
+    tile_diameter = current_options.get_tile_diameter();
 
     fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p1);
-    dw = o->tile_diameter / 2;
-    dh = o->tile_diameter / 2;
+    dw = tile_diameter / 2;
+    dh = tile_diameter / 2;
 
     if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) &&
         (p1.x == p_last.x) && (p1.y == p_last.y) ) {
@@ -120,16 +124,16 @@ int fgTileMgrUpdate( void ) {
        fgPrintf( FG_TERRAIN, FG_INFO, "  Updating Tile list for %d,%d %d,%d\n",
                  p1.lon, p1.lat, p1.x, p1.y);
        fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
-                 o->tile_diameter * o->tile_diameter);
+                 tile_diameter * tile_diameter);
 
        // wipe/initialize tile cache
        c->Init();
 
        // build the local area list and update cache
-       for ( j = 0; j < o->tile_diameter; j++ ) {
-           for ( i = 0; i < o->tile_diameter; i++ ) {
+       for ( j = 0; j < tile_diameter; j++ ) {
+           for ( i = 0; i < tile_diameter; i++ ) {
                fgBucketOffset(&p1, &p2, i - dw, j - dh);
-               fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + i]);
+               fgTileMgrLoadTile(&p2, &tiles[(j*tile_diameter) + i]);
            }
        }
     } else {
@@ -146,58 +150,58 @@ int fgTileMgrUpdate( void ) {
        if ( (p1.lon > p_last.lon) ||
             ( (p1.lon == p_last.lon) && (p1.x > p_last.x) ) ) {
            fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
-                     o->tile_diameter);
-           for ( j = 0; j < o->tile_diameter; j++ ) {
+                     tile_diameter);
+           for ( j = 0; j < tile_diameter; j++ ) {
                // scrolling East
-               for ( i = 0; i < o->tile_diameter - 1; i++ ) {
-                   tiles[(j*o->tile_diameter) + i] = 
-                       tiles[(j*o->tile_diameter) + i + 1];
+               for ( i = 0; i < tile_diameter - 1; i++ ) {
+                   tiles[(j*tile_diameter) + i] = 
+                       tiles[(j*tile_diameter) + i + 1];
                }
                // load in new column
                fgBucketOffset(&p_last, &p2, dw + 1, j - dh);
-               fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + 
-                                            o->tile_diameter - 1]);
+               fgTileMgrLoadTile(&p2, &tiles[(j*tile_diameter) + 
+                                            tile_diameter - 1]);
            }
        } else if ( (p1.lon < p_last.lon) ||
                    ( (p1.lon == p_last.lon) && (p1.x < p_last.x) ) ) {
            fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
-                     o->tile_diameter);
-           for ( j = 0; j < o->tile_diameter; j++ ) {
+                     tile_diameter);
+           for ( j = 0; j < tile_diameter; j++ ) {
                // scrolling West
-               for ( i = o->tile_diameter - 1; i > 0; i-- ) {
-                   tiles[(j*o->tile_diameter) + i] = 
-                       tiles[(j*o->tile_diameter) + i - 1];
+               for ( i = tile_diameter - 1; i > 0; i-- ) {
+                   tiles[(j*tile_diameter) + i] = 
+                       tiles[(j*tile_diameter) + i - 1];
                }
                // load in new column
                fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
-               fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + 0]);
+               fgTileMgrLoadTile(&p2, &tiles[(j*tile_diameter) + 0]);
            }
        }
 
        if ( (p1.lat > p_last.lat) ||
             ( (p1.lat == p_last.lat) && (p1.y > p_last.y) ) ) {
            fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
-                     o->tile_diameter);
-           for ( i = 0; i < o->tile_diameter; i++ ) {
+                     tile_diameter);
+           for ( i = 0; i < tile_diameter; i++ ) {
                // scrolling North
-               for ( j = 0; j < o->tile_diameter - 1; j++ ) {
-                   tiles[(j * o->tile_diameter) + i] =
-                       tiles[((j+1) * o->tile_diameter) + i];
+               for ( j = 0; j < tile_diameter - 1; j++ ) {
+                   tiles[(j * tile_diameter) + i] =
+                       tiles[((j+1) * tile_diameter) + i];
                }
                // load in new column
                fgBucketOffset(&p_last, &p2, i - dw, dh + 1);
-               fgTileMgrLoadTile(&p2, &tiles[((o->tile_diameter-1) * 
-                                              o->tile_diameter) + i]);
+               fgTileMgrLoadTile(&p2, &tiles[((tile_diameter-1) * 
+                                              tile_diameter) + i]);
            }
        } else if ( (p1.lat < p_last.lat) ||
                    ( (p1.lat == p_last.lat) && (p1.y < p_last.y) ) ) {
            fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
-                     o->tile_diameter);
-           for ( i = 0; i < o->tile_diameter; i++ ) {
+                     tile_diameter);
+           for ( i = 0; i < tile_diameter; i++ ) {
                // scrolling South
-               for ( j = o->tile_diameter - 1; j > 0; j-- ) {
-                   tiles[(j * o->tile_diameter) + i] = 
-                       tiles[((j-1) * o->tile_diameter) + i];
+               for ( j = tile_diameter - 1; j > 0; j-- ) {
+                   tiles[(j * tile_diameter) + i] = 
+                       tiles[((j-1) * tile_diameter) + i];
                }
                // load in new column
                fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
@@ -214,14 +218,16 @@ int fgTileMgrUpdate( void ) {
 
 
 // Calculate shortest distance from point to line
-static double point_line_dist( fgPoint3d *tc, fgPoint3d *vp, MAT3vec d) {
+static double point_line_dist_squared( fgPoint3d *tc, fgPoint3d *vp, 
+                                      MAT3vec d )
+{
     MAT3vec p, p0;
     double dist;
 
     p[0] = tc->x; p[1] = tc->y; p[2] = tc->z;
     p0[0] = vp->x; p0[1] = vp->y; p0[2] = vp->z;
 
-    dist = fgPointLine(p, p0, d);
+    dist = fgPointLineSquared(p, p0, d);
 
     // printf("dist = %.2f\n", dist);
 
@@ -248,6 +254,11 @@ static int viewable( fgPoint3d *cp, double radius ) {
        return(0);
     }
 
+    // Check far clip plane
+    if ( eye[2] + radius < -current_weather.visibility ) {
+       return(0);
+    }
+
     // check right clip plane (from eye perspective)
     // y = m * (x - x0) = equation of a line intercepting X axis at x0
     x1 = v->cos_fov_x * radius;
@@ -311,11 +322,115 @@ static int viewable( fgPoint3d *cp, double radius ) {
 }
 
 
+// Determine scenery altitude.  Normally this just happens when we
+// render the scene, but we'd also like to be able to do this
+// explicitely.  lat & lon are in radians.  abs_view_pos in meters.
+// Returns result in meters.
+double fgTileMgrCurElev( double lon, double lat, fgPoint3d *abs_view_pos ) {
+    fgTILECACHE *c;
+    fgTILE *t;
+    // fgVIEW *v;
+    fgFRAGMENT *frag_ptr;
+    fgBUCKET p;
+    fgPoint3d earth_center, result;
+    fgPoint3d pp;
+    MAT3vec local_up;
+    list < fgFRAGMENT > :: iterator current;
+    list < fgFRAGMENT > :: iterator last;
+    double dist, min_dist, lat_geod, alt, sea_level_r;
+    double x, y, z;
+    int index, tile_diameter, i;
+
+    c = &global_tile_cache;
+    // v = &current_view;
+
+    local_up[0] = abs_view_pos->x;
+    local_up[1] = abs_view_pos->y;
+    local_up[2] = abs_view_pos->z;
+
+    tile_diameter = current_options.get_tile_diameter();
+
+    // Find current translation offset
+    fgBucketFind(lon * RAD_TO_DEG, lat * RAD_TO_DEG, &p);
+    index = c->Exists(&p);
+    t = c->GetTile(index);
+
+    scenery.next_center.x = t->center.x;
+    scenery.next_center.y = t->center.y;
+    scenery.next_center.z = t->center.z;
+
+    earth_center.x = 0.0;
+    earth_center.y = 0.0;
+    earth_center.z = 0.0;
+
+    fgPrintf( FG_TERRAIN, FG_DEBUG, 
+             "Pos = (%.2f, %.2f) Current bucket = %d %d %d %d  Index = %ld\n", 
+             lon * RAD_TO_DEG, lat * RAD_TO_DEG,
+             p.lon, p.lat, p.x, p.y, fgBucketGenIndex(&p) );
+
+    // traverse the potentially viewable tile list
+    for ( i = 0; i < (tile_diameter * tile_diameter); i++ ) {
+       index = tiles[i];
+       // fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index);
+       t = c->GetTile(index);
+
+       // calculate tile offset
+       x = (t->offset.x = t->center.x - scenery.center.x);
+       y = (t->offset.y = t->center.y - scenery.center.y);
+       z = (t->offset.z = t->center.z - scenery.center.z);
+
+       // calc current terrain elevation calculate distance from
+       // vertical tangent line at current position to center of
+       // tile.
+       
+       /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
+              point_line_dist_squared(&(t->offset), &(v->view_pos), 
+              v->local_up), t->bounding_radius); */
+
+       dist = point_line_dist_squared( &(t->center), abs_view_pos, 
+                                       local_up );
+       if ( dist < FG_SQUARE(t->bounding_radius) ) {
+
+           // traverse fragment list for tile
+           current = t->fragment_list.begin();
+           last = t->fragment_list.end();
+
+           while ( current != last ) {
+               frag_ptr = &(*current);
+               current++;
+               /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
+                      point_line_dist_squared( &(frag_ptr->center), 
+                                       &abs_view_pos), local_up),
+                      frag_ptr->bounding_radius); */
+
+               dist = point_line_dist_squared( &(frag_ptr->center), 
+                                       abs_view_pos, local_up);
+               if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
+                   if ( frag_ptr->intersect( abs_view_pos, 
+                                             &earth_center, 0, &result ) ) {
+                       // compute geocentric coordinates of tile center
+                       pp = fgCartToPolar3d(result);
+                       // convert to geodetic coordinates
+                       fgGeocToGeod(pp.lat, pp.radius, &lat_geod, 
+                                    &alt, &sea_level_r);
+                       // printf("alt = %.2f\n", alt);
+                       // exit since we found an intersection
+                       return(alt);
+                   }
+               }
+           }
+       }
+    }
+
+    printf("no terrain intersection found\n");
+    return(0);
+}
+
+
 // Render the local tiles
 void fgTileMgrRender( void ) {
     fgTILECACHE *c;
     fgFLIGHT *f;
-    fgOPTIONS *o;
     fgTILE *t, *last_tile_ptr;
     fgVIEW *v;
     fgBUCKET p;
@@ -329,15 +444,19 @@ void fgTileMgrRender( void ) {
     list < fgFRAGMENT > :: iterator current;
     list < fgFRAGMENT > :: iterator last;
     int i, j, size;
+    int tile_diameter, textures;
     int index;
     int culled = 0;
     int drawn = 0;
+    int total_faces = 0;
 
     c = &global_tile_cache;
     f = current_aircraft.flight;
-    o = &current_options;
     v = &current_view;
 
+    tile_diameter = current_options.get_tile_diameter();
+    textures = current_options.get_textures();
+
     // Find current translation offset
     fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p);
     index = c->Exists(&p);
@@ -362,15 +481,15 @@ void fgTileMgrRender( void ) {
 
     // Pass 1
     // traverse the potentially viewable tile list
-    for ( i = 0; i < (o->tile_diameter * o->tile_diameter); i++ ) {
+    for ( i = 0; i < (tile_diameter * tile_diameter); i++ ) {
        index = tiles[i];
        // fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index);
        t = c->GetTile(index);
 
        // calculate tile offset
-       x = t->offset.x = t->center.x - scenery.center.x;
-       y = t->offset.y = t->center.y - scenery.center.y;
-       z = t->offset.z = t->center.z - scenery.center.z;
+       x = (t->offset.x = t->center.x - scenery.center.x);
+       y = (t->offset.y = t->center.y - scenery.center.y);
+       z = (t->offset.z = t->center.z - scenery.center.z);
 
        m = t->model_view;
        for ( j = 0; j < 16; j++ ) {
@@ -388,12 +507,13 @@ void fgTileMgrRender( void ) {
        // calculate distance from vertical tangent line at
        // current position to center of tile.
        
-       /* printf("distance = %.2f, bounding radius = %.2f\n", 
-              point_line_dist(&(t->offset), &(v->view_pos), v->local_up),
-              t->bounding_radius); */
+       /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
+              point_line_dist_squared(&(t->offset), &(v->view_pos), 
+              v->local_up), t->bounding_radius); */
 
-       dist = point_line_dist(&(t->center), &(v->abs_view_pos), v->local_up);
-       if ( dist < t->bounding_radius ) {
+       dist = point_line_dist_squared( &(t->center), &(v->abs_view_pos), 
+                                       v->local_up );
+       if ( dist < FG_SQUARE(t->bounding_radius) ) {
 
            // traverse fragment list for tile
            current = t->fragment_list.begin();
@@ -402,14 +522,14 @@ void fgTileMgrRender( void ) {
            while ( current != last ) {
                frag_ptr = &(*current);
                current++;
-               /* printf("distance = %.2f, bounding radius = %.2f\n", 
-                      point_line_dist( &(frag_ptr->center), 
+               /* printf("distance squared = %.2f, bounding radius = %.2f\n", 
+                      point_line_dist_squared( &(frag_ptr->center), 
                                        &(v->abs_view_pos), v->local_up),
                       frag_ptr->bounding_radius); */
 
-               dist = point_line_dist( &(frag_ptr->center), 
+               dist = point_line_dist_squared( &(frag_ptr->center), 
                                        &(v->abs_view_pos), v->local_up);
-               if ( dist <= frag_ptr->bounding_radius ) {
+               if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
                    if ( frag_ptr->intersect( &(v->abs_view_pos), 
                                              &earth_center, 0, &result ) ) {
                        // compute geocentric coordinates of tile center
@@ -453,7 +573,7 @@ void fgTileMgrRender( void ) {
                        // frag_ptr->tile_offset.y = t->offset.y;
                        // frag_ptr->tile_offset.z = t->offset.z;
 
-                       mtl_ptr = (fgMATERIAL *)(frag_ptr->material_ptr);
+                       mtl_ptr = frag_ptr->material_ptr;
                        // printf(" lookup = %s\n", mtl_ptr->texture_name);
                        if ( mtl_ptr->list_size < FG_MAX_MATERIAL_FRAGS ) {
                            mtl_ptr->list[mtl_ptr->list_size] = frag_ptr;
@@ -507,7 +627,7 @@ void fgTileMgrRender( void ) {
 
        size = mtl_ptr->list_size;
        if ( size > 0 ) {
-           if ( o->textures ) {
+           if ( textures ) {
 #ifdef GL_VERSION_1_1
                xglBindTexture(GL_TEXTURE_2D, mtl_ptr->texture_id);
 #elif GL_EXT_texture_object
@@ -525,12 +645,16 @@ void fgTileMgrRender( void ) {
            for ( i = 0; i < size; i++ ) {
                frag_ptr = mtl_ptr->list[i];
                
+               // count up the number of polygons we are drawing in
+               // case someone is interested.
+               total_faces += frag_ptr->num_faces;
+
                if ( frag_ptr->tile_ptr == last_tile_ptr ) {
                    // same tile as last time, no transform necessary
                } else {
                    // new tile, new translate
                    // xglLoadMatrixf( frag_ptr->matrix );
-                   t = (fgTILE *)(frag_ptr->tile_ptr);
+                   t = frag_ptr->tile_ptr;
                    xglLoadMatrixd(t->model_view );
                }
            
@@ -538,7 +662,7 @@ void fgTileMgrRender( void ) {
                // printf("  display_list = %d\n", frag_ptr->display_list);
                xglCallList(frag_ptr->display_list);
 
-               last_tile_ptr = (fgTILE *)(frag_ptr->tile_ptr);
+               last_tile_ptr = frag_ptr->tile_ptr;
            }
        }
 
@@ -546,10 +670,44 @@ void fgTileMgrRender( void ) {
     }
 
     xglPopMatrix();
+
+    fgPrintf( FG_TERRAIN, FG_DEBUG, "Rendered %d polygons this frame.\n", 
+             total_faces);
 }
 
 
 // $Log$
+// Revision 1.30  1998/08/22 14:49:59  curt
+// Attempting to iron out seg faults and crashes.
+// Did some shuffling to fix a initialization order problem between view
+// position, scenery elevation.
+//
+// Revision 1.29  1998/08/20 15:12:06  curt
+// Used a forward declaration of classes fgTILE and fgMATERIAL to eliminate
+// the need for "void" pointers and casts.
+// Quick hack to count the number of scenery polygons that are being drawn.
+//
+// Revision 1.28  1998/08/12 21:13:06  curt
+// material.cxx: don't load textures if they are disabled
+// obj.cxx: optimizations from Norman Vine
+// tile.cxx: minor tweaks
+// tile.hxx: addition of num_faces
+// tilemgr.cxx: minor tweaks
+//
+// Revision 1.27  1998/07/24 21:42:09  curt
+// material.cxx: whups, double method declaration with no definition.
+// obj.cxx: tweaks to avoid errors in SGI's CC.
+// tile.cxx: optimizations by Norman Vine.
+// tilemgr.cxx: optimizations by Norman Vine.
+//
+// Revision 1.26  1998/07/20 12:51:26  curt
+// Added far clip plane to fragment clipping calculations and tie this to
+// weather->visibility.  This way you can increase framerates by increasing
+// for and lowering visibility.
+//
+// Revision 1.25  1998/07/13 21:02:01  curt
+// Wrote access functions for current fgOPTIONS.
+//
 // Revision 1.24  1998/07/12 03:18:29  curt
 // Added ground collision detection.  This involved:
 // - saving the entire vertex list for each tile with the tile records.