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