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.
36 #include <simgear/constants.h>
37 #include <simgear/debug/logstream.hxx>
38 #include <simgear/math/polar3d.hxx>
39 #include <simgear/props/props.hxx>
40 #include <simgear/timing/sg_time.hxx>
42 #include <Aircraft/aircraft.hxx>
43 #include <Include/general.hxx>
44 #include <FDM/ADA.hxx>
45 #include <Main/globals.hxx>
46 #include <Main/fg_props.hxx>
47 #include <Main/viewmgr.hxx>
48 #include <Scenery/scenery.hxx>
49 #include <Time/fg_timer.hxx>
52 #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 )
67 return current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
70 float get_lat_min( void )
74 a = current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
78 d = (double) ( (int) a);
79 float lat_min = (a - d) * 60.0;
85 float get_longitude( void )
87 return current_aircraft.fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
92 get_formated_gmt_time( void )
95 const struct tm *p = globals->get_time_params()->getGmt();
96 sprintf( buf, "%d/%d/%4d %d:%02d:%02d",
97 p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
98 p->tm_hour, p->tm_min, p->tm_sec);
104 float get_long_min( void )
107 a = current_aircraft.fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
111 d = (double) ( (int) a);
112 float lon_min = (a - d) * 60.0;
117 float get_throttleval( void )
119 // Hack limiting to one engine
120 return globals->get_controls()->get_throttle( 0 );
123 float get_aileronval( void )
125 return globals->get_controls()->get_aileron();
128 float get_elevatorval( void )
130 return globals->get_controls()->get_elevator();
133 float get_elev_trimval( void )
135 return globals->get_controls()->get_elevator_trim();
138 float get_rudderval( void )
140 return globals->get_controls()->get_rudder();
143 float get_speed( void )
145 static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
147 float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
148 * speedup_node->getIntValue();
155 return current_aircraft.fdm_state->get_Mach_number();
158 float get_aoa( void )
160 return current_aircraft.fdm_state->get_Alpha() * SGD_RADIANS_TO_DEGREES;
163 float get_roll( void )
165 return current_aircraft.fdm_state->get_Phi();
168 float get_pitch( void )
170 return current_aircraft.fdm_state->get_Theta();
173 float get_heading( void )
175 return current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES;
178 float get_altitude( void )
180 static const SGPropertyNode *startup_units_node
181 = fgGetNode("/sim/startup/units");
185 if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
186 altitude = current_aircraft.fdm_state->get_Altitude();
188 altitude = (current_aircraft.fdm_state->get_Altitude()
195 float get_agl( void )
197 static const SGPropertyNode *startup_units_node
198 = fgGetNode("/sim/startup/units");
202 if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
203 agl = (current_aircraft.fdm_state->get_Altitude()
204 - globals->get_scenery()->get_cur_elev() * SG_METER_TO_FEET);
206 agl = (current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
207 - globals->get_scenery()->get_cur_elev());
213 float get_sideslip( void )
215 return current_aircraft.fdm_state->get_Beta();
218 float get_frame_rate( void )
220 return general.get_frame_rate();
223 float get_fov( void )
225 return globals->get_current_view()->get_fov();
228 float get_vfc_ratio( void )
230 // float vfc = current_view.get_vfc_ratio();
235 float get_vfc_tris_drawn ( void )
237 // float rendered = current_view.get_tris_rendered();
238 // return (rendered);
242 float get_vfc_tris_culled ( void )
244 // float culled = current_view.get_tris_culled();
249 float get_climb_rate( void )
251 static const SGPropertyNode *startup_units_node
252 = fgGetNode("/sim/startup/units");
255 if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
256 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
258 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * SG_FEET_TO_METER * 60.0;
265 float get_view_direction( void )
267 double view_off = SGD_2PI - globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS;
268 double view = ( current_aircraft.fdm_state->get_Psi() + view_off)
269 * SGD_RADIANS_TO_DEGREES;
279 // Added by Markus Hof on 5. Jan 2004
280 float get_dme( void )
282 static const SGPropertyNode * dme_node =
283 fgGetNode("/radios/dme/distance-nm");
285 return dme_node->getFloatValue();
288 // $$$ begin - added, VS Renganathan 13 Oct 2K
289 // #ifdef FIGHTER_HUD
290 float get_Vx ( void )
292 // Curt dont comment this and return zero. - Ranga
293 // Please remove comments from get_V_..() function in flight.hxx
294 float Vxx = current_aircraft.fdm_state->get_V_north_rel_ground();
298 float get_Vy ( void )
300 // Curt dont comment this and return zero. - Ranga
301 // Please remove comments from get_V_..() function in flight.hxx
302 float Vyy = current_aircraft.fdm_state->get_V_east_rel_ground();
306 float get_Vz ( void )
308 // Curt dont comment this and return zero. - Ranga
309 // Please remove comments from get_V_..() function in flight.hxx
310 float Vzz = current_aircraft.fdm_state->get_V_down_rel_ground();
314 float get_Ax ( void )
316 float Ax = current_aircraft.fdm_state->get_V_dot_north();
320 float get_Ay ( void )
322 float Ay = current_aircraft.fdm_state->get_V_dot_east();
326 float get_Az ( void )
328 float Az = current_aircraft.fdm_state->get_V_dot_down();
332 float get_anzg ( void )
334 float anzg = current_aircraft.fdm_state->get_N_Z_cg();
340 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
341 return fdm->get_iaux(1);
346 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
347 return fdm->get_iaux(2);
352 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
353 return fdm->get_iaux(3);
358 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
359 return fdm->get_iaux(4);
364 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
365 return fdm->get_iaux(5);
370 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
371 return fdm->get_iaux(6);
376 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
377 return fdm->get_iaux(7);
382 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
383 return fdm->get_iaux(8);
388 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
389 return fdm->get_iaux(9);
392 int get_iaux10 (void)
394 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
395 return fdm->get_iaux(10);
398 int get_iaux11 (void)
400 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
401 return fdm->get_iaux(11);
404 int get_iaux12 (void)
406 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
407 return fdm->get_iaux(12);
410 float get_aux1 (void)
412 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
413 return fdm->get_daux(1);
416 float get_aux2 (void)
418 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
419 return fdm->get_daux(2);
422 float get_aux3 (void)
424 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
425 return fdm->get_daux(3);
428 float get_aux4 (void)
430 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
431 return fdm->get_daux(4);
434 float get_aux5 (void)
436 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
437 return fdm->get_daux(5);
440 float get_aux6 (void)
442 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
443 return fdm->get_daux(6);
446 float get_aux7 (void)
448 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
449 return fdm->get_daux(7);
452 float get_aux8 (void)
454 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
455 return fdm->get_daux(8);
458 float get_aux9 (void)
460 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
461 return fdm->get_faux(1);
464 float get_aux10 (void)
466 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
467 return fdm->get_faux(2);
470 float get_aux11 (void)
472 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
473 return fdm->get_faux(3);
476 float get_aux12 (void)
478 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
479 return fdm->get_faux(4);
482 float get_aux13 (void)
484 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
485 return fdm->get_faux(5);
488 float get_aux14 (void)
490 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
491 return fdm->get_faux(6);
494 float get_aux15 (void)
496 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
497 return fdm->get_faux(7);
500 float get_aux16 (void)
502 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
503 return fdm->get_faux(8);
506 float get_aux17 (void)
508 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
509 return fdm->get_faux(9);
512 float get_aux18 (void)
514 FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
515 return fdm->get_faux(10);
518 // $$$ end - added, VS Renganathan 13 Oct 2K
522 /****************************************************************************/
523 /* Convert degrees to dd mm'ss.s" (DMS-Format) */
524 /****************************************************************************/
525 char *dmshh_format(double degrees)
536 min_part = 60.0 * (degrees - deg_part);
537 sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
539 /* Round off hundredths */
540 if (sec_part + 0.005 >= 60.0)
541 sec_part -= 60.0, min_part += 1;
543 min_part -= 60, deg_part += 1;
545 sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
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.
558 Input arguments: Coordinate value in decimal degrees
560 ************************************************************************/
561 static char *toDM(float dd)
572 /* round for minutes expressed to three decimal places */
573 tempdd = fabs(dd) + (5.0E-4 / 60.0);
575 mn = fabs( (tempdd - (double)(deg)) * 60.0 - 4.999E-4 );
577 sprintf(dm, "%d*%06.3f", deg, mn);
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.
588 Input arguments: Coordinate value in decimal degrees
590 ************************************************************************/
591 static char *toDMS(float dd)
594 double tempdd, tempmin;
603 /* round up for seconds expressed to one decimal place */
604 tempdd = fabs(dd) + (0.05 / 3600.0);
606 tempmin = (tempdd - (double)(deg)) * 60.0;
608 sec = fabs( (tempmin - (double)(mn)) * 60.0 - 0.049 );
610 sprintf(dms, "%d*%02d %04.1f", deg, mn, sec);
615 // Have to set the LatLon display type
616 //static char *(*fgLatLonFormat)(float) = toDM;
617 static char *(*fgLatLonFormat)(float);
619 char *coord_format_lat(float latitude)
624 // dmshh_format(latitude),
627 fgLatLonFormat(latitude),
628 latitude > 0 ? 'N' : 'S');
632 char *coord_format_lon(float longitude)
637 // dmshh_format(longitude),
640 fgLatLonFormat(longitude),
641 longitude > 0 ? 'E' : 'W');
645 void fgLatLonFormatToggle( puObject *)
647 static int toggle = 0;
650 fgLatLonFormat = toDM;
652 fgLatLonFormat = toDMS;
658 char *coord_format_latlon(double latitude, double longitude)
660 static char buf[1024];
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');
672 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
674 SG_LOG( SG_COCKPIT, SG_INFO, "Initializing cockpit subsystem" );
676 // cockpit->code = 1; /* It will be aircraft dependent */
677 // cockpit->status = 0;
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?
682 // We must insure that the existing instrument link is purged.
683 // This is done by deleting the links in the list.
685 // HI_Head is now a null pointer so we can generate a new list from the
688 fgHUDInit( cur_aircraft );
689 ac_cockpit = new fg_Cockpit();
691 // Have to set the LatLon display type
692 fgLatLonFormat = toDM;
694 SG_LOG( SG_COCKPIT, SG_INFO,
695 " Code " << ac_cockpit->code() << " Status "
696 << ac_cockpit->status() );
701 void fgCockpitUpdate( void ) {
703 SG_LOG( SG_COCKPIT, SG_DEBUG,
704 "Cockpit: code " << ac_cockpit->code() << " status "
705 << ac_cockpit->status() );
707 static const SGPropertyNode * xsize_node = fgGetNode("/sim/startup/xsize");
708 static const SGPropertyNode * ysize_node = fgGetNode("/sim/startup/ysize");
709 static const SGPropertyNode * hud_visibility_node
710 = fgGetNode("/sim/hud/visibility");
712 int iwidth = xsize_node->getIntValue();
713 int iheight = ysize_node->getIntValue();
714 float width = iwidth;
715 // float height = iheight;
717 // FIXME: inefficient
718 if ( hud_visibility_node->getBoolValue() ) {
719 // This will check the global hud linked list pointer.
720 // If these is anything to draw it will.
724 #define DISPLAY_COUNTER
725 #ifdef DISPLAY_COUNTER
729 float fps = get_frame_rate();
730 // float tris = fps * get_vfc_tris_drawn();
731 // float culled = fps * get_vfc_tris_culled();
732 // sprintf(buf,"%-4.1f %7.0f %7.0f", fps, tris, culled);
733 sprintf(buf,"%-5.1f", fps);
735 glMatrixMode( GL_PROJECTION );
738 gluOrtho2D( 0, iwidth, 0, iheight );
739 glMatrixMode( GL_MODELVIEW );
743 glDisable( GL_DEPTH_TEST );
744 glDisable( GL_LIGHTING );
746 glColor3f( 0.9, 0.4, 0.2 );
748 guiFnt.drawString( buf,
749 int(width - guiFnt.getStringWidth(buf) - 10),
751 glEnable( GL_DEPTH_TEST );
752 glEnable( GL_LIGHTING );
753 glMatrixMode( GL_PROJECTION );
755 glMatrixMode( GL_MODELVIEW );
758 #endif // #ifdef DISPLAY_COUNTER
760 glViewport( 0, 0, iwidth, iheight );