]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/hud_rwy.cxx
remove the rest of the static variables (except one); cleanup
[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/fg_props.hxx>
29 #include <Main/globals.hxx>
30 #include <Scenery/scenery.hxx>
31 #include <Aircraft/aircraft.hxx>
32 #include <Environment/environment.hxx>
33 #include <Environment/environment_mgr.hxx>
34 #include <simgear/math/sg_geodesy.hxx>
35 #include <simgear/math/polar3d.hxx>
36 #include SG_GLU_H
37 #include <ATC/ATCutils.hxx>
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"));
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() && get_active_runway(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         ssgSetCamera((sgVec4 *)cockpit_view->get_VIEW());
110         get_rwy_points(points3d);
111         //Get the current project matrix
112         ssgGetProjectionMatrix(projMat);
113 //        const sgVec4 *viewMat = globals->get_current_view()->get_VIEW();
114         //Get the current model view matrix (cockpit view)
115         ssgGetModelviewMatrix(modelView);
116         //Create a rotation matrix to correct for any offsets (other than default offsets) to the model view matrix
117         sgMat4 xy; //rotation about the Rxy, negate the sin's on Ry
118         xy[0][0] = cYaw;         xy[1][0] = 0.0f;   xy[2][0] = -sYaw;        xy[3][0] = 0.0f;
119         xy[0][1] = sPitch*-sYaw; xy[1][1] = cPitch; xy[2][1] = -sPitch*cYaw; xy[3][1] = 0.0f;
120         xy[0][2] = cPitch*sYaw;  xy[1][2] = sPitch; xy[2][2] = cPitch*cYaw;  xy[3][2] = 0.0f;
121         xy[0][3] = 0.0f;         xy[1][3] = 0.0f;   xy[2][3] = 0.0f;         xy[3][3] = 1.0f;
122         //Re-center the model view
123         sgPostMultMat4(modelView,xy);
124         //copy float matrices to double
125         for (int i = 0; i < 4; i++) {
126             for (int j = 0; j < 4; j++) {
127                 int idx = (i * 4) + j;
128                 mm[idx] = (double)modelView[i][j];
129                 pm[idx] = (double)projMat[i][j];
130             }
131         }
132
133         //Calculate the 2D points via gluProject
134         int result = GL_TRUE;
135         for (int i = 0; i < 6; i++) {
136             result = gluProject(points3d[i][0], points3d[i][1], points3d[i][2], mm,
137                     pm, view, &points2d[i][0], &points2d[i][1], &points2d[i][2]);
138         }
139         //set the line width based on our distance from the runway
140         setLineWidth();
141         //Draw the runway lines on the HUD
142         glEnable(GL_LINE_STIPPLE);
143         glLineStipple(1, stippleOut);
144         anyLines =
145                 drawLine(points3d[0], points3d[1], points2d[0], points2d[1]) | //draw top
146                 drawLine(points3d[2], points3d[1], points2d[2], points2d[1]) | //draw right
147                 drawLine(points3d[2], points3d[3], points2d[2], points2d[3]) | //draw bottom
148                 drawLine(points3d[3], points3d[0], points2d[3], points2d[0]);  //draw left
149
150         glLineStipple(1, stippleCen);
151         anyLines |= drawLine(points3d[5], points3d[4], points2d[5], points2d[4]); //draw center
152
153         //Check to see if arrow needs drawn
154         if ((!anyLines && drawIA) || drawIAAlways) {
155             drawArrow(); //draw indication arrow
156         }
157
158         //Restore the current view and any offsets
159         if (curr_view_id != 0) {
160             globals->get_viewmgr()->set_view(curr_view_id);
161             globals->get_viewmgr()->copyToCurrent();
162             curr_view->setHeadingOffset_deg(ho);
163             curr_view->setPitchOffset_deg(po);
164             curr_view->setGoalHeadingOffset_deg(gho);
165             curr_view->setGoalPitchOffset_deg(gpo);
166         }
167         //Set the camera back to the current view
168         ssgSetCamera((sgVec4 *)curr_view);
169         glPopAttrib();
170     }//if not broken
171 }
172
173
174 bool runway_instr::get_active_runway(FGRunway& runway)
175 {
176     FGEnvironment stationweather =
177             ((FGEnvironmentMgr *)globals->get_subsystem("environment"))->getEnvironment();
178     double hdg = stationweather.get_wind_from_heading_deg();
179     return globals->get_runways()->search(fgGetString("/sim/presets/airport-id"), int(hdg), &runway);
180 }
181
182
183 void runway_instr::get_rwy_points(sgdVec3 *points3d)
184 {
185     static Point3D center = globals->get_scenery()->get_center();
186
187     //Get the current tile center
188     Point3D currentCenter = globals->get_scenery()->get_center();
189     Point3D tileCenter = currentCenter;
190     if (center != currentCenter) //if changing tiles
191         tileCenter = center; //use last center
192     double alt = current_aircraft.fdm_state->get_Runway_altitude() * SG_FEET_TO_METER;
193     double length = (runway._length / 2.0) * SG_FEET_TO_METER;
194     double width = (runway._width / 2.0) * SG_FEET_TO_METER;
195     double frontLat, frontLon, backLat, backLon,az, tempLat, tempLon;
196
197     geo_direct_wgs_84(alt, runway._lat, runway._lon, runway._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, runway._lat, runway._lon, runway._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, runway._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, runway._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, runway._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, runway._heading + 90, width, &tempLat, &tempLon, &az);
213     sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, points3d[3]);
214
215     for (int i = 0; i < 6; i++) {
216         points3d[i][0] -= tileCenter.x();
217         points3d[i][1] -= tileCenter.y();
218         points3d[i][2] -= tileCenter.z();
219     }
220     center = currentCenter;
221 }
222
223
224 bool runway_instr::drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3& point1, const sgdVec3& point2)
225 {
226     sgdVec3 p1, p2;
227     sgdCopyVec3(p1, point1);
228     sgdCopyVec3(p2, point2);
229     bool p1Inside = (p1[0] >= location.left && p1[0] <= location.right
230             && p1[1] >= location.bottom && p1[1] <= location.top);
231     bool p1Insight = (p1[2] >= 0.0 && p1[2] < 1.0);
232     bool p1Valid = p1Insight && p1Inside;
233     bool p2Inside = (p2[0] >= location.left && p2[0] <= location.right
234             && p2[1] >= location.bottom && p2[1] <= location.top);
235     bool p2Insight = (p2[2] >= 0.0 && p2[2] < 1.0);
236     bool p2Valid = p2Insight && p2Inside;
237
238     if (p1Valid && p2Valid) { //Both project points are valid, draw the line
239         glBegin(GL_LINES);
240         glVertex2d(p1[0],p1[1]);
241         glVertex2d(p2[0],p2[1]);
242         glEnd();
243
244     } else if (p1Valid) { //p1 is valid and p2 is not, calculate a new valid point
245         sgdVec3 vec = {a2[0] - a1[0], a2[1] - a1[1], a2[2] - a1[2]};
246         //create the unit vector
247         sgdScaleVec3(vec, 1.0 / sgdLengthVec3(vec));
248         sgdVec3 newPt;
249         sgdCopyVec3(newPt, a1);
250         sgdAddVec3(newPt, vec);
251         if (gluProject(newPt[0], newPt[1], newPt[2], mm, pm, view, &p2[0], &p2[1], &p2[2])
252                 && (p2[2] > 0 && p2[2] < 1.0)) {
253             boundPoint(p1, p2);
254             glBegin(GL_LINES);
255             glVertex2d(p1[0], p1[1]);
256             glVertex2d(p2[0], p2[1]);
257             glEnd();
258         }
259
260     } else if (p2Valid) { //p2 is valid and p1 is not, calculate a new valid point
261         sgdVec3 vec = {a1[0] - a2[0], a1[1] - a2[1], a1[2] - a2[2]};
262         //create the unit vector
263         sgdScaleVec3(vec, 1.0 / sgdLengthVec3(vec));
264         sgdVec3 newPt;
265         sgdCopyVec3(newPt, a2);
266         sgdAddVec3(newPt, vec);
267         if (gluProject(newPt[0], newPt[1], newPt[2], mm, pm, view, &p1[0], &p1[1], &p1[2])
268                 && (p1[2] > 0 && p1[2] < 1.0)) {
269             boundPoint(p2, p1);
270             glBegin(GL_LINES);
271             glVertex2d(p2[0], p2[1]);
272             glVertex2d(p1[0], p1[1]);
273             glEnd();
274         }
275
276     } else if (p1Insight && p2Insight) { //both points are insight, but not inside
277         bool v = boundOutsidePoints(p1, p2);
278         if (v) {
279             glBegin(GL_LINES);
280                 glVertex2d(p1[0], p1[1]);
281                 glVertex2d(p2[0], p2[1]);
282             glEnd();
283         }
284         return v;
285     }
286     //else both points are not insight, don't draw anything
287     return (p1Valid && p2Valid);
288 }
289
290
291 void runway_instr::boundPoint(const sgdVec3& v, sgdVec3& m)
292 {
293     double y = v[1];
294     if (m[1] < v[1])
295         y = location.bottom;
296     else if (m[1] > v[1])
297         y = location.top;
298
299     if (m[0] == v[0]) {
300         m[1] = y;
301         return;  //prevent divide by zero
302     }
303
304     double slope = (m[1] - v[1]) / (m[0] - v[0]);
305     m[0] = (y - v[1]) / slope + v[0];
306     m[1] = y;
307
308     if (m[0] < location.left) {
309         m[0] = location.left;
310         m[1] = slope * (location.left - v[0]) + v[1];
311
312     } else if (m[0] > location.right) {
313         m[0] = location.right;
314         m[1] = slope * (location.right - v[0]) + v[1];
315     }
316 }
317
318
319 bool runway_instr::boundOutsidePoints(sgdVec3& v, sgdVec3& m)
320 {
321     bool pointsInvalid = (v[1] > location.top && m[1] > location.top) ||
322                          (v[1] < location.bottom && m[1] < location.bottom) ||
323                          (v[0] > location.right && m[0] > location.right) ||
324                          (v[0] < location.left && m[0] < location.left);
325     if (pointsInvalid)
326         return false;
327
328     if (m[0] == v[0]) {//x's are equal, vertical line
329         if (m[1] > v[1]) {
330             m[1] = location.top;
331             v[1] = location.bottom;
332         } else {
333             v[1] = location.top;
334             m[1] = location.bottom;
335         }
336         return true;
337     }
338
339     if (m[1] == v[1]) { //y's are equal, horizontal line
340         if (m[0] > v[0]) {
341             m[0] = location.right;
342             v[0] = location.left;
343         } else {
344             v[0] = location.right;
345             m[0] = location.left;
346         }
347         return true;
348     }
349
350     double slope = (m[1] - v[1]) / (m[0] - v[0]);
351     double b = v[1] - (slope * v[0]);
352     double y1 = slope * location.left + b;
353     double y2 = slope * location.right + b;
354     double x1 = (location.bottom - b) / slope;
355     double x2 = (location.top - b) / slope;
356     int counter = 0;
357
358     if  (y1 >= location.bottom && y1 <= location.top) {
359         v[0] = location.left;
360         v[1] = y1;
361         counter++;
362     }
363
364     if (y2 >= location.bottom && y2 <= location.top) {
365         if (counter > 0) {
366             m[0] = location.right;
367             m[1] = y2;
368         } else {
369             v[0] = location.right;
370             v[1] = y2;
371         }
372         counter++;
373     }
374
375     if (x1 >= location.left && x1 <= location.right) {
376         if (counter > 0) {
377             m[0] = x1;
378             m[1] = location.bottom;
379         } else {
380             v[0] = x1;
381             v[1] = location.bottom;
382         }
383         counter++;
384     }
385
386     if (x2 >= location.left && x2 <= location.right) {
387         m[0] = x1;
388         m[1] = location.bottom;
389         counter++;
390     }
391     return (counter == 2);
392 }
393
394 void runway_instr::drawArrow()
395 {
396     Point3D ac(0.0), rwy(0.0);
397     ac.setlat(current_aircraft.fdm_state->get_Latitude_deg());
398     ac.setlon(current_aircraft.fdm_state->get_Longitude_deg());
399     rwy.setlat(runway._lat);
400     rwy.setlon(runway._lon);
401     float theta = GetHeadingFromTo(ac, rwy);
402     theta -= fgGetDouble("/orientation/heading-deg");
403     theta = -theta;
404     glMatrixMode(GL_MODELVIEW);
405     glPushMatrix();
406     glTranslated((location.right + location.left) / 2.0,(location.top + location.bottom) / 2.0, 0.0);
407     glRotated(theta, 0.0, 0.0, 1.0);
408     glTranslated(0.0, arrowRad, 0.0);
409     glScaled(arrowScale, arrowScale, 0.0);
410
411     glBegin(GL_TRIANGLES);
412     glVertex2d(-5.0, 12.5);
413     glVertex2d(0.0, 25.0);
414     glVertex2d(5.0, 12.5);
415     glEnd();
416
417     glBegin(GL_QUADS);
418     glVertex2d(-2.5, 0.0);
419     glVertex2d(-2.5, 12.5);
420     glVertex2d(2.5, 12.5);
421     glVertex2d(2.5, 0.0);
422     glEnd();
423     glPopMatrix();
424 }
425
426 void runway_instr::setLineWidth()
427 {
428     //Calculate the distance from the runway, A
429     double course, distance;
430     calc_gc_course_dist(Point3D(runway._lon * SGD_DEGREES_TO_RADIANS,
431             runway._lat * SGD_DEGREES_TO_RADIANS, 0.0),
432             Point3D(current_aircraft.fdm_state->get_Longitude(),
433             current_aircraft.fdm_state->get_Latitude(), 0.0 ),
434             &course, &distance);
435     distance *= SG_METER_TO_NM;
436     //Get altitude above runway, B
437     double alt_nm = get_agl();
438     static const SGPropertyNode *startup_units_node = fgGetNode("/sim/startup/units");
439
440     if (!strcmp(startup_units_node->getStringValue(), "feet"))
441         alt_nm *= SG_FEET_TO_METER*SG_METER_TO_NM;
442     else
443         alt_nm *= SG_METER_TO_NM;
444
445     //Calculate distance away from runway, C = v(A²+B²)
446     distance = sqrt(alt_nm * alt_nm + distance*distance);
447     if (distance < scaleDist)
448         glLineWidth(1.0 + ((lnScale - 1) * ((scaleDist - distance) / scaleDist)));
449     else
450         glLineWidth(1.0);
451
452 }
453
454 void runway_instr::setArrowRotationRadius(double radius) { arrowRad = radius; }
455 void runway_instr::setArrowScale(double scale) { arrowScale = scale; }
456 void runway_instr::setDrawArrow(bool draw) {drawIA = draw;}
457 void runway_instr::setDrawArrowAlways(bool draw) {drawIAAlways = draw;}
458 void runway_instr::setLineScale(double scale) {lnScale = scale;}
459 void runway_instr::setScaleDist(double dist_m) {scaleDist = dist_m;}
460 void runway_instr::setStippleOutline(unsigned short stipple) {stippleOut = stipple;}
461 void runway_instr::setStippleCenterline(unsigned short stipple){stippleCen = stipple;}
462