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