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/mat3.h>
43 #include <simgear/math/polar3d.hxx>
45 #include <Aircraft/aircraft.hxx>
46 #include <Include/general.hxx>
47 #include <Main/options.hxx>
48 #include <Main/views.hxx>
49 #include <Scenery/scenery.hxx>
50 #include <Time/fg_timer.hxx>
51 #include <Time/fg_time.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 fntRenderer *HUDtext = 0;
62 float HUD_TextSize = 0;
66 // The following routines obtain information concerntin the aircraft's
67 // current state and return it to calling instrument display routines.
68 // They should eventually be member functions of the aircraft.
71 float get_latitude( void )
75 lat = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
82 float get_lat_min( void )
86 a = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
90 d = (double) ( (int) a);
91 float lat_min = (a - d) * 60.0;
96 float get_longitude( void )
100 lon = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
108 get_formated_gmt_time( void )
111 FGTime *t = FGTime::cur_time_params;
112 const struct tm *p = t->getGmt();
113 sprintf( buf, "%d/%d/%4d %d:%02d:%02d",
114 p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
115 p->tm_hour, p->tm_min, p->tm_sec);
120 float get_long_min( void )
123 a = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
127 d = (double) ( (int) a);
128 float lon_min = (a - d) * 60.0;
132 float get_throttleval( void )
134 float throttle = controls.get_throttle( 0 );
135 return (throttle); // Hack limiting to one engine
138 float get_aileronval( void )
140 float aileronval = controls.get_aileron();
144 float get_elevatorval( void )
146 float elevator_val = (float)controls.get_elevator();
150 float get_elev_trimval( void )
152 float elevatorval = controls.get_elevator_trim();
153 return (elevatorval);
156 float get_rudderval( void )
158 float rudderval = controls.get_rudder();
162 float get_speed( void )
164 // Make an explicit function call.
165 float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
166 * current_options.get_speed_up();
172 float mach=current_aircraft.fdm_state->get_Mach_number();
176 float get_aoa( void )
178 float aoa = current_aircraft.fdm_state->get_Alpha() * RAD_TO_DEG;
182 float get_roll( void )
184 float roll = current_aircraft.fdm_state->get_Phi();
188 float get_pitch( void )
190 float pitch = current_aircraft.fdm_state->get_Theta();
194 float get_heading( void )
196 float heading = (current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG);
200 float get_altitude( void )
203 // double rough_elev;
205 // current_aircraft.fdm_state
206 // rough_elev = mesh_altitude(f->get_Longitude() * RAD_TO_ARCSEC,
207 // f->get_Latitude() * RAD_TO_ARCSEC);
210 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
211 altitude = current_aircraft.fdm_state->get_Altitude();
213 altitude = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER);
218 float get_agl( void )
222 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
223 agl = (current_aircraft.fdm_state->get_Altitude()
224 - scenery.cur_elev * METER_TO_FEET);
226 agl = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER
232 float get_sideslip( void )
234 float sideslip = current_aircraft.fdm_state->get_Beta();
238 float get_frame_rate( void )
240 float frame_rate = general.get_frame_rate();
244 float get_fov( void )
246 float fov = current_options.get_fov();
250 float get_vfc_ratio( void )
252 float vfc = current_view.get_vfc_ratio();
256 float get_vfc_tris_drawn ( void )
258 float rendered = current_view.get_tris_rendered();
262 float get_vfc_tris_culled ( void )
264 float culled = current_view.get_tris_culled();
268 float get_climb_rate( void )
271 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
272 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
274 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * FEET_TO_METER * 60.0;
280 float get_view_direction( void )
284 view = FG_2PI - current_view.get_view_offset();
285 view = ( current_aircraft.fdm_state->get_Psi() + view) * RAD_TO_DEG;
297 /****************************************************************************/
298 /* Convert degrees to dd mm'ss.s" (DMS-Format) */
299 /****************************************************************************/
300 char *dmshh_format(double degrees)
311 min_part = 60.0 * (degrees - deg_part);
312 sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
314 /* Round off hundredths */
315 if (sec_part + 0.005 >= 60.0)
316 sec_part -= 60.0, min_part += 1;
318 min_part -= 60, deg_part += 1;
320 sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
326 /****************************************************************************/
327 /* Convert degrees to dd mm'ss.s'' (DMS-Format) */
328 /****************************************************************************/
329 static char *toDMS(float a)
339 d = (float) ((int) a);
341 m = (float) ((int) a);
355 sprintf(dms, "%.0f*%02.0f %04.1f", d, m, s);
360 /****************************************************************************/
361 /* Convert degrees to dd mm.mmm' (DMM-Format) */
362 /****************************************************************************/
363 static char *toDM(float a)
374 d = (float) ( (int) a);
383 sprintf(dm, "%.0f*%06.3f", d, m);
387 // Have to set the LatLon display type
388 //static char *(*fgLatLonFormat)(float) = toDM;
389 static char *(*fgLatLonFormat)(float);
391 char *coord_format_lat(float latitude)
396 // dmshh_format(latitude),
399 fgLatLonFormat(latitude),
400 latitude > 0 ? 'N' : 'S');
404 char *coord_format_lon(float longitude)
409 // dmshh_format(longitude),
412 fgLatLonFormat(longitude),
413 longitude > 0 ? 'E' : 'W');
417 void fgLatLonFormatToggle( puObject *)
419 static int toggle = 0;
422 fgLatLonFormat = toDM;
424 fgLatLonFormat = toDMS;
430 char *coord_format_latlon(double latitude, double longitude)
432 static char buf[1024];
434 sprintf(buf,"%s%c %s%c",
435 dmshh_format(latitude),
436 latitude > 0 ? 'N' : 'S',
437 dmshh_format(longitude),
438 longitude > 0 ? 'E' : 'W');
444 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
446 FG_LOG( FG_COCKPIT, FG_INFO, "Initializing cockpit subsystem" );
448 // cockpit->code = 1; /* It will be aircraft dependent */
449 // cockpit->status = 0;
451 // If aircraft has HUD specified we will get the specs from its def
452 // file. For now we will depend upon hard coding in hud?
454 // We must insure that the existing instrument link is purged.
455 // This is done by deleting the links in the list.
457 // HI_Head is now a null pointer so we can generate a new list from the
460 fgHUDInit( cur_aircraft );
461 ac_cockpit = new fg_Cockpit();
463 if ( current_options.get_panel_status() ) {
467 // Have to set the LatLon display type
468 fgLatLonFormat = toDM;
470 FG_LOG( FG_COCKPIT, FG_INFO,
471 " Code " << ac_cockpit->code() << " Status "
472 << ac_cockpit->status() );
474 // HUD_TextSize = (current_options.get_xsize() > 1000) ? 10 : 8;
476 HUDtext = new fntRenderer();
477 HUDtext -> setFont ( guiFntHandle ) ;
478 HUDtext -> setPointSize ( HUD_TextSize ) ;
479 HUD_TextList.setFont( HUDtext );
485 void fgCockpitUpdate( void ) {
487 int iwidth = current_view.get_winWidth();
488 int iheight = current_view.get_winHeight();
489 float width = iwidth;
490 float height = iheight;
492 FG_LOG( FG_COCKPIT, FG_DEBUG,
493 "Cockpit: code " << ac_cockpit->code() << " status "
494 << ac_cockpit->status() );
496 if ( current_options.get_hud_status() ) {
497 // This will check the global hud linked list pointer.
498 // If these is anything to draw it will.
501 #define DISPLAY_COUNTER
502 #ifdef DISPLAY_COUNTER
506 float fps = get_frame_rate();
507 // float tris = fps * get_vfc_tris_drawn();
508 // float culled = fps * get_vfc_tris_culled();
509 // sprintf(buf,"%-4.1f %7.0f %7.0f", fps, tris, culled);
510 sprintf(buf,"%-5.1f", fps);
512 glMatrixMode(GL_PROJECTION);
515 gluOrtho2D(0, width, 0, height);
516 glMatrixMode(GL_MODELVIEW);
520 glDisable(GL_DEPTH_TEST);
521 glDisable(GL_LIGHTING);
523 glColor3f (0.9, 0.4, 0.2);
525 guiFnt.drawString( buf,
526 width/2 - guiFnt.getStringWidth(buf)/2,
528 glEnable(GL_DEPTH_TEST);
529 glEnable(GL_LIGHTING);
530 glMatrixMode(GL_PROJECTION);
532 glMatrixMode(GL_MODELVIEW);
535 #endif // #ifdef DISPLAY_COUNTER
537 if( current_options.get_panel_status() &&
538 (fabs( current_view.get_view_offset() ) < 0.2) )
540 xglViewport( 0, 0, iwidth, iheight );
542 FGPanel::OurPanel->Update();