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>
34 #include <simgear/props/props_io.hxx>
39 #include <Main/globals.hxx>
40 #include <Main/fg_props.hxx>
41 #include <Viewer/viewmgr.hxx>
42 #include <Viewer/viewer.hxx>
43 #include <GUI/FGFontCache.hxx>
44 #include <GUI/gui.h> // for guiErrorMessage
47 #include "HUD_private.hxx"
55 static float clamp(float f)
57 return f < 0.0f ? 0.0f : f > 1.0f ? 1.0f : f;
60 HUD::Input::Input(const SGPropertyNode *n, float factor, float offset,
61 float min, float max) :
64 _damped(SGLimitsf::max())
68 _factor = n->getFloatValue("factor", factor);
69 _offset = n->getFloatValue("offset", offset);
70 _min = n->getFloatValue("min", min);
71 _max = n->getFloatValue("max", max);
72 _coeff = 1.0 - 1.0 / powf(10, fabs(n->getFloatValue("damp", 0.0)));
73 SGPropertyNode *p = ((SGPropertyNode *)n)->getNode("property", false);
75 const char *path = p->getStringValue();
76 if (path && path[0]) {
77 _property = fgGetNode(path, true);
84 _currentPath(fgGetNode("/sim/hud/current-path", true)),
85 _currentColor(fgGetNode("/sim/hud/current-color", true)),
86 _visibility(fgGetNode("/sim/hud/visibility[1]", true)),
87 _3DenabledN(fgGetNode("/sim/hud/enable3d[1]", true)),
88 _antialiasing(fgGetNode("/sim/hud/color/antialiased", true)),
89 _transparency(fgGetNode("/sim/hud/color/transparent", true)),
90 _red(fgGetNode("/sim/hud/color/red", true)),
91 _green(fgGetNode("/sim/hud/color/green", true)),
92 _blue(fgGetNode("/sim/hud/color/blue", true)),
93 _alpha(fgGetNode("/sim/hud/color/alpha", true)),
94 _alpha_clamp(fgGetNode("/sim/hud/color/alpha-clamp", true)),
95 _brightness(fgGetNode("/sim/hud/color/brightness", true)),
100 _a(0.67), // FIXME better names
103 _scr_widthN(fgGetNode("/sim/startup/xsize", true)),
104 _scr_heightN(fgGetNode("/sim/startup/ysize", true)),
105 _unitsN(fgGetNode("/sim/startup/units", true)),
108 _font_renderer(new fntRenderer()),
112 _listener_active(false),
115 SG_LOG(SG_COCKPIT, SG_INFO, "Initializing HUD Instrument");
117 SGPropertyNode* hud = fgGetNode("/sim/hud");
118 hud->addChangeListener(this);
124 SGPropertyNode* hud = fgGetNode("/sim/hud");
125 hud->removeChangeListener(this);
133 std::string fontName;
135 fontName = fgGetString("/sim/hud/font/name", "Helvetica.txf");
136 _font = FGFontCache::instance()->getTexFont(fontName);
139 throw sg_io_exception("/sim/hud/font/name is not a texture font",
140 sg_location(fontName));
142 _font_size = fgGetFloat("/sim/hud/font/size", 8);
143 _font_renderer->setFont(_font);
144 _font_renderer->setPointSize(_font_size);
145 _text_list.setFont(_font_renderer);
148 currentColorChanged();
149 _currentPath->fireValueChanged();
154 deque<Item *>::const_iterator it, end = _items.end();
155 for (it = _items.begin(); it != end; ++it)
157 end = _ladders.end();
158 for (it = _ladders.begin(); it != end; ++it)
173 _currentPath->fireValueChanged();
176 void HUD::update(double dt)
182 void HUD::draw(osg::State&)
187 if (_items.empty() && _ladders.empty())
195 const float normal_aspect = 640.0f / 480.0f;
196 // note: aspect_ratio is Y/X
197 float current_aspect = 1.0f / globals->get_current_view()->get_aspect_ratio();
198 if (current_aspect > normal_aspect) {
199 float aspect_adjust = current_aspect / normal_aspect;
200 float adjust = 320.0f * aspect_adjust - 320.0f;
201 draw2D(-adjust, 0.0f, 640.0f + adjust, 480.0f);
204 float aspect_adjust = normal_aspect / current_aspect;
205 float adjust = 240.0f * aspect_adjust - 240.0f;
206 draw2D(0.0f, -adjust, 640.0f, 480.0f + adjust);
209 glViewport(0, 0, _scr_width, _scr_height);
216 FGViewer* view = globals->get_current_view();
218 // Standard fgfs projection, with essentially meaningless clip
219 // planes (we'll map the whole HUD plane to z=-1)
220 glMatrixMode(GL_PROJECTION);
223 = Matrixf::perspective(view->get_v_fov(), 1/view->get_aspect_ratio(),
225 glLoadMatrix(proj.ptr());
227 glMatrixMode(GL_MODELVIEW);
230 // Standard fgfs view direction computation
232 lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
233 lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
234 lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
235 if (fabs(lookat[1]) > 9999)
236 lookat[1] = 9999; // FPU sanity
237 Matrixf mv = Matrixf::lookAt(Vec3f(0.0, 0.0, 0.0), lookat,
238 Vec3f(0.0, 1.0, 0.0));
239 glLoadMatrix(mv.ptr());
241 // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
242 // This is the default fgfs field of view, which the HUD files are
243 // written to assume.
244 float dx = 0.52056705; // tan(55/2)
245 float dy = dx * 0.75; // assumes 4:3 aspect ratio
247 m[0] = dx, m[4] = 0, m[ 8] = 0, m[12] = 0;
248 m[1] = 0, m[5] = dy, m[ 9] = 0, m[13] = 0;
249 m[2] = 0, m[6] = 0, m[10] = 1, m[14] = 0;
250 m[3] = 0, m[7] = 0, m[11] = 0, m[15] = 1;
253 // Convert the 640x480 "HUD standard" coordinate space to a square
254 // about the origin in the range [-1:1] at depth of -1
255 glScalef(1.0 / 320, 1.0 / 240, 1);
256 glTranslatef(-320, -240, -1);
260 glMatrixMode(GL_PROJECTION);
262 glMatrixMode(GL_MODELVIEW);
267 void HUD::draw2D(GLfloat x_start, GLfloat y_start, GLfloat x_end, GLfloat y_end)
270 glMatrixMode(GL_PROJECTION);
272 Matrixf proj = Matrixf::ortho2D(x_start, x_end, y_start, y_end);
273 glLoadMatrix(proj.ptr());
275 glMatrixMode(GL_MODELVIEW);
281 glMatrixMode(GL_PROJECTION);
283 glMatrixMode(GL_MODELVIEW);
288 void HUD::common_draw()
292 _stipple_line_list.erase();
294 glDisable(GL_DEPTH_TEST);
295 glDisable(GL_LIGHTING);
299 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
301 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
303 if (isAntialiased()) {
304 glEnable(GL_LINE_SMOOTH);
305 glAlphaFunc(GL_GREATER, alphaClamp());
306 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
315 deque<Item *>::const_iterator it, end = _items.end();
316 for (it = _items.begin(); it != end; ++it)
317 if ((*it)->isEnabled())
323 if (! _stipple_line_list.empty()) {
324 glEnable(GL_LINE_STIPPLE);
325 glLineStipple(1, 0x00FF);
326 _stipple_line_list.draw();
327 glDisable(GL_LINE_STIPPLE);
330 // ladders last, as they can have their own clip planes
331 end = _ladders.end();
332 for (it = _ladders.begin(); it != end; ++it)
333 if ((*it)->isEnabled())
338 if (isAntialiased()) {
339 glDisable(GL_ALPHA_TEST);
340 glDisable(GL_LINE_SMOOTH);
345 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
347 glEnable(GL_DEPTH_TEST);
348 glEnable(GL_LIGHTING);
352 int HUD::load(const char *file, float x, float y, int level, const string& indent)
354 const sgDebugPriority TREE = SG_INFO;
355 const int MAXNEST = 10;
357 SGPath path(globals->resolve_maybe_aircraft_path(file));
360 SG_LOG(SG_INPUT, SG_ALERT, "HUD: Cannot find configuration file '" << file << "'.");
365 SG_LOG(SG_INPUT, TREE, endl << "load " << file);
366 _items.erase(_items.begin(), _items.end());
367 _ladders.erase(_ladders.begin(), _ladders.end());
368 } else if (level > MAXNEST) {
369 SG_LOG(SG_INPUT, SG_ALERT, "HUD: files nested more than " << MAXNEST << " levels");
371 } else if (!file || !file[0]) {
372 SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid filename ");
377 ifstream input(path.c_str());
379 SG_LOG(SG_INPUT, SG_ALERT, "HUD: Cannot read configuration from '" << path.c_str() << "'");
385 readProperties(input, &root);
386 } catch (const sg_exception &e) {
388 guiErrorMessage("HUD: Error ", e);
393 _clip_box = new ClipBox(fgGetNode("/sim/hud/clipping"), x, y);
395 for (int i = 0; i < root.nChildren(); i++) {
396 SGPropertyNode *n = root.getChild(i);
397 const char *d = n->getStringValue("name", 0);
400 desc = string(": \"") + d + '"';
402 const char *name = n->getName();
403 if (!strcmp(name, "name")) {
406 } else if (!strcmp(name, "enable3d")) {
407 // set in the tree so that valueChanged() picks it up
408 _3DenabledN->setBoolValue(n->getBoolValue());
411 } else if (!strcmp(name, "import")) {
412 const char *fn = n->getStringValue("path", "");
413 float xoffs = n->getFloatValue("x-offset", 0.0f);
414 float yoffs = n->getFloatValue("y-offset", 0.0f);
416 SG_LOG(SG_INPUT, TREE, indent << "|__import " << fn << desc);
418 string ind = indent + string(i + 1 < root.nChildren() ? "| " : " ");
419 ret |= load(fn, x + xoffs, y + yoffs, level + 1, ind);
423 SG_LOG(SG_INPUT, TREE, indent << "|__" << name << desc);
426 if (!strcmp(name, "label")) {
427 item = static_cast<Item *>(new Label(this, n, x, y));
428 } else if (!strcmp(name, "gauge")) {
429 item = static_cast<Item *>(new Gauge(this, n, x, y));
430 } else if (!strcmp(name, "tape")) {
431 item = static_cast<Item *>(new Tape(this, n, x, y));
432 } else if (!strcmp(name, "dial")) {
433 item = static_cast<Item *>(new Dial(this, n, x, y));
434 } else if (!strcmp(name, "turn-bank-indicator")) {
435 item = static_cast<Item *>(new TurnBankIndicator(this, n, x, y));
436 } else if (!strcmp(name, "ladder")) {
437 item = static_cast<Item *>(new Ladder(this, n, x, y));
438 _ladders.insert(_ladders.begin(), item);
440 } else if (!strcmp(name, "runway")) {
441 item = static_cast<Item *>(new Runway(this, n, x, y));
442 } else if (!strcmp(name, "aiming-reticle")) {
443 item = static_cast<Item *>(new AimingReticle(this, n, x, y));
445 SG_LOG(SG_INPUT, TREE, indent << " \\...unsupported!");
448 _items.insert(_items.begin(), item);
451 SG_LOG(SG_INPUT, TREE, indent);
456 void HUD::valueChanged(SGPropertyNode *node)
458 if (_listener_active)
460 _listener_active = true;
462 bool loadNow = false;
463 _visible = _visibility->getBoolValue();
464 if (_visible && !_loaded) {
468 if (!strcmp(node->getName(), "current-path") && _visible) {
473 int pathIndex = _currentPath->getIntValue();
474 SGPropertyNode* pathNode = fgGetNode("/sim/hud/path", pathIndex);
475 std::string path("Huds/default.xml");
476 if (pathNode && pathNode->hasValue()) {
477 path = pathNode->getStringValue();
478 SG_LOG(SG_INSTR, SG_INFO, "will load Hud from " << path);
485 if (!strcmp(node->getName(), "current-color")) {
486 currentColorChanged();
489 _scr_width = _scr_widthN->getIntValue();
490 _scr_height = _scr_heightN->getIntValue();
493 _3Denabled = _3DenabledN->getBoolValue();
494 _transparent = _transparency->getBoolValue();
495 _antialiased = _antialiasing->getBoolValue();
496 float brt = _brightness->getFloatValue();
497 _r = clamp(brt * _red->getFloatValue());
498 _g = clamp(brt * _green->getFloatValue());
499 _b = clamp(brt * _blue->getFloatValue());
500 _a = clamp(_alpha->getFloatValue());
501 _cl = clamp(_alpha_clamp->getFloatValue());
503 _units = strcmp(_unitsN->getStringValue(), "feet") ? METER : FEET;
504 _listener_active = false;
507 void HUD::currentColorChanged()
509 SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
510 int index = _currentColor->getIntValue();
515 n = n->getChild("color", index, false);
520 if (n->hasValue("red"))
521 _red->setFloatValue(n->getFloatValue("red", 1.0));
522 if (n->hasValue("green"))
523 _green->setFloatValue(n->getFloatValue("green", 1.0));
524 if (n->hasValue("blue"))
525 _blue->setFloatValue(n->getFloatValue("blue", 1.0));
526 if (n->hasValue("alpha"))
527 _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
528 if (n->hasValue("alpha-clamp"))
529 _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
530 if (n->hasValue("brightness"))
531 _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
532 if (n->hasValue("antialiased"))
533 _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
534 if (n->hasValue("transparent"))
535 _transparency->setBoolValue(n->getBoolValue("transparent", false));
538 void HUD::setColor() const
541 glColor4f(_r, _g, _b, _a);
543 glColor3f(_r, _g, _b);
547 void HUD::textAlign(fntRenderer *rend, const char *s, int align,
548 float *x, float *y, float *l, float *r, float *b, float *t)
550 fntFont *font = rend->getFont();
551 float gap = font->getGap();
552 float left, right, bot, top;
553 font->getBBox(s, rend->getPointSize(), rend->getSlant(), &left, &right, &bot, &top);
555 if (align & HUD::HCENTER)
556 *x -= left - gap + (right - left - gap) / 2.0;
557 else if (align & HUD::RIGHT)
559 else if (align & HUD::LEFT)
562 if (align & HUD::VCENTER)
563 *y -= bot + (top - bot) / 2.0;
564 else if (align & HUD::TOP)
566 else if (align & HUD::BOTTOM)
578 // HUDText -- text container for TextList vector
581 HUDText::HUDText(fntRenderer *fnt, float x, float y, const char *s, int align, int d) :
587 strncpy(_msg, s, BUFSIZE);
591 HUD::textAlign(fnt, s, align, &_x, &_y, &ign, &ign, &ign, &ign);
597 if (!_digits) { // show all digits in same size
598 _fnt->start2f(_x, _y);
604 // this code is changed to display Numbers with big/small digits
605 // according to MIL Standards for example Altitude above 10000 ft
606 // is shown as 10ooo.
613 //if negative value then increase the c and p values
615 c++; // was moved to the comment. Unintentionally? TODO
619 while (tmp[i] != '\0') {
620 if ((tmp[i] >= '0') && (tmp[i] <= '9'))
625 float orig_size = _fnt->getPointSize();
627 _fnt->setPointSize(orig_size * 0.8);
629 char *tmp1 = _msg + p1;
632 _fnt->start2f(_x + p2, _y);
635 _fnt->setPointSize(orig_size * 1.2);
637 strncpy(tmp2, _msg, p1);
640 _fnt->start2f(_x, _y);
643 _fnt->setPointSize(orig_size * 1.2);
644 _fnt->start2f(_x, _y);
647 _fnt->setPointSize(orig_size);
651 void TextList::align(const char *s, int align, float *x, float *y,
652 float *l, float *r, float *b, float *t) const
654 HUD::textAlign(_font, s, align, x, y, l, r, b, t);
658 void TextList::draw()
663 glPushAttrib(GL_COLOR_BUFFER_BIT);
667 vector<HUDText>::iterator it, end = _list.end();
668 for (it = _list.begin(); it != end; ++it)
672 glDisable(GL_TEXTURE_2D);
677 ClipBox::ClipBox(const SGPropertyNode *n, float xoffset, float yoffset) :
685 // const_cast is necessary because ATM there's no matching getChild(const ...)
686 // prototype and getNode(const ..., <bool>) is wrongly interpreted as
687 // getNode(const ..., <int>)
688 _top_node = (const_cast<SGPropertyNode *>(n))->getChild("top", 0, true);
689 _bot_node = (const_cast<SGPropertyNode *>(n))->getChild("bottom", 0, true);
690 _left_node = (const_cast<SGPropertyNode *>(n))->getChild("left", 0, true);
691 _right_node = (const_cast<SGPropertyNode *>(n))->getChild("right", 0, true);
693 _left[0] = 1.0, _left[1] = _left[2] = 0.0;
694 _right[0] = -1.0, _right[1] = _right[2] = 0.0;
695 _top[0] = 0.0, _top[1] = -1.0, _top[2] = 0.0;
696 _bot[0] = 0.0, _bot[1] = 1.0, _bot[2] = 0.0;
706 _left[3] = -_left_node->getDoubleValue() - _xoffs;
707 _right[3] = _right_node->getDoubleValue() + _xoffs;
708 _bot[3] = -_bot_node->getDoubleValue() - _yoffs;
709 _top[3] = _top_node->getDoubleValue() + _yoffs;
711 glClipPlane(GL_CLIP_PLANE0, _top);
712 glEnable(GL_CLIP_PLANE0);
713 glClipPlane(GL_CLIP_PLANE1, _bot);
714 glEnable(GL_CLIP_PLANE1);
715 glClipPlane(GL_CLIP_PLANE2, _left);
716 glEnable(GL_CLIP_PLANE2);
717 glClipPlane(GL_CLIP_PLANE3, _right);
718 glEnable(GL_CLIP_PLANE3);
722 void ClipBox::unset()
725 glDisable(GL_CLIP_PLANE0);
726 glDisable(GL_CLIP_PLANE1);
727 glDisable(GL_CLIP_PLANE2);
728 glDisable(GL_CLIP_PLANE3);