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 <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() * SGD_RADIANS_TO_DEGREES;
75 float get_lat_min( void )
79 a = current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
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() * SGD_RADIANS_TO_DEGREES;
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() * SGD_RADIANS_TO_DEGREES;
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 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
158 float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
159 * speedup_node->getIntValue();
166 float mach=current_aircraft.fdm_state->get_Mach_number();
170 float get_aoa( void )
172 float aoa = current_aircraft.fdm_state->get_Alpha() * SGD_RADIANS_TO_DEGREES;
176 float get_roll( void )
178 float roll = current_aircraft.fdm_state->get_Phi();
182 float get_pitch( void )
184 float pitch = current_aircraft.fdm_state->get_Theta();
188 float get_heading( void )
190 float heading = (current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES);
194 float get_altitude( void )
196 static const SGPropertyNode *startup_units_node
197 = fgGetNode("/sim/startup/units");
201 if ( startup_units_node->getStringValue() == "feet" ) {
202 altitude = current_aircraft.fdm_state->get_Altitude();
204 altitude = (current_aircraft.fdm_state->get_Altitude()
211 float get_agl( void )
215 if ( fgGetString("/sim/startup/units") == "feet" ) {
216 agl = (current_aircraft.fdm_state->get_Altitude()
217 - scenery.cur_elev * SG_METER_TO_FEET);
219 agl = (current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
225 float get_sideslip( void )
227 float sideslip = current_aircraft.fdm_state->get_Beta();
231 float get_frame_rate( void )
233 float frame_rate = general.get_frame_rate();
237 float get_fov( void )
239 float fov = globals->get_current_view()->get_fov();
243 float get_vfc_ratio( void )
245 // float vfc = current_view.get_vfc_ratio();
250 float get_vfc_tris_drawn ( void )
252 // float rendered = current_view.get_tris_rendered();
253 // return (rendered);
257 float get_vfc_tris_culled ( void )
259 // float culled = current_view.get_tris_culled();
264 float get_climb_rate( void )
267 if ( fgGetString("/sim/startup/units") == "feet" ) {
268 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
270 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * SG_FEET_TO_METER * 60.0;
276 float get_view_direction( void )
278 double view_off = SGD_2PI - globals->get_current_view()->get_view_offset();
279 double view = ( current_aircraft.fdm_state->get_Psi() + view_off)
280 * SGD_RADIANS_TO_DEGREES;
290 // $$$ begin - added, VS Renganathan 13 Oct 2K
291 // #ifdef FIGHTER_HUD
292 float get_Vx ( void )
294 // CLO - 5 Jan 2000 - something needs to get addressed here
295 // float Vxx = current_aircraft.fdm_state->get_V_north_rel_ground();
300 float get_Vy ( void )
302 // CLO - 5 Jan 2000 - something needs to get addressed here
303 // float Vyy = current_aircraft.fdm_state->get_V_east_rel_ground();
308 float get_Vz ( void )
310 // CLO - 5 Jan 2000 - something needs to get addressed here
311 // float Vzz = current_aircraft.fdm_state->get_V_down_rel_ground();
316 float get_Ax ( void )
318 float Ax = current_aircraft.fdm_state->get_V_dot_north();
322 float get_Ay ( void )
324 float Ay = current_aircraft.fdm_state->get_V_dot_east();
328 float get_Az ( void )
330 float Az = current_aircraft.fdm_state->get_V_dot_down();
334 float get_anzg ( void )
336 float anzg = current_aircraft.fdm_state->get_N_Z_cg();
342 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
343 return fdm->get_iaux1();
348 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
349 return fdm->get_iaux2();
354 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
355 return fdm->get_iaux3();
360 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
361 return fdm->get_iaux4();
366 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
367 return fdm->get_iaux5();
372 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
373 return fdm->get_iaux6();
378 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
379 return fdm->get_iaux7();
384 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
385 return fdm->get_iaux8();
390 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
391 return fdm->get_iaux9();
394 int get_iaux10 (void)
396 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
397 return fdm->get_iaux10();
400 int get_iaux11 (void)
402 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
403 return fdm->get_iaux11();
406 int get_iaux12 (void)
408 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
409 return fdm->get_iaux12();
412 float get_aux1 (void)
414 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
415 return fdm->get_aux1();
418 float get_aux2 (void)
420 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
421 return fdm->get_aux2();
424 float get_aux3 (void)
426 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
427 return fdm->get_aux3();
430 float get_aux4 (void)
432 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
433 return fdm->get_aux4();
436 float get_aux5 (void)
438 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
439 return fdm->get_aux5();
442 float get_aux6 (void)
444 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
445 return fdm->get_aux6();
448 float get_aux7 (void)
450 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
451 return fdm->get_aux7();
454 float get_aux8 (void)
456 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
457 return fdm->get_aux8();
460 float get_aux9 (void)
462 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
463 return fdm->get_aux9();
466 float get_aux10 (void)
468 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
469 return fdm->get_aux10();
472 float get_aux11 (void)
474 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
475 return fdm->get_aux11();
478 float get_aux12 (void)
480 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
481 return fdm->get_aux12();
484 float get_aux13 (void)
486 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
487 return fdm->get_aux13();
490 float get_aux14 (void)
492 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
493 return fdm->get_aux14();
496 float get_aux15 (void)
498 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
499 return fdm->get_aux15();
502 float get_aux16 (void)
504 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
505 return fdm->get_aux16();
508 float get_aux17 (void)
510 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
511 return fdm->get_aux17();
514 float get_aux18 (void)
516 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
517 return fdm->get_aux18();
520 // $$$ end - added, VS Renganathan 13 Oct 2K
524 /****************************************************************************/
525 /* Convert degrees to dd mm'ss.s" (DMS-Format) */
526 /****************************************************************************/
527 char *dmshh_format(double degrees)
538 min_part = 60.0 * (degrees - deg_part);
539 sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
541 /* Round off hundredths */
542 if (sec_part + 0.005 >= 60.0)
543 sec_part -= 60.0, min_part += 1;
545 min_part -= 60, deg_part += 1;
547 sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
554 /************************************************************************
555 Convert degrees to dd mm.mmm' (DMM-Format)
556 Description: Converts using a round-off factor tailored to the required
557 precision of the minutes field (three decimal places). Round-off
558 prevents function from returning a minutes value of 60.
560 Input arguments: Coordinate value in decimal degrees
562 ************************************************************************/
563 static char *toDM(float dd)
574 /* round for minutes expressed to three decimal places */
575 tempdd = fabs(dd) + (5.0E-4 / 60.0);
577 mn = fabs( (tempdd - (double)(deg)) * 60.0 - 4.999E-4 );
579 sprintf(dm, "%d*%06.3f", deg, mn);
584 /************************************************************************
585 Convert degrees to dd mm'ss.s'' (DMS-Format)
586 Description: Converts using a round-off factor tailored to the required
587 precision of the seconds field (one decimal place). Round-off
588 prevents function from returning a seconds value of 60.
590 Input arguments: Coordinate value in decimal degrees
592 ************************************************************************/
593 static char *toDMS(float dd)
596 double tempdd, tempmin;
605 /* round up for seconds expressed to one decimal place */
606 tempdd = fabs(dd) + (0.05 / 3600.0);
608 tempmin = (tempdd - (double)(deg)) * 60.0;
610 sec = fabs( (tempmin - (double)(mn)) * 60.0 - 0.049 );
612 sprintf(dms, "%d*%02d %04.1f", deg, mn, sec);
617 // Have to set the LatLon display type
618 //static char *(*fgLatLonFormat)(float) = toDM;
619 static char *(*fgLatLonFormat)(float);
621 char *coord_format_lat(float latitude)
626 // dmshh_format(latitude),
629 fgLatLonFormat(latitude),
630 latitude > 0 ? 'N' : 'S');
634 char *coord_format_lon(float longitude)
639 // dmshh_format(longitude),
642 fgLatLonFormat(longitude),
643 longitude > 0 ? 'E' : 'W');
647 void fgLatLonFormatToggle( puObject *)
649 static int toggle = 0;
652 fgLatLonFormat = toDM;
654 fgLatLonFormat = toDMS;
660 char *coord_format_latlon(double latitude, double longitude)
662 static char buf[1024];
664 sprintf(buf,"%s%c %s%c",
665 dmshh_format(latitude),
666 latitude > 0 ? 'N' : 'S',
667 dmshh_format(longitude),
668 longitude > 0 ? 'E' : 'W');
674 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
676 SG_LOG( SG_COCKPIT, SG_INFO, "Initializing cockpit subsystem" );
678 // cockpit->code = 1; /* It will be aircraft dependent */
679 // cockpit->status = 0;
681 // If aircraft has HUD specified we will get the specs from its def
682 // file. For now we will depend upon hard coding in hud?
684 // We must insure that the existing instrument link is purged.
685 // This is done by deleting the links in the list.
687 // HI_Head is now a null pointer so we can generate a new list from the
690 fgHUDInit( cur_aircraft );
691 ac_cockpit = new fg_Cockpit();
693 // Have to set the LatLon display type
694 fgLatLonFormat = toDM;
696 SG_LOG( SG_COCKPIT, SG_INFO,
697 " Code " << ac_cockpit->code() << " Status "
698 << ac_cockpit->status() );
703 void fgCockpitUpdate( void ) {
705 SG_LOG( SG_COCKPIT, SG_DEBUG,
706 "Cockpit: code " << ac_cockpit->code() << " status "
707 << ac_cockpit->status() );
709 static const SGPropertyNode * xsize_node = fgGetNode("/sim/startup/xsize");
710 static const SGPropertyNode * ysize_node = fgGetNode("/sim/startup/ysize");
711 static const SGPropertyNode * hud_visibility_node
712 = fgGetNode("/sim/hud/visibility");
714 int iwidth = xsize_node->getIntValue();
715 int iheight = ysize_node->getIntValue();
716 float width = iwidth;
717 // float height = iheight;
719 // FIXME: inefficient
720 if ( hud_visibility_node->getBoolValue() ) {
721 // This will check the global hud linked list pointer.
722 // If these is anything to draw it will.
725 #define DISPLAY_COUNTER
726 #ifdef DISPLAY_COUNTER
730 float fps = get_frame_rate();
731 // float tris = fps * get_vfc_tris_drawn();
732 // float culled = fps * get_vfc_tris_culled();
733 // sprintf(buf,"%-4.1f %7.0f %7.0f", fps, tris, culled);
734 sprintf(buf,"%-5.1f", fps);
736 glMatrixMode( GL_PROJECTION );
739 gluOrtho2D( 0, iwidth, 0, iheight );
740 glMatrixMode( GL_MODELVIEW );
744 glDisable( GL_DEPTH_TEST );
745 glDisable( GL_LIGHTING );
747 glColor3f( 0.9, 0.4, 0.2 );
749 guiFnt.drawString( buf,
750 int(width - guiFnt.getStringWidth(buf) - 10),
752 glEnable( GL_DEPTH_TEST );
753 glEnable( GL_LIGHTING );
754 glMatrixMode( GL_PROJECTION );
756 glMatrixMode( GL_MODELVIEW );
759 #endif // #ifdef DISPLAY_COUNTER
761 glViewport( 0, 0, iwidth, iheight );