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>
42 #include <simgear/timing/sg_time.hxx>
44 #include <Aircraft/aircraft.hxx>
45 #include <Include/general.hxx>
46 #include <FDM/ADA.hxx>
47 #include <Main/globals.hxx>
48 #include <Main/fg_props.hxx>
49 #include <Main/viewmgr.hxx>
50 #include <Scenery/scenery.hxx>
51 #include <Time/fg_timer.hxx>
54 #include "cockpit.hxx"
57 // This is a structure that contains all data related to
58 // cockpit/panel/hud system
60 static pCockpit ac_cockpit;
61 // The following routines obtain information concerntin the aircraft's
62 // current state and return it to calling instrument display routines.
63 // They should eventually be member functions of the aircraft.
66 float get_latitude( void )
68 return current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
71 float get_lat_min( void )
75 a = current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
79 d = (double) ( (int) a);
80 float lat_min = (a - d) * 60.0;
86 float get_longitude( void )
88 return current_aircraft.fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
93 get_formated_gmt_time( void )
96 const struct tm *p = globals->get_time_params()->getGmt();
97 sprintf( buf, "%d/%d/%4d %d:%02d:%02d",
98 p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
99 p->tm_hour, p->tm_min, p->tm_sec);
105 float get_long_min( void )
108 a = current_aircraft.fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
112 d = (double) ( (int) a);
113 float lon_min = (a - d) * 60.0;
118 float get_throttleval( void )
120 // Hack limiting to one engine
121 return globals->get_controls()->get_throttle( 0 );
124 float get_aileronval( void )
126 return globals->get_controls()->get_aileron();
129 float get_elevatorval( void )
131 return globals->get_controls()->get_elevator();
134 float get_elev_trimval( void )
136 return globals->get_controls()->get_elevator_trim();
139 float get_rudderval( void )
141 return globals->get_controls()->get_rudder();
144 float get_speed( void )
146 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
148 float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
149 * speedup_node->getIntValue();
156 return current_aircraft.fdm_state->get_Mach_number();
159 float get_aoa( void )
161 return current_aircraft.fdm_state->get_Alpha() * SGD_RADIANS_TO_DEGREES;
164 float get_roll( void )
166 return current_aircraft.fdm_state->get_Phi();
169 float get_pitch( void )
171 return current_aircraft.fdm_state->get_Theta();
174 float get_heading( void )
176 return current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES;
179 float get_altitude( void )
181 static const SGPropertyNode *startup_units_node
182 = fgGetNode("/sim/startup/units");
186 if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
187 altitude = current_aircraft.fdm_state->get_Altitude();
189 altitude = (current_aircraft.fdm_state->get_Altitude()
196 float get_agl( void )
198 static const SGPropertyNode *startup_units_node
199 = fgGetNode("/sim/startup/units");
203 if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
204 agl = (current_aircraft.fdm_state->get_Altitude()
205 - globals->get_scenery()->get_cur_elev() * SG_METER_TO_FEET);
207 agl = (current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
208 - globals->get_scenery()->get_cur_elev());
214 float get_sideslip( void )
216 return current_aircraft.fdm_state->get_Beta();
219 float get_frame_rate( void )
221 return general.get_frame_rate();
224 float get_fov( void )
226 return globals->get_current_view()->get_fov();
229 float get_vfc_ratio( void )
231 // float vfc = current_view.get_vfc_ratio();
236 float get_vfc_tris_drawn ( void )
238 // float rendered = current_view.get_tris_rendered();
239 // return (rendered);
243 float get_vfc_tris_culled ( void )
245 // float culled = current_view.get_tris_culled();
250 float get_climb_rate( void )
252 static const SGPropertyNode *startup_units_node
253 = fgGetNode("/sim/startup/units");
256 if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
257 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
259 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * SG_FEET_TO_METER * 60.0;
266 float get_view_direction( void )
268 double view_off = SGD_2PI - globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS;
269 double view = ( current_aircraft.fdm_state->get_Psi() + view_off)
270 * SGD_RADIANS_TO_DEGREES;
280 // $$$ begin - added, VS Renganathan 13 Oct 2K
281 // #ifdef FIGHTER_HUD
282 float get_Vx ( void )
284 // Curt dont comment this and return zero. - Ranga
285 // Please remove comments from get_V_..() function in flight.hxx
286 float Vxx = current_aircraft.fdm_state->get_V_north_rel_ground();
290 float get_Vy ( void )
292 // Curt dont comment this and return zero. - Ranga
293 // Please remove comments from get_V_..() function in flight.hxx
294 float Vyy = current_aircraft.fdm_state->get_V_east_rel_ground();
298 float get_Vz ( void )
300 // Curt dont comment this and return zero. - Ranga
301 // Please remove comments from get_V_..() function in flight.hxx
302 float Vzz = current_aircraft.fdm_state->get_V_down_rel_ground();
306 float get_Ax ( void )
308 float Ax = current_aircraft.fdm_state->get_V_dot_north();
312 float get_Ay ( void )
314 float Ay = current_aircraft.fdm_state->get_V_dot_east();
318 float get_Az ( void )
320 float Az = current_aircraft.fdm_state->get_V_dot_down();
324 float get_anzg ( void )
326 float anzg = current_aircraft.fdm_state->get_N_Z_cg();
332 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
333 return fdm->get_iaux(1);
338 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
339 return fdm->get_iaux(2);
344 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
345 return fdm->get_iaux(3);
350 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
351 return fdm->get_iaux(4);
356 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
357 return fdm->get_iaux(5);
362 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
363 return fdm->get_iaux(6);
368 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
369 return fdm->get_iaux(7);
374 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
375 return fdm->get_iaux(8);
380 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
381 return fdm->get_iaux(9);
384 int get_iaux10 (void)
386 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
387 return fdm->get_iaux(10);
390 int get_iaux11 (void)
392 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
393 return fdm->get_iaux(11);
396 int get_iaux12 (void)
398 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
399 return fdm->get_iaux(12);
402 float get_aux1 (void)
404 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
405 return fdm->get_daux(1);
408 float get_aux2 (void)
410 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
411 return fdm->get_daux(2);
414 float get_aux3 (void)
416 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
417 return fdm->get_daux(3);
420 float get_aux4 (void)
422 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
423 return fdm->get_daux(4);
426 float get_aux5 (void)
428 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
429 return fdm->get_daux(5);
432 float get_aux6 (void)
434 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
435 return fdm->get_daux(6);
438 float get_aux7 (void)
440 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
441 return fdm->get_daux(7);
444 float get_aux8 (void)
446 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
447 return fdm->get_daux(8);
450 float get_aux9 (void)
452 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
453 return fdm->get_faux(1);
456 float get_aux10 (void)
458 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
459 return fdm->get_faux(2);
462 float get_aux11 (void)
464 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
465 return fdm->get_faux(3);
468 float get_aux12 (void)
470 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
471 return fdm->get_faux(4);
474 float get_aux13 (void)
476 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
477 return fdm->get_faux(5);
480 float get_aux14 (void)
482 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
483 return fdm->get_faux(6);
486 float get_aux15 (void)
488 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
489 return fdm->get_faux(7);
492 float get_aux16 (void)
494 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
495 return fdm->get_faux(8);
498 float get_aux17 (void)
500 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
501 return fdm->get_faux(9);
504 float get_aux18 (void)
506 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
507 return fdm->get_faux(10);
510 // $$$ end - added, VS Renganathan 13 Oct 2K
514 /****************************************************************************/
515 /* Convert degrees to dd mm'ss.s" (DMS-Format) */
516 /****************************************************************************/
517 char *dmshh_format(double degrees)
528 min_part = 60.0 * (degrees - deg_part);
529 sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
531 /* Round off hundredths */
532 if (sec_part + 0.005 >= 60.0)
533 sec_part -= 60.0, min_part += 1;
535 min_part -= 60, deg_part += 1;
537 sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
544 /************************************************************************
545 Convert degrees to dd mm.mmm' (DMM-Format)
546 Description: Converts using a round-off factor tailored to the required
547 precision of the minutes field (three decimal places). Round-off
548 prevents function from returning a minutes value of 60.
550 Input arguments: Coordinate value in decimal degrees
552 ************************************************************************/
553 static char *toDM(float dd)
564 /* round for minutes expressed to three decimal places */
565 tempdd = fabs(dd) + (5.0E-4 / 60.0);
567 mn = fabs( (tempdd - (double)(deg)) * 60.0 - 4.999E-4 );
569 sprintf(dm, "%d*%06.3f", deg, mn);
574 /************************************************************************
575 Convert degrees to dd mm'ss.s'' (DMS-Format)
576 Description: Converts using a round-off factor tailored to the required
577 precision of the seconds field (one decimal place). Round-off
578 prevents function from returning a seconds value of 60.
580 Input arguments: Coordinate value in decimal degrees
582 ************************************************************************/
583 static char *toDMS(float dd)
586 double tempdd, tempmin;
595 /* round up for seconds expressed to one decimal place */
596 tempdd = fabs(dd) + (0.05 / 3600.0);
598 tempmin = (tempdd - (double)(deg)) * 60.0;
600 sec = fabs( (tempmin - (double)(mn)) * 60.0 - 0.049 );
602 sprintf(dms, "%d*%02d %04.1f", deg, mn, sec);
607 // Have to set the LatLon display type
608 //static char *(*fgLatLonFormat)(float) = toDM;
609 static char *(*fgLatLonFormat)(float);
611 char *coord_format_lat(float latitude)
616 // dmshh_format(latitude),
619 fgLatLonFormat(latitude),
620 latitude > 0 ? 'N' : 'S');
624 char *coord_format_lon(float longitude)
629 // dmshh_format(longitude),
632 fgLatLonFormat(longitude),
633 longitude > 0 ? 'E' : 'W');
637 void fgLatLonFormatToggle( puObject *)
639 static int toggle = 0;
642 fgLatLonFormat = toDM;
644 fgLatLonFormat = toDMS;
650 char *coord_format_latlon(double latitude, double longitude)
652 static char buf[1024];
654 sprintf(buf,"%s%c %s%c",
655 dmshh_format(latitude),
656 latitude > 0 ? 'N' : 'S',
657 dmshh_format(longitude),
658 longitude > 0 ? 'E' : 'W');
664 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
666 SG_LOG( SG_COCKPIT, SG_INFO, "Initializing cockpit subsystem" );
668 // cockpit->code = 1; /* It will be aircraft dependent */
669 // cockpit->status = 0;
671 // If aircraft has HUD specified we will get the specs from its def
672 // file. For now we will depend upon hard coding in hud?
674 // We must insure that the existing instrument link is purged.
675 // This is done by deleting the links in the list.
677 // HI_Head is now a null pointer so we can generate a new list from the
680 fgHUDInit( cur_aircraft );
681 ac_cockpit = new fg_Cockpit();
683 // Have to set the LatLon display type
684 fgLatLonFormat = toDM;
686 SG_LOG( SG_COCKPIT, SG_INFO,
687 " Code " << ac_cockpit->code() << " Status "
688 << ac_cockpit->status() );
693 void fgCockpitUpdate( void ) {
695 SG_LOG( SG_COCKPIT, SG_DEBUG,
696 "Cockpit: code " << ac_cockpit->code() << " status "
697 << ac_cockpit->status() );
699 static const SGPropertyNode * xsize_node = fgGetNode("/sim/startup/xsize");
700 static const SGPropertyNode * ysize_node = fgGetNode("/sim/startup/ysize");
701 static const SGPropertyNode * hud_visibility_node
702 = fgGetNode("/sim/hud/visibility");
704 int iwidth = xsize_node->getIntValue();
705 int iheight = ysize_node->getIntValue();
706 float width = iwidth;
707 // float height = iheight;
709 // FIXME: inefficient
710 if ( hud_visibility_node->getBoolValue() ) {
711 // This will check the global hud linked list pointer.
712 // If these is anything to draw it will.
716 #define DISPLAY_COUNTER
717 #ifdef DISPLAY_COUNTER
721 float fps = get_frame_rate();
722 // float tris = fps * get_vfc_tris_drawn();
723 // float culled = fps * get_vfc_tris_culled();
724 // sprintf(buf,"%-4.1f %7.0f %7.0f", fps, tris, culled);
725 sprintf(buf,"%-5.1f", fps);
727 glMatrixMode( GL_PROJECTION );
730 gluOrtho2D( 0, iwidth, 0, iheight );
731 glMatrixMode( GL_MODELVIEW );
735 glDisable( GL_DEPTH_TEST );
736 glDisable( GL_LIGHTING );
738 glColor3f( 0.9, 0.4, 0.2 );
740 guiFnt.drawString( buf,
741 int(width - guiFnt.getStringWidth(buf) - 10),
743 glEnable( GL_DEPTH_TEST );
744 glEnable( GL_LIGHTING );
745 glMatrixMode( GL_PROJECTION );
747 glMatrixMode( GL_MODELVIEW );
750 #endif // #ifdef DISPLAY_COUNTER
752 glViewport( 0, 0, iwidth, iheight );