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.
38 #include <simgear/constants.h>
39 #include <simgear/debug/logstream.hxx>
40 #include <simgear/math/polar3d.hxx>
41 #include <simgear/misc/props.hxx>
43 #include <Aircraft/aircraft.hxx>
44 #include <Include/general.hxx>
45 #include <FDM/ADA.hxx>
46 #include <Main/globals.hxx>
47 #include <Main/fg_props.hxx>
48 #include <Main/viewmgr.hxx>
49 #include <Scenery/scenery.hxx>
50 #include <Time/fg_timer.hxx>
53 #include "cockpit.hxx"
56 // This is a structure that contains all data related to
57 // cockpit/panel/hud system
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.
65 float get_latitude( void )
69 lat = current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
76 float get_lat_min( void )
80 a = current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
84 d = (double) ( (int) a);
85 float lat_min = (a - d) * 60.0;
90 float get_longitude( void )
94 lon = current_aircraft.fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
102 get_formated_gmt_time( void )
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);
113 float get_long_min( void )
116 a = current_aircraft.fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
120 d = (double) ( (int) a);
121 float lon_min = (a - d) * 60.0;
125 float get_throttleval( void )
127 float throttle = globals->get_controls()->get_throttle( 0 );
128 return (throttle); // Hack limiting to one engine
131 float get_aileronval( void )
133 float aileronval = globals->get_controls()->get_aileron();
137 float get_elevatorval( void )
139 float elevator_val = (float)globals->get_controls()->get_elevator();
143 float get_elev_trimval( void )
145 float elevatorval = globals->get_controls()->get_elevator_trim();
146 return (elevatorval);
149 float get_rudderval( void )
151 float rudderval = globals->get_controls()->get_rudder();
155 float get_speed( void )
157 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
159 float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
160 * speedup_node->getIntValue();
167 float mach=current_aircraft.fdm_state->get_Mach_number();
171 float get_aoa( void )
173 float aoa = current_aircraft.fdm_state->get_Alpha() * SGD_RADIANS_TO_DEGREES;
177 float get_roll( void )
179 float roll = current_aircraft.fdm_state->get_Phi();
183 float get_pitch( void )
185 float pitch = current_aircraft.fdm_state->get_Theta();
189 float get_heading( void )
191 float heading = (current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES);
195 float get_altitude( void )
197 static const SGPropertyNode *startup_units_node
198 = fgGetNode("/sim/startup/units");
202 if ( startup_units_node->getStringValue() == "feet" ) {
203 altitude = current_aircraft.fdm_state->get_Altitude();
205 altitude = (current_aircraft.fdm_state->get_Altitude()
212 float get_agl( void )
216 if ( fgGetString("/sim/startup/units") == "feet" ) {
217 agl = (current_aircraft.fdm_state->get_Altitude()
218 - scenery.cur_elev * SG_METER_TO_FEET);
220 agl = (current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
226 float get_sideslip( void )
228 float sideslip = current_aircraft.fdm_state->get_Beta();
232 float get_frame_rate( void )
234 float frame_rate = general.get_frame_rate();
238 float get_fov( void )
240 float fov = globals->get_current_view()->get_fov();
244 float get_vfc_ratio( void )
246 // float vfc = current_view.get_vfc_ratio();
251 float get_vfc_tris_drawn ( void )
253 // float rendered = current_view.get_tris_rendered();
254 // return (rendered);
258 float get_vfc_tris_culled ( void )
260 // float culled = current_view.get_tris_culled();
265 float get_climb_rate( void )
268 if ( fgGetString("/sim/startup/units") == "feet" ) {
269 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
271 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * SG_FEET_TO_METER * 60.0;
277 float get_view_direction( void )
279 double view_off = SGD_2PI - globals->get_current_view()->get_view_offset();
280 double view = ( current_aircraft.fdm_state->get_Psi() + view_off)
281 * SGD_RADIANS_TO_DEGREES;
291 // $$$ begin - added, VS Renganathan 13 Oct 2K
292 // #ifdef FIGHTER_HUD
293 float get_Vx ( void )
295 // CLO - 5 Jan 2000 - something needs to get addressed here
296 // float Vxx = current_aircraft.fdm_state->get_V_north_rel_ground();
301 float get_Vy ( void )
303 // CLO - 5 Jan 2000 - something needs to get addressed here
304 // float Vyy = current_aircraft.fdm_state->get_V_east_rel_ground();
309 float get_Vz ( void )
311 // CLO - 5 Jan 2000 - something needs to get addressed here
312 // float Vzz = current_aircraft.fdm_state->get_V_down_rel_ground();
317 float get_Ax ( void )
319 float Ax = current_aircraft.fdm_state->get_V_dot_north();
323 float get_Ay ( void )
325 float Ay = current_aircraft.fdm_state->get_V_dot_east();
329 float get_Az ( void )
331 float Az = current_aircraft.fdm_state->get_V_dot_down();
335 float get_anzg ( void )
337 float anzg = current_aircraft.fdm_state->get_N_Z_cg();
343 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
344 return fdm->get_iaux1();
349 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
350 return fdm->get_iaux2();
355 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
356 return fdm->get_iaux3();
361 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
362 return fdm->get_iaux4();
367 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
368 return fdm->get_iaux5();
373 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
374 return fdm->get_iaux6();
379 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
380 return fdm->get_iaux7();
385 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
386 return fdm->get_iaux8();
391 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
392 return fdm->get_iaux9();
395 int get_iaux10 (void)
397 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
398 return fdm->get_iaux10();
401 int get_iaux11 (void)
403 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
404 return fdm->get_iaux11();
407 int get_iaux12 (void)
409 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
410 return fdm->get_iaux12();
413 float get_aux1 (void)
415 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
416 return fdm->get_aux1();
419 float get_aux2 (void)
421 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
422 return fdm->get_aux2();
425 float get_aux3 (void)
427 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
428 return fdm->get_aux3();
431 float get_aux4 (void)
433 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
434 return fdm->get_aux4();
437 float get_aux5 (void)
439 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
440 return fdm->get_aux5();
443 float get_aux6 (void)
445 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
446 return fdm->get_aux6();
449 float get_aux7 (void)
451 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
452 return fdm->get_aux7();
455 float get_aux8 (void)
457 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
458 return fdm->get_aux8();
461 float get_aux9 (void)
463 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
464 return fdm->get_aux9();
467 float get_aux10 (void)
469 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
470 return fdm->get_aux10();
473 float get_aux11 (void)
475 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
476 return fdm->get_aux11();
479 float get_aux12 (void)
481 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
482 return fdm->get_aux12();
485 float get_aux13 (void)
487 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
488 return fdm->get_aux13();
491 float get_aux14 (void)
493 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
494 return fdm->get_aux14();
497 float get_aux15 (void)
499 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
500 return fdm->get_aux15();
503 float get_aux16 (void)
505 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
506 return fdm->get_aux16();
509 float get_aux17 (void)
511 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
512 return fdm->get_aux17();
515 float get_aux18 (void)
517 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
518 return fdm->get_aux18();
521 // $$$ end - added, VS Renganathan 13 Oct 2K
525 /****************************************************************************/
526 /* Convert degrees to dd mm'ss.s" (DMS-Format) */
527 /****************************************************************************/
528 char *dmshh_format(double degrees)
539 min_part = 60.0 * (degrees - deg_part);
540 sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
542 /* Round off hundredths */
543 if (sec_part + 0.005 >= 60.0)
544 sec_part -= 60.0, min_part += 1;
546 min_part -= 60, deg_part += 1;
548 sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
555 /************************************************************************
556 Convert degrees to dd mm.mmm' (DMM-Format)
557 Description: Converts using a round-off factor tailored to the required
558 precision of the minutes field (three decimal places). Round-off
559 prevents function from returning a minutes value of 60.
561 Input arguments: Coordinate value in decimal degrees
563 ************************************************************************/
564 static char *toDM(float dd)
575 /* round for minutes expressed to three decimal places */
576 tempdd = fabs(dd) + (5.0E-4 / 60.0);
578 mn = fabs( (tempdd - (double)(deg)) * 60.0 - 4.999E-4 );
580 sprintf(dm, "%d*%06.3f", deg, mn);
585 /************************************************************************
586 Convert degrees to dd mm'ss.s'' (DMS-Format)
587 Description: Converts using a round-off factor tailored to the required
588 precision of the seconds field (one decimal place). Round-off
589 prevents function from returning a seconds value of 60.
591 Input arguments: Coordinate value in decimal degrees
593 ************************************************************************/
594 static char *toDMS(float dd)
597 double tempdd, tempmin;
606 /* round up for seconds expressed to one decimal place */
607 tempdd = fabs(dd) + (0.05 / 3600.0);
609 tempmin = (tempdd - (double)(deg)) * 60.0;
611 sec = fabs( (tempmin - (double)(mn)) * 60.0 - 0.049 );
613 sprintf(dms, "%d*%02d %04.1f", deg, mn, sec);
618 // Have to set the LatLon display type
619 //static char *(*fgLatLonFormat)(float) = toDM;
620 static char *(*fgLatLonFormat)(float);
622 char *coord_format_lat(float latitude)
627 // dmshh_format(latitude),
630 fgLatLonFormat(latitude),
631 latitude > 0 ? 'N' : 'S');
635 char *coord_format_lon(float longitude)
640 // dmshh_format(longitude),
643 fgLatLonFormat(longitude),
644 longitude > 0 ? 'E' : 'W');
648 void fgLatLonFormatToggle( puObject *)
650 static int toggle = 0;
653 fgLatLonFormat = toDM;
655 fgLatLonFormat = toDMS;
661 char *coord_format_latlon(double latitude, double longitude)
663 static char buf[1024];
665 sprintf(buf,"%s%c %s%c",
666 dmshh_format(latitude),
667 latitude > 0 ? 'N' : 'S',
668 dmshh_format(longitude),
669 longitude > 0 ? 'E' : 'W');
675 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
677 SG_LOG( SG_COCKPIT, SG_INFO, "Initializing cockpit subsystem" );
679 // cockpit->code = 1; /* It will be aircraft dependent */
680 // cockpit->status = 0;
682 // If aircraft has HUD specified we will get the specs from its def
683 // file. For now we will depend upon hard coding in hud?
685 // We must insure that the existing instrument link is purged.
686 // This is done by deleting the links in the list.
688 // HI_Head is now a null pointer so we can generate a new list from the
691 fgHUDInit( cur_aircraft );
692 ac_cockpit = new fg_Cockpit();
694 // Have to set the LatLon display type
695 fgLatLonFormat = toDM;
697 SG_LOG( SG_COCKPIT, SG_INFO,
698 " Code " << ac_cockpit->code() << " Status "
699 << ac_cockpit->status() );
704 void fgCockpitUpdate( void ) {
706 SG_LOG( SG_COCKPIT, SG_DEBUG,
707 "Cockpit: code " << ac_cockpit->code() << " status "
708 << ac_cockpit->status() );
710 static const SGPropertyNode * xsize_node = fgGetNode("/sim/startup/xsize");
711 static const SGPropertyNode * ysize_node = fgGetNode("/sim/startup/ysize");
712 static const SGPropertyNode * hud_visibility_node
713 = fgGetNode("/sim/hud/visibility");
715 int iwidth = xsize_node->getIntValue();
716 int iheight = ysize_node->getIntValue();
717 float width = iwidth;
718 // float height = iheight;
720 // FIXME: inefficient
721 if ( hud_visibility_node->getBoolValue() ) {
722 // This will check the global hud linked list pointer.
723 // If these is anything to draw it will.
727 #define DISPLAY_COUNTER
728 #ifdef DISPLAY_COUNTER
732 float fps = get_frame_rate();
733 // float tris = fps * get_vfc_tris_drawn();
734 // float culled = fps * get_vfc_tris_culled();
735 // sprintf(buf,"%-4.1f %7.0f %7.0f", fps, tris, culled);
736 sprintf(buf,"%-5.1f", fps);
738 glMatrixMode( GL_PROJECTION );
741 gluOrtho2D( 0, iwidth, 0, iheight );
742 glMatrixMode( GL_MODELVIEW );
746 glDisable( GL_DEPTH_TEST );
747 glDisable( GL_LIGHTING );
749 glColor3f( 0.9, 0.4, 0.2 );
751 guiFnt.drawString( buf,
752 int(width - guiFnt.getStringWidth(buf) - 10),
754 glEnable( GL_DEPTH_TEST );
755 glEnable( GL_LIGHTING );
756 glMatrixMode( GL_PROJECTION );
758 glMatrixMode( GL_MODELVIEW );
761 #endif // #ifdef DISPLAY_COUNTER
763 glViewport( 0, 0, iwidth, iheight );