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 <Main/options.hxx>
47 #include <Main/views.hxx>
48 #include <Scenery/scenery.hxx>
49 #include <Time/fg_timer.hxx>
50 #include <Time/fg_time.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() * RAD_TO_DEG;
76 float get_lat_min( void )
80 a = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
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() * RAD_TO_DEG;
102 get_formated_gmt_time( void )
105 FGTime *t = FGTime::cur_time_params;
106 const struct tm *p = t->getGmt();
107 sprintf( buf, "%d/%d/%4d %d:%02d:%02d",
108 p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
109 p->tm_hour, p->tm_min, p->tm_sec);
114 float get_long_min( void )
117 a = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
121 d = (double) ( (int) a);
122 float lon_min = (a - d) * 60.0;
126 float get_throttleval( void )
128 float throttle = controls.get_throttle( 0 );
129 return (throttle); // Hack limiting to one engine
132 float get_aileronval( void )
134 float aileronval = controls.get_aileron();
138 float get_elevatorval( void )
140 float elevator_val = (float)controls.get_elevator();
144 float get_elev_trimval( void )
146 float elevatorval = controls.get_elevator_trim();
147 return (elevatorval);
150 float get_rudderval( void )
152 float rudderval = controls.get_rudder();
156 float get_speed( void )
158 // Make an explicit function call.
159 float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
160 * current_options.get_speed_up();
166 float mach=current_aircraft.fdm_state->get_Mach_number();
170 float get_aoa( void )
172 float aoa = current_aircraft.fdm_state->get_Alpha() * RAD_TO_DEG;
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() * RAD_TO_DEG);
194 float get_altitude( void )
197 // double rough_elev;
199 // current_aircraft.fdm_state
200 // rough_elev = mesh_altitude(f->get_Longitude() * RAD_TO_ARCSEC,
201 // f->get_Latitude() * RAD_TO_ARCSEC);
204 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
205 altitude = current_aircraft.fdm_state->get_Altitude();
207 altitude = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER);
212 float get_agl( void )
216 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
217 agl = (current_aircraft.fdm_state->get_Altitude()
218 - scenery.cur_elev * METER_TO_FEET);
220 agl = (current_aircraft.fdm_state->get_Altitude() * 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 = current_options.get_fov();
244 float get_vfc_ratio( void )
246 float vfc = current_view.get_vfc_ratio();
250 float get_vfc_tris_drawn ( void )
252 float rendered = current_view.get_tris_rendered();
256 float get_vfc_tris_culled ( void )
258 float culled = current_view.get_tris_culled();
262 float get_climb_rate( void )
265 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
266 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
268 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * FEET_TO_METER * 60.0;
274 float get_view_direction( void )
278 view = FG_2PI - current_view.get_view_offset();
279 view = ( current_aircraft.fdm_state->get_Psi() + view) * RAD_TO_DEG;
291 /****************************************************************************/
292 /* Convert degrees to dd mm'ss.s" (DMS-Format) */
293 /****************************************************************************/
294 char *dmshh_format(double degrees)
305 min_part = 60.0 * (degrees - deg_part);
306 sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
308 /* Round off hundredths */
309 if (sec_part + 0.005 >= 60.0)
310 sec_part -= 60.0, min_part += 1;
312 min_part -= 60, deg_part += 1;
314 sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
320 /****************************************************************************/
321 /* Convert degrees to dd mm'ss.s'' (DMS-Format) */
322 /****************************************************************************/
323 static char *toDMS(float a)
333 d = (float) ((int) a);
335 m = (float) ((int) a);
349 sprintf(dms, "%.0f*%02.0f %04.1f", d, m, s);
354 /****************************************************************************/
355 /* Convert degrees to dd mm.mmm' (DMM-Format) */
356 /****************************************************************************/
357 static char *toDM(float a)
368 d = (float) ( (int) a);
377 sprintf(dm, "%.0f*%06.3f", d, m);
381 // Have to set the LatLon display type
382 //static char *(*fgLatLonFormat)(float) = toDM;
383 static char *(*fgLatLonFormat)(float);
385 char *coord_format_lat(float latitude)
390 // dmshh_format(latitude),
393 fgLatLonFormat(latitude),
394 latitude > 0 ? 'N' : 'S');
398 char *coord_format_lon(float longitude)
403 // dmshh_format(longitude),
406 fgLatLonFormat(longitude),
407 longitude > 0 ? 'E' : 'W');
411 void fgLatLonFormatToggle( puObject *)
413 static int toggle = 0;
416 fgLatLonFormat = toDM;
418 fgLatLonFormat = toDMS;
424 char *coord_format_latlon(double latitude, double longitude)
426 static char buf[1024];
428 sprintf(buf,"%s%c %s%c",
429 dmshh_format(latitude),
430 latitude > 0 ? 'N' : 'S',
431 dmshh_format(longitude),
432 longitude > 0 ? 'E' : 'W');
438 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
440 FG_LOG( FG_COCKPIT, FG_INFO, "Initializing cockpit subsystem" );
442 // cockpit->code = 1; /* It will be aircraft dependent */
443 // cockpit->status = 0;
445 // If aircraft has HUD specified we will get the specs from its def
446 // file. For now we will depend upon hard coding in hud?
448 // We must insure that the existing instrument link is purged.
449 // This is done by deleting the links in the list.
451 // HI_Head is now a null pointer so we can generate a new list from the
454 fgHUDInit( cur_aircraft );
455 ac_cockpit = new fg_Cockpit();
457 // Have to set the LatLon display type
458 fgLatLonFormat = toDM;
460 FG_LOG( FG_COCKPIT, FG_INFO,
461 " Code " << ac_cockpit->code() << " Status "
462 << ac_cockpit->status() );
467 void fgCockpitUpdate( void ) {
469 FG_LOG( FG_COCKPIT, FG_DEBUG,
470 "Cockpit: code " << ac_cockpit->code() << " status "
471 << ac_cockpit->status() );
473 int iwidth = current_view.get_winWidth();
474 int iheight = current_view.get_winHeight();
475 float width = iwidth;
476 float height = iheight;
478 if ( current_options.get_hud_status() ) {
479 // This will check the global hud linked list pointer.
480 // If these is anything to draw it will.
483 #define DISPLAY_COUNTER
484 #ifdef DISPLAY_COUNTER
488 float fps = get_frame_rate();
489 // float tris = fps * get_vfc_tris_drawn();
490 // float culled = fps * get_vfc_tris_culled();
491 // sprintf(buf,"%-4.1f %7.0f %7.0f", fps, tris, culled);
492 sprintf(buf,"%-5.1f", fps);
494 glMatrixMode(GL_PROJECTION);
497 gluOrtho2D(0, width, 0, height);
498 glMatrixMode(GL_MODELVIEW);
502 glDisable(GL_DEPTH_TEST);
503 glDisable(GL_LIGHTING);
505 glColor3f (0.9, 0.4, 0.2);
507 guiFnt.drawString( buf,
508 // width/2 - guiFnt.getStringWidth(buf)/2,
509 int(width - guiFnt.getStringWidth(buf) - 10),
511 glEnable(GL_DEPTH_TEST);
512 glEnable(GL_LIGHTING);
513 glMatrixMode(GL_PROJECTION);
515 glMatrixMode(GL_MODELVIEW);
518 #endif // #ifdef DISPLAY_COUNTER
520 xglViewport( 0, 0, iwidth, iheight );
522 current_panel->update();