]> git.mxchange.org Git - flightgear.git/blob - src/Cockpit/hud.cxx
- Added ultra-light traffic is now a separate traffic class that can have its
[flightgear.git] / src / Cockpit / hud.cxx
1 // hud.cxx -- hud defines and prototypes
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  - micheleamerica@geocities.com
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 #include <simgear/compiler.h>
24 #include <simgear/structure/exception.hxx>
25
26 #include STL_STRING
27 #include STL_FSTREAM
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #ifdef HAVE_WINDOWS_H
34 #  include <windows.h>
35 #endif
36
37 #ifdef __BORLANDC__
38 #  define exception c_exception
39 #endif
40
41 #include <math.h>
42
43 #include <stdlib.h>
44 #include <stdio.h>              // char related functions
45 #include <string.h>             // strcmp()
46
47 #include SG_GLU_H
48
49 #include <simgear/constants.h>
50 #include <simgear/debug/logstream.hxx>
51 #include <simgear/misc/sg_path.hxx>
52
53 #include <Aircraft/aircraft.hxx>
54 //#include <Autopilot/xmlauto.hxx>
55 #include <GUI/new_gui.hxx>           // FGFontCache
56 #include <Main/globals.hxx>
57 #include <Scenery/scenery.hxx>
58
59 #include "hud.hxx"
60
61
62 static HUD_Properties *HUDprop = 0;
63
64 static char units[5];
65
66 deque<instr_item *> HUD_deque;
67
68 fgTextList HUD_TextList;
69 fgLineList HUD_LineList;
70 fgLineList HUD_StippleLineList;
71
72 fntRenderer *HUDtext = 0;
73 fntTexFont *HUD_Font = 0;
74 float HUD_TextSize = 0;
75 int HUD_style = 0;
76
77 float HUD_matrix[16];
78
79 int readHud( istream &input );
80 int readInstrument ( const SGPropertyNode * node);
81
82 static void drawHUD(osg::State*);
83 static void fgUpdateHUDVirtual(osg::State*);
84
85
86 class locRECT {
87 public:
88     RECT rect;
89
90     locRECT( UINT left, UINT top, UINT right, UINT bottom);
91     RECT get_rect(void) { return rect; }
92 };
93
94 locRECT :: locRECT( UINT left, UINT top, UINT right, UINT bottom)
95 {
96     rect.left   =  left;
97     rect.top    =  top;
98     rect.right  =  right;
99     rect.bottom =  bottom;
100
101 }
102 // #define DEBUG
103
104
105
106
107 int readInstrument(const SGPropertyNode * node)
108 {
109     static const SGPropertyNode *startup_units_node
110         = fgGetNode("/sim/startup/units");
111
112     instr_item *HIptr;
113
114     if ( !strcmp(startup_units_node->getStringValue(), "feet") ) {
115         strcpy(units, " ft");
116     } else {
117         strcpy(units, " m");
118     }
119
120     const SGPropertyNode * ladder_group = node->getNode("ladders");
121
122     if (ladder_group != 0) {
123         int nLadders = ladder_group->nChildren();
124         for (int j = 0; j < nLadders; j++) {
125             HIptr = static_cast<instr_item *>(new HudLadder(ladder_group->getChild(j)));
126             HUD_deque.insert(HUD_deque.begin(), HIptr);
127         }
128     }
129
130     const SGPropertyNode * card_group = node->getNode("cards");
131     if (card_group != 0) {
132         int nCards = card_group->nChildren();
133         for (int j = 0; j < nCards; j++) {
134             const char *type = card_group->getChild(j)->getStringValue("type", "gauge");
135
136             if (!strcmp(type, "gauge"))
137                 HIptr = static_cast<instr_item *>(new gauge_instr(card_group->getChild(j)));
138             else if (!strcmp(type, "dial") || !strcmp(type, "tape"))
139                 HIptr = static_cast<instr_item *>(new hud_card(card_group->getChild(j)));
140             else {
141                 SG_LOG(SG_INPUT, SG_WARN, "HUD: unknown 'card' type: " << type);
142                 continue;
143             }
144             HUD_deque.insert(HUD_deque.begin(), HIptr);
145         }
146     }
147
148     const SGPropertyNode * label_group = node->getNode("labels");
149     if (label_group != 0) {
150         int nLabels = label_group->nChildren();
151         for (int j = 0; j < nLabels; j++) {
152             HIptr = static_cast<instr_item *>(new instr_label(label_group->getChild(j)));
153             HUD_deque.insert(HUD_deque.begin(), HIptr);
154         }
155     }
156
157     const SGPropertyNode * tbi_group = node->getNode("tbis");
158     if (tbi_group != 0) {
159         int nTbis = tbi_group->nChildren();
160         for (int j = 0; j < nTbis; j++) {
161             HIptr = static_cast<instr_item *>(new fgTBI_instr(tbi_group->getChild(j)));
162             HUD_deque.insert(HUD_deque.begin(), HIptr);
163         }
164     }
165
166     const SGPropertyNode * rwy_group = node->getNode("runways");
167     if (rwy_group != 0) {
168         int nRwy = rwy_group->nChildren();
169         for (int j = 0; j < nRwy; j++) {
170             HIptr = static_cast<instr_item *>(new runway_instr(rwy_group->getChild(j)));
171             HUD_deque.insert(HUD_deque.begin(), HIptr);
172         }
173     }
174     return 0;
175 } //end readinstrument
176
177
178 int readHud( istream &input )
179 {
180
181     SGPropertyNode root;
182
183     try {
184         readProperties(input, &root);
185     } catch (const sg_exception &e) {
186         guiErrorMessage("Error reading HUD: ", e);
187         return 0;
188     }
189
190
191     SG_LOG(SG_INPUT, SG_INFO, "Read properties for  " <<
192            root.getStringValue("name"));
193
194
195     HUD_deque.erase( HUD_deque.begin(), HUD_deque.end());
196
197
198     SG_LOG(SG_INPUT, SG_INFO, "Reading Hud instruments");
199
200     const SGPropertyNode * instrument_group = root.getChild("instruments");
201     int nInstruments = instrument_group->nChildren();
202
203     for (int i = 0; i < nInstruments; i++) {
204
205         const SGPropertyNode * node = instrument_group->getChild(i);
206
207         SGPath path( globals->get_fg_root() );
208         path.append(node->getStringValue("path"));
209
210         SG_LOG(SG_INPUT, SG_INFO, "Reading Instrument "
211                << node->getName()
212                << " from "
213                << path.str());
214
215         SGPropertyNode root2;
216         try {
217             readProperties(path.str(), &root2);
218         } catch (const sg_exception &e) {
219             guiErrorMessage("Error reading HUD instrument: ", e);
220             continue;
221         }
222         readInstrument(&root2);
223     }//for loop(i)
224
225     return 0;
226 }
227
228
229 // fgHUDInit
230 //
231 // Constructs a HUD object and then adds in instruments. At the present
232 // the instruments are hard coded into the routine. Ultimately these need
233 // to be defined by the aircraft's instrumentation records so that the
234 // display for a Piper Cub doesn't show the speed range of a North American
235 // mustange and the engine readouts of a B36!
236 //
237 int fgHUDInit( fgAIRCRAFT * /* current_aircraft */ )
238 {
239
240     HUD_style = 1;
241
242     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
243
244     string hud_path =
245         fgGetString("/sim/hud/path", "Huds/Default/default.xml");
246     SGPath path(globals->get_fg_root());
247     path.append(hud_path);
248
249     ifstream input(path.c_str());
250     if (!input.good()) {
251         SG_LOG(SG_INPUT, SG_ALERT,
252                "Cannot read Hud configuration from " << path.str());
253     } else {
254         readHud(input);
255         input.close();
256     }
257
258     if ( HUDtext ) {
259         // this chunk of code is not necessarily thread safe if the
260         // compiler optimizer reorders these statements.  Note that
261         // "delete ptr" does not set "ptr = NULL".  We have to do that
262         // ourselves.
263         fntRenderer *tmp = HUDtext;
264         HUDtext = NULL;
265         delete tmp;
266     }
267
268     FGFontCache *fc = globals->get_fontcache();
269     HUD_Font = fc->getTexFont(fgGetString("/sim/hud/font/name", "Helvetica.txf"));
270     if (!HUD_Font)
271         throw sg_throwable(string("/sim/hud/font/name is not a texture font"));
272
273     HUD_TextSize = fgGetFloat("/sim/hud/font/size", 10);
274
275     HUDtext = new fntRenderer();
276     HUDtext->setFont(HUD_Font);
277     HUDtext->setPointSize(HUD_TextSize);
278     HUD_TextList.setFont( HUDtext );
279
280     if (!HUDprop)
281         HUDprop = new HUD_Properties;
282     return 0;  // For now. Later we may use this for an error code.
283
284 }
285
286
287 int fgHUDInit2( fgAIRCRAFT * /* current_aircraft */ )
288 {
289
290     HUD_style = 2;
291
292     SG_LOG( SG_COCKPIT, SG_INFO, "Initializing current aircraft HUD" );
293
294     SGPath path(globals->get_fg_root());
295     path.append("Huds/Minimal/default.xml");
296
297
298     ifstream input(path.c_str());
299     if (!input.good()) {
300         SG_LOG(SG_INPUT, SG_ALERT,
301                "Cannot read Hud configuration from " << path.str());
302     } else {
303         readHud(input);
304         input.close();
305     }
306
307     if (!HUDprop)
308         HUDprop = new HUD_Properties;
309     return 0;  // For now. Later we may use this for an error code.
310
311 }
312 //$$$ End - added, Neetha, 28 Nov 2k
313
314
315 // fgUpdateHUD
316 //
317 // Performs a once around the list of calls to instruments installed in
318 // the HUD object with requests for redraw. Kinda. It will when this is
319 // all C++.
320 //
321 void fgUpdateHUD( osg::State* state ) {
322
323     static const SGPropertyNode *enable3d_node = fgGetNode("/sim/hud/enable3d");
324     if ( HUD_style == 1 && enable3d_node->getBoolValue() ) {
325         fgUpdateHUDVirtual(state);
326         return;
327     }
328
329     static const float normal_aspect = float(640) / float(480);
330     // note: aspect_ratio is Y/X
331     float current_aspect = 1.0f/globals->get_current_view()->get_aspect_ratio();
332     if ( current_aspect > normal_aspect ) {
333         float aspect_adjust = current_aspect / normal_aspect;
334         float adjust = 320.0f*aspect_adjust - 320.0f;
335         fgUpdateHUD( state, -adjust, 0.0f, 640.0f+adjust, 480.0f );
336     } else {
337         float aspect_adjust = normal_aspect / current_aspect;
338         float adjust = 240.0f*aspect_adjust - 240.0f;
339         fgUpdateHUD( state, 0.0f, -adjust, 640.0f, 480.0f+adjust );
340     }
341 }
342
343 void fgUpdateHUDVirtual(osg::State* state)
344 {
345     FGViewer* view = globals->get_current_view();
346
347     // Standard fgfs projection, with essentially meaningless clip
348     // planes (we'll map the whole HUD plane to z=-1)
349     glMatrixMode(GL_PROJECTION);
350     glPushMatrix();
351     glLoadIdentity();
352     gluPerspective(view->get_v_fov(), 1/view->get_aspect_ratio(), 0.1, 10);
353
354     glMatrixMode(GL_MODELVIEW);
355     glPushMatrix();
356     glLoadIdentity();
357
358     // Standard fgfs view direction computation
359     float lookat[3];
360     lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
361     lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
362     lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
363     if (fabs(lookat[1]) > 9999)
364         lookat[1] = 9999; // FPU sanity
365     gluLookAt(0, 0, 0, lookat[0], lookat[1], lookat[2], 0, 1, 0);
366
367     // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
368     // This is the default fgfs field of view, which the HUD files are
369     // written to assume.
370     float dx = 0.52056705; // tan(55/2)
371     float dy = dx * 0.75;  // assumes 4:3 aspect ratio
372     float m[16];
373     m[0] = dx; m[4] =  0; m[ 8] = 0; m[12] = 0;
374     m[1] =  0; m[5] = dy; m[ 9] = 0; m[13] = 0;
375     m[2] =  0; m[6] =  0; m[10] = 1; m[14] = 0;
376     m[3] =  0; m[7] =  0; m[11] = 0; m[15] = 1;
377     glMultMatrixf(m);
378
379     // Convert the 640x480 "HUD standard" coordinate space to a square
380     // about the origin in the range [-1:1] at depth of -1
381     glScalef(1./320, 1./240, 1);
382     glTranslatef(-320, -240, -1);
383
384     // Do the deed
385     drawHUD(state);
386
387     // Clean up our mess
388     glMatrixMode(GL_PROJECTION);
389     glPopMatrix();
390     glMatrixMode(GL_MODELVIEW);
391     glPopMatrix();
392 }
393
394
395 void fgUpdateHUD( osg::State* state, GLfloat x_start, GLfloat y_start,
396                   GLfloat x_end, GLfloat y_end )
397 {
398     glMatrixMode(GL_PROJECTION);
399     glPushMatrix();
400     glLoadIdentity();
401     gluOrtho2D(x_start, x_end, y_start, y_end);
402
403     glMatrixMode(GL_MODELVIEW);
404     glPushMatrix();
405     glLoadIdentity();
406
407     drawHUD(state);
408
409     glMatrixMode(GL_PROJECTION);
410     glPopMatrix();
411     glMatrixMode(GL_MODELVIEW);
412     glPopMatrix();
413 }
414
415
416 void drawHUD(osg::State* state)
417 {
418     if ( !HUD_deque.size() ) // Trust everyone, but ALWAYS cut the cards!
419         return;
420
421     HUD_TextList.erase();
422     HUD_LineList.erase();
423     // HUD_StippleLineList.erase();
424
425     glDisable(GL_DEPTH_TEST);
426     glDisable(GL_LIGHTING);
427
428     static const SGPropertyNode *heading_enabled
429         = fgGetNode("/autopilot/locks/heading", true);
430     static const SGPropertyNode *altitude_enabled
431         = fgGetNode("/autopilot/locks/altitude", true);
432
433     static char hud_hdg_text[256];
434     static char hud_wp0_text[256];
435     static char hud_wp1_text[256];
436     static char hud_wp2_text[256];
437     static char hud_alt_text[256];
438
439     glEnable(GL_BLEND);
440     if (HUDprop->isTransparent())
441         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
442     else
443         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
444
445     if (HUDprop->isAntialiased()) {
446         glEnable(GL_LINE_SMOOTH);
447         glAlphaFunc(GL_GREATER, HUDprop->alphaClamp());
448         glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
449         //glLineWidth(1.5);
450     } else {
451         //glLineWidth(1.0);
452     }
453
454     HUDprop->setColor();
455     for_each(HUD_deque.begin(), HUD_deque.end(), HUDdraw());
456
457     //HUD_TextList.add( fgText(40, 10, get_formated_gmt_time(), 0) );
458
459
460     int apY = 480 - 80;
461
462
463     if (strcmp( heading_enabled->getStringValue(), "dg-heading-hold") == 0 ) {
464         snprintf( hud_hdg_text, 256, "hdg = %.1f\n",
465                   fgGetDouble("/autopilot/settings/heading-bug-deg") );
466         HUD_TextList.add( fgText( 40, apY, hud_hdg_text ) );
467         apY -= 15;
468     } else if ( strcmp(heading_enabled->getStringValue(), "true-heading-hold") == 0 ) {
469         snprintf( hud_hdg_text, 256, "hdg = %.1f\n",
470                   fgGetDouble("/autopilot/settings/true-heading-deg") );
471         HUD_TextList.add( fgText( 40, apY, hud_hdg_text ) );
472         apY -= 15;
473
474         string wp0_id = fgGetString( "/autopilot/route-manager/wp[0]/id" );
475         if ( wp0_id.length() > 0 ) {
476             snprintf( hud_wp0_text, 256, "%5s %6.1fnm %s", wp0_id.c_str(),
477                       fgGetDouble( "/autopilot/route-manager/wp[0]/dist" ),
478                       fgGetString( "/autopilot/route-manager/wp[0]/eta" ) );
479             HUD_TextList.add( fgText( 40, apY, hud_wp0_text ) );
480             apY -= 15;
481         }
482         string wp1_id = fgGetString( "/autopilot/route-manager/wp[1]/id" );
483         if ( wp1_id.length() > 0 ) {
484             snprintf( hud_wp1_text, 256, "%5s %6.1fnm %s", wp1_id.c_str(),
485                       fgGetDouble( "/autopilot/route-manager/wp[1]/dist" ),
486                       fgGetString( "/autopilot/route-manager/wp[1]/eta" ) );
487             HUD_TextList.add( fgText( 40, apY, hud_wp1_text ) );
488             apY -= 15;
489         }
490         string wp2_id = fgGetString( "/autopilot/route-manager/wp-last/id" );
491         if ( wp2_id.length() > 0 ) {
492             snprintf( hud_wp2_text, 256, "%5s %6.1fnm %s", wp2_id.c_str(),
493                       fgGetDouble( "/autopilot/route-manager/wp-last/dist" ),
494                       fgGetString( "/autopilot/route-manager/wp-last/eta" ) );
495             HUD_TextList.add( fgText( 40, apY, hud_wp2_text ) );
496             apY -= 15;
497         }
498     }
499
500     if ( strcmp( altitude_enabled->getStringValue(), "altitude-hold" ) == 0 ) {
501         snprintf( hud_alt_text, 256, "alt = %.0f\n",
502                   fgGetDouble("/autopilot/settings/target-altitude-ft") );
503         HUD_TextList.add( fgText( 40, apY, hud_alt_text ) );
504         apY -= 15;
505     } else if ( strcmp( altitude_enabled->getStringValue(), "agl-hold" ) == 0 ){
506         snprintf( hud_alt_text, 256, "agl = %.0f\n",
507                   fgGetDouble("/autopilot/settings/target-agl-ft") );
508         HUD_TextList.add( fgText( 40, apY, hud_alt_text ) );
509         apY -= 15;
510     }
511
512     HUD_TextList.draw();
513     HUD_LineList.draw();
514
515     // glEnable(GL_LINE_STIPPLE);
516     // glLineStipple( 1, 0x00FF );
517     // HUD_StippleLineList.draw();
518     // glDisable(GL_LINE_STIPPLE);
519
520     if (HUDprop->isAntialiased()) {
521         glDisable(GL_ALPHA_TEST);
522         glDisable(GL_LINE_SMOOTH);
523         //glLineWidth(1.0);
524     }
525
526     if (HUDprop->isTransparent())
527         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
528
529     glEnable(GL_DEPTH_TEST);
530     glEnable(GL_LIGHTING);
531 }
532
533
534 void fgTextList::draw()
535 {
536     if (!Font)
537         return;
538
539     vector<fgText>::iterator curString = List.begin();
540     vector<fgText>::iterator lastString = List.end();
541
542     glPushAttrib(GL_COLOR_BUFFER_BIT);
543     glEnable(GL_BLEND);
544     if (HUDprop->isTransparent())
545         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
546     else
547         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
548
549     if (HUDprop->isAntialiased()) {
550         glEnable(GL_ALPHA_TEST);
551         glAlphaFunc(GL_GREATER, HUDprop->alphaClamp());
552     }
553
554     Font->begin();
555     for (; curString != lastString; curString++)
556         curString->Draw(Font);
557     Font->end();
558
559     glDisable(GL_TEXTURE_2D);
560     glPopAttrib();
561 }
562
563
564 // HUD property listener class
565 //
566 HUD_Properties::HUD_Properties() :
567     _current(fgGetNode("/sim/hud/current-color", true)),
568     _visibility(fgGetNode("/sim/hud/visibility", true)),
569     _antialiasing(fgGetNode("/sim/hud/color/antialiased", true)),
570     _transparency(fgGetNode("/sim/hud/color/transparent", true)),
571     _red(fgGetNode("/sim/hud/color/red", true)),
572     _green(fgGetNode("/sim/hud/color/green", true)),
573     _blue(fgGetNode("/sim/hud/color/blue", true)),
574     _alpha(fgGetNode("/sim/hud/color/alpha", true)),
575     _alpha_clamp(fgGetNode("/sim/hud/color/alpha-clamp", true)),
576     _brightness(fgGetNode("/sim/hud/color/brightness", true)),
577     _visible(false),
578     _antialiased(false),
579     _transparent(false),
580     _a(0.67),
581     _cl(0.01)
582 {
583     _visibility->addChangeListener(this);
584     _antialiasing->addChangeListener(this);
585     _transparency->addChangeListener(this);
586     _red->addChangeListener(this);
587     _green->addChangeListener(this);
588     _blue->addChangeListener(this);
589     _alpha->addChangeListener(this);
590     _alpha_clamp->addChangeListener(this);
591     _brightness->addChangeListener(this);
592     _current->addChangeListener(this, true);
593 }
594
595
596 void HUD_Properties::valueChanged(SGPropertyNode *node)
597 {
598     if (!strcmp(node->getName(), "current-color")) {
599         int i = node->getIntValue();
600         if (i < 0)
601             i = 0;
602         SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
603         if ((n = n->getChild("color", i, false))) {
604             if (n->hasValue("red"))
605                 _red->setFloatValue(n->getFloatValue("red", 1.0));
606             if (n->hasValue("green"))
607                 _green->setFloatValue(n->getFloatValue("green", 1.0));
608             if (n->hasValue("blue"))
609                 _blue->setFloatValue(n->getFloatValue("blue", 1.0));
610             if (n->hasValue("alpha"))
611                 _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
612             if (n->hasValue("alpha-clamp"))
613                 _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
614             if (n->hasValue("brightness"))
615                 _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
616             if (n->hasValue("antialiased"))
617                 _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
618             if (n->hasValue("transparent"))
619                 _transparency->setBoolValue(n->getBoolValue("transparent", false));
620         }
621     }
622     _visible = _visibility->getBoolValue();
623     _transparent = _transparency->getBoolValue();
624     _antialiased = _antialiasing->getBoolValue();
625     float brt = _brightness->getFloatValue();
626     _r = clamp(brt * _red->getFloatValue());
627     _g = clamp(brt * _green->getFloatValue());
628     _b = clamp(brt * _blue->getFloatValue());
629     _a = clamp(_alpha->getFloatValue());
630     _cl = clamp(_alpha_clamp->getFloatValue());
631 }
632
633
634 void HUD_Properties::setColor() const
635 {
636     if (_antialiased)
637         glColor4f(_r, _g, _b, _a);
638     else
639         glColor3f(_r, _g, _b);
640 }
641
642