]> git.mxchange.org Git - flightgear.git/blobdiff - Main/views.cxx
Modifications to incorporate Jon S. Berndts flight model code.
[flightgear.git] / Main / views.cxx
index e9885efa4ab47d2392baa1b6be1e1da257540002..8bad74132b0a511844b759d66039909a930ad417 100644 (file)
@@ -28,6 +28,7 @@
 #endif
 
 #include <Aircraft/aircraft.hxx>
+#include <Cockpit/panel.hxx>
 #include <Debug/logstream.hxx>
 #include <Include/fg_constants.h>
 #include <Math/mat3.h>
 #include "views.hxx"
 
 
+// temporary (hopefully) hack
+static int panel_hist = 0;
+
+
+// specify code paths ... these are done as variable rather than
+// #define's because down the road we may want to choose between them
+// on the fly for different flight models ... this way magic carpet
+// and external modes wouldn't need to recreate the LaRCsim matrices
+// themselves.
+
+static const bool use_larcsim_local_to_body = false;
+
+
 // This is a record containing current view parameters
-fgVIEW current_view;
+FGView current_view;
 
 
 // Constructor
-fgVIEW::fgVIEW( void ) {
+FGView::FGView( void ) {
 }
 
 
 // Initialize a view structure
-void fgVIEW::Init( void ) {
+void FGView::Init( void ) {
     FG_LOG( FG_VIEW, FG_INFO, "Initializing View parameters" );
 
     view_offset = 0.0;
     goal_view_offset = 0.0;
 
-    winWidth = 640;  // FG_DEFAULT_WIN_WIDTH
-    winHeight = 480; // FG_DEFAULT_WIN_HEIGHT
+    winWidth = current_options.get_xsize();
+    winHeight = current_options.get_ysize();
     win_ratio = (double) winWidth / (double) winHeight;
-    update_fov = true;
+    force_update_fov_math();
 }
 
 
-// Update the field of view parameters
-void fgVIEW::UpdateFOV( fgOPTIONS *o ) {
+// Update the field of view coefficients
+void FGView::UpdateFOV( const fgOPTIONS& o ) {
     double fov, theta_x, theta_y;
 
-    fov = o->get_fov();
+    fov = o.get_fov();
        
     // printf("win_ratio = %.2f\n", win_ratio);
     // calculate sin() and cos() of fov / 2 in X direction;
@@ -100,7 +114,7 @@ void fgVIEW::UpdateFOV( fgOPTIONS *o ) {
 // Basically, this is a modified version of the Mesa gluLookAt()
 // function that's been modified slightly so we can capture the
 // result before sending it off to OpenGL land.
-void fgVIEW::LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
+void FGView::LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
                     GLdouble centerx, GLdouble centery, GLdouble centerz,
                     GLdouble upx, GLdouble upy, GLdouble upz ) {
     GLdouble *m;
@@ -146,7 +160,7 @@ void fgVIEW::LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
     if (mag) {
        x[0] /= mag;
        x[1] /= mag;
-      x[2] /= mag;
+       x[2] /= mag;
     }
 
     mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
@@ -183,29 +197,28 @@ void fgVIEW::LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
 
 
 // Update the view volume, position, and orientation
-void fgVIEW::UpdateViewParams( void ) {
-    fgFLIGHT *f;
-    fgLIGHT *l;
-
-    f = current_aircraft.flight;
-    l = &cur_light_params;
+void FGView::UpdateViewParams( void ) {
+    FGInterface *f = current_aircraft.fdm_state;
 
     UpdateViewMath(f);
     UpdateWorldToEye(f);
+    
+    if ((current_options.get_panel_status() != panel_hist) &&                          (current_options.get_panel_status()))
+    {
+       fgPanelReInit( 0, 0, 1024, 768);
+    }
+
+    if ( ! current_options.get_panel_status() ) {
+       xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
+    } else {
+       xglViewport(0, (GLint)((winHeight)*0.5768), (GLint)(winWidth), 
+                    (GLint)((winHeight)*0.4232) );
+    }
 
-    // if (!o->panel_status) {
-    // xglViewport( 0, (GLint)((winHeight) / 2 ) , 
-    // (GLint)(winWidth), (GLint)(winHeight) / 2 );
-    // Tell GL we are about to modify the projection parameters
-    // xglMatrixMode(GL_PROJECTION);
-    // xglLoadIdentity();
-    // gluPerspective(o->fov, win_ratio / 2.0, 1.0, 100000.0);
-    // } else {
-    xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
     // Tell GL we are about to modify the projection parameters
     xglMatrixMode(GL_PROJECTION);
     xglLoadIdentity();
-    if ( FG_Altitude * FEET_TO_METER - scenery.cur_elev > 10.0 ) {
+    if ( f->get_Altitude() * FEET_TO_METER - scenery.cur_elev > 10.0 ) {
        gluPerspective(current_options.get_fov(), win_ratio, 10.0, 100000.0);
     } else {
        gluPerspective(current_options.get_fov(), win_ratio, 0.5, 100000.0);
@@ -244,21 +257,20 @@ void fgVIEW::UpdateViewParams( void ) {
               view_pos.z() + surface_south[2],
               view_up[0], view_up[1], view_up[2]); */
 
-    // set the sun position
-    xglLightfv( GL_LIGHT0, GL_POSITION, l->sun_vec );
+    panel_hist = current_options.get_panel_status();
 }
 
 
 // Update the view parameters
-void fgVIEW::UpdateViewMath( fgFLIGHT *f ) {
+void FGView::UpdateViewMath( FGInterface *f ) {
     Point3D p;
     MAT3vec vec, forward, v0, minus_z;
     MAT3mat R, TMP, UP, LOCAL, VIEW;
     double ntmp;
 
-    if(update_fov == true) {
+    if ( update_fov ) {
        // printf("Updating fov\n");
-       UpdateFOV(&current_options);
+       UpdateFOV( current_options );
        update_fov = false;
     }
                
@@ -268,66 +280,73 @@ void fgVIEW::UpdateViewMath( fgFLIGHT *f ) {
     //        scenery.center.y, scenery.center.z);
 
     // calculate the cartesion coords of the current lat/lon/0 elev
-    p = Point3D( FG_Longitude
-                FG_Lat_geocentric
-                FG_Sea_level_radius * FEET_TO_METER );
+    p = Point3D( f->get_Longitude()
+                f->get_Lat_geocentric()
+                f->get_Sea_level_radius() * FEET_TO_METER );
 
     cur_zero_elev = fgPolarToCart3d(p) - scenery.center;
 
     // calculate view position in current FG view coordinate system
     // p.lon & p.lat are already defined earlier, p.radius was set to
     // the sea level radius, so now we add in our altitude.
-    if ( FG_Altitude * FEET_TO_METER > 
+    if ( f->get_Altitude() * FEET_TO_METER > 
         (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
-       p.setz( p.radius() + FG_Altitude * FEET_TO_METER );
+       p.setz( p.radius() + f->get_Altitude() * FEET_TO_METER );
     } else {
        p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
     }
 
+
     abs_view_pos = fgPolarToCart3d(p);
     view_pos = abs_view_pos - scenery.center;
 
-    FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = "
-           << abs_view_pos.x() << ", " 
-           << abs_view_pos.y() << ", " 
-           << abs_view_pos.z() );
-    FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = "
-           << view_pos.x() << ", " << view_pos.y() << ", " << view_pos.z() );
+    FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
+    FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
+    FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = " << view_pos );
 
     // Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw)
     // from FG_T_local_to_body[3][3]
 
-    // Question: Why is the LaRCsim matrix arranged so differently
-    // than the one we need???
-    LOCAL[0][0] = FG_T_local_to_body_33;
-    LOCAL[0][1] = -FG_T_local_to_body_32;
-    LOCAL[0][2] = -FG_T_local_to_body_31;
-    LOCAL[0][3] = 0.0;
-    LOCAL[1][0] = -FG_T_local_to_body_23;
-    LOCAL[1][1] = FG_T_local_to_body_22;
-    LOCAL[1][2] = FG_T_local_to_body_21;
-    LOCAL[1][3] = 0.0;
-    LOCAL[2][0] = -FG_T_local_to_body_13;
-    LOCAL[2][1] = FG_T_local_to_body_12;
-    LOCAL[2][2] = FG_T_local_to_body_11;
-    LOCAL[2][3] = 0.0;
-    LOCAL[3][0] = LOCAL[3][1] = LOCAL[3][2] = LOCAL[3][3] = 0.0;
-    LOCAL[3][3] = 1.0;
-    // printf("LaRCsim LOCAL matrix\n");
-    // MAT3print(LOCAL, stdout);
-
-#ifdef OLD_LOCAL_TO_BODY_CODE
-        // old code to calculate LOCAL matrix calculated from Phi,
-        // Theta, and Psi (roll, pitch, yaw)
-
-        MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
-       MAT3rotate(R, vec, FG_Phi);
+    if ( use_larcsim_local_to_body ) {
+
+       // Question: Why is the LaRCsim matrix arranged so differently
+       // than the one we need???
+
+       // Answer (I think): The LaRCsim matrix is generated in a
+       // different reference frame than we've set up for our world
+
+       LOCAL[0][0] = f->get_T_local_to_body_33();
+       LOCAL[0][1] = -f->get_T_local_to_body_32();
+       LOCAL[0][2] = -f->get_T_local_to_body_31();
+       LOCAL[0][3] = 0.0;
+       LOCAL[1][0] = -f->get_T_local_to_body_23();
+       LOCAL[1][1] = f->get_T_local_to_body_22();
+       LOCAL[1][2] = f->get_T_local_to_body_21();
+       LOCAL[1][3] = 0.0;
+       LOCAL[2][0] = -f->get_T_local_to_body_13();
+       LOCAL[2][1] = f->get_T_local_to_body_12();
+       LOCAL[2][2] = f->get_T_local_to_body_11();
+       LOCAL[2][3] = 0.0;
+       LOCAL[3][0] = LOCAL[3][1] = LOCAL[3][2] = LOCAL[3][3] = 0.0;
+       LOCAL[3][3] = 1.0;
+
+       // printf("LaRCsim LOCAL matrix\n");
+       // MAT3print(LOCAL, stdout);
+
+    } else {
+
+       // code to calculate LOCAL matrix calculated from Phi, Theta, and
+       // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
+       // flight model
+
+       MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
+       MAT3rotate(R, vec, f->get_Phi());
        /* printf("Roll matrix\n"); */
        /* MAT3print(R, stdout); */
 
        MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
        /* MAT3mult_vec(vec, vec, R); */
-       MAT3rotate(TMP, vec, FG_Theta);
+       MAT3rotate(TMP, vec, f->get_Theta());
        /* printf("Pitch matrix\n"); */
        /* MAT3print(TMP, stdout); */
        MAT3mult(R, R, TMP);
@@ -335,24 +354,25 @@ void fgVIEW::UpdateViewMath( fgFLIGHT *f ) {
        MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
        /* MAT3mult_vec(vec, vec, R); */
        /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
-       MAT3rotate(TMP, vec, -FG_Psi);
+       MAT3rotate(TMP, vec, -f->get_Psi());
        /* printf("Yaw matrix\n");
           MAT3print(TMP, stdout); */
        MAT3mult(LOCAL, R, TMP);
        // printf("FG derived LOCAL matrix\n");
        // MAT3print(LOCAL, stdout);
-#endif // OLD_LOCAL_TO_BODY_CODE
+
+    } // if ( use_larcsim_local_to_body ) 
 
     // Derive the local UP transformation matrix based on *geodetic*
     // coordinates
     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
-    MAT3rotate(R, vec, FG_Longitude);     // R = rotate about Z axis
+    MAT3rotate(R, vec, f->get_Longitude());     // R = rotate about Z axis
     // printf("Longitude matrix\n");
     // MAT3print(R, stdout);
 
     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
     MAT3mult_vec(vec, vec, R);
-    MAT3rotate(TMP, vec, -FG_Latitude);  // TMP = rotate about X axis
+    MAT3rotate(TMP, vec, -f->get_Latitude());  // TMP = rotate about X axis
     // printf("Latitude matrix\n");
     // MAT3print(TMP, stdout);
 
@@ -413,28 +433,50 @@ void fgVIEW::UpdateViewMath( fgFLIGHT *f ) {
 
 // Update the "World to Eye" transformation matrix
 // This is most useful for view frustum culling
-void fgVIEW::UpdateWorldToEye( fgFLIGHT *f ) {
+void FGView::UpdateWorldToEye( FGInterface *f ) {
     MAT3mat R_Phi, R_Theta, R_Psi, R_Lat, R_Lon, T_view;
     MAT3mat TMP;
     MAT3hvec vec;
 
-    // if we have a view offset use slow way for now
-    if(fabs(view_offset)>FG_EPSILON){ 
+    if ( use_larcsim_local_to_body ) {
+
+       // Question: hey this is even different then LOCAL[][] above??
+       // Answer: yet another coordinate system, this time the
+       // coordinate system in which we do our view frustum culling.
+
+       AIRCRAFT[0][0] = -f->get_T_local_to_body_22();
+       AIRCRAFT[0][1] = -f->get_T_local_to_body_23();
+       AIRCRAFT[0][2] = f->get_T_local_to_body_21();
+       AIRCRAFT[0][3] = 0.0;
+       AIRCRAFT[1][0] = f->get_T_local_to_body_32();
+       AIRCRAFT[1][1] = f->get_T_local_to_body_33();
+       AIRCRAFT[1][2] = -f->get_T_local_to_body_31();
+       AIRCRAFT[1][3] = 0.0;
+       AIRCRAFT[2][0] = f->get_T_local_to_body_12();
+       AIRCRAFT[2][1] = f->get_T_local_to_body_13();
+       AIRCRAFT[2][2] = -f->get_T_local_to_body_11();
+       AIRCRAFT[2][3] = 0.0;
+       AIRCRAFT[3][0] = AIRCRAFT[3][1] = AIRCRAFT[3][2] = AIRCRAFT[3][3] = 0.0;
+       AIRCRAFT[3][3] = 1.0;
+
+    } else {
+
        // Roll Matrix
        MAT3_SET_HVEC(vec, 0.0, 0.0, -1.0, 1.0);
-       MAT3rotate(R_Phi, vec, FG_Phi);
+       MAT3rotate(R_Phi, vec, f->get_Phi());
        // printf("Roll matrix (Phi)\n");
        // MAT3print(R_Phi, stdout);
 
        // Pitch Matrix
        MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
-       MAT3rotate(R_Theta, vec, FG_Theta);
+       MAT3rotate(R_Theta, vec, f->get_Theta());
        // printf("\nPitch matrix (Theta)\n");
        // MAT3print(R_Theta, stdout);
 
        // Yaw Matrix
        MAT3_SET_HVEC(vec, 0.0, -1.0, 0.0, 1.0);
-       MAT3rotate(R_Psi, vec, FG_Psi + FG_PI - view_offset );
+       MAT3rotate(R_Psi, vec, f->get_Psi() + FG_PI /* - view_offset */ );
+       // MAT3rotate(R_Psi, vec, f->get_Psi() + FG_PI - view_offset );
        // printf("\nYaw matrix (Psi)\n");
        // MAT3print(R_Psi, stdout);
 
@@ -442,34 +484,21 @@ void fgVIEW::UpdateWorldToEye( fgFLIGHT *f ) {
        MAT3mult(TMP, R_Phi, R_Theta);
        MAT3mult(AIRCRAFT, TMP, R_Psi);
 
-    } else { // JUST USE LOCAL_TO_BODY  NHV 5/25/98
-       // hey this is even different then LOCAL[][] above ??
-        
-       AIRCRAFT[0][0] = -FG_T_local_to_body_22;
-       AIRCRAFT[0][1] = -FG_T_local_to_body_23;
-       AIRCRAFT[0][2] = FG_T_local_to_body_21;
-       AIRCRAFT[0][3] = 0.0;
-       AIRCRAFT[1][0] = FG_T_local_to_body_32;
-       AIRCRAFT[1][1] = FG_T_local_to_body_33;
-       AIRCRAFT[1][2] = -FG_T_local_to_body_31;
-       AIRCRAFT[1][3] = 0.0;
-       AIRCRAFT[2][0] = FG_T_local_to_body_12;
-       AIRCRAFT[2][1] = FG_T_local_to_body_13;
-       AIRCRAFT[2][2] = -FG_T_local_to_body_11;
-       AIRCRAFT[2][3] = 0.0;
-       AIRCRAFT[3][0] = AIRCRAFT[3][1] = AIRCRAFT[3][2] = AIRCRAFT[3][3] = 0.0;
-       AIRCRAFT[3][3] = 1.0;
+    } // if ( use_larcsim_local_to_body )
 
-       // ??? SOMETHING LIKE THIS SHOULD WORK    NHV
-       // Rotate about LOCAL_UP  (AIRCRAFT[2][])
-       // MAT3_SET_HVEC(vec, AIRCRAFT[2][0], AIRCRAFT[2][1],
-       //                        AIRCRAFT[2][2], AIRCRAFT[2][3]);
-       // MAT3rotate(TMP, vec, FG_PI - view_offset );
-       // MAT3mult(AIRCRAFT, AIRCRAFT, TMP);
-    }
-    // printf("\naircraft roll pitch yaw\n");
+    // printf("AIRCRAFT matrix\n");
     // MAT3print(AIRCRAFT, stdout);
 
+    // View rotation matrix relative to current aircraft orientation
+    MAT3_SET_HVEC(vec, 0.0, -1.0, 0.0, 1.0);
+    MAT3mult_vec(vec, vec, AIRCRAFT);
+    // printf("aircraft up vector = %.2f %.2f %.2f\n", 
+    //        vec[0], vec[1], vec[2]);
+    MAT3rotate(TMP, vec, -view_offset );
+    MAT3mult(VIEW_OFFSET, AIRCRAFT, TMP);
+    // printf("VIEW_OFFSET matrix\n");
+    // MAT3print(VIEW_OFFSET, stdout);
+
     // View position in scenery centered coordinates
     MAT3_SET_HVEC(vec, view_pos.x(), view_pos.y(), view_pos.z(), 1.0);
     MAT3translate(T_view, vec);
@@ -479,37 +508,23 @@ void fgVIEW::UpdateWorldToEye( fgFLIGHT *f ) {
     // Latitude
     MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
     // R_Lat = rotate about X axis
-    MAT3rotate(R_Lat, vec, FG_Latitude);
+    MAT3rotate(R_Lat, vec, f->get_Latitude());
     // printf("\nLatitude matrix\n");
     // MAT3print(R_Lat, stdout);
 
     // Longitude
     MAT3_SET_HVEC(vec, 0.0, 0.0, 1.0, 1.0);
     // R_Lon = rotate about Z axis
-    MAT3rotate(R_Lon, vec, FG_Longitude - FG_PI_2 );
+    MAT3rotate(R_Lon, vec, f->get_Longitude() - FG_PI_2 );
     // printf("\nLongitude matrix\n");
     // MAT3print(R_Lon, stdout);
 
-#ifdef THIS_IS_OLD_CODE
-    // View position in scenery centered coordinates
-    MAT3_SET_HVEC(vec, view_pos.x, view_pos.y, view_pos.z, 1.0);
-    MAT3translate(T_view, vec);
-    // printf("\nTranslation matrix\n");
-    // MAT3print(T_view, stdout);
-
-    // aircraft roll/pitch/yaw
-    MAT3mult(TMP, R_Phi, R_Theta);
-    MAT3mult(AIRCRAFT, TMP, R_Psi);
-    // printf("\naircraft roll pitch yaw\n");
-    // MAT3print(AIRCRAFT, stdout);
-#endif THIS_IS_OLD_CODE
-
     // lon/lat
     MAT3mult(WORLD, R_Lat, R_Lon);
     // printf("\nworld\n");
     // MAT3print(WORLD, stdout);
 
-    MAT3mult(EYE_TO_WORLD, AIRCRAFT, WORLD);
+    MAT3mult(EYE_TO_WORLD, VIEW_OFFSET, WORLD);
     MAT3mult(EYE_TO_WORLD, EYE_TO_WORLD, T_view);
     // printf("\nEye to world\n");
     // MAT3print(EYE_TO_WORLD, stdout);
@@ -537,7 +552,7 @@ void fgVIEW::UpdateWorldToEye( fgFLIGHT *f ) {
 // Olson curt@me.umn.edu and Norman Vine nhv@yahoo.com with 'gentle
 // guidance' from Steve Baker sbaker@link.com
 int
-fgVIEW::SphereClip( const Point3D& cp, const double radius )
+FGView::SphereClip( const Point3D& cp, const double radius )
 {
     double x1, y1;
 
@@ -581,11 +596,40 @@ fgVIEW::SphereClip( const Point3D& cp, const double radius )
 
 
 // Destructor
-fgVIEW::~fgVIEW( void ) {
+FGView::~FGView( void ) {
 }
 
 
 // $Log$
+// Revision 1.33  1999/02/05 21:29:14  curt
+// Modifications to incorporate Jon S. Berndts flight model code.
+//
+// Revision 1.32  1999/01/07 20:25:12  curt
+// Updated struct fgGENERAL to class FGGeneral.
+//
+// Revision 1.31  1998/12/11 20:26:28  curt
+// Fixed view frustum culling accuracy bug so we can look out the sides and
+// back without tri-stripes dropping out.
+//
+// Revision 1.30  1998/12/09 18:50:28  curt
+// Converted "class fgVIEW" to "class FGView" and updated to make data
+// members private and make required accessor functions.
+//
+// Revision 1.29  1998/12/05 15:54:24  curt
+// Renamed class fgFLIGHT to class FGState as per request by JSB.
+//
+// Revision 1.28  1998/12/03 01:17:20  curt
+// Converted fgFLIGHT to a class.
+//
+// Revision 1.27  1998/11/16 14:00:06  curt
+// Added pow() macro bug work around.
+// Added support for starting FGFS at various resolutions.
+// Added some initial serial port support.
+// Specify default log levels in main().
+//
+// Revision 1.26  1998/11/09 23:39:25  curt
+// Tweaks for the instrument panel.
+//
 // Revision 1.25  1998/11/06 21:18:15  curt
 // Converted to new logstream debugging facility.  This allows release
 // builds with no messages at all (and no performance impact) by using