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