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