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