]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/cockpit.cxx
c26c65e867f71cfd324afbe839729c8ec75e9c47
[flightgear.git] / src / Cockpit / cockpit.cxx
1 // cockpit.cxx -- routines to draw a cockpit (initial draft)
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  - nomimarketing@mail.telepac.pt
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H          
29 #  include <windows.h>
30 #endif
31
32 #include <GL/glut.h>
33 #include <simgear/xgl/xgl.h>
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38
39 #include <simgear/constants.h>
40 #include <simgear/debug/logstream.hxx>
41 #include <simgear/math/fg_random.h>
42 #include <simgear/math/polar3d.hxx>
43
44 #include <Aircraft/aircraft.hxx>
45 #include <Include/general.hxx>
46 #include <Main/globals.hxx>
47 #include <Main/options.hxx>
48 #include <Main/views.hxx>
49 #include <Scenery/scenery.hxx>
50 #include <Time/fg_timer.hxx>
51 #include <GUI/gui.h>
52
53 #include "cockpit.hxx"
54
55
56 // This is a structure that contains all data related to
57 // cockpit/panel/hud system
58
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.
63 //
64
65 float get_latitude( void )
66 {
67     double lat;
68
69     lat = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
70
71     float flat = lat;
72     return(flat);
73
74 }
75
76 float get_lat_min( void )
77 {
78     double      a, d;
79
80     a = current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;    
81     if (a < 0.0) {
82         a = -a;
83     }
84     d = (double) ( (int) a);
85     float lat_min = (a - d) * 60.0;
86     return(lat_min );
87 }
88
89
90 float get_longitude( void )
91 {
92     double lon;
93
94     lon = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
95
96     float flon = lon;
97     return(flon);
98 }
99
100
101 char*
102 get_formated_gmt_time( void )
103 {
104     static char buf[32];
105     const struct tm *p = globals->get_time_params()->getGmt();
106     sprintf( buf, "%d/%d/%4d %d:%02d:%02d", 
107          p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
108          p->tm_hour, p->tm_min, p->tm_sec);
109     return buf;
110 }
111
112
113 float get_long_min( void )
114 {
115     double  a, d;
116     a = current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;   
117     if (a < 0.0) {
118         a = -a;
119     }
120     d = (double) ( (int) a);
121     float lon_min = (a - d) * 60.0; 
122     return(lon_min);
123 }
124
125 float get_throttleval( void )
126 {
127     float throttle = controls.get_throttle( 0 );
128     return (throttle);     // Hack limiting to one engine
129 }
130
131 float get_aileronval( void )
132 {
133     float aileronval = controls.get_aileron();
134     return (aileronval);
135 }
136
137 float get_elevatorval( void )
138 {
139     float elevator_val = (float)controls.get_elevator();
140     return elevator_val;
141 }
142
143 float get_elev_trimval( void )
144 {
145     float elevatorval = controls.get_elevator_trim();
146     return (elevatorval);
147 }
148
149 float get_rudderval( void )
150 {
151     float rudderval = controls.get_rudder();
152     return (rudderval);
153 }
154
155 float get_speed( void )
156 {
157     // Make an explicit function call.
158     float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
159         * current_options.get_speed_up();
160     return( speed );
161 }
162
163 float get_mach(void)
164 {
165         float mach=current_aircraft.fdm_state->get_Mach_number();
166         return mach;
167 }       
168
169 float get_aoa( void )
170 {
171     float aoa = current_aircraft.fdm_state->get_Alpha() * RAD_TO_DEG;
172     return( aoa );
173 }
174
175 float get_roll( void )
176 {
177     float roll = current_aircraft.fdm_state->get_Phi();
178     return( roll );
179 }
180
181 float get_pitch( void )
182 {
183     float pitch = current_aircraft.fdm_state->get_Theta();
184     return( pitch );
185 }
186
187 float get_heading( void )
188 {
189     float heading = (current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG);
190     return( heading );
191 }
192
193 float get_altitude( void )
194 {
195 //  FGState *f;
196     // double rough_elev;
197
198 //  current_aircraft.fdm_state
199     // rough_elev = mesh_altitude(f->get_Longitude() * RAD_TO_ARCSEC,
200     //                         f->get_Latitude()  * RAD_TO_ARCSEC);
201     float altitude;
202
203     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
204         altitude = current_aircraft.fdm_state->get_Altitude();
205     } else {
206         altitude = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER);
207     }
208     return altitude;
209 }
210
211 float get_agl( void )
212 {
213     float agl;
214
215     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
216         agl = (current_aircraft.fdm_state->get_Altitude()
217                - scenery.cur_elev * METER_TO_FEET);
218     } else {
219         agl = (current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER
220                - scenery.cur_elev);
221     }
222     return agl;
223 }
224
225 float get_sideslip( void )
226 {
227     float sideslip = current_aircraft.fdm_state->get_Beta();
228     return( sideslip );
229 }
230
231 float get_frame_rate( void )
232 {
233     float frame_rate = general.get_frame_rate();
234     return (frame_rate); 
235 }
236
237 float get_fov( void )
238 {
239     float fov = current_options.get_fov(); 
240     return (fov);
241 }
242
243 float get_vfc_ratio( void )
244 {
245     // float vfc = current_view.get_vfc_ratio();
246     // return (vfc);
247     return 0.0;
248 }
249
250 float get_vfc_tris_drawn   ( void )
251 {
252     // float rendered = current_view.get_tris_rendered();
253     // return (rendered);
254     return 0.0;
255 }
256
257 float get_vfc_tris_culled   ( void )
258 {
259     // float culled = current_view.get_tris_culled();
260     // return (culled);
261     return 0.0;
262 }
263
264 float get_climb_rate( void )
265 {
266     float climb_rate;
267     if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
268         climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
269     } else {
270         climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * FEET_TO_METER * 60.0;
271     }
272     return (climb_rate);
273 }
274
275
276 float get_view_direction( void )
277 {
278     double view;
279  
280     view = FG_2PI - current_view.get_view_offset();
281     view = ( current_aircraft.fdm_state->get_Psi() + view) * RAD_TO_DEG;
282     
283     if(view > 360.)
284         view -= 360.;
285     else if(view<0.)
286         view += 360.;
287     
288     float fview = view;
289     return( fview );
290 }
291
292 #ifdef NOT_USED
293 /****************************************************************************/
294 /* Convert degrees to dd mm'ss.s" (DMS-Format)                              */
295 /****************************************************************************/
296 char *dmshh_format(double degrees)
297 {
298     static char buf[16];    
299     int deg_part;
300     int min_part;
301     double sec_part;
302     
303     if (degrees < 0)
304       degrees = -degrees;
305
306     deg_part = degrees;
307     min_part = 60.0 * (degrees - deg_part);
308     sec_part = 3600.0 * (degrees - deg_part - min_part / 60.0);
309
310     /* Round off hundredths */
311     if (sec_part + 0.005 >= 60.0)
312       sec_part -= 60.0, min_part += 1;
313     if (min_part >= 60)
314       min_part -= 60, deg_part += 1;
315
316     sprintf(buf,"%02d*%02d %05.2f",deg_part,min_part,sec_part);
317
318     return buf;
319 }
320 #endif // 0
321
322
323 /************************************************************************
324  Convert degrees to dd mm.mmm' (DMM-Format)
325  Description: Converts using a round-off factor tailored to the required
326  precision of the minutes field (three decimal places).  Round-off
327  prevents function from returning a minutes value of 60.
328
329  Input arguments: Coordinate value in decimal degrees
330
331 ************************************************************************/
332 static char *toDM(float dd)
333 {
334     static char  dm[16];
335     double tempdd;
336     double mn;
337     double sign = 1;
338     int deg;
339
340     if (dd < 0) {
341         sign = -1;
342     }
343     /* round for minutes expressed to three decimal places */
344     tempdd = fabs(dd) + (5.0E-4 / 60.0);
345     deg = (int)tempdd;
346     mn = fabs( (tempdd - (double)(deg)) * 60.0 - 4.999E-4 );
347     deg *= (int)sign;
348     sprintf(dm, "%d*%06.3f", deg, mn);
349     return dm;
350 }
351
352
353 /************************************************************************
354  Convert degrees to dd mm'ss.s'' (DMS-Format)
355  Description: Converts using a round-off factor tailored to the required
356  precision of the seconds field (one decimal place).  Round-off
357  prevents function from returning a seconds value of 60.
358
359  Input arguments: Coordinate value in decimal degrees
360
361 ************************************************************************/
362 static char *toDMS(float dd)
363 {
364     static char  dms[16];
365     double tempdd, tempmin;
366     int deg;
367     int mn;
368     double sec;
369     double sign = 1;
370
371     if(dd < 0) {
372         sign = -1;
373     }
374     /* round up for seconds expressed to one decimal place */
375     tempdd = fabs(dd) + (0.05 / 3600.0);
376     deg = (int)tempdd;
377     tempmin =  (tempdd - (double)(deg)) * 60.0;
378     mn = (int)tempmin;
379     sec = fabs( (tempmin - (double)(mn)) * 60.0 - 0.049 );
380     deg *= (int)sign;
381     sprintf(dms, "%d*%02d %04.1f", deg, mn, sec);
382     return dms;
383 }
384
385
386 // Have to set the LatLon display type
387 //static char *(*fgLatLonFormat)(float) = toDM;
388 static char *(*fgLatLonFormat)(float);
389
390 char *coord_format_lat(float latitude)
391 {
392     static char buf[16];
393
394     sprintf(buf,"%s%c",
395 //      dmshh_format(latitude),
396 //      toDMS(latitude),
397 //      toDM(latitude),
398         fgLatLonFormat(latitude),           
399         latitude > 0 ? 'N' : 'S');
400     return buf;
401 }
402
403 char *coord_format_lon(float longitude)
404 {
405     static char buf[80];
406
407     sprintf(buf,"%s%c",
408 //      dmshh_format(longitude),
409 //      toDMS(longitude),
410 //      toDM(longitude),
411         fgLatLonFormat(longitude),
412         longitude > 0 ? 'E' : 'W');
413     return buf;
414 }
415
416 void fgLatLonFormatToggle( puObject *)
417 {
418     static int toggle = 0;
419
420     if ( toggle ) 
421         fgLatLonFormat = toDM;
422     else
423         fgLatLonFormat = toDMS;
424     
425     toggle = ~toggle;
426 }
427
428 #ifdef NOT_USED
429 char *coord_format_latlon(double latitude, double longitude)
430 {
431     static char buf[1024];
432
433     sprintf(buf,"%s%c %s%c",
434         dmshh_format(latitude),
435         latitude > 0 ? 'N' : 'S',
436         dmshh_format(longitude),
437         longitude > 0 ? 'E' : 'W');
438     return buf;
439 }
440 #endif
441
442
443 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
444 {
445     FG_LOG( FG_COCKPIT, FG_INFO, "Initializing cockpit subsystem" );
446
447     //  cockpit->code = 1;  /* It will be aircraft dependent */
448     //  cockpit->status = 0;
449
450     // If aircraft has HUD specified we will get the specs from its def
451     // file. For now we will depend upon hard coding in hud?
452     
453     // We must insure that the existing instrument link is purged.
454     // This is done by deleting the links in the list.
455     
456     // HI_Head is now a null pointer so we can generate a new list from the
457     // current aircraft.
458
459     fgHUDInit( cur_aircraft );
460     ac_cockpit = new fg_Cockpit();
461     
462     // Have to set the LatLon display type
463     fgLatLonFormat = toDM;
464     
465     FG_LOG( FG_COCKPIT, FG_INFO,
466         "  Code " << ac_cockpit->code() << " Status " 
467         << ac_cockpit->status() );
468
469         return true;
470 }
471
472 void fgCockpitUpdate( void ) {
473
474     FG_LOG( FG_COCKPIT, FG_DEBUG,
475         "Cockpit: code " << ac_cockpit->code() << " status " 
476         << ac_cockpit->status() );
477
478         int iwidth   = current_view.get_winWidth();
479         int iheight  = current_view.get_winHeight();
480         float width  = iwidth;
481         float height = iheight;
482
483     if ( current_options.get_hud_status() ) {
484         // This will check the global hud linked list pointer.
485         // If these is anything to draw it will.
486         fgUpdateHUD();
487     }
488 #define DISPLAY_COUNTER
489 #ifdef DISPLAY_COUNTER
490     else
491     {
492         char buf[64];
493         float fps    =       get_frame_rate();
494 //      float tris   = fps * get_vfc_tris_drawn();
495 //      float culled = fps * get_vfc_tris_culled();
496 //      sprintf(buf,"%-4.1f  %7.0f  %7.0f", fps, tris, culled);
497         sprintf(buf,"%-5.1f", fps);
498
499         glMatrixMode(GL_PROJECTION);
500         glPushMatrix();
501         glLoadIdentity();
502         gluOrtho2D(0, width, 0, height);
503         glMatrixMode(GL_MODELVIEW);
504         glPushMatrix();
505         glLoadIdentity();
506
507         glDisable(GL_DEPTH_TEST);
508         glDisable(GL_LIGHTING);
509         
510         glColor3f (0.9, 0.4, 0.2);
511
512         guiFnt.drawString( buf,
513                            // width/2 - guiFnt.getStringWidth(buf)/2,
514                            int(width - guiFnt.getStringWidth(buf) - 10),
515                            10 );
516         glEnable(GL_DEPTH_TEST);
517         glEnable(GL_LIGHTING);
518         glMatrixMode(GL_PROJECTION);
519         glPopMatrix();
520         glMatrixMode(GL_MODELVIEW);
521         glPopMatrix();
522     }
523 #endif // #ifdef DISPLAY_COUNTER
524     
525     xglViewport( 0, 0, iwidth, iheight );
526
527     if (current_panel != 0)
528       current_panel->update();
529 }