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