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