]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/cockpit.cxx
3a88b6a5dc7d02e227bb620a57172bc89a336b33
[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/polar3d.hxx>
43
44 #include <Aircraft/aircraft.hxx>
45 #include <Include/general.hxx>
46 #include <FDM/ADA.hxx>
47 #include <Main/globals.hxx>
48 #include <Main/options.hxx>
49 #include <Scenery/scenery.hxx>
50 #include <Time/fg_timer.hxx>
51 #include <GUI/gui.h>
52
53 #include "cockpit.hxx"
54
55
56 // This is a structure that contains all data related to
57 // cockpit/panel/hud system
58
59 static pCockpit ac_cockpit;
60 // The following routines obtain information concerntin the aircraft's
61 // current state and return it to calling instrument display routines.
62 // They should eventually be member functions of the aircraft.
63 //
64
65 float get_latitude( void )
66 {
67     double lat;
68
69     lat = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
70
71     float flat = lat;
72     return(flat);
73
74 }
75
76 float get_lat_min( void )
77 {
78     double      a, d;
79
80     a = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;    
81     if (a < 0.0) {
82         a = -a;
83     }
84     d = (double) ( (int) a);
85     float lat_min = (a - d) * 60.0;
86     return(lat_min );
87 }
88
89
90 float get_longitude( void )
91 {
92     double lon;
93
94     lon = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
95
96     float flon = lon;
97     return(flon);
98 }
99
100
101 char*
102 get_formated_gmt_time( void )
103 {
104     static char buf[32];
105     const struct tm *p = globals->get_time_params()->getGmt();
106     sprintf( buf, "%d/%d/%4d %d:%02d:%02d", 
107          p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
108          p->tm_hour, p->tm_min, p->tm_sec);
109     return buf;
110 }
111
112
113 float get_long_min( void )
114 {
115     double  a, d;
116     a = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;   
117     if (a < 0.0) {
118         a = -a;
119     }
120     d = (double) ( (int) a);
121     float lon_min = (a - d) * 60.0; 
122     return(lon_min);
123 }
124
125 float get_throttleval( void )
126 {
127     float throttle = controls.get_throttle( 0 );
128     return (throttle);     // Hack limiting to one engine
129 }
130
131 float get_aileronval( void )
132 {
133     float aileronval = controls.get_aileron();
134     return (aileronval);
135 }
136
137 float get_elevatorval( void )
138 {
139     float elevator_val = (float)controls.get_elevator();
140     return elevator_val;
141 }
142
143 float get_elev_trimval( void )
144 {
145     float elevatorval = controls.get_elevator_trim();
146     return (elevatorval);
147 }
148
149 float get_rudderval( void )
150 {
151     float rudderval = controls.get_rudder();
152     return (rudderval);
153 }
154
155 float get_speed( void )
156 {
157     // Make an explicit function call.
158     float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
159         * current_options.get_speed_up();
160     return( speed );
161 }
162
163 float get_mach(void)
164 {
165         float mach=current_aircraft.fdm_state->get_Mach_number();
166         return mach;
167 }       
168
169 float get_aoa( void )
170 {
171     float aoa = current_aircraft.fdm_state->get_Alpha() * RAD_TO_DEG;
172     return( aoa );
173 }
174
175 float get_roll( void )
176 {
177     float roll = current_aircraft.fdm_state->get_Phi();
178     return( roll );
179 }
180
181 float get_pitch( void )
182 {
183     float pitch = current_aircraft.fdm_state->get_Theta();
184     return( pitch );
185 }
186
187 float get_heading( void )
188 {
189     float heading = (current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG);
190     return( heading );
191 }
192
193 float get_altitude( void )
194 {
195 //  FGState *f;
196     // double rough_elev;
197
198 //  current_aircraft.fdm_state
199     // rough_elev = mesh_altitude(f->get_Longitude() * RAD_TO_ARCSEC,
200     //                         f->get_Latitude()  * RAD_TO_ARCSEC);
201     float altitude;
202
203     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
204         altitude = current_aircraft.fdm_state->get_Altitude();
205     } else {
206         altitude = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER);
207     }
208     return altitude;
209 }
210
211 float get_agl( void )
212 {
213     float agl;
214
215     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
216         agl = (current_aircraft.fdm_state->get_Altitude()
217                - scenery.cur_elev * METER_TO_FEET);
218     } else {
219         agl = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER
220                - scenery.cur_elev);
221     }
222     return agl;
223 }
224
225 float get_sideslip( void )
226 {
227     float sideslip = current_aircraft.fdm_state->get_Beta();
228     return( sideslip );
229 }
230
231 float get_frame_rate( void )
232 {
233     float frame_rate = general.get_frame_rate();
234     return (frame_rate); 
235 }
236
237 float get_fov( void )
238 {
239     float fov = current_options.get_fov(); 
240     return (fov);
241 }
242
243 float get_vfc_ratio( void )
244 {
245     // float vfc = current_view.get_vfc_ratio();
246     // return (vfc);
247     return 0.0;
248 }
249
250 float get_vfc_tris_drawn   ( void )
251 {
252     // float rendered = current_view.get_tris_rendered();
253     // return (rendered);
254     return 0.0;
255 }
256
257 float get_vfc_tris_culled   ( void )
258 {
259     // float culled = current_view.get_tris_culled();
260     // return (culled);
261     return 0.0;
262 }
263
264 float get_climb_rate( void )
265 {
266     float climb_rate;
267     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
268         climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
269     } else {
270         climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * FEET_TO_METER * 60.0;
271     }
272     return (climb_rate);
273 }
274
275
276 float get_view_direction( void )
277 {
278     double view_off = FG_2PI - globals->get_current_view()->get_view_offset();
279     double view = ( current_aircraft.fdm_state->get_Psi() + view_off)
280         * RAD_TO_DEG;
281     
282     if(view > 360.)
283         view -= 360.;
284     else if(view<0.)
285         view += 360.;
286     
287     return view;
288 }
289
290 // $$$ begin - added, VS Renganathan 13 Oct 2K
291 #ifdef FIGHTER_HUD
292 float get_Vx   ( void )
293 {
294     float Vxx = current_aircraft.fdm_state->get_V_north_rel_ground();
295     return (Vxx);
296 }
297
298 float get_Vy   ( void )
299 {
300     float Vyy = current_aircraft.fdm_state->get_V_east_rel_ground();
301     return (Vyy);
302 }
303
304 float get_Vz   ( void )
305 {
306     float Vzz = current_aircraft.fdm_state->get_V_down_rel_ground();
307     return (Vzz);
308 }
309
310 float get_Ax   ( void )
311 {
312     float Ax = current_aircraft.fdm_state->get_V_dot_north();
313     return (Ax);
314 }
315
316 float get_Ay   ( void )
317 {
318     float Ay = current_aircraft.fdm_state->get_V_dot_east();
319     return (Ay);
320 }
321
322 float get_Az   ( void )
323 {
324     float Az = current_aircraft.fdm_state->get_V_dot_down();
325     return (Az);
326 }
327
328 float get_anzg   ( void )
329 {
330     float anzg = current_aircraft.fdm_state->get_N_Z_cg();
331     return (anzg);
332 }
333
334 int get_iaux1 (void)
335 {
336     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
337     return fdm->get_iaux1();
338 }
339
340 int get_iaux2 (void)
341 {
342     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
343     return fdm->get_iaux2();
344 }
345
346 int get_iaux3 (void)
347 {
348     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
349     return fdm->get_iaux3();
350 }
351
352 int get_iaux4 (void)
353 {
354     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
355     return fdm->get_iaux4();
356 }
357
358 int get_iaux5 (void)
359 {
360     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
361     return fdm->get_iaux5();
362 }
363
364 int get_iaux6 (void)
365 {
366     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
367     return fdm->get_iaux6();
368 }
369
370 int get_iaux7 (void)
371 {
372     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
373     return fdm->get_iaux7();
374 }
375
376 int get_iaux8 (void)
377 {
378     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
379     return fdm->get_iaux8();
380 }
381
382 int get_iaux9 (void)
383 {
384     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
385     return fdm->get_iaux9();
386 }
387
388 int get_iaux10 (void)
389 {
390     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
391     return fdm->get_iaux10();
392 }
393
394 int get_iaux11 (void)
395 {
396     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
397     return fdm->get_iaux11();
398 }
399
400 int get_iaux12 (void)
401 {
402      FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
403      return fdm->get_iaux12();
404 }
405
406 float get_aux1 (void)
407 {
408     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
409     return fdm->get_aux1();
410 }
411
412 float get_aux2 (void)
413 {
414     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
415     return fdm->get_aux2();
416 }
417
418 float get_aux3 (void)
419 {
420     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
421     return fdm->get_aux3();
422 }
423
424 float get_aux4 (void)
425 {
426     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
427     return fdm->get_aux4();
428 }
429
430 float get_aux5 (void)
431 {
432     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
433     return fdm->get_aux5();
434 }
435
436 float get_aux6 (void)
437 {
438     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
439     return fdm->get_aux6();
440 }
441
442 float get_aux7 (void)
443 {
444     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
445     return fdm->get_aux7();
446 }
447
448 float get_aux8 (void)
449 {
450     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
451     return fdm->get_aux8();
452 }
453
454 float get_aux9 (void)
455 {
456     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
457     return fdm->get_aux9();
458 }
459
460 float get_aux10 (void)
461 {
462     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
463     return fdm->get_aux10();
464 }
465
466 float get_aux11 (void)
467 {
468     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
469     return fdm->get_aux11();
470 }
471
472 float get_aux12 (void)
473 {
474     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
475     return fdm->get_aux12();
476 }
477
478 float get_aux13 (void)
479 {
480     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
481     return fdm->get_aux13();
482 }
483
484 float get_aux14 (void)
485 {
486     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
487     return fdm->get_aux14();
488 }
489
490 float get_aux15 (void)
491 {
492     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
493     return fdm->get_aux15();
494 }
495
496 float get_aux16 (void)
497 {
498     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
499     return fdm->get_aux16();
500 }
501
502 float get_aux17 (void)
503 {
504     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
505     return fdm->get_aux17();
506 }
507
508 float get_aux18 (void)
509 {
510     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
511     return fdm->get_aux18();
512 }
513 #endif
514 // $$$ end - added, VS Renganathan 13 Oct 2K
515
516
517 #ifdef NOT_USED
518 /****************************************************************************/
519 /* Convert degrees to dd mm'ss.s" (DMS-Format)                              */
520 /****************************************************************************/
521 char *dmshh_format(double degrees)
522 {
523     static char buf[16];    
524     int deg_part;
525     int min_part;
526     double sec_part;
527     
528     if (degrees < 0)
529       degrees = -degrees;
530
531     deg_part = degrees;
532     min_part = 60.0 * (degrees - deg_part);
533     sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
534
535     /* Round off hundredths */
536     if (sec_part + 0.005 >= 60.0)
537       sec_part -= 60.0, min_part += 1;
538     if (min_part >= 60)
539       min_part -= 60, deg_part += 1;
540
541     sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
542
543     return buf;
544 }
545 #endif // 0
546
547
548 /************************************************************************
549  Convert degrees to dd mm.mmm' (DMM-Format)
550  Description: Converts using a round-off factor tailored to the required
551  precision of the minutes field (three decimal places).  Round-off
552  prevents function from returning a minutes value of 60.
553
554  Input arguments: Coordinate value in decimal degrees
555
556 ************************************************************************/
557 static char *toDM(float dd)
558 {
559     static char  dm[16];
560     double tempdd;
561     double mn;
562     double sign = 1;
563     int deg;
564
565     if (dd < 0) {
566         sign = -1;
567     }
568     /* round for minutes expressed to three decimal places */
569     tempdd = fabs(dd) + (5.0E-4 / 60.0);
570     deg = (int)tempdd;
571     mn = fabs( (tempdd - (double)(deg)) * 60.0 - 4.999E-4 );
572     deg *= (int)sign;
573     sprintf(dm, "%d*%06.3f", deg, mn);
574     return dm;
575 }
576
577
578 /************************************************************************
579  Convert degrees to dd mm'ss.s'' (DMS-Format)
580  Description: Converts using a round-off factor tailored to the required
581  precision of the seconds field (one decimal place).  Round-off
582  prevents function from returning a seconds value of 60.
583
584  Input arguments: Coordinate value in decimal degrees
585
586 ************************************************************************/
587 static char *toDMS(float dd)
588 {
589     static char  dms[16];
590     double tempdd, tempmin;
591     int deg;
592     int mn;
593     double sec;
594     double sign = 1;
595
596     if(dd < 0) {
597         sign = -1;
598     }
599     /* round up for seconds expressed to one decimal place */
600     tempdd = fabs(dd) + (0.05 / 3600.0);
601     deg = (int)tempdd;
602     tempmin =  (tempdd - (double)(deg)) * 60.0;
603     mn = (int)tempmin;
604     sec = fabs( (tempmin - (double)(mn)) * 60.0 - 0.049 );
605     deg *= (int)sign;
606     sprintf(dms, "%d*%02d %04.1f", deg, mn, sec);
607     return dms;
608 }
609
610
611 // Have to set the LatLon display type
612 //static char *(*fgLatLonFormat)(float) = toDM;
613 static char *(*fgLatLonFormat)(float);
614
615 char *coord_format_lat(float latitude)
616 {
617     static char buf[16];
618
619     sprintf(buf,"%s%c",
620 //      dmshh_format(latitude),
621 //      toDMS(latitude),
622 //      toDM(latitude),
623         fgLatLonFormat(latitude),           
624         latitude > 0 ? 'N' : 'S');
625     return buf;
626 }
627
628 char *coord_format_lon(float longitude)
629 {
630     static char buf[80];
631
632     sprintf(buf,"%s%c",
633 //      dmshh_format(longitude),
634 //      toDMS(longitude),
635 //      toDM(longitude),
636         fgLatLonFormat(longitude),
637         longitude > 0 ? 'E' : 'W');
638     return buf;
639 }
640
641 void fgLatLonFormatToggle( puObject *)
642 {
643     static int toggle = 0;
644
645     if ( toggle ) 
646         fgLatLonFormat = toDM;
647     else
648         fgLatLonFormat = toDMS;
649     
650     toggle = ~toggle;
651 }
652
653 #ifdef NOT_USED
654 char *coord_format_latlon(double latitude, double longitude)
655 {
656     static char buf[1024];
657
658     sprintf(buf,"%s%c %s%c",
659         dmshh_format(latitude),
660         latitude > 0 ? 'N' : 'S',
661         dmshh_format(longitude),
662         longitude > 0 ? 'E' : 'W');
663     return buf;
664 }
665 #endif
666
667
668 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
669 {
670     FG_LOG( FG_COCKPIT, FG_INFO, "Initializing cockpit subsystem" );
671
672     //  cockpit->code = 1;  /* It will be aircraft dependent */
673     //  cockpit->status = 0;
674
675     // If aircraft has HUD specified we will get the specs from its def
676     // file. For now we will depend upon hard coding in hud?
677     
678     // We must insure that the existing instrument link is purged.
679     // This is done by deleting the links in the list.
680     
681     // HI_Head is now a null pointer so we can generate a new list from the
682     // current aircraft.
683
684     fgHUDInit( cur_aircraft );
685     ac_cockpit = new fg_Cockpit();
686     
687     // Have to set the LatLon display type
688     fgLatLonFormat = toDM;
689     
690     FG_LOG( FG_COCKPIT, FG_INFO,
691         "  Code " << ac_cockpit->code() << " Status " 
692         << ac_cockpit->status() );
693
694         return true;
695 }
696
697 void fgCockpitUpdate( void ) {
698
699     FG_LOG( FG_COCKPIT, FG_DEBUG,
700         "Cockpit: code " << ac_cockpit->code() << " status " 
701         << ac_cockpit->status() );
702
703         int iwidth   = globals->get_current_view()->get_winWidth();
704         int iheight  = globals->get_current_view()->get_winHeight();
705         float width  = iwidth;
706         float height = iheight;
707
708     if ( current_options.get_hud_status() ) {
709         // This will check the global hud linked list pointer.
710         // If these is anything to draw it will.
711         fgUpdateHUD();
712     }
713 #define DISPLAY_COUNTER
714 #ifdef DISPLAY_COUNTER
715     else
716     {
717         char buf[64];
718         float fps    =       get_frame_rate();
719 //      float tris   = fps * get_vfc_tris_drawn();
720 //      float culled = fps * get_vfc_tris_culled();
721 //      sprintf(buf,"%-4.1f  %7.0f  %7.0f", fps, tris, culled);
722         sprintf(buf,"%-5.1f", fps);
723
724         glMatrixMode(GL_PROJECTION);
725         glPushMatrix();
726         glLoadIdentity();
727         gluOrtho2D(0, width, 0, height);
728         glMatrixMode(GL_MODELVIEW);
729         glPushMatrix();
730         glLoadIdentity();
731
732         glDisable(GL_DEPTH_TEST);
733         glDisable(GL_LIGHTING);
734         
735         glColor3f (0.9, 0.4, 0.2);
736
737         guiFnt.drawString( buf,
738                            // width/2 - guiFnt.getStringWidth(buf)/2,
739                            int(width - guiFnt.getStringWidth(buf) - 10),
740                            10 );
741         glEnable(GL_DEPTH_TEST);
742         glEnable(GL_LIGHTING);
743         glMatrixMode(GL_PROJECTION);
744         glPopMatrix();
745         glMatrixMode(GL_MODELVIEW);
746         glPopMatrix();
747     }
748 #endif // #ifdef DISPLAY_COUNTER
749     
750     xglViewport( 0, 0, iwidth, iheight );
751
752     if (current_panel != 0)
753       current_panel->update();
754 }