1 // cockpit.cxx -- routines to draw a cockpit (initial draft)
3 // Written by Michele America, started September 1997.
5 // Copyright (C) 1997 Michele F. America - nomimarketing@mail.telepac.pt
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.
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.
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.
33 // #include <simgear/xgl/xgl.h>
39 #include <simgear/constants.h>
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/math/fg_random.h>
42 #include <simgear/math/polar3d.hxx>
44 #include <Aircraft/aircraft.hxx>
45 #include <Include/general.hxx>
46 #include <FDM/ADA.hxx>
47 #include <Main/globals.hxx>
48 #include <Scenery/scenery.hxx>
49 #include <Time/fg_timer.hxx>
52 #include "cockpit.hxx"
55 // This is a structure that contains all data related to
56 // cockpit/panel/hud system
58 static pCockpit ac_cockpit;
59 // The following routines obtain information concerntin the aircraft's
60 // current state and return it to calling instrument display routines.
61 // They should eventually be member functions of the aircraft.
64 float get_latitude( void )
68 lat = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
75 float get_lat_min( void )
79 a = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
83 d = (double) ( (int) a);
84 float lat_min = (a - d) * 60.0;
89 float get_longitude( void )
93 lon = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
101 get_formated_gmt_time( void )
104 const struct tm *p = globals->get_time_params()->getGmt();
105 sprintf( buf, "%d/%d/%4d %d:%02d:%02d",
106 p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
107 p->tm_hour, p->tm_min, p->tm_sec);
112 float get_long_min( void )
115 a = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
119 d = (double) ( (int) a);
120 float lon_min = (a - d) * 60.0;
124 float get_throttleval( void )
126 float throttle = controls.get_throttle( 0 );
127 return (throttle); // Hack limiting to one engine
130 float get_aileronval( void )
132 float aileronval = controls.get_aileron();
136 float get_elevatorval( void )
138 float elevator_val = (float)controls.get_elevator();
142 float get_elev_trimval( void )
144 float elevatorval = controls.get_elevator_trim();
145 return (elevatorval);
148 float get_rudderval( void )
150 float rudderval = controls.get_rudder();
154 float get_speed( void )
156 // Make an explicit function call.
157 float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
158 * globals->get_options()->get_speed_up();
164 float mach=current_aircraft.fdm_state->get_Mach_number();
168 float get_aoa( void )
170 float aoa = current_aircraft.fdm_state->get_Alpha() * RAD_TO_DEG;
174 float get_roll( void )
176 float roll = current_aircraft.fdm_state->get_Phi();
180 float get_pitch( void )
182 float pitch = current_aircraft.fdm_state->get_Theta();
186 float get_heading( void )
188 float heading = (current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG);
192 float get_altitude( void )
195 // double rough_elev;
197 // current_aircraft.fdm_state
198 // rough_elev = mesh_altitude(f->get_Longitude() * RAD_TO_ARCSEC,
199 // f->get_Latitude() * RAD_TO_ARCSEC);
202 if ( globals->get_options()->get_units() == FGOptions::FG_UNITS_FEET ) {
203 altitude = current_aircraft.fdm_state->get_Altitude();
205 altitude = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER);
210 float get_agl( void )
214 if ( globals->get_options()->get_units() == FGOptions::FG_UNITS_FEET ) {
215 agl = (current_aircraft.fdm_state->get_Altitude()
216 - scenery.cur_elev * METER_TO_FEET);
218 agl = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER
224 float get_sideslip( void )
226 float sideslip = current_aircraft.fdm_state->get_Beta();
230 float get_frame_rate( void )
232 float frame_rate = general.get_frame_rate();
236 float get_fov( void )
238 float fov = globals->get_current_view()->get_fov();
242 float get_vfc_ratio( void )
244 // float vfc = current_view.get_vfc_ratio();
249 float get_vfc_tris_drawn ( void )
251 // float rendered = current_view.get_tris_rendered();
252 // return (rendered);
256 float get_vfc_tris_culled ( void )
258 // float culled = current_view.get_tris_culled();
263 float get_climb_rate( void )
266 if ( globals->get_options()->get_units() == FGOptions::FG_UNITS_FEET ) {
267 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
269 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * FEET_TO_METER * 60.0;
275 float get_view_direction( void )
277 double view_off = FG_2PI - globals->get_current_view()->get_view_offset();
278 double view = ( current_aircraft.fdm_state->get_Psi() + view_off)
289 // $$$ begin - added, VS Renganathan 13 Oct 2K
291 float get_Vx ( void )
293 float Vxx = current_aircraft.fdm_state->get_V_north_rel_ground();
297 float get_Vy ( void )
299 float Vyy = current_aircraft.fdm_state->get_V_east_rel_ground();
303 float get_Vz ( void )
305 float Vzz = current_aircraft.fdm_state->get_V_down_rel_ground();
309 float get_Ax ( void )
311 float Ax = current_aircraft.fdm_state->get_V_dot_north();
315 float get_Ay ( void )
317 float Ay = current_aircraft.fdm_state->get_V_dot_east();
321 float get_Az ( void )
323 float Az = current_aircraft.fdm_state->get_V_dot_down();
327 float get_anzg ( void )
329 float anzg = current_aircraft.fdm_state->get_N_Z_cg();
335 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
336 return fdm->get_iaux1();
341 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
342 return fdm->get_iaux2();
347 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
348 return fdm->get_iaux3();
353 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
354 return fdm->get_iaux4();
359 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
360 return fdm->get_iaux5();
365 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
366 return fdm->get_iaux6();
371 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
372 return fdm->get_iaux7();
377 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
378 return fdm->get_iaux8();
383 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
384 return fdm->get_iaux9();
387 int get_iaux10 (void)
389 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
390 return fdm->get_iaux10();
393 int get_iaux11 (void)
395 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
396 return fdm->get_iaux11();
399 int get_iaux12 (void)
401 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
402 return fdm->get_iaux12();
405 float get_aux1 (void)
407 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
408 return fdm->get_aux1();
411 float get_aux2 (void)
413 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
414 return fdm->get_aux2();
417 float get_aux3 (void)
419 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
420 return fdm->get_aux3();
423 float get_aux4 (void)
425 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
426 return fdm->get_aux4();
429 float get_aux5 (void)
431 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
432 return fdm->get_aux5();
435 float get_aux6 (void)
437 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
438 return fdm->get_aux6();
441 float get_aux7 (void)
443 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
444 return fdm->get_aux7();
447 float get_aux8 (void)
449 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
450 return fdm->get_aux8();
453 float get_aux9 (void)
455 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
456 return fdm->get_aux9();
459 float get_aux10 (void)
461 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
462 return fdm->get_aux10();
465 float get_aux11 (void)
467 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
468 return fdm->get_aux11();
471 float get_aux12 (void)
473 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
474 return fdm->get_aux12();
477 float get_aux13 (void)
479 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
480 return fdm->get_aux13();
483 float get_aux14 (void)
485 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
486 return fdm->get_aux14();
489 float get_aux15 (void)
491 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
492 return fdm->get_aux15();
495 float get_aux16 (void)
497 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
498 return fdm->get_aux16();
501 float get_aux17 (void)
503 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
504 return fdm->get_aux17();
507 float get_aux18 (void)
509 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
510 return fdm->get_aux18();
513 // $$$ end - added, VS Renganathan 13 Oct 2K
517 /****************************************************************************/
518 /* Convert degrees to dd mm'ss.s" (DMS-Format) */
519 /****************************************************************************/
520 char *dmshh_format(double degrees)
531 min_part = 60.0 * (degrees - deg_part);
532 sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
534 /* Round off hundredths */
535 if (sec_part + 0.005 >= 60.0)
536 sec_part -= 60.0, min_part += 1;
538 min_part -= 60, deg_part += 1;
540 sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
547 /************************************************************************
548 Convert degrees to dd mm.mmm' (DMM-Format)
549 Description: Converts using a round-off factor tailored to the required
550 precision of the minutes field (three decimal places). Round-off
551 prevents function from returning a minutes value of 60.
553 Input arguments: Coordinate value in decimal degrees
555 ************************************************************************/
556 static char *toDM(float dd)
567 /* round for minutes expressed to three decimal places */
568 tempdd = fabs(dd) + (5.0E-4 / 60.0);
570 mn = fabs( (tempdd - (double)(deg)) * 60.0 - 4.999E-4 );
572 sprintf(dm, "%d*%06.3f", deg, mn);
577 /************************************************************************
578 Convert degrees to dd mm'ss.s'' (DMS-Format)
579 Description: Converts using a round-off factor tailored to the required
580 precision of the seconds field (one decimal place). Round-off
581 prevents function from returning a seconds value of 60.
583 Input arguments: Coordinate value in decimal degrees
585 ************************************************************************/
586 static char *toDMS(float dd)
589 double tempdd, tempmin;
598 /* round up for seconds expressed to one decimal place */
599 tempdd = fabs(dd) + (0.05 / 3600.0);
601 tempmin = (tempdd - (double)(deg)) * 60.0;
603 sec = fabs( (tempmin - (double)(mn)) * 60.0 - 0.049 );
605 sprintf(dms, "%d*%02d %04.1f", deg, mn, sec);
610 // Have to set the LatLon display type
611 //static char *(*fgLatLonFormat)(float) = toDM;
612 static char *(*fgLatLonFormat)(float);
614 char *coord_format_lat(float latitude)
619 // dmshh_format(latitude),
622 fgLatLonFormat(latitude),
623 latitude > 0 ? 'N' : 'S');
627 char *coord_format_lon(float longitude)
632 // dmshh_format(longitude),
635 fgLatLonFormat(longitude),
636 longitude > 0 ? 'E' : 'W');
640 void fgLatLonFormatToggle( puObject *)
642 static int toggle = 0;
645 fgLatLonFormat = toDM;
647 fgLatLonFormat = toDMS;
653 char *coord_format_latlon(double latitude, double longitude)
655 static char buf[1024];
657 sprintf(buf,"%s%c %s%c",
658 dmshh_format(latitude),
659 latitude > 0 ? 'N' : 'S',
660 dmshh_format(longitude),
661 longitude > 0 ? 'E' : 'W');
667 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
669 FG_LOG( FG_COCKPIT, FG_INFO, "Initializing cockpit subsystem" );
671 // cockpit->code = 1; /* It will be aircraft dependent */
672 // cockpit->status = 0;
674 // If aircraft has HUD specified we will get the specs from its def
675 // file. For now we will depend upon hard coding in hud?
677 // We must insure that the existing instrument link is purged.
678 // This is done by deleting the links in the list.
680 // HI_Head is now a null pointer so we can generate a new list from the
683 fgHUDInit( cur_aircraft );
684 ac_cockpit = new fg_Cockpit();
686 // Have to set the LatLon display type
687 fgLatLonFormat = toDM;
689 FG_LOG( FG_COCKPIT, FG_INFO,
690 " Code " << ac_cockpit->code() << " Status "
691 << ac_cockpit->status() );
696 void fgCockpitUpdate( void ) {
698 FG_LOG( FG_COCKPIT, FG_DEBUG,
699 "Cockpit: code " << ac_cockpit->code() << " status "
700 << ac_cockpit->status() );
702 int iwidth = globals->get_options()->get_xsize();
703 int iheight = globals->get_options()->get_ysize();
704 float width = iwidth;
705 float height = iheight;
707 if ( globals->get_options()->get_hud_status() ) {
708 // This will check the global hud linked list pointer.
709 // If these is anything to draw it will.
712 #define DISPLAY_COUNTER
713 #ifdef DISPLAY_COUNTER
717 float fps = get_frame_rate();
718 // float tris = fps * get_vfc_tris_drawn();
719 // float culled = fps * get_vfc_tris_culled();
720 // sprintf(buf,"%-4.1f %7.0f %7.0f", fps, tris, culled);
721 sprintf(buf,"%-5.1f", fps);
723 glMatrixMode( GL_PROJECTION );
726 gluOrtho2D( 0, globals->get_options()->get_xsize(),
727 0, globals->get_options()->get_ysize() );
728 glMatrixMode( GL_MODELVIEW );
732 glDisable( GL_DEPTH_TEST );
733 glDisable( GL_LIGHTING );
735 glColor3f( 0.9, 0.4, 0.2 );
737 guiFnt.drawString( buf,
738 int(width - guiFnt.getStringWidth(buf) - 10),
740 glEnable( GL_DEPTH_TEST );
741 glEnable( GL_LIGHTING );
742 glMatrixMode( GL_PROJECTION );
744 glMatrixMode( GL_MODELVIEW );
747 #endif // #ifdef DISPLAY_COUNTER
750 globals->get_options()->get_xsize(),
751 globals->get_options()->get_ysize() );
753 if (current_panel != 0)
754 current_panel->update();