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.
39 #include <Aircraft/aircraft.hxx>
40 #include <Debug/logstream.hxx>
41 #include <Include/fg_constants.h>
42 #include <Include/general.hxx>
43 #include <Main/options.hxx>
44 #include <Main/views.hxx>
45 #include <Math/fg_random.h>
46 #include <Math/mat3.h>
47 #include <Math/polar3d.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 fntRenderer *HUDtext = 0;
61 float HUD_TextSize = 0;
65 // The following routines obtain information concerntin the aircraft's
66 // current state and return it to calling instrument display routines.
67 // They should eventually be member functions of the aircraft.
70 float get_latitude( void )
74 lat = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
81 float get_lat_min( void )
85 a = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
89 d = (double) ( (int) a);
90 float lat_min = (a - d) * 60.0;
95 float get_longitude( void )
99 lon = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
107 get_formated_gmt_time( void )
110 FGTime *t = FGTime::cur_time_params;
111 const struct tm *p = t->getGmt();
112 sprintf( buf, "%d/%d/%2d %d:%02d:%02d",
113 p->tm_mon+1, p->tm_mday, p->tm_year,
114 p->tm_hour, p->tm_min, p->tm_sec);
119 float get_long_min( void )
122 a = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
126 d = (double) ( (int) a);
127 float lon_min = (a - d) * 60.0;
131 float get_throttleval( void )
133 float throttle = controls.get_throttle( 0 );
134 return (throttle); // Hack limiting to one engine
137 float get_aileronval( void )
139 float aileronval = controls.get_aileron();
143 float get_elevatorval( void )
145 float elevator_val = (float)controls.get_elevator();
149 float get_elev_trimval( void )
151 float elevatorval = controls.get_elevator_trim();
152 return (elevatorval);
155 float get_rudderval( void )
157 float rudderval = controls.get_rudder();
161 float get_speed( void )
163 // Make an explicit function call.
164 float speed = current_aircraft.fdm_state->get_V_equiv_kts();
168 float get_aoa( void )
170 float aoa = current_aircraft.fdm_state->get_Alpha() * RAD_TO_DEG;
174 float get_roll( void )
176 float roll = current_aircraft.fdm_state->get_Phi();
180 float get_pitch( void )
182 float pitch = current_aircraft.fdm_state->get_Theta();
186 float get_heading( void )
188 float heading = (current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG);
192 float get_altitude( void )
195 // double rough_elev;
197 // current_aircraft.fdm_state
198 // rough_elev = mesh_altitude(f->get_Longitude() * RAD_TO_ARCSEC,
199 // f->get_Latitude() * RAD_TO_ARCSEC);
202 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
203 altitude = current_aircraft.fdm_state->get_Altitude();
205 altitude = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER);
210 float get_agl( void )
214 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
215 agl = (current_aircraft.fdm_state->get_Altitude()
216 - scenery.cur_elev * METER_TO_FEET);
218 agl = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER
224 float get_sideslip( void )
226 float sideslip = current_aircraft.fdm_state->get_Beta();
230 float get_frame_rate( void )
232 float frame_rate = general.get_frame_rate();
236 float get_fov( void )
238 float fov = current_options.get_fov();
242 float get_vfc_ratio( void )
244 float vfc = current_view.get_vfc_ratio();
248 float get_vfc_tris_drawn ( void )
250 float rendered = current_view.get_tris_rendered();
254 float get_vfc_tris_culled ( void )
256 float culled = current_view.get_tris_culled();
260 float get_climb_rate( void )
263 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
264 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
266 climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * FEET_TO_METER * 60.0;
272 float get_view_direction( void )
276 view = FG_2PI - current_view.get_view_offset();
277 view = ( current_aircraft.fdm_state->get_Psi() + view) * RAD_TO_DEG;
289 /****************************************************************************/
290 /* Convert degrees to dd mm'ss.s" (DMS-Format) */
291 /****************************************************************************/
292 char *dmshh_format(double degrees)
303 min_part = 60.0 * (degrees - deg_part);
304 sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
306 /* Round off hundredths */
307 if (sec_part + 0.005 >= 60.0)
308 sec_part -= 60.0, min_part += 1;
310 min_part -= 60, deg_part += 1;
312 sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
318 /****************************************************************************/
319 /* Convert degrees to dd mm'ss.s'' (DMS-Format) */
320 /****************************************************************************/
321 static char *toDMS(float a)
331 d = (float) ((int) a);
333 m = (float) ((int) a);
347 sprintf(dms, "%.0f*%02.0f %04.1f", d, m, s);
352 /****************************************************************************/
353 /* Convert degrees to dd mm.mmm' (DMM-Format) */
354 /****************************************************************************/
355 static char *toDM(float a)
366 d = (float) ( (int) a);
375 sprintf(dm, "%.0f*%06.3f", d, m);
379 // Have to set the LatLon display type
380 //static char *(*fgLatLonFormat)(float) = toDM;
381 static char *(*fgLatLonFormat)(float);
383 char *coord_format_lat(float latitude)
388 // dmshh_format(latitude),
391 fgLatLonFormat(latitude),
392 latitude > 0 ? 'N' : 'S');
396 char *coord_format_lon(float longitude)
401 // dmshh_format(longitude),
404 fgLatLonFormat(longitude),
405 longitude > 0 ? 'E' : 'W');
409 void fgLatLonFormatToggle( puObject *)
411 static int toggle = 0;
414 fgLatLonFormat = toDM;
416 fgLatLonFormat = toDMS;
422 char *coord_format_latlon(double latitude, double longitude)
424 static char buf[1024];
426 sprintf(buf,"%s%c %s%c",
427 dmshh_format(latitude),
428 latitude > 0 ? 'N' : 'S',
429 dmshh_format(longitude),
430 longitude > 0 ? 'E' : 'W');
436 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
438 FG_LOG( FG_COCKPIT, FG_INFO, "Initializing cockpit subsystem" );
440 // cockpit->code = 1; /* It will be aircraft dependent */
441 // cockpit->status = 0;
443 // If aircraft has HUD specified we will get the specs from its def
444 // file. For now we will depend upon hard coding in hud?
446 // We must insure that the existing instrument link is purged.
447 // This is done by deleting the links in the list.
449 // HI_Head is now a null pointer so we can generate a new list from the
452 fgHUDInit( cur_aircraft );
453 ac_cockpit = new fg_Cockpit();
455 if ( current_options.get_panel_status() ) {
459 // Have to set the LatLon display type
460 fgLatLonFormat = toDM;
462 FG_LOG( FG_COCKPIT, FG_INFO,
463 " Code " << ac_cockpit->code() << " Status "
464 << ac_cockpit->status() );
466 // HUD_TextSize = (current_options.get_xsize() > 1000) ? 10 : 8;
468 HUDtext = new fntRenderer();
469 HUDtext -> setFont ( guiFntHandle ) ;
470 HUDtext -> setPointSize ( HUD_TextSize ) ;
471 HUD_TextList.setFont( HUDtext );
477 void fgCockpitUpdate( void ) {
479 int iwidth = current_view.get_winWidth();
480 int iheight = current_view.get_winHeight();
481 float width = iwidth;
482 float height = iheight;
484 FG_LOG( FG_COCKPIT, FG_DEBUG,
485 "Cockpit: code " << ac_cockpit->code() << " status "
486 << ac_cockpit->status() );
488 if ( current_options.get_hud_status() ) {
489 // This will check the global hud linked list pointer.
490 // If these is anything to draw it will.
493 #define DISPLAY_COUNTER
494 #ifdef DISPLAY_COUNTER
498 float fps = get_frame_rate();
499 // float tris = fps * get_vfc_tris_drawn();
500 // float culled = fps * get_vfc_tris_culled();
501 // sprintf(buf,"%-4.1f %7.0f %7.0f", fps, tris, culled);
502 sprintf(buf,"%-5.1f", fps);
504 glMatrixMode(GL_PROJECTION);
507 gluOrtho2D(0, width, 0, height);
508 glMatrixMode(GL_MODELVIEW);
512 glDisable(GL_DEPTH_TEST);
513 glDisable(GL_LIGHTING);
515 glColor3f (0.9, 0.4, 0.2);
517 guiFnt.drawString( buf,
518 width/2 - guiFnt.getStringWidth(buf)/2,
520 glEnable(GL_DEPTH_TEST);
521 glEnable(GL_LIGHTING);
522 glMatrixMode(GL_PROJECTION);
524 glMatrixMode(GL_MODELVIEW);
527 #endif // #ifdef DISPLAY_COUNTER
529 if( current_options.get_panel_status() &&
530 (fabs( current_view.get_view_offset() ) < 0.2) )
532 xglViewport( 0, 0, iwidth, iheight );
534 FGPanel::OurPanel->Update();