]> git.mxchange.org Git - flightgear.git/blobdiff - src/Instrumentation/HUD/HUD_runway.cxx
Reset: work with threaded OSG modes
[flightgear.git] / src / Instrumentation / HUD / HUD_runway.cxx
index 61a2ac2f9a640f480d3d311235472896e30d2c9c..5ad4592a1d6141bf6ba53d9e2dd95ad92423ea6a 100644 (file)
 
 #include <simgear/compiler.h>
 #include <simgear/math/sg_geodesy.hxx>
-#include <simgear/math/polar3d.hxx>
-#include SG_GLU_H
+#include <simgear/scene/util/project.hxx>
 
 #include <Main/globals.hxx>
+#include <Main/fg_props.hxx>
 #include <Scenery/scenery.hxx>
-#include <Aircraft/aircraft.hxx>
+#include <Aircraft/controls.hxx>
+#include <FDM/flight.hxx>
 #include <Environment/environment.hxx>
 #include <Environment/environment_mgr.hxx>
-#include <Main/viewer.hxx>
-#include <Main/viewmgr.hxx>
-#include <ATC/ATCutils.hxx>
+#include <Viewer/viewer.hxx>
+#include <Viewer/viewmgr.hxx>
+#include <ATCDCL/ATCutils.hxx>
 
 #include "HUD.hxx"
-
+#include "HUD_private.hxx"
 
 HUD::Runway::Runway(HUD *hud, const SGPropertyNode *node, float x, float y) :
     Item(hud, node, x, y),
@@ -49,7 +50,6 @@ HUD::Runway::Runway(HUD *hud, const SGPropertyNode *node, float x, float y) :
     _scale_dist(node->getDoubleValue("scale-dist-nm")),
     _default_pitch(fgGetDouble("/sim/view[0]/config/pitch-pitch-deg", 0.0)),
     _default_heading(fgGetDouble("/sim/view[0]/config/pitch-heading-deg", 0.0)),
-    _cockpit_view(globals->get_viewmgr()->get_view(0)),
     _stipple_out(node->getIntValue("outer_stipple", 0xFFFF)),
     _stipple_center(node->getIntValue("center-stipple", 0xFFFF)),
     _draw_arrow(_arrow_scale > 0 ? true : false),
@@ -60,57 +60,58 @@ HUD::Runway::Runway(HUD *hud, const SGPropertyNode *node, float x, float y) :
     _view[2] = 640;
     _view[3] = 480;
 
-    _center.x = _view[2] / 2;
-    _center.y = _view[3] / 2;
+    _center_x = _view[2] / 2;
+    _center_y = _view[3] / 2;
 
-    _location.left = _center.x - (get_width() / 2) + get_x();
-    _location.right = _center.x + (get_width() / 2) + get_x();
-    _location.bottom = _center.y - (get_height() / 2) + get_y();
-    _location.top = _center.y + (get_height() / 2) + get_y();
+    _left = _center_x - (_w / 2) + _x;
+    _right = _center_x + (_w / 2) + _x;
+    _bottom = _center_y - (_h / 2) + _y;
+    _top = _center_y + (_h / 2) + _y;
 }
 
-
 void HUD::Runway::draw()
 {
-    if (!get_active_runway(_runway))
+    _runway = get_active_runway();
+    if (!_runway)
         return;
 
     glPushAttrib(GL_LINE_STIPPLE | GL_LINE_STIPPLE_PATTERN | GL_LINE_WIDTH);
-    float modelView[4][4], projMat[4][4];
+    float projMat[4][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
+    float modelView[4][4];
     bool anyLines;
     //Get the current view
-    FGViewer* curr_view = globals->get_viewmgr()->get_current_view();
-    int curr_view_id = globals->get_viewmgr()->get_current();
-    double gpo = curr_view->getGoalPitchOffset_deg();
-    double gho = curr_view->getGoalHeadingOffset_deg();
-    double po = curr_view->getPitchOffset_deg();
-    double ho = curr_view->getHeadingOffset_deg();
-
-    double yaw = -(_cockpit_view->getHeadingOffset_deg() - _default_heading) * SG_DEGREES_TO_RADIANS;
-    double pitch = (_cockpit_view->getPitchOffset_deg() - _default_pitch) * SG_DEGREES_TO_RADIANS;
+//    FGViewer* curr_view = globals->get_viewmgr()->get_current_view();
+//    int curr_view_id = globals->get_viewmgr()->get_current();
+//    double gpo = curr_view->getGoalPitchOffset_deg();
+//    double gho = curr_view->getGoalHeadingOffset_deg();
+//    double po = curr_view->getPitchOffset_deg();
+//    double ho = curr_view->getHeadingOffset_deg();
+
+    FGViewer* cockpitView = globals->get_viewmgr()->get_view(0);
+    
+    double yaw = -(cockpitView->getHeadingOffset_deg() - _default_heading) * SG_DEGREES_TO_RADIANS;
+    double pitch = (cockpitView->getPitchOffset_deg() - _default_pitch) * SG_DEGREES_TO_RADIANS;
     //double roll = fgGetDouble("/sim/view[0]/config/roll-offset-deg",0.0) //TODO: adjust for default roll offset
     double sPitch = sin(pitch), cPitch = cos(pitch),
            sYaw = sin(yaw), cYaw = cos(yaw);
 
-    //Assuming that the "Cockpit View" is always at position zero!!!
-    if (curr_view_id != 0) {
-        globals->get_viewmgr()->set_view(0);
-        globals->get_viewmgr()->copyToCurrent();
-    }
     //Set the camera to the cockpit view to get the view of the runway from the cockpit
-    ssgSetCamera((sgVec4 *)_cockpit_view->get_VIEW());
+    // OSGFIXME
+//     ssgSetCamera((sgVec4 *)_cockpit_view->get_VIEW());
     get_rwy_points(_points3d);
     //Get the current project matrix
-    ssgGetProjectionMatrix(projMat);
+    // OSGFIXME
+//     ssgGetProjectionMatrix(projMat);
 //    const sgVec4 *viewMat = globals->get_current_view()->get_VIEW();
     //Get the current model view matrix (cockpit view)
-    ssgGetModelviewMatrix(modelView);
+    // OSGFIXME
+//     ssgGetModelviewMatrix(modelView);
     //Create a rotation matrix to correct for any offsets (other than default offsets) to the model view matrix
     sgMat4 xy; //rotation about the Rxy, negate the sin's on Ry
-    xy[0][0] = cYaw;         xy[1][0] = 0.0f;   xy[2][0] = -sYaw;        xy[3][0] = 0.0f;
-    xy[0][1] = sPitch*-sYaw; xy[1][1] = cPitch; xy[2][1] = -sPitch*cYaw; xy[3][1] = 0.0f;
-    xy[0][2] = cPitch*sYaw;  xy[1][2] = sPitch; xy[2][2] = cPitch*cYaw;  xy[3][2] = 0.0f;
-    xy[0][3] = 0.0f;         xy[1][3] = 0.0f;   xy[2][3] = 0.0f;         xy[3][3] = 1.0f;
+    xy[0][0] = cYaw,         xy[1][0] = 0.0f,   xy[2][0] = -sYaw,        xy[3][0] = 0.0f;
+    xy[0][1] = sPitch*-sYaw, xy[1][1] = cPitch, xy[2][1] = -sPitch*cYaw, xy[3][1] = 0.0f;
+    xy[0][2] = cPitch*sYaw,  xy[1][2] = sPitch, xy[2][2] = cPitch*cYaw,  xy[3][2] = 0.0f;
+    xy[0][3] = 0.0f,         xy[1][3] = 0.0f,   xy[2][3] = 0.0f,         xy[3][3] = 1.0f;
     //Re-center the model view
     sgPostMultMat4(modelView,xy);
     //copy float matrices to double
@@ -123,10 +124,11 @@ void HUD::Runway::draw()
     }
 
     //Calculate the 2D points via gluProject
-    int result = GL_TRUE;
+//    int result = GL_TRUE;
     for (int i = 0; i < 6; i++) {
-        result = gluProject(_points3d[i][0], _points3d[i][1], _points3d[i][2], _mm,
-                _pm, _view, &_points2d[i][0], &_points2d[i][1], &_points2d[i][2]);
+        /*result = */simgear::project(_points3d[i][0], _points3d[i][1], _points3d[i][2],
+                                  _mm, _pm, _view,
+                                  &_points2d[i][0], &_points2d[i][1], &_points2d[i][2]);
     }
     //set the line width based on our distance from the runway
     setLineWidth();
@@ -147,69 +149,47 @@ void HUD::Runway::draw()
         drawArrow(); //draw indication arrow
     }
 
-    //Restore the current view and any offsets
-    if (curr_view_id != 0) {
-        globals->get_viewmgr()->set_view(curr_view_id);
-        globals->get_viewmgr()->copyToCurrent();
-        curr_view->setHeadingOffset_deg(ho);
-        curr_view->setPitchOffset_deg(po);
-        curr_view->setGoalHeadingOffset_deg(gho);
-        curr_view->setGoalPitchOffset_deg(gpo);
-    }
     //Set the camera back to the current view
-    ssgSetCamera((sgVec4 *)curr_view);
+    // OSGFIXME
+//     ssgSetCamera((sgVec4 *)curr_view);
     glPopAttrib();
 }
 
 
-bool HUD::Runway::get_active_runway(FGRunway& runway)
+FGRunway* HUD::Runway::get_active_runway()
 {
-    FGEnvironment stationweather =
-            ((FGEnvironmentMgr *)globals->get_subsystem("environment"))->getEnvironment();
-    double hdg = stationweather.get_wind_from_heading_deg();
-    return globals->get_runways()->search(fgGetString("/sim/presets/airport-id"), int(hdg), &runway);
+  const FGAirport* apt = fgFindAirportID(fgGetString("/sim/presets/airport-id"));
+  if (!apt) return NULL;
+  
+  return apt->getActiveRunwayForUsage();
 }
 
 
+
 void HUD::Runway::get_rwy_points(sgdVec3 *_points3d)
 {
-    static Point3D center = globals->get_scenery()->get_center();
+    double alt = _runway->geod().getElevationM();
+    double length = _runway->lengthM() * 0.5;
+    double width = _runway->widthM() * 0.5;
+    double frontLat = 0.0, frontLon = 0.0, backLat = 0.0, backLon = 0.0, az = 0.0, tempLat = 0.0, tempLon = 0.0;
 
-    //Get the current tile center
-    Point3D currentCenter = globals->get_scenery()->get_center();
-    Point3D tileCenter = currentCenter;
-    if (center != currentCenter) //if changing tiles
-        tileCenter = center; //use last center
-
-    double alt = current_aircraft.fdm_state->get_Runway_altitude() * SG_FEET_TO_METER;
-    double length = (_runway._length / 2.0) * SG_FEET_TO_METER;
-    double width = (_runway._width / 2.0) * SG_FEET_TO_METER;
-    double frontLat, frontLon, backLat, backLon,az, tempLat, tempLon;
-
-    geo_direct_wgs_84(alt, _runway._lat, _runway._lon, _runway._heading, length, &backLat, &backLon, &az);
+    geo_direct_wgs_84(alt, _runway->latitude(), _runway->longitude(), _runway->headingDeg(), length, &backLat, &backLon, &az);
     sgGeodToCart(backLat * SG_DEGREES_TO_RADIANS, backLon * SG_DEGREES_TO_RADIANS, alt, _points3d[4]);
 
-    geo_direct_wgs_84(alt, _runway._lat, _runway._lon, _runway._heading + 180, length, &frontLat, &frontLon, &az);
+    geo_direct_wgs_84(alt, _runway->latitude(), _runway->longitude(), _runway->headingDeg() + 180, length, &frontLat, &frontLon, &az);
     sgGeodToCart(frontLat * SG_DEGREES_TO_RADIANS, frontLon * SG_DEGREES_TO_RADIANS, alt, _points3d[5]);
 
-    geo_direct_wgs_84(alt, backLat, backLon, _runway._heading + 90, width, &tempLat, &tempLon, &az);
+    geo_direct_wgs_84(alt, backLat, backLon, _runway->headingDeg() + 90, width, &tempLat, &tempLon, &az);
     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, _points3d[0]);
 
-    geo_direct_wgs_84(alt, backLat, backLon, _runway._heading - 90, width, &tempLat, &tempLon, &az);
+    geo_direct_wgs_84(alt, backLat, backLon, _runway->headingDeg() - 90, width, &tempLat, &tempLon, &az);
     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, _points3d[1]);
 
-    geo_direct_wgs_84(alt, frontLat, frontLon, _runway._heading - 90, width, &tempLat, &tempLon, &az);
+    geo_direct_wgs_84(alt, frontLat, frontLon, _runway->headingDeg() - 90, width, &tempLat, &tempLon, &az);
     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, _points3d[2]);
 
-    geo_direct_wgs_84(alt, frontLat, frontLon, _runway._heading + 90, width, &tempLat, &tempLon, &az);
+    geo_direct_wgs_84(alt, frontLat, frontLon, _runway->headingDeg() + 90, width, &tempLat, &tempLon, &az);
     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, _points3d[3]);
-
-    for (int i = 0; i < 6; i++) {
-        _points3d[i][0] -= tileCenter.x();
-        _points3d[i][1] -= tileCenter.y();
-        _points3d[i][2] -= tileCenter.z();
-    }
-    center = currentCenter;
 }
 
 
@@ -218,12 +198,10 @@ bool HUD::Runway::drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3&
     sgdVec3 p1, p2;
     sgdCopyVec3(p1, point1);
     sgdCopyVec3(p2, point2);
-    bool p1Inside = (p1[0] >= _location.left && p1[0] <= _location.right
-            && p1[1] >= _location.bottom && p1[1] <= _location.top);
+    bool p1Inside = (p1[0] >= _left && p1[0] <= _right && p1[1] >= _bottom && p1[1] <= _top);
     bool p1Insight = (p1[2] >= 0.0 && p1[2] < 1.0);
     bool p1Valid = p1Insight && p1Inside;
-    bool p2Inside = (p2[0] >= _location.left && p2[0] <= _location.right
-            && p2[1] >= _location.bottom && p2[1] <= _location.top);
+    bool p2Inside = (p2[0] >= _left && p2[0] <= _right && p2[1] >= _bottom && p2[1] <= _top);
     bool p2Insight = (p2[2] >= 0.0 && p2[2] < 1.0);
     bool p2Valid = p2Insight && p2Inside;
 
@@ -240,7 +218,8 @@ bool HUD::Runway::drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3&
         sgdVec3 newPt;
         sgdCopyVec3(newPt, a1);
         sgdAddVec3(newPt, vec);
-        if (gluProject(newPt[0], newPt[1], newPt[2], _mm, _pm, _view, &p2[0], &p2[1], &p2[2])
+        if (simgear::project(newPt[0], newPt[1], newPt[2], _mm, _pm, _view,
+                             &p2[0], &p2[1], &p2[2])
                 && (p2[2] > 0 && p2[2] < 1.0)) {
             boundPoint(p1, p2);
             glBegin(GL_LINES);
@@ -256,7 +235,8 @@ bool HUD::Runway::drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3&
         sgdVec3 newPt;
         sgdCopyVec3(newPt, a2);
         sgdAddVec3(newPt, vec);
-        if (gluProject(newPt[0], newPt[1], newPt[2], _mm, _pm, _view, &p1[0], &p1[1], &p1[2])
+        if (simgear::project(newPt[0], newPt[1], newPt[2], _mm, _pm, _view,
+                             &p1[0], &p1[1], &p1[2])
                 && (p1[2] > 0 && p1[2] < 1.0)) {
             boundPoint(p2, p1);
             glBegin(GL_LINES);
@@ -284,9 +264,9 @@ void HUD::Runway::boundPoint(const sgdVec3& v, sgdVec3& m)
 {
     double y = v[1];
     if (m[1] < v[1])
-        y = _location.bottom;
+        y = _bottom;
     else if (m[1] > v[1])
-        y = _location.top;
+        y = _top;
 
     if (m[0] == v[0]) {
         m[1] = y;
@@ -297,87 +277,87 @@ void HUD::Runway::boundPoint(const sgdVec3& v, sgdVec3& m)
     m[0] = (y - v[1]) / slope + v[0];
     m[1] = y;
 
-    if (m[0] < _location.left) {
-        m[0] = _location.left;
-        m[1] = slope * (_location.left - v[0]) + v[1];
+    if (m[0] < _left) {
+        m[0] = _left;
+        m[1] = slope * (_left - v[0]) + v[1];
 
-    } else if (m[0] > _location.right) {
-        m[0] = _location.right;
-        m[1] = slope * (_location.right - v[0]) + v[1];
+    } else if (m[0] > _right) {
+        m[0] = _right;
+        m[1] = slope * (_right - v[0]) + v[1];
     }
 }
 
 
 bool HUD::Runway::boundOutsidePoints(sgdVec3& v, sgdVec3& m)
 {
-    bool pointsInvalid = (v[1] > _location.top && m[1] > _location.top) ||
-                         (v[1] < _location.bottom && m[1] < _location.bottom) ||
-                         (v[0] > _location.right && m[0] > _location.right) ||
-                         (v[0] < _location.left && m[0] < _location.left);
+    bool pointsInvalid = (v[1] > _top && m[1] > _top) ||
+                         (v[1] < _bottom && m[1] < _bottom) ||
+                         (v[0] > _right && m[0] > _right) ||
+                         (v[0] < _left && m[0] < _left);
     if (pointsInvalid)
         return false;
 
     if (m[0] == v[0]) {//x's are equal, vertical line
         if (m[1] > v[1]) {
-            m[1] = _location.top;
-            v[1] = _location.bottom;
+            m[1] = _top;
+            v[1] = _bottom;
         } else {
-            v[1] = _location.top;
-            m[1] = _location.bottom;
+            v[1] = _top;
+            m[1] = _bottom;
         }
         return true;
     }
 
     if (m[1] == v[1]) { //y's are equal, horizontal line
         if (m[0] > v[0]) {
-            m[0] = _location.right;
-            v[0] = _location.left;
+            m[0] = _right;
+            v[0] = _left;
         } else {
-            v[0] = _location.right;
-            m[0] = _location.left;
+            v[0] = _right;
+            m[0] = _left;
         }
         return true;
     }
 
     double slope = (m[1] - v[1]) / (m[0] - v[0]);
     double b = v[1] - (slope * v[0]);
-    double y1 = slope * _location.left + b;
-    double y2 = slope * _location.right + b;
-    double x1 = (_location.bottom - b) / slope;
-    double x2 = (_location.top - b) / slope;
+    double y1 = slope * _left + b;
+    double y2 = slope * _right + b;
+    double x1 = (_bottom - b) / slope;
+    double x2 = (_top - b) / slope;
     int counter = 0;
 
-    if  (y1 >= _location.bottom && y1 <= _location.top) {
-        v[0] = _location.left;
+    if (y1 >= _bottom && y1 <= _top) {
+        v[0] = _left;
         v[1] = y1;
         counter++;
     }
 
-    if (y2 >= _location.bottom && y2 <= _location.top) {
+    if (y2 >= _bottom && y2 <= _top) {
         if (counter > 0) {
-            m[0] = _location.right;
+            m[0] = _right;
             m[1] = y2;
         } else {
-            v[0] = _location.right;
+            v[0] = _right;
             v[1] = y2;
         }
         counter++;
     }
 
-    if (x1 >= _location.left && x1 <= _location.right) {
+    if (x1 >= _left && x1 <= _right) {
         if (counter > 0) {
             m[0] = x1;
-            m[1] = _location.bottom;
+            m[1] = _bottom;
         } else {
             v[0] = x1;
-            v[1] = _location.bottom;
+            v[1] = _bottom;
         }
         counter++;
     }
 
-    if (x2 >= _location.left && x2 <= _location.right) {
+    if (x2 >= _left && x2 <= _right) {
         m[0] = x1;
-        m[1] = _location.bottom;
+        m[1] = _bottom;
         counter++;
     }
     return (counter == 2);
@@ -386,17 +366,15 @@ bool HUD::Runway::boundOutsidePoints(sgdVec3& v, sgdVec3& m)
 
 void HUD::Runway::drawArrow()
 {
-    Point3D ac(0.0), rwy(0.0);
-    ac.setlat(current_aircraft.fdm_state->get_Latitude_deg());
-    ac.setlon(current_aircraft.fdm_state->get_Longitude_deg());
-    rwy.setlat(_runway._lat);
-    rwy.setlon(_runway._lon);
-    float theta = GetHeadingFromTo(ac, rwy);
+    SGGeod acPos(SGGeod::fromDeg(
+        fgGetDouble("/position/longitude-deg"), 
+        fgGetDouble("/position/latitude-deg")));
+    float theta = SGGeodesy::courseDeg(acPos, _runway->geod());
     theta -= fgGetDouble("/orientation/heading-deg");
     theta = -theta;
     glMatrixMode(GL_MODELVIEW);
     glPushMatrix();
-    glTranslated((_location.right + _location.left) / 2.0,(_location.top + _location.bottom) / 2.0, 0.0);
+    glTranslated((_right + _left) / 2.0, (_top + _bottom) / 2.0, 0.0);
     glRotated(theta, 0.0, 0.0, 1.0);
     glTranslated(0.0, _arrow_radius, 0.0);
     glScaled(_arrow_scale, _arrow_scale, 0.0);
@@ -420,13 +398,10 @@ void HUD::Runway::drawArrow()
 void HUD::Runway::setLineWidth()
 {
     //Calculate the distance from the runway, A
-    double course, distance;
-    calc_gc_course_dist(Point3D(_runway._lon * SGD_DEGREES_TO_RADIANS,
-            _runway._lat * SGD_DEGREES_TO_RADIANS, 0.0),
-            Point3D(current_aircraft.fdm_state->get_Longitude(),
-            current_aircraft.fdm_state->get_Latitude(), 0.0 ),
-            &course, &distance);
-    distance *= SG_METER_TO_NM;
+    SGGeod acPos(SGGeod::fromDeg(
+        fgGetDouble("/position/longitude-deg"), 
+        fgGetDouble("/position/latitude-deg")));
+    double distance = SGGeodesy::distanceNm(acPos, _runway->geod());
     //Get altitude above runway, B
     double alt_nm = _agl->getDoubleValue();
 
@@ -435,7 +410,7 @@ void HUD::Runway::setLineWidth()
 
     alt_nm *= SG_METER_TO_NM;
 
-    //Calculate distance away from runway, C = v(A²+B²)
+    //Calculate distance away from runway, C = v(A≤+B≤)
     distance = sqrt(alt_nm * alt_nm + distance*distance);
     if (distance < _scale_dist)
         glLineWidth(1.0 + ((_line_scale - 1) * ((_scale_dist - distance) / _scale_dist)));