]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD.cxx
- remove the SG_GLxxxx_H #defines, since OSG provides its own versions
[flightgear.git] / src / Instrumentation / HUD / HUD.cxx
1 // HUD.cxx -- Head Up Display
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  [micheleamerica#geocities:com]
6 // Copyright (C) 2006  Melchior FRANZ  [mfranz#aon:at]
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
22 #include <simgear/compiler.h>
23 #include <simgear/structure/exception.hxx>
24
25 #include <string>
26 #include <fstream>
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include <simgear/constants.h>
33 #include <simgear/misc/sg_path.hxx>
34 #include <osg/GLU>
35
36 #include <Main/globals.hxx>
37 #include <Main/viewmgr.hxx>
38
39 #include "HUD.hxx"
40
41
42 static float clamp(float f)
43 {
44     return f < 0.0f ? 0.0f : f > 1.0f ? 1.0f : f;
45 }
46
47
48 HUD::HUD() :
49     _current(fgGetNode("/sim/hud/current-color", true)),
50     _visibility(fgGetNode("/sim/hud/visibility[1]", true)),
51     _3DenabledN(fgGetNode("/sim/hud/enable3d[1]", true)),
52     _antialiasing(fgGetNode("/sim/hud/color/antialiased", true)),
53     _transparency(fgGetNode("/sim/hud/color/transparent", true)),
54     _red(fgGetNode("/sim/hud/color/red", true)),
55     _green(fgGetNode("/sim/hud/color/green", true)),
56     _blue(fgGetNode("/sim/hud/color/blue", true)),
57     _alpha(fgGetNode("/sim/hud/color/alpha", true)),
58     _alpha_clamp(fgGetNode("/sim/hud/color/alpha-clamp", true)),
59     _brightness(fgGetNode("/sim/hud/color/brightness", true)),
60     _visible(false),
61     _antialiased(false),
62     _transparent(false),
63     _a(0.67),                                                                   // FIXME better names
64     _cl(0.01),
65     //
66     _scr_widthN(fgGetNode("/sim/startup/xsize", true)),
67     _scr_heightN(fgGetNode("/sim/startup/ysize", true)),
68     _unitsN(fgGetNode("/sim/startup/units", true)),
69     _timer(0.0),
70     //
71     _font_renderer(new fntRenderer()),
72     _font(0),
73     _font_size(0.0),
74     _style(0)
75 {
76     SG_LOG(SG_COCKPIT, SG_INFO, "Initializing HUD Instrument");
77
78     _visibility->addChangeListener(this);
79     _3DenabledN->addChangeListener(this);
80     _antialiasing->addChangeListener(this);
81     _transparency->addChangeListener(this);
82     _red->addChangeListener(this);
83     _green->addChangeListener(this);
84     _blue->addChangeListener(this);
85     _alpha->addChangeListener(this);
86     _alpha_clamp->addChangeListener(this);
87     _brightness->addChangeListener(this);
88     _current->addChangeListener(this);
89     _scr_widthN->addChangeListener(this);
90     _scr_heightN->addChangeListener(this);
91     _unitsN->addChangeListener(this, true);
92 }
93
94
95 HUD::~HUD()
96 {
97     _visibility->removeChangeListener(this);
98     _3DenabledN->removeChangeListener(this);
99     _antialiasing->removeChangeListener(this);
100     _transparency->removeChangeListener(this);
101     _red->removeChangeListener(this);
102     _green->removeChangeListener(this);
103     _blue->removeChangeListener(this);
104     _alpha->removeChangeListener(this);
105     _alpha_clamp->removeChangeListener(this);
106     _brightness->removeChangeListener(this);
107     _current->removeChangeListener(this);
108     _scr_widthN->removeChangeListener(this);
109     _scr_heightN->removeChangeListener(this);
110     _unitsN->removeChangeListener(this);
111     delete _font_renderer;
112
113     deque<Item *>::const_iterator it, end = _items.end();
114     for (it = _items.begin(); it != end; ++it)
115         delete *it;
116 }
117
118
119 void HUD::init()
120 {
121     _font_cache = globals->get_fontcache();
122     if (!_font)
123         _font = _font_cache->getTexFont(fgGetString("/sim/hud/font/name", "Helvetica.txf"));
124     if (!_font)
125         throw sg_throwable(string("/sim/hud/font/name is not a texture font"));
126
127     _font_size = fgGetFloat("/sim/hud/font/size", 8);
128     _font_renderer->setFont(_font);
129     _font_renderer->setPointSize(_font_size);
130     _text_list.setFont(_font_renderer);
131
132     load(fgGetString("/sim/hud/path[1]", "Huds/default.xml"));
133 }
134
135
136 void HUD::update(double dt)
137 {
138     _timer += dt;
139 }
140
141
142 void HUD::draw(osg::State&)
143 {
144     if (!isVisible())
145         return;
146
147     if (!_items.size())
148         return;
149
150     if (is3D()) {
151         draw3D();
152         return;
153     }
154
155     const float normal_aspect = 640.0f / 480.0f;
156     // note: aspect_ratio is Y/X
157     float current_aspect = 1.0f / globals->get_current_view()->get_aspect_ratio();
158     if (current_aspect > normal_aspect) {
159         float aspect_adjust = current_aspect / normal_aspect;
160         float adjust = 320.0f * aspect_adjust - 320.0f;
161         draw2D(-adjust, 0.0f, 640.0f + adjust, 480.0f);
162
163     } else {
164         float aspect_adjust = normal_aspect / current_aspect;
165         float adjust = 240.0f * aspect_adjust - 240.0f;
166         draw2D(0.0f, -adjust, 640.0f, 480.0f + adjust);
167     }
168
169     glViewport(0, 0, _scr_width, _scr_height);
170 }
171
172
173 void HUD::draw3D()
174 {
175     FGViewer* view = globals->get_current_view();
176
177     // Standard fgfs projection, with essentially meaningless clip
178     // planes (we'll map the whole HUD plane to z=-1)
179     glMatrixMode(GL_PROJECTION);
180     glPushMatrix();
181     glLoadIdentity();
182     gluPerspective(view->get_v_fov(), 1.0 / view->get_aspect_ratio(), 0.1, 10);
183
184     glMatrixMode(GL_MODELVIEW);
185     glPushMatrix();
186     glLoadIdentity();
187
188     // Standard fgfs view direction computation
189     float lookat[3];
190     lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
191     lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
192     lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
193     if (fabs(lookat[1]) > 9999)
194         lookat[1] = 9999; // FPU sanity
195     gluLookAt(0, 0, 0, lookat[0], lookat[1], lookat[2], 0, 1, 0);
196
197     // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
198     // This is the default fgfs field of view, which the HUD files are
199     // written to assume.
200     float dx = 0.52056705; // tan(55/2)
201     float dy = dx * 0.75;  // assumes 4:3 aspect ratio
202     float m[16];
203     m[0] = dx, m[4] =  0, m[ 8] = 0, m[12] = 0;
204     m[1] =  0, m[5] = dy, m[ 9] = 0, m[13] = 0;
205     m[2] =  0, m[6] =  0, m[10] = 1, m[14] = 0;
206     m[3] =  0, m[7] =  0, m[11] = 0, m[15] = 1;
207     glMultMatrixf(m);
208
209     // Convert the 640x480 "HUD standard" coordinate space to a square
210     // about the origin in the range [-1:1] at depth of -1
211     glScalef(1.0 / 320, 1.0 / 240, 1);
212     glTranslatef(-320, -240, -1);
213
214     common_draw();
215
216     glMatrixMode(GL_PROJECTION);
217     glPopMatrix();
218     glMatrixMode(GL_MODELVIEW);
219     glPopMatrix();
220 }
221
222
223 void HUD::draw2D(GLfloat x_start, GLfloat y_start, GLfloat x_end, GLfloat y_end)
224 {
225     glMatrixMode(GL_PROJECTION);
226     glPushMatrix();
227     glLoadIdentity();
228     gluOrtho2D(x_start, x_end, y_start, y_end);
229
230     glMatrixMode(GL_MODELVIEW);
231     glPushMatrix();
232     glLoadIdentity();
233
234     common_draw();
235
236     glMatrixMode(GL_PROJECTION);
237     glPopMatrix();
238     glMatrixMode(GL_MODELVIEW);
239     glPopMatrix();
240 }
241
242
243 void HUD::common_draw()
244 {
245     _text_list.erase();
246     _line_list.erase();
247     _stipple_line_list.erase();
248
249     glDisable(GL_DEPTH_TEST);
250     glDisable(GL_LIGHTING);
251
252     glEnable(GL_BLEND);
253     if (isTransparent())
254         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
255     else
256         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
257
258     if (isAntialiased()) {
259         glEnable(GL_LINE_SMOOTH);
260         glAlphaFunc(GL_GREATER, alphaClamp());
261         glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
262         //glLineWidth(1.5);
263     } else {
264         //glLineWidth(1.0);
265     }
266
267     setColor();
268     deque<Item *>::const_iterator it, end = _items.end();
269     for (it = _items.begin(); it != end; ++it)
270         if ((*it)->isEnabled())
271             (*it)->draw();
272
273     _text_list.draw();
274     _line_list.draw();
275
276     if (_stipple_line_list.size()) {
277         glEnable(GL_LINE_STIPPLE);
278         glLineStipple(1, 0x00FF);
279         _stipple_line_list.draw();
280         glDisable(GL_LINE_STIPPLE);
281     }
282
283     if (isAntialiased()) {
284         glDisable(GL_ALPHA_TEST);
285         glDisable(GL_LINE_SMOOTH);
286         //glLineWidth(1.0);
287     }
288
289     if (isTransparent())
290         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
291
292     glEnable(GL_DEPTH_TEST);
293     glEnable(GL_LIGHTING);
294 }
295
296
297 int HUD::load(const char *file, float x, float y, int level, const string& indent)
298 {
299     const sgDebugPriority TREE = SG_INFO;
300     const int MAXNEST = 10;
301
302     SGPath path(globals->get_fg_root());
303     path.append(file);
304
305     if (!level) {
306         SG_LOG(SG_INPUT, TREE, endl << "load " << file);
307         _items.erase(_items.begin(), _items.end());
308     } else if (level > MAXNEST) {
309         SG_LOG(SG_INPUT, SG_ALERT, "HUD: files nested more than " << MAXNEST << " levels");
310         return 0x1;
311     } else if (!file || !file[0]) {
312         SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid filename ");
313         return 0x2;
314     }
315
316     int ret = 0;
317     ifstream input(path.c_str());
318     if (!input.good()) {
319         SG_LOG(SG_INPUT, SG_ALERT, "HUD: Cannot read configuration from " << path.str());
320         return 0x4;
321     }
322
323     SGPropertyNode root;
324     try {
325         readProperties(input, &root);
326     } catch (const sg_exception &e) {
327         input.close();
328         guiErrorMessage("HUD: Error ", e);
329         return 0x8;
330     }
331
332     for (int i = 0; i < root.nChildren(); i++) {
333         SGPropertyNode *n = root.getChild(i);
334         const char *d = n->getStringValue("name", 0);
335         string desc;
336         if (d)
337             desc = string(": \"") + d + '"';
338
339         const char *name = n->getName();
340         if (!strcmp(name, "name")) {
341             continue;
342
343         } else if (!strcmp(name, "enable3d")) {
344             // set in the tree so that valueChanged() picks it up
345             _3DenabledN->setBoolValue(n->getBoolValue());
346             continue;
347
348         } else if (!strcmp(name, "import")) {
349             const char *fn = n->getStringValue("path", "");
350             float xoffs = n->getFloatValue("x-offset", 0.0f);
351             float yoffs = n->getFloatValue("y-offset", 0.0f);
352
353             SG_LOG(SG_INPUT, TREE, indent << "|__import " << fn << desc);
354
355             string ind = indent + string(i + 1 < root.nChildren() ? "|    " : "     ");
356             ret |= load(fn, x + xoffs, y + yoffs, level + 1, ind);
357             continue;
358         }
359
360         SG_LOG(SG_INPUT, TREE, indent << "|__" << name << desc);
361
362         Item *item;
363         if (!strcmp(name, "label")) {
364             item = static_cast<Item *>(new Label(this, n, x, y));
365         } else if (!strcmp(name, "gauge")) {
366             item = static_cast<Item *>(new Gauge(this, n, x, y));
367         } else if (!strcmp(name, "tape")) {
368             item = static_cast<Item *>(new Tape(this, n, x, y));
369         } else if (!strcmp(name, "dial")) {
370             item = static_cast<Item *>(new Dial(this, n, x, y));
371         } else if (!strcmp(name, "turn-bank-indicator")) {
372             item = static_cast<Item *>(new TurnBankIndicator(this, n, x, y));
373         } else if (!strcmp(name, "ladder")) {
374             item = static_cast<Item *>(new Ladder(this, n, x, y));
375         } else if (!strcmp(name, "runway")) {
376             item = static_cast<Item *>(new Runway(this, n, x, y));
377         } else if (!strcmp(name, "aiming-reticle")) {
378             item = static_cast<Item *>(new AimingReticle(this, n, x, y));
379         } else {
380             SG_LOG(SG_INPUT, TREE, indent << "      \\...unsupported!");
381             continue;
382         }
383         _items.insert(_items.begin(), item);
384     }
385     input.close();
386     SG_LOG(SG_INPUT, TREE, indent);
387     return ret;
388 }
389
390
391 void HUD::valueChanged(SGPropertyNode *node)
392 {
393     if (!strcmp(node->getName(), "current-color")) {
394         int i = node->getIntValue();
395         if (i < 0)
396             i = 0;
397         SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
398         if ((n = n->getChild("color", i, false))) {
399             if (n->hasValue("red"))
400                 _red->setFloatValue(n->getFloatValue("red", 1.0));
401             if (n->hasValue("green"))
402                 _green->setFloatValue(n->getFloatValue("green", 1.0));
403             if (n->hasValue("blue"))
404                 _blue->setFloatValue(n->getFloatValue("blue", 1.0));
405             if (n->hasValue("alpha"))
406                 _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
407             if (n->hasValue("alpha-clamp"))
408                 _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
409             if (n->hasValue("brightness"))
410                 _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
411             if (n->hasValue("antialiased"))
412                 _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
413             if (n->hasValue("transparent"))
414                 _transparency->setBoolValue(n->getBoolValue("transparent", false));
415         }
416     }
417     _scr_width = _scr_widthN->getIntValue();
418     _scr_height = _scr_heightN->getIntValue();
419
420     _visible = _visibility->getBoolValue();
421     _3Denabled = _3DenabledN->getBoolValue();
422     _transparent = _transparency->getBoolValue();
423     _antialiased = _antialiasing->getBoolValue();
424     float brt = _brightness->getFloatValue();
425     _r = clamp(brt * _red->getFloatValue());
426     _g = clamp(brt * _green->getFloatValue());
427     _b = clamp(brt * _blue->getFloatValue());
428     _a = clamp(_alpha->getFloatValue());
429     _cl = clamp(_alpha_clamp->getFloatValue());
430
431     _units = strcmp(_unitsN->getStringValue(), "feet") ? METER : FEET;
432 }
433
434
435 void HUD::setColor() const
436 {
437     if (_antialiased)
438         glColor4f(_r, _g, _b, _a);
439     else
440         glColor3f(_r, _g, _b);
441 }
442
443
444
445
446 void HUD::textAlign(fntRenderer *rend, const char *s, int align,
447         float *x, float *y, float *l, float *r, float *t, float *b)
448 {
449     fntFont *font = rend->getFont();
450     float gap = font->getGap();
451     float left, right, bot, top;
452     font->getBBox(s, rend->getPointSize(), rend->getSlant(), &left, &right, &bot, &top);
453
454     if (align & HUD::HCENTER)
455         *x -= left - gap + (right - left - gap) / 2.0;
456     else if (align & HUD::RIGHT)
457         *x -= right;
458     else if (align & HUD::LEFT)
459         *x -= left;
460
461     if (align & HUD::VCENTER)
462         *y -= bot + (top - bot) / 2.0;
463     else if (align & HUD::TOP)
464         *y -= top;
465     else if (align & HUD::BOTTOM)
466         *y -= bot;
467
468     *l = *x + left;
469     *r = *x + right;
470     *b = *y + bot;
471     *t = *y + top;
472 }
473
474
475
476
477 // HUDText -- text container for TextList vector
478
479
480 HUDText::HUDText(fntRenderer *fnt, float x, float y, const char *s, int align, int d) :
481     _fnt(fnt),
482     _x(x),
483     _y(y),
484     _digits(d)
485 {
486     strncpy(_msg, s, BUFSIZE);
487     if (!align || !s[0])
488         return;
489     float ign;
490     HUD::textAlign(fnt, s, align, &_x, &_y, &ign, &ign, &ign, &ign);
491 }
492
493
494 void HUDText::draw()
495 {
496     if (!_digits) { // show all digits in same size
497         _fnt->start2f(_x, _y);
498         _fnt->puts(_msg);
499         return;
500     }
501
502     // FIXME
503     // this code is changed to display Numbers with big/small digits
504     // according to MIL Standards for example Altitude above 10000 ft
505     // is shown as 10ooo.
506
507     int c = 0, i = 0;
508     char *t = _msg;
509     int p = 4;
510
511     if (t[0] == '-') {
512         //if negative value then increase the c and p values
513         //for '-' sign.
514         c++; // was moved to the comment. Unintentionally?   TODO
515         p++;
516     }
517     char *tmp = _msg;
518     while (tmp[i] != '\0') {
519         if ((tmp[i] >= '0') && (tmp[i] <= '9'))
520             c++;
521         i++;
522     }
523
524     float orig_size = _fnt->getPointSize();
525     if (c > p) {
526         _fnt->setPointSize(orig_size * 0.8);
527         int p1 = c - 3;
528         char *tmp1 = _msg + p1;
529         int p2 = p1 * 8;
530
531         _fnt->start2f(_x + p2, _y);
532         _fnt->puts(tmp1);
533
534         _fnt->setPointSize(orig_size * 1.2);
535         char tmp2[BUFSIZE];
536         strncpy(tmp2, _msg, p1);
537         tmp2[p1] = '\0';
538
539         _fnt->start2f(_x, _y);
540         _fnt->puts(tmp2);
541     } else {
542         _fnt->setPointSize(orig_size * 1.2);
543         _fnt->start2f(_x, _y);
544         _fnt->puts(tmp);
545     }
546     _fnt->setPointSize(orig_size);
547 }
548
549
550 void TextList::align(const char *s, int align, float *x, float *y,
551         float *l, float *r, float *b, float *t) const
552 {
553     HUD::textAlign(_font, s, align, x, y, l, r, b, t);
554 }
555
556
557 void TextList::draw()
558 {
559     assert(_font);
560
561     // FIXME
562     glPushAttrib(GL_COLOR_BUFFER_BIT);
563     glEnable(GL_BLEND);
564
565     _font->begin();
566     vector<HUDText>::iterator it, end = _list.end();
567     for (it = _list.begin(); it != end; ++it)
568         it->draw();
569     _font->end();
570
571     glDisable(GL_TEXTURE_2D);
572     glPopAttrib();
573 }
574
575