]> git.mxchange.org Git - flightgear.git/commitdiff
Attempting to iron out seg faults and crashes.
authorcurt <curt>
Sat, 22 Aug 1998 14:49:55 +0000 (14:49 +0000)
committercurt <curt>
Sat, 22 Aug 1998 14:49:55 +0000 (14:49 +0000)
Did some shuffling to fix a initialization order problem between view
position, scenery elevation.

FDM/flight.c
LaRCsim/atmos_62.c
LaRCsim/ls_interface.c
Main/GLUTmain.cxx
Main/fg_init.cxx
Scenery/tile.cxx
Scenery/tile.hxx
Scenery/tilemgr.cxx
Scenery/tilemgr.hxx

index 591f9e8250ebecd43b0e42a60fc03589a6263a10..455417c87842bf12497cd1fd007f104924a09e5d 100644 (file)
@@ -37,6 +37,7 @@ fgFLIGHT cur_flight_params;
 
 /* Initialize the flight model parameters */
 int fgFlightModelInit(int model, fgFLIGHT *f, double dt) {
+    double save_alt = 0.0;
     int result;
 
     fgPrintf(FG_FLIGHT,FG_INFO,"Initializing flight model\n");
@@ -44,10 +45,21 @@ int fgFlightModelInit(int model, fgFLIGHT *f, double dt) {
     if ( model == FG_SLEW ) {
        // fgSlewInit(dt);
     } else if ( model == FG_LARCSIM ) {
+       /* lets try to avoid really screwing up the LaRCsim model */
+       if ( FG_Altitude < -9000 ) {
+           save_alt = FG_Altitude;
+           FG_Altitude = 0;
+       }
+
        fgFlight_2_LaRCsim(f);  /* translate FG to LaRCsim structure */
        fgLaRCsimInit(dt);
        fgPrintf( FG_FLIGHT, FG_INFO, "FG pos = %.2f\n", FG_Latitude );
        fgLaRCsim_2_Flight(f);  /* translate LaRCsim back to FG structure */
+
+       /* but lets restore our original bogus altitude when we are done */
+       if ( save_alt < -9000 ) {
+           FG_Altitude = save_alt;
+       }
     } else {
        fgPrintf( FG_FLIGHT, FG_WARN,
                  "Unimplemented flight model == %d\n", model );
@@ -101,9 +113,14 @@ int fgFlightModelSetAltitude(int model, fgFLIGHT *f, double alt_meters) {
 
 
 /* $Log$
-/* Revision 1.15  1998/07/30 23:44:36  curt
-/* Beginning to add support for multiple flight models.
+/* Revision 1.16  1998/08/22 14:49:55  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.15  1998/07/30 23:44:36  curt
+ * Beginning to add support for multiple flight models.
+ *
  * Revision 1.14  1998/07/12 03:08:27  curt
  * Added fgFlightModelSetAltitude() to force the altitude to something
  * other than the current altitude.  LaRCsim doesn't let you do this by just
index d50c98ad32f34e1c16575f26cd93353269c7428e..f2863a82a009fddfcdce76f73053b35f7e686528 100644 (file)
@@ -213,7 +213,18 @@ void ls_atmos( SCALAR altitude, SCALAR * sigma, SCALAR * v_sound,
        { 238000.,       1.18020E-07,    9.52550E+02,    2.29613E-16,   -1.45786E-08    },
        { 240000.,       1.08270E-07,    9.47120E+02,    0.00000E+00,    0.00000E+00    }
     };
-    
+
+    /* for purposes of doing the table lookup, force the incoming
+       altitude to be >= 0 */
+
+    // printf("altitude = %.2f\n", altitude);
+
+    if ( altitude < 0.0 ) {
+       altitude = 0.0;
+    }
+
+    // printf("altitude = %.2f\n", altitude);
+
     index = (int)( altitude / 2000 );
     if (index > (MAX_ALT_INDEX-2))
     {
@@ -239,6 +250,7 @@ void ls_atmos( SCALAR altitude, SCALAR * sigma, SCALAR * v_sound,
     if (altitude < HLEV)    /* BUG - these curve fits are only good to about 75000 ft */
       {
        t_amb_r = 1. - 6.875e-6*altitude;
+       // printf("index = %d  t_amb_r = %.2f\n", index, t_amb_r);
        p_amb_r = pow( t_amb_r, 5.256 );
       }
     else
index 06c025de2d9e0c0d38d6386ed704e2eb8fa9e225..4017b7c935fe5efff4397a9fa39be1f92e9628b4 100644 (file)
@@ -549,6 +549,7 @@ int fgLaRCsimInit(double dt) {
 
 /* Run an iteration of the EOM (equations of motion) */
 int fgLaRCsimUpdate(fgFLIGHT *f, int multiloop) {
+    double save_alt = 0.0;
     int        i;
 
     if (speedup > 0) {
@@ -560,8 +561,14 @@ int fgLaRCsimUpdate(fgFLIGHT *f, int multiloop) {
        
        // run Autopilot system
        // fgPrintf( FG_ALL, FG_BULK, "Attempting autopilot run\n");
-                     
        fgAPRun();
+
+       /* lets try to avoid really screwing up the LaRCsim model */
+       if ( FG_Altitude < -9000 ) {
+           save_alt = FG_Altitude;
+           FG_Altitude = 0;
+       }
+
        // translate FG to LaRCsim structure
        fgFlight_2_LaRCsim(f);
        // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
@@ -579,6 +586,11 @@ int fgLaRCsimUpdate(fgFLIGHT *f, int multiloop) {
        // autopilot (and the rest of the sim can use the updated
        // values
        fgLaRCsim_2_Flight(f);
+
+       /* but lets restore our original bogus altitude when we are done */
+       if ( save_alt < -9000 ) {
+           FG_Altitude = save_alt;
+       }
     }
 
     return(1);
@@ -948,6 +960,11 @@ int ls_ForceAltitude(double alt_feet) {
 /* Flight Gear Modification Log
  *
  * $Log$
+ * Revision 1.21  1998/08/22 14:49:56  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.20  1998/07/12 03:11:03  curt
  * Removed some printf()'s.
  * Fixed the autopilot integration so it should be able to update it's control
index 613ea59d1c9956709bb68b7e94b088bafe8f09d0..a3a1761244715e52fe1d08fae3a6af29a585b4ea 100644 (file)
@@ -253,7 +253,7 @@ static void fgRenderFrame( void ) {
        //        FG_Altitude * FEET_TO_METER);
     
        // this is just a temporary hack, to make me understand Pui
-       timerText -> setLabel (ctime (&t->cur_time));
+       // timerText -> setLabel (ctime (&t->cur_time));
        // end of hack
 
        // update view volume parameters
@@ -872,6 +872,11 @@ int main( int argc, char **argv ) {
 
 
 // $Log$
+// Revision 1.46  1998/08/22 14:49:56  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.45  1998/08/20 20:32:31  curt
 // Reshuffled some of the code in and around views.[ch]xx
 //
index 21c333911631deea9925646d2ff626ab1b03d283..3a1726fbc80a203c8ed7932bf830433605bfdb5f 100644 (file)
@@ -52,7 +52,9 @@
 #include <Cockpit/cockpit.hxx>
 #include <Debug/fg_debug.h>
 #include <Joystick/joystick.h>
+#include <Math/fg_geodesy.h>
 #include <Math/fg_random.h>
+#include <Math/polar3d.hxx>
 #include <Scenery/scenery.hxx>
 #include <Scenery/tilemgr.hxx>
 #include <Time/event.hxx>
@@ -153,6 +155,33 @@ int fgInitGeneral( void ) {
 }
 
 
+// convert a geodetic point lon(radians), lat(radians), elev(meter) to
+// a cartesian point
+fgPoint3d geod_to_cart(double geod[3]) {
+    fgPoint3d cp;
+    fgPoint3d pp;
+    double gc_lon, gc_lat, sl_radius;
+
+    // printf("A geodetic point is (%.2f, %.2f, %.2f)\n", 
+    //        geod[0], geod[1], geod[2]);
+
+    gc_lon = geod[0];
+    fgGeodToGeoc(geod[1], geod[2], &sl_radius, &gc_lat);
+
+    // printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon, 
+    //        gc_lat, sl_radius+geod[2]);
+
+    pp.lon = gc_lon;
+    pp.lat = gc_lat;
+    pp.radius = sl_radius + geod[2];
+    cp = fgPolarToCart3d(pp);
+    
+    // printf("A cart point is (%.8f, %.8f, %.8f)\n", cp.x, cp.y, cp.z);
+
+    return(cp);
+}
+
+
 // This is the top level init routine which calls all the other
 // initialization routines.  If you are adding a subsystem to flight
 // gear, its initialization call should located in this routine.
@@ -162,6 +191,8 @@ int fgInitSubsystems( void ) {
     fgLIGHT *l;
     fgTIME *t;
     fgVIEW *v;
+    double geod_pos[3];
+    fgPoint3d abs_view_pos;
 
     l = &cur_light_params;
     t = &cur_time_params;
@@ -173,18 +204,57 @@ int fgInitSubsystems( void ) {
     // seed the random number generater
     fg_srandom();
 
-    // The following section sets up the flight model EOM parameters
-    // and should really be read in from one or more files.
-
-    // Must happen before any of the flight model or control
-    // parameters are set
-
+    // allocates structures so must happen before any of the flight
+    // model or control parameters are set
     fgAircraftInit();   // In the future this might not be the case.
     f = current_aircraft.flight;
 
     // set the initial position
     fgInitPosition();
 
+    // Initialize the Scenery Management subsystem
+    if ( fgSceneryInit() ) {
+       // Scenery initialized ok.
+    } else {
+       fgPrintf( FG_GENERAL, FG_EXIT, "Error in Scenery initialization!\n" );
+    }
+
+    if( fgTileMgrInit() ) {
+       // Load the local scenery data
+       fgTileMgrUpdate();
+    } else {
+       fgPrintf( FG_GENERAL, FG_EXIT, 
+                 "Error in Tile Manager initialization!\n" );
+    }
+
+    // calculalate a cartesian point somewhere along the line between
+    // the center of the earth and our view position
+    geod_pos[0] = FG_Longitude;
+    geod_pos[1] = FG_Latitude;
+    // doesn't have to be the exact elevation (this is good because we
+    // don't know it yet :-)
+    geod_pos[2] = 0;
+    abs_view_pos = geod_to_cart(geod_pos);
+
+    // Calculate ground elevation at starting point
+    scenery.cur_elev = 
+       fgTileMgrCurElev( FG_Longitude, FG_Latitude, &abs_view_pos );
+    FG_Runway_altitude = scenery.cur_elev * METER_TO_FEET;
+
+    // Reset our altitude if we are below ground
+    if ( FG_Altitude < FG_Runway_altitude + 3.758099) {
+       FG_Altitude = FG_Runway_altitude + 3.758099;
+    }
+
+    fgPrintf( FG_GENERAL, FG_INFO,
+             "Updated position (after elevation adj): (%.4f, %.4f, %.2f)\n",
+             FG_Latitude * RAD_TO_DEG, FG_Longitude * RAD_TO_DEG,
+             FG_Altitude * FEET_TO_METER);
+    // end of thing that I just stuck in that I should probably move
+               
+    // The following section sets up the flight model EOM parameters
+    // and should really be read in from one or more files.
+
     // Initial Velocity
     FG_V_north = 0.0;   //  7.287719E+00
     FG_V_east  = 0.0;   //  1.521770E+03
@@ -281,35 +351,6 @@ int fgInitSubsystems( void ) {
     // Initialize the "sky"
     fgSkyInit();
 
-    // Initialize the Scenery Management subsystem
-    if ( fgSceneryInit() ) {
-       // Scenery initialized ok.
-    } else {
-       fgPrintf( FG_GENERAL, FG_EXIT, "Error in Scenery initialization!\n" );
-    }
-
-    if( fgTileMgrInit() ) {
-       // Load the local scenery data
-       fgTileMgrUpdate();
-    } else {
-       fgPrintf( FG_GENERAL, FG_EXIT, 
-                 "Error in Tile Manager initialization!\n" );
-    }
-
-    // I'm just sticking this here for now, it should probably move
-    // eventually
-    scenery.cur_elev = FG_Runway_altitude * FEET_TO_METER;
-
-    if ( FG_Altitude < FG_Runway_altitude + 3.758099) {
-       FG_Altitude = FG_Runway_altitude + 3.758099;
-    }
-
-    fgPrintf( FG_GENERAL, FG_INFO,
-             "Updated position (after elevation adj): (%.4f, %.4f, %.2f)\n",
-             FG_Latitude * RAD_TO_DEG, FG_Longitude * RAD_TO_DEG,
-             FG_Altitude * FEET_TO_METER);
-    // end of thing that I just stuck in that I should probably move
-               
     // Initialize the flight model subsystem data structures base on
     // above values
 
@@ -347,6 +388,11 @@ int fgInitSubsystems( void ) {
 
 
 // $Log$
+// Revision 1.31  1998/08/22 14:49:57  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.30  1998/08/20 20:32:33  curt
 // Reshuffled some of the code in and around views.[ch]xx
 //
index b5a87245f3d14d46b9c57b64d843ed92ff09657b..5f0a97c1c3ebc3ad1f1d77312313f6303d4d5059 100644 (file)
@@ -157,8 +157,8 @@ static double fg_max3 (double a, double b, double c)
 // test if line intesects with this fragment.  p0 and p1 are the two
 // line end points of the line.  If side_flag is true, check to see
 // that end points are on opposite sides of face.  Returns 1 if it
-// does, 0 otherwise.  If it intesects, result is the point of
-// intersection
+// intersection found, 0 otherwise.  If it intesects, result is the
+// point of intersection
 
 int fgFRAGMENT::intersect( fgPoint3d *end0, fgPoint3d *end1, int side_flag,
                           fgPoint3d *result)
@@ -448,6 +448,11 @@ fgTILE::~fgTILE ( void ) {
 
 
 // $Log$
+// Revision 1.8  1998/08/22 14:49:58  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.7  1998/08/20 15:12:05  curt
 // Used a forward declaration of classes fgTILE and fgMATERIAL to eliminate
 // the need for "void" pointers and casts.
index 925090995dcac6ba0a0f3133db043abb0e28c09c..dff5e12f8141db136fa2223a60cdbab870919a9b 100644 (file)
@@ -117,8 +117,8 @@ public:
     // test if line intesects with this fragment.  p0 and p1 are the
     // two line end points of the line.  If side_flag is true, check
     // to see that end points are on opposite sides of face.  Returns
-    // 1 if it does, 0 otherwise.  If it intesects, result is the
-    // point of intersection
+    // 1 if it intersection found, 0 otherwise.  If it intesects,
+    // result is the point of intersection
     int intersect( fgPoint3d *end0, fgPoint3d *end1, int side_flag,
                   fgPoint3d *result);
 
@@ -171,6 +171,11 @@ public:
 
 
 // $Log$
+// Revision 1.17  1998/08/22 14:49:58  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.16  1998/08/22 02:01:34  curt
 // increased fragment list size.
 //
index 3f715114c4946e4e465131f1c5da3f5a0f6ad787..fa70e32c0c15e4d890df07fe71b043be913a9ec1 100644 (file)
@@ -322,6 +322,111 @@ 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;
@@ -572,6 +677,11 @@ void fgTileMgrRender( void ) {
 
 
 // $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.
index 29d7e64f3efd740d91ceae1922063bc31a1661ef..e927874513100b5768207ce342510035fe69fafc 100644 (file)
@@ -40,6 +40,13 @@ int fgTileMgrInit( void );
 int fgTileMgrUpdate( void );
 
 
+// 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 );
+
+
 // Render the local tiles --- hack, hack, hack
 void fgTileMgrRender( void );
 
@@ -48,6 +55,11 @@ void fgTileMgrRender( void );
 
 
 // $Log$
+// Revision 1.3  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.2  1998/05/20 20:53:56  curt
 // Moved global ref point and radius (bounding sphere info, and offset) to
 // data file rather than calculating it on the fly.