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