]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD_runway.cxx
- fix unzoomed tapes (TODO: restore tick length)
[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/math/polar3d.hxx>
29 #include SG_GLU_H
30
31 #include <Main/globals.hxx>
32 #include <Scenery/scenery.hxx>
33 #include <Aircraft/aircraft.hxx>
34 #include <Environment/environment.hxx>
35 #include <Environment/environment_mgr.hxx>
36 #include <Main/viewer.hxx>
37 #include <Main/viewmgr.hxx>
38 #include <ATC/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     _cockpit_view(globals->get_viewmgr()->get_view(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
72
73 void HUD::Runway::draw()
74 {
75     if (!get_active_runway(_runway))
76         return;
77
78     glPushAttrib(GL_LINE_STIPPLE | GL_LINE_STIPPLE_PATTERN | GL_LINE_WIDTH);
79     float modelView[4][4], projMat[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     double yaw = -(_cockpit_view->getHeadingOffset_deg() - _default_heading) * SG_DEGREES_TO_RADIANS;
90     double pitch = (_cockpit_view->getPitchOffset_deg() - _default_pitch) * SG_DEGREES_TO_RADIANS;
91     //double roll = fgGetDouble("/sim/view[0]/config/roll-offset-deg",0.0) //TODO: adjust for default roll offset
92     double sPitch = sin(pitch), cPitch = cos(pitch),
93            sYaw = sin(yaw), cYaw = cos(yaw);
94
95     //Assuming that the "Cockpit View" is always at position zero!!!
96     if (curr_view_id != 0) {
97         globals->get_viewmgr()->set_view(0);
98         globals->get_viewmgr()->copyToCurrent();
99     }
100     //Set the camera to the cockpit view to get the view of the runway from the cockpit
101     ssgSetCamera((sgVec4 *)_cockpit_view->get_VIEW());
102     get_rwy_points(_points3d);
103     //Get the current project matrix
104     ssgGetProjectionMatrix(projMat);
105 //    const sgVec4 *viewMat = globals->get_current_view()->get_VIEW();
106     //Get the current model view matrix (cockpit view)
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 = gluProject(_points3d[i][0], _points3d[i][1], _points3d[i][2], _mm,
129                 _pm, _view, &_points2d[i][0], &_points2d[i][1], &_points2d[i][2]);
130     }
131     //set the line width based on our distance from the runway
132     setLineWidth();
133     //Draw the runway lines on the HUD
134     glEnable(GL_LINE_STIPPLE);
135     glLineStipple(1, _stipple_out);
136     anyLines =
137             drawLine(_points3d[0], _points3d[1], _points2d[0], _points2d[1]) | //draw top
138             drawLine(_points3d[2], _points3d[1], _points2d[2], _points2d[1]) | //draw right
139             drawLine(_points3d[2], _points3d[3], _points2d[2], _points2d[3]) | //draw bottom
140             drawLine(_points3d[3], _points3d[0], _points2d[3], _points2d[0]);  //draw left
141
142     glLineStipple(1, _stipple_center);
143     anyLines |= drawLine(_points3d[5], _points3d[4], _points2d[5], _points2d[4]); //draw center
144
145     //Check to see if arrow needs drawn
146     if ((!anyLines && _draw_arrow) || _draw_arrow_always) {
147         drawArrow(); //draw indication arrow
148     }
149
150     //Restore the current view and any offsets
151     if (curr_view_id != 0) {
152         globals->get_viewmgr()->set_view(curr_view_id);
153         globals->get_viewmgr()->copyToCurrent();
154         curr_view->setHeadingOffset_deg(ho);
155         curr_view->setPitchOffset_deg(po);
156         curr_view->setGoalHeadingOffset_deg(gho);
157         curr_view->setGoalPitchOffset_deg(gpo);
158     }
159     //Set the camera back to the current view
160     ssgSetCamera((sgVec4 *)curr_view);
161     glPopAttrib();
162 }
163
164
165 bool HUD::Runway::get_active_runway(FGRunway& runway)
166 {
167     FGEnvironment stationweather =
168             ((FGEnvironmentMgr *)globals->get_subsystem("environment"))->getEnvironment();
169     double hdg = stationweather.get_wind_from_heading_deg();
170     return globals->get_runways()->search(fgGetString("/sim/presets/airport-id"), int(hdg), &runway);
171 }
172
173
174 void HUD::Runway::get_rwy_points(sgdVec3 *_points3d)
175 {
176     static Point3D center = globals->get_scenery()->get_center();
177
178     //Get the current tile center
179     Point3D currentCenter = globals->get_scenery()->get_center();
180     Point3D tileCenter = currentCenter;
181     if (center != currentCenter) //if changing tiles
182         tileCenter = center; //use last center
183
184     double alt = current_aircraft.fdm_state->get_Runway_altitude() * SG_FEET_TO_METER;
185     double length = (_runway._length / 2.0) * SG_FEET_TO_METER;
186     double width = (_runway._width / 2.0) * SG_FEET_TO_METER;
187     double frontLat, frontLon, backLat, backLon,az, tempLat, tempLon;
188
189     geo_direct_wgs_84(alt, _runway._lat, _runway._lon, _runway._heading, length, &backLat, &backLon, &az);
190     sgGeodToCart(backLat * SG_DEGREES_TO_RADIANS, backLon * SG_DEGREES_TO_RADIANS, alt, _points3d[4]);
191
192     geo_direct_wgs_84(alt, _runway._lat, _runway._lon, _runway._heading + 180, length, &frontLat, &frontLon, &az);
193     sgGeodToCart(frontLat * SG_DEGREES_TO_RADIANS, frontLon * SG_DEGREES_TO_RADIANS, alt, _points3d[5]);
194
195     geo_direct_wgs_84(alt, backLat, backLon, _runway._heading + 90, width, &tempLat, &tempLon, &az);
196     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, _points3d[0]);
197
198     geo_direct_wgs_84(alt, backLat, backLon, _runway._heading - 90, width, &tempLat, &tempLon, &az);
199     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, _points3d[1]);
200
201     geo_direct_wgs_84(alt, frontLat, frontLon, _runway._heading - 90, width, &tempLat, &tempLon, &az);
202     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, _points3d[2]);
203
204     geo_direct_wgs_84(alt, frontLat, frontLon, _runway._heading + 90, width, &tempLat, &tempLon, &az);
205     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, _points3d[3]);
206
207     for (int i = 0; i < 6; i++) {
208         _points3d[i][0] -= tileCenter.x();
209         _points3d[i][1] -= tileCenter.y();
210         _points3d[i][2] -= tileCenter.z();
211     }
212     center = currentCenter;
213 }
214
215
216 bool HUD::Runway::drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3& point1, const sgdVec3& point2)
217 {
218     sgdVec3 p1, p2;
219     sgdCopyVec3(p1, point1);
220     sgdCopyVec3(p2, point2);
221     bool p1Inside = (p1[0] >= _left && p1[0] <= _right
222             && p1[1] >= _bottom && p1[1] <= _top);
223     bool p1Insight = (p1[2] >= 0.0 && p1[2] < 1.0);
224     bool p1Valid = p1Insight && p1Inside;
225     bool p2Inside = (p2[0] >= _left && p2[0] <= _right
226             && p2[1] >= _bottom && p2[1] <= _top);
227     bool p2Insight = (p2[2] >= 0.0 && p2[2] < 1.0);
228     bool p2Valid = p2Insight && p2Inside;
229
230     if (p1Valid && p2Valid) { //Both project points are valid, draw the line
231         glBegin(GL_LINES);
232         glVertex2d(p1[0],p1[1]);
233         glVertex2d(p2[0],p2[1]);
234         glEnd();
235
236     } else if (p1Valid) { //p1 is valid and p2 is not, calculate a new valid point
237         sgdVec3 vec = {a2[0] - a1[0], a2[1] - a1[1], a2[2] - a1[2]};
238         //create the unit vector
239         sgdScaleVec3(vec, 1.0 / sgdLengthVec3(vec));
240         sgdVec3 newPt;
241         sgdCopyVec3(newPt, a1);
242         sgdAddVec3(newPt, vec);
243         if (gluProject(newPt[0], newPt[1], newPt[2], _mm, _pm, _view, &p2[0], &p2[1], &p2[2])
244                 && (p2[2] > 0 && p2[2] < 1.0)) {
245             boundPoint(p1, p2);
246             glBegin(GL_LINES);
247             glVertex2d(p1[0], p1[1]);
248             glVertex2d(p2[0], p2[1]);
249             glEnd();
250         }
251
252     } else if (p2Valid) { //p2 is valid and p1 is not, calculate a new valid point
253         sgdVec3 vec = {a1[0] - a2[0], a1[1] - a2[1], a1[2] - a2[2]};
254         //create the unit vector
255         sgdScaleVec3(vec, 1.0 / sgdLengthVec3(vec));
256         sgdVec3 newPt;
257         sgdCopyVec3(newPt, a2);
258         sgdAddVec3(newPt, vec);
259         if (gluProject(newPt[0], newPt[1], newPt[2], _mm, _pm, _view, &p1[0], &p1[1], &p1[2])
260                 && (p1[2] > 0 && p1[2] < 1.0)) {
261             boundPoint(p2, p1);
262             glBegin(GL_LINES);
263             glVertex2d(p2[0], p2[1]);
264             glVertex2d(p1[0], p1[1]);
265             glEnd();
266         }
267
268     } else if (p1Insight && p2Insight) { //both points are insight, but not inside
269         bool v = boundOutsidePoints(p1, p2);
270         if (v) {
271             glBegin(GL_LINES);
272                 glVertex2d(p1[0], p1[1]);
273                 glVertex2d(p2[0], p2[1]);
274             glEnd();
275         }
276         return v;
277     }
278     //else both points are not insight, don't draw anything
279     return (p1Valid && p2Valid);
280 }
281
282
283 void HUD::Runway::boundPoint(const sgdVec3& v, sgdVec3& m)
284 {
285     double y = v[1];
286     if (m[1] < v[1])
287         y = _bottom;
288     else if (m[1] > v[1])
289         y = _top;
290
291     if (m[0] == v[0]) {
292         m[1] = y;
293         return;  //prevent divide by zero
294     }
295
296     double slope = (m[1] - v[1]) / (m[0] - v[0]);
297     m[0] = (y - v[1]) / slope + v[0];
298     m[1] = y;
299
300     if (m[0] < _left) {
301         m[0] = _left;
302         m[1] = slope * (_left - v[0]) + v[1];
303
304     } else if (m[0] > _right) {
305         m[0] = _right;
306         m[1] = slope * (_right - v[0]) + v[1];
307     }
308 }
309
310
311 bool HUD::Runway::boundOutsidePoints(sgdVec3& v, sgdVec3& m)
312 {
313     bool pointsInvalid = (v[1] > _top && m[1] > _top) ||
314                          (v[1] < _bottom && m[1] < _bottom) ||
315                          (v[0] > _right && m[0] > _right) ||
316                          (v[0] < _left && m[0] < _left);
317     if (pointsInvalid)
318         return false;
319
320     if (m[0] == v[0]) {//x's are equal, vertical line
321         if (m[1] > v[1]) {
322             m[1] = _top;
323             v[1] = _bottom;
324         } else {
325             v[1] = _top;
326             m[1] = _bottom;
327         }
328         return true;
329     }
330
331     if (m[1] == v[1]) { //y's are equal, horizontal line
332         if (m[0] > v[0]) {
333             m[0] = _right;
334             v[0] = _left;
335         } else {
336             v[0] = _right;
337             m[0] = _left;
338         }
339         return true;
340     }
341
342     double slope = (m[1] - v[1]) / (m[0] - v[0]);
343     double b = v[1] - (slope * v[0]);
344     double y1 = slope * _left + b;
345     double y2 = slope * _right + b;
346     double x1 = (_bottom - b) / slope;
347     double x2 = (_top - b) / slope;
348     int counter = 0;
349
350     if (y1 >= _bottom && y1 <= _top) {
351         v[0] = _left;
352         v[1] = y1;
353         counter++;
354     }
355
356     if (y2 >= _bottom && y2 <= _top) {
357         if (counter > 0) {
358             m[0] = _right;
359             m[1] = y2;
360         } else {
361             v[0] = _right;
362             v[1] = y2;
363         }
364         counter++;
365     }
366
367     if (x1 >= _left && x1 <= _right) {
368         if (counter > 0) {
369             m[0] = x1;
370             m[1] = _bottom;
371         } else {
372             v[0] = x1;
373             v[1] = _bottom;
374         }
375         counter++;
376     }
377
378     if (x2 >= _left && x2 <= _right) {
379         m[0] = x1;
380         m[1] = _bottom;
381         counter++;
382     }
383     return (counter == 2);
384 }
385
386
387 void HUD::Runway::drawArrow()
388 {
389     Point3D ac(0.0), rwy(0.0);
390     ac.setlat(current_aircraft.fdm_state->get_Latitude_deg());
391     ac.setlon(current_aircraft.fdm_state->get_Longitude_deg());
392     rwy.setlat(_runway._lat);
393     rwy.setlon(_runway._lon);
394     float theta = GetHeadingFromTo(ac, rwy);
395     theta -= fgGetDouble("/orientation/heading-deg");
396     theta = -theta;
397     glMatrixMode(GL_MODELVIEW);
398     glPushMatrix();
399     glTranslated((_right + _left) / 2.0, (_top + _bottom) / 2.0, 0.0);
400     glRotated(theta, 0.0, 0.0, 1.0);
401     glTranslated(0.0, _arrow_radius, 0.0);
402     glScaled(_arrow_scale, _arrow_scale, 0.0);
403
404     glBegin(GL_TRIANGLES);
405     glVertex2d(-5.0, 12.5);
406     glVertex2d(0.0, 25.0);
407     glVertex2d(5.0, 12.5);
408     glEnd();
409
410     glBegin(GL_QUADS);
411     glVertex2d(-2.5, 0.0);
412     glVertex2d(-2.5, 12.5);
413     glVertex2d(2.5, 12.5);
414     glVertex2d(2.5, 0.0);
415     glEnd();
416     glPopMatrix();
417 }
418
419
420 void HUD::Runway::setLineWidth()
421 {
422     //Calculate the distance from the runway, A
423     double course, distance;
424     calc_gc_course_dist(Point3D(_runway._lon * SGD_DEGREES_TO_RADIANS,
425             _runway._lat * SGD_DEGREES_TO_RADIANS, 0.0),
426             Point3D(current_aircraft.fdm_state->get_Longitude(),
427             current_aircraft.fdm_state->get_Latitude(), 0.0 ),
428             &course, &distance);
429     distance *= SG_METER_TO_NM;
430     //Get altitude above runway, B
431     double alt_nm = _agl->getDoubleValue();
432
433     if (_hud->getUnits() == FEET)
434         alt_nm *= SG_FEET_TO_METER;
435
436     alt_nm *= SG_METER_TO_NM;
437
438     //Calculate distance away from runway, C = v(A²+B²)
439     distance = sqrt(alt_nm * alt_nm + distance*distance);
440     if (distance < _scale_dist)
441         glLineWidth(1.0 + ((_line_scale - 1) * ((_scale_dist - distance) / _scale_dist)));
442     else
443         glLineWidth(1.0);
444
445 }
446
447