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