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