]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/cockpit.cxx
840f0f47bd9f65c631aa33dafd7b0a694d8ab163
[flightgear.git] / src / Cockpit / cockpit.cxx
1 // cockpit.cxx -- routines to draw a cockpit (initial draft)
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  - nomimarketing@mail.telepac.pt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H          
29 #  include <windows.h>
30 #endif
31
32 #include <GL/glut.h>
33 #include <simgear/xgl/xgl.h>
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38
39 #include <simgear/constants.h>
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/math/fg_random.h>
42 #include <simgear/math/mat3.h>
43 #include <simgear/math/polar3d.hxx>
44
45 #include <Aircraft/aircraft.hxx>
46 #include <Include/general.hxx>
47 #include <Main/options.hxx>
48 #include <Main/views.hxx>
49 #include <Scenery/scenery.hxx>
50 #include <Time/fg_timer.hxx>
51 #include <Time/fg_time.hxx>
52 #include <GUI/gui.h>
53
54 #include "cockpit.hxx"
55
56
57 // This is a structure that contains all data related to
58 // cockpit/panel/hud system
59
60 static pCockpit ac_cockpit;
61 fntRenderer *HUDtext = 0;
62 float  HUD_TextSize = 0;
63 int HUD_style = 0;
64
65 float HUD_matrix[16];
66 // The following routines obtain information concerntin the aircraft's
67 // current state and return it to calling instrument display routines.
68 // They should eventually be member functions of the aircraft.
69 //
70
71 float get_latitude( void )
72 {
73     double lat;
74
75     lat = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
76
77     float flat = lat;
78     return(flat);
79
80 }
81
82 float get_lat_min( void )
83 {
84     double      a, d;
85
86     a = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;    
87     if (a < 0.0) {
88         a = -a;
89     }
90     d = (double) ( (int) a);
91     float lat_min = (a - d) * 60.0;
92     return(lat_min );
93 }
94
95
96 float get_longitude( void )
97 {
98     double lon;
99
100     lon = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
101
102     float flon = lon;
103     return(flon);
104 }
105
106
107 char*
108 get_formated_gmt_time( void )
109 {
110     static char buf[32];
111     FGTime *t = FGTime::cur_time_params;
112     const struct tm *p = t->getGmt();
113     sprintf( buf, "%d/%d/%4d %d:%02d:%02d", 
114          p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
115          p->tm_hour, p->tm_min, p->tm_sec);
116     return buf;
117 }
118
119
120 float get_long_min( void )
121 {
122     double  a, d;
123     a = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;   
124     if (a < 0.0) {
125         a = -a;
126     }
127     d = (double) ( (int) a);
128     float lon_min = (a - d) * 60.0; 
129     return(lon_min);
130 }
131
132 float get_throttleval( void )
133 {
134     float throttle = controls.get_throttle( 0 );
135     return (throttle);     // Hack limiting to one engine
136 }
137
138 float get_aileronval( void )
139 {
140     float aileronval = controls.get_aileron();
141     return (aileronval);
142 }
143
144 float get_elevatorval( void )
145 {
146     float elevator_val = (float)controls.get_elevator();
147     return elevator_val;
148 }
149
150 float get_elev_trimval( void )
151 {
152     float elevatorval = controls.get_elevator_trim();
153     return (elevatorval);
154 }
155
156 float get_rudderval( void )
157 {
158     float rudderval = controls.get_rudder();
159     return (rudderval);
160 }
161
162 float get_speed( void )
163 {
164     // Make an explicit function call.
165     float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
166         * current_options.get_speed_up();
167     return( speed );
168 }
169
170 float get_mach(void)
171 {
172         float mach=current_aircraft.fdm_state->get_Mach_number();
173         return mach;
174 }       
175
176 float get_aoa( void )
177 {
178     float aoa = current_aircraft.fdm_state->get_Alpha() * RAD_TO_DEG;
179     return( aoa );
180 }
181
182 float get_roll( void )
183 {
184     float roll = current_aircraft.fdm_state->get_Phi();
185     return( roll );
186 }
187
188 float get_pitch( void )
189 {
190     float pitch = current_aircraft.fdm_state->get_Theta();
191     return( pitch );
192 }
193
194 float get_heading( void )
195 {
196     float heading = (current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG);
197     return( heading );
198 }
199
200 float get_altitude( void )
201 {
202 //  FGState *f;
203     // double rough_elev;
204
205 //  current_aircraft.fdm_state
206     // rough_elev = mesh_altitude(f->get_Longitude() * RAD_TO_ARCSEC,
207     //                         f->get_Latitude()  * RAD_TO_ARCSEC);
208     float altitude;
209
210     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
211         altitude = current_aircraft.fdm_state->get_Altitude();
212     } else {
213         altitude = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER);
214     }
215     return altitude;
216 }
217
218 float get_agl( void )
219 {
220     float agl;
221
222     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
223         agl = (current_aircraft.fdm_state->get_Altitude()
224                - scenery.cur_elev * METER_TO_FEET);
225     } else {
226         agl = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER
227                - scenery.cur_elev);
228     }
229     return agl;
230 }
231
232 float get_sideslip( void )
233 {
234     float sideslip = current_aircraft.fdm_state->get_Beta();
235     return( sideslip );
236 }
237
238 float get_frame_rate( void )
239 {
240     float frame_rate = general.get_frame_rate();
241     return (frame_rate); 
242 }
243
244 float get_fov( void )
245 {
246     float fov = current_options.get_fov(); 
247     return (fov);
248 }
249
250 float get_vfc_ratio( void )
251 {
252     float vfc = current_view.get_vfc_ratio();
253     return (vfc);
254 }
255
256 float get_vfc_tris_drawn   ( void )
257 {
258     float rendered = current_view.get_tris_rendered();
259     return (rendered);
260 }
261
262 float get_vfc_tris_culled   ( void )
263 {
264     float culled = current_view.get_tris_culled();
265     return (culled);
266 }
267
268 float get_climb_rate( void )
269 {
270     float climb_rate;
271     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
272         climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
273     } else {
274         climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * FEET_TO_METER * 60.0;
275     }
276     return (climb_rate);
277 }
278
279
280 float get_view_direction( void )
281 {
282     double view;
283  
284     view = FG_2PI - current_view.get_view_offset();
285     view = ( current_aircraft.fdm_state->get_Psi() + view) * RAD_TO_DEG;
286     
287     if(view > 360.)
288         view -= 360.;
289     else if(view<0.)
290         view += 360.;
291     
292     float fview = view;
293     return( fview );
294 }
295
296 #ifdef NOT_USED
297 /****************************************************************************/
298 /* Convert degrees to dd mm'ss.s" (DMS-Format)                              */
299 /****************************************************************************/
300 char *dmshh_format(double degrees)
301 {
302     static char buf[16];    
303     int deg_part;
304     int min_part;
305     double sec_part;
306     
307     if (degrees < 0)
308       degrees = -degrees;
309
310     deg_part = degrees;
311     min_part = 60.0 * (degrees - deg_part);
312     sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
313
314     /* Round off hundredths */
315     if (sec_part + 0.005 >= 60.0)
316       sec_part -= 60.0, min_part += 1;
317     if (min_part >= 60)
318       min_part -= 60, deg_part += 1;
319
320     sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
321
322     return buf;
323 }
324 #endif // 0
325
326 /****************************************************************************/
327 /* Convert degrees to dd mm'ss.s'' (DMS-Format)                              */
328 /****************************************************************************/
329 static char *toDMS(float a)
330 {
331   int        neg = 0;
332   float       d, m, s;
333   static char  dms[16];
334
335   if (a < 0.0f) {
336     a   = -a;
337     neg = 1;
338   }
339   d = (float) ((int) a); 
340   a = (a - d) * 60.0f;
341   m = (float) ((int) a);
342   s = (a - m) * 60.0f;
343   
344   if (s > 59.5f) {
345     s  = 0.0f;
346     m += 1.0f;
347   }
348   if (m > 59.5f) {
349     m  = 0.0f;
350     d += 1.0f;
351   }
352   if (neg)
353     d = -d;
354   
355   sprintf(dms, "%.0f*%02.0f %04.1f", d, m, s);
356   return dms;
357 }
358
359
360 /****************************************************************************/
361 /* Convert degrees to dd mm.mmm' (DMM-Format)                               */
362 /****************************************************************************/
363 static char *toDM(float a)
364 {
365   int        neg = 0;
366   float       d, m;
367   static char  dm[16];
368   
369   if (a < 0.0f) {
370     a = -a;
371     neg = 1;
372   }
373
374   d = (float) ( (int) a);
375   m = (a - d) * 60.0f;
376   
377   if (m > 59.5f) {
378     m  = 0.0f;
379     d += 1.0f;
380   }
381   if (neg) d = -d;
382   
383   sprintf(dm, "%.0f*%06.3f", d, m);
384   return dm;
385 }
386
387 // Have to set the LatLon display type
388 //static char *(*fgLatLonFormat)(float) = toDM;
389 static char *(*fgLatLonFormat)(float);
390
391 char *coord_format_lat(float latitude)
392 {
393     static char buf[16];
394
395     sprintf(buf,"%s%c",
396 //      dmshh_format(latitude),
397 //      toDMS(latitude),
398 //      toDM(latitude),
399         fgLatLonFormat(latitude),           
400         latitude > 0 ? 'N' : 'S');
401     return buf;
402 }
403
404 char *coord_format_lon(float longitude)
405 {
406     static char buf[80];
407
408     sprintf(buf,"%s%c",
409 //      dmshh_format(longitude),
410 //      toDMS(longitude),
411 //      toDM(longitude),
412         fgLatLonFormat(longitude),
413         longitude > 0 ? 'E' : 'W');
414     return buf;
415 }
416
417 void fgLatLonFormatToggle( puObject *)
418 {
419     static int toggle = 0;
420
421     if ( toggle ) 
422         fgLatLonFormat = toDM;
423     else
424         fgLatLonFormat = toDMS;
425     
426     toggle = ~toggle;
427 }
428
429 #ifdef NOT_USED
430 char *coord_format_latlon(double latitude, double longitude)
431 {
432     static char buf[1024];
433
434     sprintf(buf,"%s%c %s%c",
435         dmshh_format(latitude),
436         latitude > 0 ? 'N' : 'S',
437         dmshh_format(longitude),
438         longitude > 0 ? 'E' : 'W');
439     return buf;
440 }
441 #endif
442
443
444 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
445 {
446     FG_LOG( FG_COCKPIT, FG_INFO, "Initializing cockpit subsystem" );
447
448     //  cockpit->code = 1;  /* It will be aircraft dependent */
449     //  cockpit->status = 0;
450
451     // If aircraft has HUD specified we will get the specs from its def
452     // file. For now we will depend upon hard coding in hud?
453     
454     // We must insure that the existing instrument link is purged.
455     // This is done by deleting the links in the list.
456     
457     // HI_Head is now a null pointer so we can generate a new list from the
458     // current aircraft.
459
460     fgHUDInit( cur_aircraft );
461     ac_cockpit = new fg_Cockpit();
462     
463     if ( current_options.get_panel_status() ) {
464         new FGPanel;
465     }
466
467     // Have to set the LatLon display type
468     fgLatLonFormat = toDM;
469     
470     FG_LOG( FG_COCKPIT, FG_INFO,
471         "  Code " << ac_cockpit->code() << " Status " 
472         << ac_cockpit->status() );
473
474 //  HUD_TextSize = (current_options.get_xsize() > 1000) ? 10 : 8;
475     HUD_TextSize = 10;
476     HUDtext = new fntRenderer();
477     HUDtext -> setFont      ( guiFntHandle ) ;
478     HUDtext -> setPointSize ( HUD_TextSize ) ;
479     HUD_TextList.setFont( HUDtext );
480
481     return true;
482 }
483
484
485 void fgCockpitUpdate( void ) {
486
487     int iwidth   = current_view.get_winWidth();
488     int iheight  = current_view.get_winHeight();
489     float width  = iwidth;
490     float height = iheight;
491     
492     FG_LOG( FG_COCKPIT, FG_DEBUG,
493         "Cockpit: code " << ac_cockpit->code() << " status " 
494         << ac_cockpit->status() );
495
496     if ( current_options.get_hud_status() ) {
497         // This will check the global hud linked list pointer.
498         // If these is anything to draw it will.
499         fgUpdateHUD();
500     }
501 #define DISPLAY_COUNTER
502 #ifdef DISPLAY_COUNTER
503     else
504     {
505         char buf[64];
506         float fps    =       get_frame_rate();
507 //      float tris   = fps * get_vfc_tris_drawn();
508 //      float culled = fps * get_vfc_tris_culled();
509 //      sprintf(buf,"%-4.1f  %7.0f  %7.0f", fps, tris, culled);
510         sprintf(buf,"%-5.1f", fps);
511
512         glMatrixMode(GL_PROJECTION);
513         glPushMatrix();
514         glLoadIdentity();
515         gluOrtho2D(0, width, 0, height);
516         glMatrixMode(GL_MODELVIEW);
517         glPushMatrix();
518         glLoadIdentity();
519
520         glDisable(GL_DEPTH_TEST);
521         glDisable(GL_LIGHTING);
522         
523         glColor3f (0.9, 0.4, 0.2);
524
525         guiFnt.drawString( buf,
526                            width/2 - guiFnt.getStringWidth(buf)/2,
527                            10 );
528         glEnable(GL_DEPTH_TEST);
529         glEnable(GL_LIGHTING);
530         glMatrixMode(GL_PROJECTION);
531         glPopMatrix();
532         glMatrixMode(GL_MODELVIEW);
533         glPopMatrix();
534     }
535 #endif // #ifdef DISPLAY_COUNTER
536     
537     if( current_options.get_panel_status() && 
538          (fabs( current_view.get_view_offset() ) < 0.2) )
539     {
540         xglViewport( 0, 0, iwidth, iheight );
541
542         FGPanel::OurPanel->Update();
543     }
544 }