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 const char* fontName = 0;
134 _font_cache = globals->get_fontcache();
136 fontName = fgGetString("/sim/hud/font/name", "Helvetica.txf");
137 _font = _font_cache->getTexFont(fontName);
140 throw sg_io_exception("/sim/hud/font/name is not a texture font",
141 sg_location(fontName));
143 _font_size = fgGetFloat("/sim/hud/font/size", 8);
144 _font_renderer->setFont(_font);
145 _font_renderer->setPointSize(_font_size);
146 _text_list.setFont(_font_renderer);
149 currentColorChanged();
150 _currentPath->fireValueChanged();
155 deque<Item *>::const_iterator it, end = _items.end();
156 for (it = _items.begin(); it != end; ++it)
158 end = _ladders.end();
159 for (it = _ladders.begin(); it != end; ++it)
174 _currentPath->fireValueChanged();
177 void HUD::update(double dt)
183 void HUD::draw(osg::State&)
188 if (_items.empty() && _ladders.empty())
196 const float normal_aspect = 640.0f / 480.0f;
197 // note: aspect_ratio is Y/X
198 float current_aspect = 1.0f / globals->get_current_view()->get_aspect_ratio();
199 if (current_aspect > normal_aspect) {
200 float aspect_adjust = current_aspect / normal_aspect;
201 float adjust = 320.0f * aspect_adjust - 320.0f;
202 draw2D(-adjust, 0.0f, 640.0f + adjust, 480.0f);
205 float aspect_adjust = normal_aspect / current_aspect;
206 float adjust = 240.0f * aspect_adjust - 240.0f;
207 draw2D(0.0f, -adjust, 640.0f, 480.0f + adjust);
210 glViewport(0, 0, _scr_width, _scr_height);
217 FGViewer* view = globals->get_current_view();
219 // Standard fgfs projection, with essentially meaningless clip
220 // planes (we'll map the whole HUD plane to z=-1)
221 glMatrixMode(GL_PROJECTION);
224 = Matrixf::perspective(view->get_v_fov(), 1/view->get_aspect_ratio(),
226 glLoadMatrix(proj.ptr());
228 glMatrixMode(GL_MODELVIEW);
231 // Standard fgfs view direction computation
233 lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
234 lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
235 lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
236 if (fabs(lookat[1]) > 9999)
237 lookat[1] = 9999; // FPU sanity
238 Matrixf mv = Matrixf::lookAt(Vec3f(0.0, 0.0, 0.0), lookat,
239 Vec3f(0.0, 1.0, 0.0));
240 glLoadMatrix(mv.ptr());
242 // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
243 // This is the default fgfs field of view, which the HUD files are
244 // written to assume.
245 float dx = 0.52056705; // tan(55/2)
246 float dy = dx * 0.75; // assumes 4:3 aspect ratio
248 m[0] = dx, m[4] = 0, m[ 8] = 0, m[12] = 0;
249 m[1] = 0, m[5] = dy, m[ 9] = 0, m[13] = 0;
250 m[2] = 0, m[6] = 0, m[10] = 1, m[14] = 0;
251 m[3] = 0, m[7] = 0, m[11] = 0, m[15] = 1;
254 // Convert the 640x480 "HUD standard" coordinate space to a square
255 // about the origin in the range [-1:1] at depth of -1
256 glScalef(1.0 / 320, 1.0 / 240, 1);
257 glTranslatef(-320, -240, -1);
261 glMatrixMode(GL_PROJECTION);
263 glMatrixMode(GL_MODELVIEW);
268 void HUD::draw2D(GLfloat x_start, GLfloat y_start, GLfloat x_end, GLfloat y_end)
271 glMatrixMode(GL_PROJECTION);
273 Matrixf proj = Matrixf::ortho2D(x_start, x_end, y_start, y_end);
274 glLoadMatrix(proj.ptr());
276 glMatrixMode(GL_MODELVIEW);
282 glMatrixMode(GL_PROJECTION);
284 glMatrixMode(GL_MODELVIEW);
289 void HUD::common_draw()
293 _stipple_line_list.erase();
295 glDisable(GL_DEPTH_TEST);
296 glDisable(GL_LIGHTING);
300 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
302 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
304 if (isAntialiased()) {
305 glEnable(GL_LINE_SMOOTH);
306 glAlphaFunc(GL_GREATER, alphaClamp());
307 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
316 deque<Item *>::const_iterator it, end = _items.end();
317 for (it = _items.begin(); it != end; ++it)
318 if ((*it)->isEnabled())
324 if (! _stipple_line_list.empty()) {
325 glEnable(GL_LINE_STIPPLE);
326 glLineStipple(1, 0x00FF);
327 _stipple_line_list.draw();
328 glDisable(GL_LINE_STIPPLE);
331 // ladders last, as they can have their own clip planes
332 end = _ladders.end();
333 for (it = _ladders.begin(); it != end; ++it)
334 if ((*it)->isEnabled())
339 if (isAntialiased()) {
340 glDisable(GL_ALPHA_TEST);
341 glDisable(GL_LINE_SMOOTH);
346 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
348 glEnable(GL_DEPTH_TEST);
349 glEnable(GL_LIGHTING);
353 int HUD::load(const char *file, float x, float y, int level, const string& indent)
355 const sgDebugPriority TREE = SG_INFO;
356 const int MAXNEST = 10;
358 SGPath path(globals->resolve_maybe_aircraft_path(file));
361 SG_LOG(SG_INPUT, SG_ALERT, "HUD: Cannot find configuration file '" << file << "'.");
366 SG_LOG(SG_INPUT, TREE, endl << "load " << file);
367 _items.erase(_items.begin(), _items.end());
368 _ladders.erase(_ladders.begin(), _ladders.end());
369 } else if (level > MAXNEST) {
370 SG_LOG(SG_INPUT, SG_ALERT, "HUD: files nested more than " << MAXNEST << " levels");
372 } else if (!file || !file[0]) {
373 SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid filename ");
378 ifstream input(path.c_str());
380 SG_LOG(SG_INPUT, SG_ALERT, "HUD: Cannot read configuration from '" << path.c_str() << "'");
386 readProperties(input, &root);
387 } catch (const sg_exception &e) {
389 guiErrorMessage("HUD: Error ", e);
394 _clip_box = new ClipBox(fgGetNode("/sim/hud/clipping"), x, y);
396 for (int i = 0; i < root.nChildren(); i++) {
397 SGPropertyNode *n = root.getChild(i);
398 const char *d = n->getStringValue("name", 0);
401 desc = string(": \"") + d + '"';
403 const char *name = n->getName();
404 if (!strcmp(name, "name")) {
407 } else if (!strcmp(name, "enable3d")) {
408 // set in the tree so that valueChanged() picks it up
409 _3DenabledN->setBoolValue(n->getBoolValue());
412 } else if (!strcmp(name, "import")) {
413 const char *fn = n->getStringValue("path", "");
414 float xoffs = n->getFloatValue("x-offset", 0.0f);
415 float yoffs = n->getFloatValue("y-offset", 0.0f);
417 SG_LOG(SG_INPUT, TREE, indent << "|__import " << fn << desc);
419 string ind = indent + string(i + 1 < root.nChildren() ? "| " : " ");
420 ret |= load(fn, x + xoffs, y + yoffs, level + 1, ind);
424 SG_LOG(SG_INPUT, TREE, indent << "|__" << name << desc);
427 if (!strcmp(name, "label")) {
428 item = static_cast<Item *>(new Label(this, n, x, y));
429 } else if (!strcmp(name, "gauge")) {
430 item = static_cast<Item *>(new Gauge(this, n, x, y));
431 } else if (!strcmp(name, "tape")) {
432 item = static_cast<Item *>(new Tape(this, n, x, y));
433 } else if (!strcmp(name, "dial")) {
434 item = static_cast<Item *>(new Dial(this, n, x, y));
435 } else if (!strcmp(name, "turn-bank-indicator")) {
436 item = static_cast<Item *>(new TurnBankIndicator(this, n, x, y));
437 } else if (!strcmp(name, "ladder")) {
438 item = static_cast<Item *>(new Ladder(this, n, x, y));
439 _ladders.insert(_ladders.begin(), item);
441 } else if (!strcmp(name, "runway")) {
442 item = static_cast<Item *>(new Runway(this, n, x, y));
443 } else if (!strcmp(name, "aiming-reticle")) {
444 item = static_cast<Item *>(new AimingReticle(this, n, x, y));
446 SG_LOG(SG_INPUT, TREE, indent << " \\...unsupported!");
449 _items.insert(_items.begin(), item);
452 SG_LOG(SG_INPUT, TREE, indent);
457 void HUD::valueChanged(SGPropertyNode *node)
459 if (_listener_active)
461 _listener_active = true;
463 bool loadNow = false;
464 _visible = _visibility->getBoolValue();
465 if (_visible && !_loaded) {
469 if (!strcmp(node->getName(), "current-path") && _visible) {
474 int pathIndex = _currentPath->getIntValue();
475 SGPropertyNode* pathNode = fgGetNode("/sim/hud/path", pathIndex);
476 std::string path("Huds/default.xml");
477 if (pathNode && pathNode->hasValue()) {
478 path = pathNode->getStringValue();
479 SG_LOG(SG_INSTR, SG_INFO, "will load Hud from " << path);
486 if (!strcmp(node->getName(), "current-color")) {
487 currentColorChanged();
490 _scr_width = _scr_widthN->getIntValue();
491 _scr_height = _scr_heightN->getIntValue();
494 _3Denabled = _3DenabledN->getBoolValue();
495 _transparent = _transparency->getBoolValue();
496 _antialiased = _antialiasing->getBoolValue();
497 float brt = _brightness->getFloatValue();
498 _r = clamp(brt * _red->getFloatValue());
499 _g = clamp(brt * _green->getFloatValue());
500 _b = clamp(brt * _blue->getFloatValue());
501 _a = clamp(_alpha->getFloatValue());
502 _cl = clamp(_alpha_clamp->getFloatValue());
504 _units = strcmp(_unitsN->getStringValue(), "feet") ? METER : FEET;
505 _listener_active = false;
508 void HUD::currentColorChanged()
510 SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
511 int index = _currentColor->getIntValue();
516 n = n->getChild("color", index, false);
521 if (n->hasValue("red"))
522 _red->setFloatValue(n->getFloatValue("red", 1.0));
523 if (n->hasValue("green"))
524 _green->setFloatValue(n->getFloatValue("green", 1.0));
525 if (n->hasValue("blue"))
526 _blue->setFloatValue(n->getFloatValue("blue", 1.0));
527 if (n->hasValue("alpha"))
528 _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
529 if (n->hasValue("alpha-clamp"))
530 _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
531 if (n->hasValue("brightness"))
532 _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
533 if (n->hasValue("antialiased"))
534 _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
535 if (n->hasValue("transparent"))
536 _transparency->setBoolValue(n->getBoolValue("transparent", false));
539 void HUD::setColor() const
542 glColor4f(_r, _g, _b, _a);
544 glColor3f(_r, _g, _b);
548 void HUD::textAlign(fntRenderer *rend, const char *s, int align,
549 float *x, float *y, float *l, float *r, float *b, float *t)
551 fntFont *font = rend->getFont();
552 float gap = font->getGap();
553 float left, right, bot, top;
554 font->getBBox(s, rend->getPointSize(), rend->getSlant(), &left, &right, &bot, &top);
556 if (align & HUD::HCENTER)
557 *x -= left - gap + (right - left - gap) / 2.0;
558 else if (align & HUD::RIGHT)
560 else if (align & HUD::LEFT)
563 if (align & HUD::VCENTER)
564 *y -= bot + (top - bot) / 2.0;
565 else if (align & HUD::TOP)
567 else if (align & HUD::BOTTOM)
579 // HUDText -- text container for TextList vector
582 HUDText::HUDText(fntRenderer *fnt, float x, float y, const char *s, int align, int d) :
588 strncpy(_msg, s, BUFSIZE);
592 HUD::textAlign(fnt, s, align, &_x, &_y, &ign, &ign, &ign, &ign);
598 if (!_digits) { // show all digits in same size
599 _fnt->start2f(_x, _y);
605 // this code is changed to display Numbers with big/small digits
606 // according to MIL Standards for example Altitude above 10000 ft
607 // is shown as 10ooo.
614 //if negative value then increase the c and p values
616 c++; // was moved to the comment. Unintentionally? TODO
620 while (tmp[i] != '\0') {
621 if ((tmp[i] >= '0') && (tmp[i] <= '9'))
626 float orig_size = _fnt->getPointSize();
628 _fnt->setPointSize(orig_size * 0.8);
630 char *tmp1 = _msg + p1;
633 _fnt->start2f(_x + p2, _y);
636 _fnt->setPointSize(orig_size * 1.2);
638 strncpy(tmp2, _msg, p1);
641 _fnt->start2f(_x, _y);
644 _fnt->setPointSize(orig_size * 1.2);
645 _fnt->start2f(_x, _y);
648 _fnt->setPointSize(orig_size);
652 void TextList::align(const char *s, int align, float *x, float *y,
653 float *l, float *r, float *b, float *t) const
655 HUD::textAlign(_font, s, align, x, y, l, r, b, t);
659 void TextList::draw()
664 glPushAttrib(GL_COLOR_BUFFER_BIT);
668 vector<HUDText>::iterator it, end = _list.end();
669 for (it = _list.begin(); it != end; ++it)
673 glDisable(GL_TEXTURE_2D);
678 ClipBox::ClipBox(const SGPropertyNode *n, float xoffset, float yoffset) :
686 // const_cast is necessary because ATM there's no matching getChild(const ...)
687 // prototype and getNode(const ..., <bool>) is wrongly interpreted as
688 // getNode(const ..., <int>)
689 _top_node = (const_cast<SGPropertyNode *>(n))->getChild("top", 0, true);
690 _bot_node = (const_cast<SGPropertyNode *>(n))->getChild("bottom", 0, true);
691 _left_node = (const_cast<SGPropertyNode *>(n))->getChild("left", 0, true);
692 _right_node = (const_cast<SGPropertyNode *>(n))->getChild("right", 0, true);
694 _left[0] = 1.0, _left[1] = _left[2] = 0.0;
695 _right[0] = -1.0, _right[1] = _right[2] = 0.0;
696 _top[0] = 0.0, _top[1] = -1.0, _top[2] = 0.0;
697 _bot[0] = 0.0, _bot[1] = 1.0, _bot[2] = 0.0;
707 _left[3] = -_left_node->getDoubleValue() - _xoffs;
708 _right[3] = _right_node->getDoubleValue() + _xoffs;
709 _bot[3] = -_bot_node->getDoubleValue() - _yoffs;
710 _top[3] = _top_node->getDoubleValue() + _yoffs;
712 glClipPlane(GL_CLIP_PLANE0, _top);
713 glEnable(GL_CLIP_PLANE0);
714 glClipPlane(GL_CLIP_PLANE1, _bot);
715 glEnable(GL_CLIP_PLANE1);
716 glClipPlane(GL_CLIP_PLANE2, _left);
717 glEnable(GL_CLIP_PLANE2);
718 glClipPlane(GL_CLIP_PLANE3, _right);
719 glEnable(GL_CLIP_PLANE3);
723 void ClipBox::unset()
726 glDisable(GL_CLIP_PLANE0);
727 glDisable(GL_CLIP_PLANE1);
728 glDisable(GL_CLIP_PLANE2);
729 glDisable(GL_CLIP_PLANE3);