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