1 // HUD_runway.cxx -- An instrument that renders a virtual runway on the HUD
2 //
3 // Written by Aaron Wilson & Phillip Merritt, Nov 2004.
4 //
5 // Copyright (C) 2004 Aaron Wilson, Aaron.I.Wilson@nasa.gov
6 // Copyright (C) 2004 Phillip Merritt, Phillip.M.Merritt@nasa.gov
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 
22 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25 
26 #include <simgear/compiler.h>
27 #include <simgear/math/sg_geodesy.hxx>
28 #include <simgear/scene/util/project.hxx>
29 
30 #include <Main/globals.hxx>
31 #include <Main/fg_props.hxx>
32 #include <Scenery/scenery.hxx>
33 #include <Aircraft/controls.hxx>
34 #include <FDM/flight.hxx>
35 #include <Environment/environment.hxx>
36 #include <Environment/environment_mgr.hxx>
37 #include <Viewer/view.hxx>
38 #include <Viewer/viewmgr.hxx>
39 #include <Airports/airport.hxx>
40 
41 #include "HUD.hxx"
42 #include "HUD_private.hxx"
43 
Runway(HUD * hud,const SGPropertyNode * node,float x,float y)44 HUD::Runway::Runway(HUD *hud, const SGPropertyNode *node, float x, float y) :
45     Item(hud, node, x, y),
46     _agl(fgGetNode("/position/altitude-agl-ft", true)),
47     _arrow_scale(node->getDoubleValue("arrow-scale", 1.0)),
48     _arrow_radius(node->getDoubleValue("arrow-radius")),
49     _line_scale(node->getDoubleValue("line-scale", 1.0)),
50     _scale_dist(node->getDoubleValue("scale-dist-nm")),
51     _default_pitch(fgGetDouble("/sim/view[0]/config/pitch-pitch-deg", 0.0)),
52     _default_heading(fgGetDouble("/sim/view[0]/config/pitch-heading-deg", 0.0)),
53     _stipple_out(node->getIntValue("outer_stipple", 0xFFFF)),
54     _stipple_center(node->getIntValue("center-stipple", 0xFFFF)),
55     _draw_arrow(_arrow_scale > 0 ? true : false),
56     _draw_arrow_always(_arrow_scale > 0 ? node->getBoolValue("arrow-always") : false)
57 {
58     _view[0] = 0;
59     _view[1] = 0;
60     _view[2] = 640;
61     _view[3] = 480;
62 
63     _center_x = _view[2] / 2;
64     _center_y = _view[3] / 2;
65 
66     _left = _center_x - (_w / 2) + _x;
67     _right = _center_x + (_w / 2) + _x;
68     _bottom = _center_y - (_h / 2) + _y;
69     _top = _center_y + (_h / 2) + _y;
70 }
71 
draw()72 void HUD::Runway::draw()
73 {
74     _runway = get_active_runway();
75     if (!_runway)
76         return;
77 
78     glPushAttrib(GL_LINE_STIPPLE | GL_LINE_STIPPLE_PATTERN | GL_LINE_WIDTH);
79     float projMat[4][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
80     float modelView[4][4];
81     bool anyLines;
82     //Get the current view
83 //    FGViewer* curr_view = globals->get_viewmgr()->get_current_view();
84 //    int curr_view_id = globals->get_viewmgr()->get_current();
85 //    double gpo = curr_view->getGoalPitchOffset_deg();
86 //    double gho = curr_view->getGoalHeadingOffset_deg();
87 //    double po = curr_view->getPitchOffset_deg();
88 //    double ho = curr_view->getHeadingOffset_deg();
89 
90     flightgear::View* cockpitView = globals->get_viewmgr()->get_view(0);
91 
92     double yaw = -(cockpitView->getHeadingOffset_deg() - _default_heading) * SG_DEGREES_TO_RADIANS;
93     double pitch = (cockpitView->getPitchOffset_deg() - _default_pitch) * SG_DEGREES_TO_RADIANS;
94     //double roll = fgGetDouble("/sim/view[0]/config/roll-offset-deg",0.0) //TODO: adjust for default roll offset
95     double sPitch = sin(pitch), cPitch = cos(pitch),
96            sYaw = sin(yaw), cYaw = cos(yaw);
97 
98     //Set the camera to the cockpit view to get the view of the runway from the cockpit
99     // OSGFIXME
100 //     ssgSetCamera((sgVec4 *)_cockpit_view->get_VIEW());
101     get_rwy_points(_points3d);
102     //Get the current project matrix
103     // OSGFIXME
104 //     ssgGetProjectionMatrix(projMat);
105 //    const sgVec4 *viewMat = globals->get_current_view()->get_VIEW();
106     //Get the current model view matrix (cockpit view)
107     // OSGFIXME
108 //     ssgGetModelviewMatrix(modelView);
109     //Create a rotation matrix to correct for any offsets (other than default offsets) to the model view matrix
110     sgMat4 xy; //rotation about the Rxy, negate the sin's on Ry
111     xy[0][0] = cYaw,         xy[1][0] = 0.0f,   xy[2][0] = -sYaw,        xy[3][0] = 0.0f;
112     xy[0][1] = sPitch*-sYaw, xy[1][1] = cPitch, xy[2][1] = -sPitch*cYaw, xy[3][1] = 0.0f;
113     xy[0][2] = cPitch*sYaw,  xy[1][2] = sPitch, xy[2][2] = cPitch*cYaw,  xy[3][2] = 0.0f;
114     xy[0][3] = 0.0f,         xy[1][3] = 0.0f,   xy[2][3] = 0.0f,         xy[3][3] = 1.0f;
115     //Re-center the model view
116     sgPostMultMat4(modelView,xy);
117     //copy float matrices to double
118     for (int i = 0; i < 4; i++) {
119         for (int j = 0; j < 4; j++) {
120             int idx = (i * 4) + j;
121             _mm[idx] = (double)modelView[i][j];
122             _pm[idx] = (double)projMat[i][j];
123         }
124     }
125 
126     //Calculate the 2D points via gluProject
127 //    int result = GL_TRUE;
128     for (int i = 0; i < 6; i++) {
129         /*result = */simgear::project(_points3d[i][0], _points3d[i][1], _points3d[i][2],
130                                   _mm, _pm, _view,
131                                   &_points2d[i][0], &_points2d[i][1], &_points2d[i][2]);
132     }
133     //set the line width based on our distance from the runway
134     setLineWidth();
135     //Draw the runway lines on the HUD
136     glEnable(GL_LINE_STIPPLE);
137     glLineStipple(1, _stipple_out);
138     anyLines =
139             drawLine(_points3d[0], _points3d[1], _points2d[0], _points2d[1]) | //draw top
140             drawLine(_points3d[2], _points3d[1], _points2d[2], _points2d[1]) | //draw right
141             drawLine(_points3d[2], _points3d[3], _points2d[2], _points2d[3]) | //draw bottom
142             drawLine(_points3d[3], _points3d[0], _points2d[3], _points2d[0]);  //draw left
143 
144     glLineStipple(1, _stipple_center);
145     anyLines |= drawLine(_points3d[5], _points3d[4], _points2d[5], _points2d[4]); //draw center
146 
147     //Check to see if arrow needs drawn
148     if ((!anyLines && _draw_arrow) || _draw_arrow_always) {
149         drawArrow(); //draw indication arrow
150     }
151 
152     //Set the camera back to the current view
153     // OSGFIXME
154 //     ssgSetCamera((sgVec4 *)curr_view);
155     glPopAttrib();
156 }
157 
158 
get_active_runway()159 FGRunway* HUD::Runway::get_active_runway()
160 {
161   const FGAirport* apt = fgFindAirportID(fgGetString("/sim/presets/airport-id"));
162   if (!apt) return NULL;
163 
164   return apt->getActiveRunwayForUsage();
165 }
166 
167 
168 
get_rwy_points(sgdVec3 * _points3d)169 void HUD::Runway::get_rwy_points(sgdVec3 *_points3d)
170 {
171     double alt = _runway->geod().getElevationM();
172     double length = _runway->lengthM() * 0.5;
173     double width = _runway->widthM() * 0.5;
174     double frontLat = 0.0, frontLon = 0.0, backLat = 0.0, backLon = 0.0, az = 0.0, tempLat = 0.0, tempLon = 0.0;
175 
176     geo_direct_wgs_84(alt, _runway->latitude(), _runway->longitude(), _runway->headingDeg(), length, &backLat, &backLon, &az);
177     sgGeodToCart(backLat * SG_DEGREES_TO_RADIANS, backLon * SG_DEGREES_TO_RADIANS, alt, _points3d[4]);
178 
179     geo_direct_wgs_84(alt, _runway->latitude(), _runway->longitude(), _runway->headingDeg() + 180, length, &frontLat, &frontLon, &az);
180     sgGeodToCart(frontLat * SG_DEGREES_TO_RADIANS, frontLon * SG_DEGREES_TO_RADIANS, alt, _points3d[5]);
181 
182     geo_direct_wgs_84(alt, backLat, backLon, _runway->headingDeg() + 90, width, &tempLat, &tempLon, &az);
183     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, _points3d[0]);
184 
185     geo_direct_wgs_84(alt, backLat, backLon, _runway->headingDeg() - 90, width, &tempLat, &tempLon, &az);
186     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, _points3d[1]);
187 
188     geo_direct_wgs_84(alt, frontLat, frontLon, _runway->headingDeg() - 90, width, &tempLat, &tempLon, &az);
189     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, _points3d[2]);
190 
191     geo_direct_wgs_84(alt, frontLat, frontLon, _runway->headingDeg() + 90, width, &tempLat, &tempLon, &az);
192     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, _points3d[3]);
193 }
194 
195 
drawLine(const sgdVec3 & a1,const sgdVec3 & a2,const sgdVec3 & point1,const sgdVec3 & point2)196 bool HUD::Runway::drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3& point1, const sgdVec3& point2)
197 {
198     sgdVec3 p1, p2;
199     sgdCopyVec3(p1, point1);
200     sgdCopyVec3(p2, point2);
201     bool p1Inside = (p1[0] >= _left && p1[0] <= _right && p1[1] >= _bottom && p1[1] <= _top);
202     bool p1Insight = (p1[2] >= 0.0 && p1[2] < 1.0);
203     bool p1Valid = p1Insight && p1Inside;
204     bool p2Inside = (p2[0] >= _left && p2[0] <= _right && p2[1] >= _bottom && p2[1] <= _top);
205     bool p2Insight = (p2[2] >= 0.0 && p2[2] < 1.0);
206     bool p2Valid = p2Insight && p2Inside;
207 
208     if (p1Valid && p2Valid) { //Both project points are valid, draw the line
209         glBegin(GL_LINES);
210         glVertex2d(p1[0],p1[1]);
211         glVertex2d(p2[0],p2[1]);
212         glEnd();
213 
214     } else if (p1Valid) { //p1 is valid and p2 is not, calculate a new valid point
215         sgdVec3 vec = {a2[0] - a1[0], a2[1] - a1[1], a2[2] - a1[2]};
216         //create the unit vector
217         sgdScaleVec3(vec, 1.0 / sgdLengthVec3(vec));
218         sgdVec3 newPt;
219         sgdCopyVec3(newPt, a1);
220         sgdAddVec3(newPt, vec);
221         if (simgear::project(newPt[0], newPt[1], newPt[2], _mm, _pm, _view,
222                              &p2[0], &p2[1], &p2[2])
223                 && (p2[2] > 0 && p2[2] < 1.0)) {
224             boundPoint(p1, p2);
225             glBegin(GL_LINES);
226             glVertex2d(p1[0], p1[1]);
227             glVertex2d(p2[0], p2[1]);
228             glEnd();
229         }
230 
231     } else if (p2Valid) { //p2 is valid and p1 is not, calculate a new valid point
232         sgdVec3 vec = {a1[0] - a2[0], a1[1] - a2[1], a1[2] - a2[2]};
233         //create the unit vector
234         sgdScaleVec3(vec, 1.0 / sgdLengthVec3(vec));
235         sgdVec3 newPt;
236         sgdCopyVec3(newPt, a2);
237         sgdAddVec3(newPt, vec);
238         if (simgear::project(newPt[0], newPt[1], newPt[2], _mm, _pm, _view,
239                              &p1[0], &p1[1], &p1[2])
240                 && (p1[2] > 0 && p1[2] < 1.0)) {
241             boundPoint(p2, p1);
242             glBegin(GL_LINES);
243             glVertex2d(p2[0], p2[1]);
244             glVertex2d(p1[0], p1[1]);
245             glEnd();
246         }
247 
248     } else if (p1Insight && p2Insight) { //both points are insight, but not inside
249         bool v = boundOutsidePoints(p1, p2);
250         if (v) {
251             glBegin(GL_LINES);
252                 glVertex2d(p1[0], p1[1]);
253                 glVertex2d(p2[0], p2[1]);
254             glEnd();
255         }
256         return v;
257     }
258     //else both points are not insight, don't draw anything
259     return (p1Valid && p2Valid);
260 }
261 
262 
boundPoint(const sgdVec3 & v,sgdVec3 & m)263 void HUD::Runway::boundPoint(const sgdVec3& v, sgdVec3& m)
264 {
265     double y = v[1];
266     if (m[1] < v[1])
267         y = _bottom;
268     else if (m[1] > v[1])
269         y = _top;
270 
271     if (m[0] == v[0]) {
272         m[1] = y;
273         return;  //prevent divide by zero
274     }
275 
276     double slope = (m[1] - v[1]) / (m[0] - v[0]);
277     m[0] = (y - v[1]) / slope + v[0];
278     m[1] = y;
279 
280     if (m[0] < _left) {
281         m[0] = _left;
282         m[1] = slope * (_left - v[0]) + v[1];
283 
284     } else if (m[0] > _right) {
285         m[0] = _right;
286         m[1] = slope * (_right - v[0]) + v[1];
287     }
288 }
289 
290 
boundOutsidePoints(sgdVec3 & v,sgdVec3 & m)291 bool HUD::Runway::boundOutsidePoints(sgdVec3& v, sgdVec3& m)
292 {
293     bool pointsInvalid = (v[1] > _top && m[1] > _top) ||
294                          (v[1] < _bottom && m[1] < _bottom) ||
295                          (v[0] > _right && m[0] > _right) ||
296                          (v[0] < _left && m[0] < _left);
297     if (pointsInvalid)
298         return false;
299 
300     if (m[0] == v[0]) {//x's are equal, vertical line
301         if (m[1] > v[1]) {
302             m[1] = _top;
303             v[1] = _bottom;
304         } else {
305             v[1] = _top;
306             m[1] = _bottom;
307         }
308         return true;
309     }
310 
311     if (m[1] == v[1]) { //y's are equal, horizontal line
312         if (m[0] > v[0]) {
313             m[0] = _right;
314             v[0] = _left;
315         } else {
316             v[0] = _right;
317             m[0] = _left;
318         }
319         return true;
320     }
321 
322     double slope = (m[1] - v[1]) / (m[0] - v[0]);
323     double b = v[1] - (slope * v[0]);
324     double y1 = slope * _left + b;
325     double y2 = slope * _right + b;
326     double x1 = (_bottom - b) / slope;
327     double x2 = (_top - b) / slope;
328     int counter = 0;
329 
330     if (y1 >= _bottom && y1 <= _top) {
331         v[0] = _left;
332         v[1] = y1;
333         counter++;
334     }
335 
336     if (y2 >= _bottom && y2 <= _top) {
337         if (counter > 0) {
338             m[0] = _right;
339             m[1] = y2;
340         } else {
341             v[0] = _right;
342             v[1] = y2;
343         }
344         counter++;
345     }
346 
347     if (x1 >= _left && x1 <= _right) {
348         if (counter > 0) {
349             m[0] = x1;
350             m[1] = _bottom;
351         } else {
352             v[0] = x1;
353             v[1] = _bottom;
354         }
355         counter++;
356     }
357 
358     if (x2 >= _left && x2 <= _right) {
359         m[0] = x1;
360         m[1] = _bottom;
361         counter++;
362     }
363     return (counter == 2);
364 }
365 
366 
drawArrow()367 void HUD::Runway::drawArrow()
368 {
369     SGGeod acPos(SGGeod::fromDeg(
370         fgGetDouble("/position/longitude-deg"),
371         fgGetDouble("/position/latitude-deg")));
372     float theta = SGGeodesy::courseDeg(acPos, _runway->geod());
373     theta -= fgGetDouble("/orientation/heading-deg");
374     theta = -theta;
375     glMatrixMode(GL_MODELVIEW);
376     glPushMatrix();
377     glTranslated((_right + _left) / 2.0, (_top + _bottom) / 2.0, 0.0);
378     glRotated(theta, 0.0, 0.0, 1.0);
379     glTranslated(0.0, _arrow_radius, 0.0);
380     glScaled(_arrow_scale, _arrow_scale, 0.0);
381 
382     glBegin(GL_TRIANGLES);
383     glVertex2d(-5.0, 12.5);
384     glVertex2d(0.0, 25.0);
385     glVertex2d(5.0, 12.5);
386     glEnd();
387 
388     glBegin(GL_QUADS);
389     glVertex2d(-2.5, 0.0);
390     glVertex2d(-2.5, 12.5);
391     glVertex2d(2.5, 12.5);
392     glVertex2d(2.5, 0.0);
393     glEnd();
394     glPopMatrix();
395 }
396 
397 
setLineWidth()398 void HUD::Runway::setLineWidth()
399 {
400     //Calculate the distance from the runway, A
401     SGGeod acPos(SGGeod::fromDeg(
402         fgGetDouble("/position/longitude-deg"),
403         fgGetDouble("/position/latitude-deg")));
404     double distance = SGGeodesy::distanceNm(acPos, _runway->geod());
405     //Get altitude above runway, B
406     double alt_nm = _agl->getDoubleValue();
407 
408     if (_hud->getUnits() == FEET)
409         alt_nm *= SG_FEET_TO_METER;
410 
411     alt_nm *= SG_METER_TO_NM;
412 
413     //Calculate distance away from runway, C = v(A≤+B≤)
414     distance = sqrt(alt_nm * alt_nm + distance*distance);
415     if (distance < _scale_dist)
416         glLineWidth(1.0 + ((_line_scale - 1) * ((_scale_dist - distance) / _scale_dist)));
417     else
418         glLineWidth(1.0);
419 
420 }
421 
422 
423