]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/cockpit.cxx
- Added ultra-light traffic is now a separate traffic class that can have its
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 <simgear/compiler.h>
33
34 #include SG_GLU_H
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39
40 #include <simgear/constants.h>
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/math/polar3d.hxx>
43 #include <simgear/props/props.hxx>
44 #include <simgear/timing/sg_time.hxx>
45
46 #include <Aircraft/aircraft.hxx>
47 #include <Include/general.hxx>
48 #ifdef ENABLE_SP_FDM
49 #include <FDM/SP/ADA.hxx>
50 #endif
51 #include <Main/globals.hxx>
52 #include <Main/fg_props.hxx>
53 #include <Main/viewmgr.hxx>
54 #include <Scenery/scenery.hxx>
55 #include <Time/fg_timer.hxx>
56 #include <GUI/gui.h>
57
58 #include "cockpit.hxx"
59 #include "hud.hxx"
60
61
62 // The following routines obtain information concerntin the aircraft's
63 // current state and return it to calling instrument display routines.
64 // They should eventually be member functions of the aircraft.
65 //
66
67 float get_latitude( void )
68 {
69     return current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
70 }
71
72 float get_lat_min( void )
73 {
74     double a, d;
75
76     a = current_aircraft.fdm_state->get_Latitude() * SGD_RADIANS_TO_DEGREES;
77     if (a < 0.0) {
78         a = -a;
79     }
80     d = (double) ( (int) a);
81     float lat_min = (a - d) * 60.0;
82
83     return lat_min;
84 }
85
86
87 float get_longitude( void )
88 {
89     return current_aircraft.fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
90 }
91
92
93 char*
94 get_formated_gmt_time( void )
95 {
96     static char buf[32];
97     const struct tm *p = globals->get_time_params()->getGmt();
98     sprintf( buf, "%d/%d/%4d %d:%02d:%02d",
99          p->tm_mon+1, p->tm_mday, 1900 + p->tm_year,
100          p->tm_hour, p->tm_min, p->tm_sec);
101
102     return buf;
103 }
104
105
106 float get_long_min( void )
107 {
108     double  a, d;
109     a = current_aircraft.fdm_state->get_Longitude() * SGD_RADIANS_TO_DEGREES;
110     if (a < 0.0) {
111         a = -a;
112     }
113     d = (double) ( (int) a);
114     float lon_min = (a - d) * 60.0;
115
116     return lon_min;
117 }
118
119 float get_throttleval( void )
120 {
121     // Hack limiting to one engine
122     return globals->get_controls()->get_throttle( 0 );
123 }
124
125 float get_aileronval( void )
126 {
127     return globals->get_controls()->get_aileron();
128 }
129
130 float get_elevatorval( void )
131 {
132     return globals->get_controls()->get_elevator();
133 }
134
135 float get_elev_trimval( void )
136 {
137     return globals->get_controls()->get_elevator_trim();
138 }
139
140 float get_rudderval( void )
141 {
142     return globals->get_controls()->get_rudder();
143 }
144
145 float get_speed( void )
146 {
147     static const SGPropertyNode * speedup_node = fgGetNode("/sim/speed-up");
148
149     float speed = current_aircraft.fdm_state->get_V_calibrated_kts()
150         * speedup_node->getIntValue();
151
152     return speed;
153 }
154
155 float get_mach(void)
156 {
157     return current_aircraft.fdm_state->get_Mach_number();
158 }
159
160 float get_aoa( void )
161 {
162     return current_aircraft.fdm_state->get_Alpha() * SGD_RADIANS_TO_DEGREES;
163 }
164
165 float get_roll( void )
166 {
167     return current_aircraft.fdm_state->get_Phi();
168 }
169
170 float get_pitch( void )
171 {
172     return current_aircraft.fdm_state->get_Theta();
173 }
174
175 float get_heading( void )
176 {
177     return current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES;
178 }
179
180 float get_altitude( void )
181 {
182     static const SGPropertyNode *startup_units_node
183         = fgGetNode("/sim/startup/units");
184
185     float altitude;
186
187     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
188         altitude = current_aircraft.fdm_state->get_Altitude();
189     } else {
190         altitude = (current_aircraft.fdm_state->get_Altitude()
191                     * SG_FEET_TO_METER);
192     }
193
194     return altitude;
195 }
196
197 float get_agl( void )
198 {
199     static const SGPropertyNode *startup_units_node
200         = fgGetNode("/sim/startup/units");
201
202     float agl;
203
204     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
205         agl = (current_aircraft.fdm_state->get_Altitude()
206                - current_aircraft.fdm_state->get_Runway_altitude());
207     } else {
208         agl = (current_aircraft.fdm_state->get_Altitude()
209                - current_aircraft.fdm_state->get_Runway_altitude()) * SG_FEET_TO_METER;
210     }
211
212     return agl;
213 }
214
215 float get_sideslip( void )
216 {
217     return current_aircraft.fdm_state->get_Beta();
218 }
219
220 float get_frame_rate( void )
221 {
222     return general.get_frame_rate();
223 }
224
225 float get_fov( void )
226 {
227     return globals->get_current_view()->get_fov();
228 }
229
230 float get_vfc_ratio( void )
231 {
232     // float vfc = current_view.get_vfc_ratio();
233     // return (vfc);
234     return 0.0;
235 }
236
237 float get_vfc_tris_drawn   ( void )
238 {
239     // float rendered = current_view.get_tris_rendered();
240     // return (rendered);
241     return 0.0;
242 }
243
244 float get_vfc_tris_culled   ( void )
245 {
246     // float culled = current_view.get_tris_culled();
247     // return (culled);
248     return 0.0;
249 }
250
251 float get_climb_rate( void )
252 {
253     static const SGPropertyNode *startup_units_node
254         = fgGetNode("/sim/startup/units");
255
256     float climb_rate;
257     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
258         climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
259     } else {
260         climb_rate = current_aircraft.fdm_state->get_Climb_Rate() * SG_FEET_TO_METER * 60.0;
261     }
262
263     return climb_rate;
264 }
265
266
267 float get_view_direction( void )
268 {
269     double view_off = SGD_2PI - globals->get_current_view()->getHeadingOffset_deg() * SGD_DEGREES_TO_RADIANS;
270     double view = ( current_aircraft.fdm_state->get_Psi() + view_off)
271         * SGD_RADIANS_TO_DEGREES;
272
273     if (view > 360.)
274         view -= 360.;
275     else if (view<0.)
276         view += 360.;
277
278     return view;
279 }
280
281 // Added by Markus Hof on 5. Jan 2004
282 float get_dme( void )
283 {
284     static const SGPropertyNode * dme_node =
285         fgGetNode("/instrumentation/dme/indicated-distance-nm");
286
287     return dme_node->getFloatValue();
288 }
289
290 // $$$ begin - added, VS Renganathan 13 Oct 2K
291 // #ifdef FIGHTER_HUD
292 float get_Vx   ( void )
293 {
294     // Curt dont comment this and return zero. - Ranga
295     // Please remove comments from get_V_..() function in flight.hxx
296     float Vxx = current_aircraft.fdm_state->get_V_north_rel_ground();
297     return Vxx;
298 }
299
300 float get_Vy   ( void )
301 {
302     // Curt dont comment this and return zero. - Ranga
303     // Please remove comments from get_V_..() function in flight.hxx
304     float Vyy = current_aircraft.fdm_state->get_V_east_rel_ground();
305     return Vyy;
306 }
307
308 float get_Vz   ( void )
309 {
310     // Curt dont comment this and return zero. - Ranga
311     // Please remove comments from get_V_..() function in flight.hxx
312     float Vzz = current_aircraft.fdm_state->get_V_down_rel_ground();
313     return Vzz;
314 }
315
316 float get_Ax   ( void )
317 {
318     float Ax = current_aircraft.fdm_state->get_V_dot_north();
319     return Ax;
320 }
321
322 float get_Ay   ( void )
323 {
324     float Ay = current_aircraft.fdm_state->get_V_dot_east();
325     return Ay;
326 }
327
328 float get_Az   ( void )
329 {
330     float Az = current_aircraft.fdm_state->get_V_dot_down();
331     return Az;
332 }
333
334 float get_anzg   ( void )
335 {
336     float anzg = current_aircraft.fdm_state->get_N_Z_cg();
337     return anzg;
338 }
339
340 #ifdef ENABLE_SP_FDM
341 int get_iaux1 (void)
342 {
343     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
344     return fdm->get_iaux(1);
345 }
346
347 int get_iaux2 (void)
348 {
349     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
350     return fdm->get_iaux(2);
351 }
352
353 int get_iaux3 (void)
354 {
355     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
356     return fdm->get_iaux(3);
357 }
358
359 int get_iaux4 (void)
360 {
361     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
362     return fdm->get_iaux(4);
363 }
364
365 int get_iaux5 (void)
366 {
367     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
368     return fdm->get_iaux(5);
369 }
370
371 int get_iaux6 (void)
372 {
373     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
374     return fdm->get_iaux(6);
375 }
376
377 int get_iaux7 (void)
378 {
379     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
380     return fdm->get_iaux(7);
381 }
382
383 int get_iaux8 (void)
384 {
385     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
386     return fdm->get_iaux(8);
387 }
388
389 int get_iaux9 (void)
390 {
391     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
392     return fdm->get_iaux(9);
393 }
394
395 int get_iaux10 (void)
396 {
397     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
398     return fdm->get_iaux(10);
399 }
400
401 int get_iaux11 (void)
402 {
403     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
404     return fdm->get_iaux(11);
405 }
406
407 int get_iaux12 (void)
408 {
409      FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
410      return fdm->get_iaux(12);
411 }
412
413 float get_aux1 (void)
414 {
415     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
416     return fdm->get_daux(1);
417 }
418
419 float get_aux2 (void)
420 {
421     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
422     return fdm->get_daux(2);
423 }
424
425 float get_aux3 (void)
426 {
427     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
428     return fdm->get_daux(3);
429 }
430
431 float get_aux4 (void)
432 {
433     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
434     return fdm->get_daux(4);
435 }
436
437 float get_aux5 (void)
438 {
439     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
440     return fdm->get_daux(5);
441 }
442
443 float get_aux6 (void)
444 {
445     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
446     return fdm->get_daux(6);
447 }
448
449 float get_aux7 (void)
450 {
451     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
452     return fdm->get_daux(7);
453 }
454
455 float get_aux8 (void)
456 {
457     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
458     return fdm->get_daux(8);
459 }
460
461 float get_aux9 (void)
462 {
463     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
464     return fdm->get_faux(1);
465 }
466
467 float get_aux10 (void)
468 {
469     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
470     return fdm->get_faux(2);
471 }
472
473 float get_aux11 (void)
474 {
475     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
476     return fdm->get_faux(3);
477 }
478
479 float get_aux12 (void)
480 {
481     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
482     return fdm->get_faux(4);
483 }
484
485 float get_aux13 (void)
486 {
487     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
488     return fdm->get_faux(5);
489 }
490
491 float get_aux14 (void)
492 {
493     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
494     return fdm->get_faux(6);
495 }
496
497 float get_aux15 (void)
498 {
499     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
500     return fdm->get_faux(7);
501 }
502
503 float get_aux16 (void)
504 {
505     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
506     return fdm->get_faux(8);
507 }
508
509 float get_aux17 (void)
510 {
511     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
512     return fdm->get_faux(9);
513 }
514
515 float get_aux18 (void)
516 {
517     FGADA *fdm = (FGADA *)current_aircraft.fdm_state;
518     return fdm->get_faux(10);
519 }
520 #endif
521
522
523 bool fgCockpitInit( fgAIRCRAFT *cur_aircraft )
524 {
525     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing cockpit subsystem" );
526
527     //  cockpit->code = 1;  /* It will be aircraft dependent */
528     //  cockpit->status = 0;
529
530     // If aircraft has HUD specified we will get the specs from its def
531     // file. For now we will depend upon hard coding in hud?
532
533     // We must insure that the existing instrument link is purged.
534     // This is done by deleting the links in the list.
535
536     // HI_Head is now a null pointer so we can generate a new list from the
537     // current aircraft.
538
539     fgHUDInit( cur_aircraft );
540
541     return true;
542 }
543
544 void fgCockpitUpdate( osg::State* state ) {
545
546     static const SGPropertyNode * xsize_node = fgGetNode("/sim/startup/xsize");
547     static const SGPropertyNode * ysize_node = fgGetNode("/sim/startup/ysize");
548     static const SGPropertyNode * hud_visibility_node
549         = fgGetNode("/sim/hud/visibility");
550
551     int iwidth   = xsize_node->getIntValue();
552     int iheight  = ysize_node->getIntValue();
553
554                                 // FIXME: inefficient
555     if ( hud_visibility_node->getBoolValue() ) {
556         // This will check the global hud linked list pointer.
557         // If there is anything to draw it will.
558         fgUpdateHUD( state );
559     }
560
561     glViewport( 0, 0, iwidth, iheight );
562 }
563
564
565
566
567
568 struct FuncTable {
569     char *name;
570     FLTFNPTR func;
571 } fn_table[] = {
572     { "agl", get_agl },
573     { "aileronval", get_aileronval },
574     { "altitude", get_altitude },
575     { "anzg", get_anzg },
576     { "aoa", get_aoa },
577     { "ax", get_Ax },
578     { "climb", get_climb_rate },
579     { "elevatortrimval", get_elev_trimval },
580     { "elevatorval", get_elevatorval },
581     { "fov", get_fov },
582     { "framerate", get_frame_rate },
583     { "heading", get_heading },
584     { "latitude", get_latitude },
585     { "longitude", get_longitude },
586     { "mach", get_mach },
587     { "rudderval", get_rudderval },
588     { "speed", get_speed },
589     { "throttleval", get_throttleval },
590     { "view_direction", get_view_direction },
591     { "vfc_tris_culled", get_vfc_tris_culled },
592     { "vfc_tris_drawn", get_vfc_tris_drawn },
593 #ifdef ENABLE_SP_FDM
594     { "aux1", get_aux1 },
595     { "aux2", get_aux2 },
596     { "aux3", get_aux3 },
597     { "aux4", get_aux4 },
598     { "aux5", get_aux5 },
599     { "aux6", get_aux6 },
600     { "aux7", get_aux7 },
601     { "aux8", get_aux8 },
602     { "aux9", get_aux9 },
603     { "aux10", get_aux10 },
604     { "aux11", get_aux11 },
605     { "aux12", get_aux12 },
606     { "aux13", get_aux13 },
607     { "aux14", get_aux14 },
608     { "aux15", get_aux15 },
609     { "aux16", get_aux16 },
610     { "aux17", get_aux17 },
611     { "aux18", get_aux18 },
612 #endif
613     { 0, 0 },
614 };
615
616
617 FLTFNPTR get_func(const char *name)
618 {
619     for (int i = 0; fn_table[i].name; i++)
620         if (!strcmp(fn_table[i].name, name))
621             return fn_table[i].func;
622
623     return 0;
624 }
625
626