1 // hud_rwy.cxx -- An instrument that renders a virtual runway on the HUD
3 // Written by Aaron Wilson & Phillip Merritt, Nov 2004.
5 // Copyright (C) 2004 Aaron Wilson, Aaron.I.Wilson@nasa.gov
6 // Copyright (C) 2004 Phillip Merritt, Phillip.M.Merritt@nasa.gov
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.
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.
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.
23 #include <simgear/compiler.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>
36 #include <ATC/ATCutils.hxx>
39 // int x, int y, int width, int height, float scale_data, bool working)
41 runway_instr::runway_instr(const SGPropertyNode *node) :
43 node->getIntValue("x"),
44 node->getIntValue("y"),
45 node->getIntValue("width"),
46 node->getIntValue("height"),
48 node->getDoubleValue("scale"),
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)
63 SG_LOG(SG_INPUT, SG_INFO, "Done reading runway instrument "
64 << node->getStringValue("name", "[unnamed]"));
71 center.x = view[2] >> 1;
72 center.y = view[3] >> 1;
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();
82 void runway_instr::draw()
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];
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();
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);
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();
107 //Set the camera to the cockpit view to get the view of the runway from the cockpit
108 ssgSetCamera((sgVec4 *)cockpit_view->get_VIEW());
109 get_rwy_points(points3d);
110 //Get the current project matrix
111 ssgGetProjectionMatrix(projMat);
112 // const sgVec4 *viewMat = globals->get_current_view()->get_VIEW();
113 //Get the current model view matrix (cockpit view)
114 ssgGetModelviewMatrix(modelView);
115 //Create a rotation matrix to correct for any offsets (other than default offsets) to the model view matrix
116 sgMat4 xy; //rotation about the Rxy, negate the sin's on Ry
117 xy[0][0] = cYaw; xy[1][0] = 0.0f; xy[2][0] = -sYaw; xy[3][0] = 0.0f;
118 xy[0][1] = sPitch*-sYaw; xy[1][1] = cPitch; xy[2][1] = -sPitch*cYaw; xy[3][1] = 0.0f;
119 xy[0][2] = cPitch*sYaw; xy[1][2] = sPitch; xy[2][2] = cPitch*cYaw; xy[3][2] = 0.0f;
120 xy[0][3] = 0.0f; xy[1][3] = 0.0f; xy[2][3] = 0.0f; xy[3][3] = 1.0f;
121 //Re-center the model view
122 sgPostMultMat4(modelView,xy);
123 //copy float matrices to double
124 for (int i = 0; i < 4; i++) {
125 for (int j = 0; j < 4; j++) {
126 int idx = (i * 4) + j;
127 mm[idx] = (double)modelView[i][j];
128 pm[idx] = (double)projMat[i][j];
132 //Calculate the 2D points via gluProject
133 int result = GL_TRUE;
134 for (int i = 0; i < 6; i++) {
135 result = gluProject(points3d[i][0], points3d[i][1], points3d[i][2], mm,
136 pm, view, &points2d[i][0], &points2d[i][1], &points2d[i][2]);
138 //set the line width based on our distance from the runway
140 //Draw the runway lines on the HUD
141 glEnable(GL_LINE_STIPPLE);
142 glLineStipple(1, stippleOut);
144 drawLine(points3d[0], points3d[1], points2d[0], points2d[1]) | //draw top
145 drawLine(points3d[2], points3d[1], points2d[2], points2d[1]) | //draw right
146 drawLine(points3d[2], points3d[3], points2d[2], points2d[3]) | //draw bottom
147 drawLine(points3d[3], points3d[0], points2d[3], points2d[0]); //draw left
149 glLineStipple(1, stippleCen);
150 anyLines |= drawLine(points3d[5], points3d[4], points2d[5], points2d[4]); //draw center
152 //Check to see if arrow needs drawn
153 if ((!anyLines && drawIA) || drawIAAlways) {
154 drawArrow(); //draw indication arrow
157 //Restore the current view and any offsets
158 if (curr_view_id != 0) {
159 globals->get_viewmgr()->set_view(curr_view_id);
160 globals->get_viewmgr()->copyToCurrent();
161 curr_view->setHeadingOffset_deg(ho);
162 curr_view->setPitchOffset_deg(po);
163 curr_view->setGoalHeadingOffset_deg(gho);
164 curr_view->setGoalPitchOffset_deg(gpo);
166 //Set the camera back to the current view
167 ssgSetCamera((sgVec4 *)curr_view);
173 bool runway_instr::get_active_runway(FGRunway& runway)
175 FGEnvironment stationweather =
176 ((FGEnvironmentMgr *)globals->get_subsystem("environment"))->getEnvironment();
177 double hdg = stationweather.get_wind_from_heading_deg();
178 return globals->get_runways()->search(fgGetString("/sim/presets/airport-id"), int(hdg), &runway);
182 void runway_instr::get_rwy_points(sgdVec3 *points3d)
184 static Point3D center = globals->get_scenery()->get_center();
186 //Get the current tile center
187 Point3D currentCenter = globals->get_scenery()->get_center();
188 Point3D tileCenter = currentCenter;
189 if (center != currentCenter) //if changing tiles
190 tileCenter = center; //use last center
191 double alt = current_aircraft.fdm_state->get_Runway_altitude() * SG_FEET_TO_METER;
192 double length = (runway._length / 2.0) * SG_FEET_TO_METER;
193 double width = (runway._width / 2.0) * SG_FEET_TO_METER;
194 double frontLat, frontLon, backLat, backLon,az, tempLat, tempLon;
196 geo_direct_wgs_84(alt, runway._lat, runway._lon, runway._heading, length, &backLat, &backLon, &az);
197 sgGeodToCart(backLat * SG_DEGREES_TO_RADIANS, backLon * SG_DEGREES_TO_RADIANS, alt, points3d[4]);
199 geo_direct_wgs_84(alt, runway._lat, runway._lon, runway._heading + 180, length, &frontLat, &frontLon, &az);
200 sgGeodToCart(frontLat * SG_DEGREES_TO_RADIANS, frontLon * SG_DEGREES_TO_RADIANS, alt, points3d[5]);
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[0]);
205 geo_direct_wgs_84(alt, backLat, backLon, runway._heading - 90, width, &tempLat, &tempLon, &az);
206 sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, points3d[1]);
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[2]);
211 geo_direct_wgs_84(alt, frontLat, frontLon, runway._heading + 90, width, &tempLat, &tempLon, &az);
212 sgGeodToCart(tempLat * SG_DEGREES_TO_RADIANS, tempLon * SG_DEGREES_TO_RADIANS, alt, points3d[3]);
214 for (int i = 0; i < 6; i++) {
215 points3d[i][0] -= tileCenter.x();
216 points3d[i][1] -= tileCenter.y();
217 points3d[i][2] -= tileCenter.z();
219 center = currentCenter;
223 bool runway_instr::drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3& point1, const sgdVec3& point2)
226 sgdCopyVec3(p1, point1);
227 sgdCopyVec3(p2, point2);
228 bool p1Inside = (p1[0] >= location.left && p1[0] <= location.right
229 && p1[1] >= location.bottom && p1[1] <= location.top);
230 bool p1Insight = (p1[2] >= 0.0 && p1[2] < 1.0);
231 bool p1Valid = p1Insight && p1Inside;
232 bool p2Inside = (p2[0] >= location.left && p2[0] <= location.right
233 && p2[1] >= location.bottom && p2[1] <= location.top);
234 bool p2Insight = (p2[2] >= 0.0 && p2[2] < 1.0);
235 bool p2Valid = p2Insight && p2Inside;
237 if (p1Valid && p2Valid) { //Both project points are valid, draw the line
239 glVertex2d(p1[0],p1[1]);
240 glVertex2d(p2[0],p2[1]);
243 } else if (p1Valid) { //p1 is valid and p2 is not, calculate a new valid point
244 sgdVec3 vec = {a2[0] - a1[0], a2[1] - a1[1], a2[2] - a1[2]};
245 //create the unit vector
246 sgdScaleVec3(vec, 1.0 / sgdLengthVec3(vec));
248 sgdCopyVec3(newPt, a1);
249 sgdAddVec3(newPt, vec);
250 if (gluProject(newPt[0], newPt[1], newPt[2], mm, pm, view, &p2[0], &p2[1], &p2[2])
251 && (p2[2] > 0 && p2[2] < 1.0)) {
254 glVertex2d(p1[0], p1[1]);
255 glVertex2d(p2[0], p2[1]);
259 } else if (p2Valid) { //p2 is valid and p1 is not, calculate a new valid point
260 sgdVec3 vec = {a1[0] - a2[0], a1[1] - a2[1], a1[2] - a2[2]};
261 //create the unit vector
262 sgdScaleVec3(vec, 1.0 / sgdLengthVec3(vec));
264 sgdCopyVec3(newPt, a2);
265 sgdAddVec3(newPt, vec);
266 if (gluProject(newPt[0], newPt[1], newPt[2], mm, pm, view, &p1[0], &p1[1], &p1[2])
267 && (p1[2] > 0 && p1[2] < 1.0)) {
270 glVertex2d(p2[0], p2[1]);
271 glVertex2d(p1[0], p1[1]);
275 } else if (p1Insight && p2Insight) { //both points are insight, but not inside
276 bool v = boundOutsidePoints(p1, p2);
279 glVertex2d(p1[0], p1[1]);
280 glVertex2d(p2[0], p2[1]);
285 //else both points are not insight, don't draw anything
286 return (p1Valid && p2Valid);
290 void runway_instr::boundPoint(const sgdVec3& v, sgdVec3& m)
295 else if (m[1] > v[1])
300 return; //prevent divide by zero
303 double slope = (m[1] - v[1]) / (m[0] - v[0]);
304 m[0] = (y - v[1]) / slope + v[0];
307 if (m[0] < location.left) {
308 m[0] = location.left;
309 m[1] = slope * (location.left - v[0]) + v[1];
311 } else if (m[0] > location.right) {
312 m[0] = location.right;
313 m[1] = slope * (location.right - v[0]) + v[1];
318 bool runway_instr::boundOutsidePoints(sgdVec3& v, sgdVec3& m)
320 bool pointsInvalid = (v[1] > location.top && m[1] > location.top) ||
321 (v[1] < location.bottom && m[1] < location.bottom) ||
322 (v[0] > location.right && m[0] > location.right) ||
323 (v[0] < location.left && m[0] < location.left);
327 if (m[0] == v[0]) {//x's are equal, vertical line
330 v[1] = location.bottom;
333 m[1] = location.bottom;
338 if (m[1] == v[1]) { //y's are equal, horizontal line
340 m[0] = location.right;
341 v[0] = location.left;
343 v[0] = location.right;
344 m[0] = location.left;
349 double slope = (m[1] - v[1]) / (m[0] - v[0]);
350 double b = v[1] - (slope * v[0]);
351 double y1 = slope * location.left + b;
352 double y2 = slope * location.right + b;
353 double x1 = (location.bottom - b) / slope;
354 double x2 = (location.top - b) / slope;
357 if (y1 >= location.bottom && y1 <= location.top) {
358 v[0] = location.left;
363 if (y2 >= location.bottom && y2 <= location.top) {
365 m[0] = location.right;
368 v[0] = location.right;
374 if (x1 >= location.left && x1 <= location.right) {
377 m[1] = location.bottom;
380 v[1] = location.bottom;
385 if (x2 >= location.left && x2 <= location.right) {
387 m[1] = location.bottom;
390 return (counter == 2);
393 void runway_instr::drawArrow()
395 Point3D ac(0.0), rwy(0.0);
396 ac.setlat(current_aircraft.fdm_state->get_Latitude_deg());
397 ac.setlon(current_aircraft.fdm_state->get_Longitude_deg());
398 rwy.setlat(runway._lat);
399 rwy.setlon(runway._lon);
400 float theta = GetHeadingFromTo(ac, rwy);
401 theta -= fgGetDouble("/orientation/heading-deg");
403 glMatrixMode(GL_MODELVIEW);
405 glTranslated((location.right + location.left) / 2.0,(location.top + location.bottom) / 2.0, 0.0);
406 glRotated(theta, 0.0, 0.0, 1.0);
407 glTranslated(0.0, arrowRad, 0.0);
408 glScaled(arrowScale, arrowScale, 0.0);
410 glBegin(GL_TRIANGLES);
411 glVertex2d(-5.0, 12.5);
412 glVertex2d(0.0, 25.0);
413 glVertex2d(5.0, 12.5);
417 glVertex2d(-2.5, 0.0);
418 glVertex2d(-2.5, 12.5);
419 glVertex2d(2.5, 12.5);
420 glVertex2d(2.5, 0.0);
425 void runway_instr::setLineWidth()
427 //Calculate the distance from the runway, A
428 double course, distance;
429 calc_gc_course_dist(Point3D(runway._lon * SGD_DEGREES_TO_RADIANS,
430 runway._lat * SGD_DEGREES_TO_RADIANS, 0.0),
431 Point3D(current_aircraft.fdm_state->get_Longitude(),
432 current_aircraft.fdm_state->get_Latitude(), 0.0 ),
434 distance *= SG_METER_TO_NM;
435 //Get altitude above runway, B
436 double alt_nm = get_agl();
437 static const SGPropertyNode *startup_units_node = fgGetNode("/sim/startup/units");
439 if (!strcmp(startup_units_node->getStringValue(), "feet"))
440 alt_nm *= SG_FEET_TO_METER*SG_METER_TO_NM;
442 alt_nm *= SG_METER_TO_NM;
444 //Calculate distance away from runway, C = v(A²+B²)
445 distance = sqrt(alt_nm * alt_nm + distance*distance);
446 if (distance < scaleDist)
447 glLineWidth(1.0 + ((lnScale - 1) * ((scaleDist - distance) / scaleDist)));
453 void runway_instr::setArrowRotationRadius(double radius) { arrowRad = radius; }
454 void runway_instr::setArrowScale(double scale) { arrowScale = scale; }
455 void runway_instr::setDrawArrow(bool draw) {drawIA = draw;}
456 void runway_instr::setDrawArrowAlways(bool draw) {drawIAAlways = draw;}
457 void runway_instr::setLineScale(double scale) {lnScale = scale;}
458 void runway_instr::setScaleDist(double dist_m) {scaleDist = dist_m;}
459 void runway_instr::setStippleOutline(unsigned short stipple) {stippleOut = stipple;}
460 void runway_instr::setStippleCenterline(unsigned short stipple){stippleCen = stipple;}