1 // HUD.cxx -- Head Up Display
3 // Written by Michele America, started September 1997.
5 // Copyright (C) 1997 Michele F. America [micheleamerica#geocities:com]
6 // Copyright (C) 2006 Melchior FRANZ [mfranz#aon:at]
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.
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.
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.
22 #include <simgear/compiler.h>
23 #include <simgear/structure/exception.hxx>
32 #include <simgear/constants.h>
33 #include <simgear/misc/sg_path.hxx>
36 #include <Main/globals.hxx>
37 #include <Main/viewmgr.hxx>
38 #include <Main/viewer.hxx>
43 static float clamp(float f)
45 return f < 0.0f ? 0.0f : f > 1.0f ? 1.0f : f;
50 _path(fgGetNode("/sim/hud/path[1]", "Huds/default.xml")),
51 _current(fgGetNode("/sim/hud/current-color", true)),
52 _visibility(fgGetNode("/sim/hud/visibility[1]", true)),
53 _3DenabledN(fgGetNode("/sim/hud/enable3d[1]", true)),
54 _antialiasing(fgGetNode("/sim/hud/color/antialiased", true)),
55 _transparency(fgGetNode("/sim/hud/color/transparent", true)),
56 _red(fgGetNode("/sim/hud/color/red", true)),
57 _green(fgGetNode("/sim/hud/color/green", true)),
58 _blue(fgGetNode("/sim/hud/color/blue", true)),
59 _alpha(fgGetNode("/sim/hud/color/alpha", true)),
60 _alpha_clamp(fgGetNode("/sim/hud/color/alpha-clamp", true)),
61 _brightness(fgGetNode("/sim/hud/color/brightness", true)),
65 _a(0.67), // FIXME better names
68 _scr_widthN(fgGetNode("/sim/startup/xsize", true)),
69 _scr_heightN(fgGetNode("/sim/startup/ysize", true)),
70 _unitsN(fgGetNode("/sim/startup/units", true)),
73 _font_renderer(new fntRenderer()),
77 _listener_active(false),
80 SG_LOG(SG_COCKPIT, SG_INFO, "Initializing HUD Instrument");
82 _path->addChangeListener(this);
83 _visibility->addChangeListener(this);
84 _3DenabledN->addChangeListener(this);
85 _antialiasing->addChangeListener(this);
86 _transparency->addChangeListener(this);
87 _red->addChangeListener(this);
88 _green->addChangeListener(this);
89 _blue->addChangeListener(this);
90 _alpha->addChangeListener(this);
91 _alpha_clamp->addChangeListener(this);
92 _brightness->addChangeListener(this);
93 _current->addChangeListener(this);
94 _scr_widthN->addChangeListener(this);
95 _scr_heightN->addChangeListener(this);
96 _unitsN->addChangeListener(this, true);
102 _path->removeChangeListener(this);
103 _visibility->removeChangeListener(this);
104 _3DenabledN->removeChangeListener(this);
105 _antialiasing->removeChangeListener(this);
106 _transparency->removeChangeListener(this);
107 _red->removeChangeListener(this);
108 _green->removeChangeListener(this);
109 _blue->removeChangeListener(this);
110 _alpha->removeChangeListener(this);
111 _alpha_clamp->removeChangeListener(this);
112 _brightness->removeChangeListener(this);
113 _current->removeChangeListener(this);
114 _scr_widthN->removeChangeListener(this);
115 _scr_heightN->removeChangeListener(this);
116 _unitsN->removeChangeListener(this);
117 delete _font_renderer;
125 const char* fontName = 0;
126 _font_cache = globals->get_fontcache();
128 fontName = fgGetString("/sim/hud/font/name", "Helvetica.txf");
129 _font = _font_cache->getTexFont(fontName);
132 throw sg_io_exception("/sim/hud/font/name is not a texture font",
133 sg_location(fontName));
135 _font_size = fgGetFloat("/sim/hud/font/size", 8);
136 _font_renderer->setFont(_font);
137 _font_renderer->setPointSize(_font_size);
138 _text_list.setFont(_font_renderer);
140 _path->fireValueChanged();
145 deque<Item *>::const_iterator it, end = _items.end();
146 for (it = _items.begin(); it != end; ++it)
148 end = _ladders.end();
149 for (it = _ladders.begin(); it != end; ++it)
162 _path->fireValueChanged();
165 void HUD::update(double dt)
171 void HUD::draw(osg::State&)
176 if (!_items.size() && !_ladders.size())
184 const float normal_aspect = 640.0f / 480.0f;
185 // note: aspect_ratio is Y/X
186 float current_aspect = 1.0f / globals->get_current_view()->get_aspect_ratio();
187 if (current_aspect > normal_aspect) {
188 float aspect_adjust = current_aspect / normal_aspect;
189 float adjust = 320.0f * aspect_adjust - 320.0f;
190 draw2D(-adjust, 0.0f, 640.0f + adjust, 480.0f);
193 float aspect_adjust = normal_aspect / current_aspect;
194 float adjust = 240.0f * aspect_adjust - 240.0f;
195 draw2D(0.0f, -adjust, 640.0f, 480.0f + adjust);
198 glViewport(0, 0, _scr_width, _scr_height);
205 FGViewer* view = globals->get_current_view();
207 // Standard fgfs projection, with essentially meaningless clip
208 // planes (we'll map the whole HUD plane to z=-1)
209 glMatrixMode(GL_PROJECTION);
212 = Matrixf::perspective(view->get_v_fov(), 1/view->get_aspect_ratio(),
214 glLoadMatrix(proj.ptr());
216 glMatrixMode(GL_MODELVIEW);
219 // Standard fgfs view direction computation
221 lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
222 lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
223 lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
224 if (fabs(lookat[1]) > 9999)
225 lookat[1] = 9999; // FPU sanity
226 Matrixf mv = Matrixf::lookAt(Vec3f(0.0, 0.0, 0.0), lookat,
227 Vec3f(0.0, 1.0, 0.0));
228 glLoadMatrix(mv.ptr());
230 // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
231 // This is the default fgfs field of view, which the HUD files are
232 // written to assume.
233 float dx = 0.52056705; // tan(55/2)
234 float dy = dx * 0.75; // assumes 4:3 aspect ratio
236 m[0] = dx, m[4] = 0, m[ 8] = 0, m[12] = 0;
237 m[1] = 0, m[5] = dy, m[ 9] = 0, m[13] = 0;
238 m[2] = 0, m[6] = 0, m[10] = 1, m[14] = 0;
239 m[3] = 0, m[7] = 0, m[11] = 0, m[15] = 1;
242 // Convert the 640x480 "HUD standard" coordinate space to a square
243 // about the origin in the range [-1:1] at depth of -1
244 glScalef(1.0 / 320, 1.0 / 240, 1);
245 glTranslatef(-320, -240, -1);
249 glMatrixMode(GL_PROJECTION);
251 glMatrixMode(GL_MODELVIEW);
256 void HUD::draw2D(GLfloat x_start, GLfloat y_start, GLfloat x_end, GLfloat y_end)
259 glMatrixMode(GL_PROJECTION);
261 Matrixf proj = Matrixf::ortho2D(x_start, x_end, y_start, y_end);
262 glLoadMatrix(proj.ptr());
264 glMatrixMode(GL_MODELVIEW);
270 glMatrixMode(GL_PROJECTION);
272 glMatrixMode(GL_MODELVIEW);
277 void HUD::common_draw()
281 _stipple_line_list.erase();
283 glDisable(GL_DEPTH_TEST);
284 glDisable(GL_LIGHTING);
288 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
290 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
292 if (isAntialiased()) {
293 glEnable(GL_LINE_SMOOTH);
294 glAlphaFunc(GL_GREATER, alphaClamp());
295 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
304 deque<Item *>::const_iterator it, end = _items.end();
305 for (it = _items.begin(); it != end; ++it)
306 if ((*it)->isEnabled())
312 if (_stipple_line_list.size()) {
313 glEnable(GL_LINE_STIPPLE);
314 glLineStipple(1, 0x00FF);
315 _stipple_line_list.draw();
316 glDisable(GL_LINE_STIPPLE);
319 // ladders last, as they can have their own clip planes
320 end = _ladders.end();
321 for (it = _ladders.begin(); it != end; ++it)
322 if ((*it)->isEnabled())
327 if (isAntialiased()) {
328 glDisable(GL_ALPHA_TEST);
329 glDisable(GL_LINE_SMOOTH);
334 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
336 glEnable(GL_DEPTH_TEST);
337 glEnable(GL_LIGHTING);
341 int HUD::load(const char *file, float x, float y, int level, const string& indent)
343 const sgDebugPriority TREE = SG_INFO;
344 const int MAXNEST = 10;
346 SGPath path(globals->resolve_maybe_aircraft_path(file));
349 SG_LOG(SG_INPUT, TREE, endl << "load " << file);
350 _items.erase(_items.begin(), _items.end());
351 _ladders.erase(_ladders.begin(), _ladders.end());
352 } else if (level > MAXNEST) {
353 SG_LOG(SG_INPUT, SG_ALERT, "HUD: files nested more than " << MAXNEST << " levels");
355 } else if (!file || !file[0]) {
356 SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid filename ");
361 ifstream input(path.c_str());
363 SG_LOG(SG_INPUT, SG_ALERT, "HUD: Cannot read configuration from " << path.str());
369 readProperties(input, &root);
370 } catch (const sg_exception &e) {
372 guiErrorMessage("HUD: Error ", e);
377 _clip_box = new ClipBox(fgGetNode("/sim/hud/clipping"), x, y);
379 for (int i = 0; i < root.nChildren(); i++) {
380 SGPropertyNode *n = root.getChild(i);
381 const char *d = n->getStringValue("name", 0);
384 desc = string(": \"") + d + '"';
386 const char *name = n->getName();
387 if (!strcmp(name, "name")) {
390 } else if (!strcmp(name, "enable3d")) {
391 // set in the tree so that valueChanged() picks it up
392 _3DenabledN->setBoolValue(n->getBoolValue());
395 } else if (!strcmp(name, "import")) {
396 const char *fn = n->getStringValue("path", "");
397 float xoffs = n->getFloatValue("x-offset", 0.0f);
398 float yoffs = n->getFloatValue("y-offset", 0.0f);
400 SG_LOG(SG_INPUT, TREE, indent << "|__import " << fn << desc);
402 string ind = indent + string(i + 1 < root.nChildren() ? "| " : " ");
403 ret |= load(fn, x + xoffs, y + yoffs, level + 1, ind);
407 SG_LOG(SG_INPUT, TREE, indent << "|__" << name << desc);
410 if (!strcmp(name, "label")) {
411 item = static_cast<Item *>(new Label(this, n, x, y));
412 } else if (!strcmp(name, "gauge")) {
413 item = static_cast<Item *>(new Gauge(this, n, x, y));
414 } else if (!strcmp(name, "tape")) {
415 item = static_cast<Item *>(new Tape(this, n, x, y));
416 } else if (!strcmp(name, "dial")) {
417 item = static_cast<Item *>(new Dial(this, n, x, y));
418 } else if (!strcmp(name, "turn-bank-indicator")) {
419 item = static_cast<Item *>(new TurnBankIndicator(this, n, x, y));
420 } else if (!strcmp(name, "ladder")) {
421 item = static_cast<Item *>(new Ladder(this, n, x, y));
422 _ladders.insert(_ladders.begin(), item);
424 } else if (!strcmp(name, "runway")) {
425 item = static_cast<Item *>(new Runway(this, n, x, y));
426 } else if (!strcmp(name, "aiming-reticle")) {
427 item = static_cast<Item *>(new AimingReticle(this, n, x, y));
429 SG_LOG(SG_INPUT, TREE, indent << " \\...unsupported!");
432 _items.insert(_items.begin(), item);
435 SG_LOG(SG_INPUT, TREE, indent);
440 void HUD::valueChanged(SGPropertyNode *node)
442 if (_listener_active)
444 _listener_active = true;
445 if (!strcmp(node->getName(), "path"))
446 load(fgGetString("/sim/hud/path[1]", "Huds/default.xml"));
448 if (!strcmp(node->getName(), "current-color")) {
449 int i = node->getIntValue();
452 SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
453 if ((n = n->getChild("color", i, false))) {
454 if (n->hasValue("red"))
455 _red->setFloatValue(n->getFloatValue("red", 1.0));
456 if (n->hasValue("green"))
457 _green->setFloatValue(n->getFloatValue("green", 1.0));
458 if (n->hasValue("blue"))
459 _blue->setFloatValue(n->getFloatValue("blue", 1.0));
460 if (n->hasValue("alpha"))
461 _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
462 if (n->hasValue("alpha-clamp"))
463 _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
464 if (n->hasValue("brightness"))
465 _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
466 if (n->hasValue("antialiased"))
467 _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
468 if (n->hasValue("transparent"))
469 _transparency->setBoolValue(n->getBoolValue("transparent", false));
472 _scr_width = _scr_widthN->getIntValue();
473 _scr_height = _scr_heightN->getIntValue();
475 _visible = _visibility->getBoolValue();
476 _3Denabled = _3DenabledN->getBoolValue();
477 _transparent = _transparency->getBoolValue();
478 _antialiased = _antialiasing->getBoolValue();
479 float brt = _brightness->getFloatValue();
480 _r = clamp(brt * _red->getFloatValue());
481 _g = clamp(brt * _green->getFloatValue());
482 _b = clamp(brt * _blue->getFloatValue());
483 _a = clamp(_alpha->getFloatValue());
484 _cl = clamp(_alpha_clamp->getFloatValue());
486 _units = strcmp(_unitsN->getStringValue(), "feet") ? METER : FEET;
487 _listener_active = false;
491 void HUD::setColor() const
494 glColor4f(_r, _g, _b, _a);
496 glColor3f(_r, _g, _b);
500 void HUD::textAlign(fntRenderer *rend, const char *s, int align,
501 float *x, float *y, float *l, float *r, float *b, float *t)
503 fntFont *font = rend->getFont();
504 float gap = font->getGap();
505 float left, right, bot, top;
506 font->getBBox(s, rend->getPointSize(), rend->getSlant(), &left, &right, &bot, &top);
508 if (align & HUD::HCENTER)
509 *x -= left - gap + (right - left - gap) / 2.0;
510 else if (align & HUD::RIGHT)
512 else if (align & HUD::LEFT)
515 if (align & HUD::VCENTER)
516 *y -= bot + (top - bot) / 2.0;
517 else if (align & HUD::TOP)
519 else if (align & HUD::BOTTOM)
531 // HUDText -- text container for TextList vector
534 HUDText::HUDText(fntRenderer *fnt, float x, float y, const char *s, int align, int d) :
540 strncpy(_msg, s, BUFSIZE);
544 HUD::textAlign(fnt, s, align, &_x, &_y, &ign, &ign, &ign, &ign);
550 if (!_digits) { // show all digits in same size
551 _fnt->start2f(_x, _y);
557 // this code is changed to display Numbers with big/small digits
558 // according to MIL Standards for example Altitude above 10000 ft
559 // is shown as 10ooo.
566 //if negative value then increase the c and p values
568 c++; // was moved to the comment. Unintentionally? TODO
572 while (tmp[i] != '\0') {
573 if ((tmp[i] >= '0') && (tmp[i] <= '9'))
578 float orig_size = _fnt->getPointSize();
580 _fnt->setPointSize(orig_size * 0.8);
582 char *tmp1 = _msg + p1;
585 _fnt->start2f(_x + p2, _y);
588 _fnt->setPointSize(orig_size * 1.2);
590 strncpy(tmp2, _msg, p1);
593 _fnt->start2f(_x, _y);
596 _fnt->setPointSize(orig_size * 1.2);
597 _fnt->start2f(_x, _y);
600 _fnt->setPointSize(orig_size);
604 void TextList::align(const char *s, int align, float *x, float *y,
605 float *l, float *r, float *b, float *t) const
607 HUD::textAlign(_font, s, align, x, y, l, r, b, t);
611 void TextList::draw()
616 glPushAttrib(GL_COLOR_BUFFER_BIT);
620 vector<HUDText>::iterator it, end = _list.end();
621 for (it = _list.begin(); it != end; ++it)
625 glDisable(GL_TEXTURE_2D);
630 ClipBox::ClipBox(const SGPropertyNode *n, float xoffset, float yoffset) :
638 // const_cast is necessary because ATM there's no matching getChild(const ...)
639 // prototype and getNode(const ..., <bool>) is wrongly interpreted as
640 // getNode(const ..., <int>)
641 _top_node = (const_cast<SGPropertyNode *>(n))->getChild("top", 0, true);
642 _bot_node = (const_cast<SGPropertyNode *>(n))->getChild("bottom", 0, true);
643 _left_node = (const_cast<SGPropertyNode *>(n))->getChild("left", 0, true);
644 _right_node = (const_cast<SGPropertyNode *>(n))->getChild("right", 0, true);
646 _left[0] = 1.0, _left[1] = _left[2] = 0.0;
647 _right[0] = -1.0, _right[1] = _right[2] = 0.0;
648 _top[0] = 0.0, _top[1] = -1.0, _top[2] = 0.0;
649 _bot[0] = 0.0, _bot[1] = 1.0, _bot[2] = 0.0;
659 _left[3] = -_left_node->getDoubleValue() - _xoffs;
660 _right[3] = _right_node->getDoubleValue() + _xoffs;
661 _bot[3] = -_bot_node->getDoubleValue() - _yoffs;
662 _top[3] = _top_node->getDoubleValue() + _yoffs;
664 glClipPlane(GL_CLIP_PLANE0, _top);
665 glEnable(GL_CLIP_PLANE0);
666 glClipPlane(GL_CLIP_PLANE1, _bot);
667 glEnable(GL_CLIP_PLANE1);
668 glClipPlane(GL_CLIP_PLANE2, _left);
669 glEnable(GL_CLIP_PLANE2);
670 glClipPlane(GL_CLIP_PLANE3, _right);
671 glEnable(GL_CLIP_PLANE3);
675 void ClipBox::unset()
678 glDisable(GL_CLIP_PLANE0);
679 glDisable(GL_CLIP_PLANE1);
680 glDisable(GL_CLIP_PLANE2);
681 glDisable(GL_CLIP_PLANE3);