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