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