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