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 <Viewer/viewmgr.hxx>
41 #include <Viewer/viewer.hxx>
42 #include <GUI/FGFontCache.hxx>
49 static float clamp(float f)
51 return f < 0.0f ? 0.0f : f > 1.0f ? 1.0f : f;
56 _currentPath(fgGetNode("/sim/hud/current-path", true)),
57 _currentColor(fgGetNode("/sim/hud/current-color", true)),
58 _visibility(fgGetNode("/sim/hud/visibility[1]", true)),
59 _3DenabledN(fgGetNode("/sim/hud/enable3d[1]", true)),
60 _antialiasing(fgGetNode("/sim/hud/color/antialiased", true)),
61 _transparency(fgGetNode("/sim/hud/color/transparent", true)),
62 _red(fgGetNode("/sim/hud/color/red", true)),
63 _green(fgGetNode("/sim/hud/color/green", true)),
64 _blue(fgGetNode("/sim/hud/color/blue", true)),
65 _alpha(fgGetNode("/sim/hud/color/alpha", true)),
66 _alpha_clamp(fgGetNode("/sim/hud/color/alpha-clamp", true)),
67 _brightness(fgGetNode("/sim/hud/color/brightness", true)),
72 _a(0.67), // FIXME better names
75 _scr_widthN(fgGetNode("/sim/startup/xsize", true)),
76 _scr_heightN(fgGetNode("/sim/startup/ysize", true)),
77 _unitsN(fgGetNode("/sim/startup/units", true)),
80 _font_renderer(new fntRenderer()),
84 _listener_active(false),
87 SG_LOG(SG_COCKPIT, SG_INFO, "Initializing HUD Instrument");
89 SGPropertyNode* hud = fgGetNode("/sim/hud");
90 hud->addChangeListener(this);
96 SGPropertyNode* hud = fgGetNode("/sim/hud");
97 hud->removeChangeListener(this);
105 const char* fontName = 0;
106 _font_cache = globals->get_fontcache();
108 fontName = fgGetString("/sim/hud/font/name", "Helvetica.txf");
109 _font = _font_cache->getTexFont(fontName);
112 throw sg_io_exception("/sim/hud/font/name is not a texture font",
113 sg_location(fontName));
115 _font_size = fgGetFloat("/sim/hud/font/size", 8);
116 _font_renderer->setFont(_font);
117 _font_renderer->setPointSize(_font_size);
118 _text_list.setFont(_font_renderer);
121 currentColorChanged();
122 _currentPath->fireValueChanged();
127 deque<Item *>::const_iterator it, end = _items.end();
128 for (it = _items.begin(); it != end; ++it)
130 end = _ladders.end();
131 for (it = _ladders.begin(); it != end; ++it)
146 _currentPath->fireValueChanged();
149 void HUD::update(double dt)
155 void HUD::draw(osg::State&)
160 if (!_items.size() && !_ladders.size())
168 const float normal_aspect = 640.0f / 480.0f;
169 // note: aspect_ratio is Y/X
170 float current_aspect = 1.0f / globals->get_current_view()->get_aspect_ratio();
171 if (current_aspect > normal_aspect) {
172 float aspect_adjust = current_aspect / normal_aspect;
173 float adjust = 320.0f * aspect_adjust - 320.0f;
174 draw2D(-adjust, 0.0f, 640.0f + adjust, 480.0f);
177 float aspect_adjust = normal_aspect / current_aspect;
178 float adjust = 240.0f * aspect_adjust - 240.0f;
179 draw2D(0.0f, -adjust, 640.0f, 480.0f + adjust);
182 glViewport(0, 0, _scr_width, _scr_height);
189 FGViewer* view = globals->get_current_view();
191 // Standard fgfs projection, with essentially meaningless clip
192 // planes (we'll map the whole HUD plane to z=-1)
193 glMatrixMode(GL_PROJECTION);
196 = Matrixf::perspective(view->get_v_fov(), 1/view->get_aspect_ratio(),
198 glLoadMatrix(proj.ptr());
200 glMatrixMode(GL_MODELVIEW);
203 // Standard fgfs view direction computation
205 lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
206 lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
207 lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
208 if (fabs(lookat[1]) > 9999)
209 lookat[1] = 9999; // FPU sanity
210 Matrixf mv = Matrixf::lookAt(Vec3f(0.0, 0.0, 0.0), lookat,
211 Vec3f(0.0, 1.0, 0.0));
212 glLoadMatrix(mv.ptr());
214 // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
215 // This is the default fgfs field of view, which the HUD files are
216 // written to assume.
217 float dx = 0.52056705; // tan(55/2)
218 float dy = dx * 0.75; // assumes 4:3 aspect ratio
220 m[0] = dx, m[4] = 0, m[ 8] = 0, m[12] = 0;
221 m[1] = 0, m[5] = dy, m[ 9] = 0, m[13] = 0;
222 m[2] = 0, m[6] = 0, m[10] = 1, m[14] = 0;
223 m[3] = 0, m[7] = 0, m[11] = 0, m[15] = 1;
226 // Convert the 640x480 "HUD standard" coordinate space to a square
227 // about the origin in the range [-1:1] at depth of -1
228 glScalef(1.0 / 320, 1.0 / 240, 1);
229 glTranslatef(-320, -240, -1);
233 glMatrixMode(GL_PROJECTION);
235 glMatrixMode(GL_MODELVIEW);
240 void HUD::draw2D(GLfloat x_start, GLfloat y_start, GLfloat x_end, GLfloat y_end)
243 glMatrixMode(GL_PROJECTION);
245 Matrixf proj = Matrixf::ortho2D(x_start, x_end, y_start, y_end);
246 glLoadMatrix(proj.ptr());
248 glMatrixMode(GL_MODELVIEW);
254 glMatrixMode(GL_PROJECTION);
256 glMatrixMode(GL_MODELVIEW);
261 void HUD::common_draw()
265 _stipple_line_list.erase();
267 glDisable(GL_DEPTH_TEST);
268 glDisable(GL_LIGHTING);
272 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
274 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
276 if (isAntialiased()) {
277 glEnable(GL_LINE_SMOOTH);
278 glAlphaFunc(GL_GREATER, alphaClamp());
279 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
288 deque<Item *>::const_iterator it, end = _items.end();
289 for (it = _items.begin(); it != end; ++it)
290 if ((*it)->isEnabled())
296 if (_stipple_line_list.size()) {
297 glEnable(GL_LINE_STIPPLE);
298 glLineStipple(1, 0x00FF);
299 _stipple_line_list.draw();
300 glDisable(GL_LINE_STIPPLE);
303 // ladders last, as they can have their own clip planes
304 end = _ladders.end();
305 for (it = _ladders.begin(); it != end; ++it)
306 if ((*it)->isEnabled())
311 if (isAntialiased()) {
312 glDisable(GL_ALPHA_TEST);
313 glDisable(GL_LINE_SMOOTH);
318 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
320 glEnable(GL_DEPTH_TEST);
321 glEnable(GL_LIGHTING);
325 int HUD::load(const char *file, float x, float y, int level, const string& indent)
327 const sgDebugPriority TREE = SG_INFO;
328 const int MAXNEST = 10;
330 SGPath path(globals->resolve_maybe_aircraft_path(file));
333 SG_LOG(SG_INPUT, SG_ALERT, "HUD: Cannot find configuration file '" << file << "'.");
338 SG_LOG(SG_INPUT, TREE, endl << "load " << file);
339 _items.erase(_items.begin(), _items.end());
340 _ladders.erase(_ladders.begin(), _ladders.end());
341 } else if (level > MAXNEST) {
342 SG_LOG(SG_INPUT, SG_ALERT, "HUD: files nested more than " << MAXNEST << " levels");
344 } else if (!file || !file[0]) {
345 SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid filename ");
350 ifstream input(path.c_str());
352 SG_LOG(SG_INPUT, SG_ALERT, "HUD: Cannot read configuration from '" << path.c_str() << "'");
358 readProperties(input, &root);
359 } catch (const sg_exception &e) {
361 guiErrorMessage("HUD: Error ", e);
366 _clip_box = new ClipBox(fgGetNode("/sim/hud/clipping"), x, y);
368 for (int i = 0; i < root.nChildren(); i++) {
369 SGPropertyNode *n = root.getChild(i);
370 const char *d = n->getStringValue("name", 0);
373 desc = string(": \"") + d + '"';
375 const char *name = n->getName();
376 if (!strcmp(name, "name")) {
379 } else if (!strcmp(name, "enable3d")) {
380 // set in the tree so that valueChanged() picks it up
381 _3DenabledN->setBoolValue(n->getBoolValue());
384 } else if (!strcmp(name, "import")) {
385 const char *fn = n->getStringValue("path", "");
386 float xoffs = n->getFloatValue("x-offset", 0.0f);
387 float yoffs = n->getFloatValue("y-offset", 0.0f);
389 SG_LOG(SG_INPUT, TREE, indent << "|__import " << fn << desc);
391 string ind = indent + string(i + 1 < root.nChildren() ? "| " : " ");
392 ret |= load(fn, x + xoffs, y + yoffs, level + 1, ind);
396 SG_LOG(SG_INPUT, TREE, indent << "|__" << name << desc);
399 if (!strcmp(name, "label")) {
400 item = static_cast<Item *>(new Label(this, n, x, y));
401 } else if (!strcmp(name, "gauge")) {
402 item = static_cast<Item *>(new Gauge(this, n, x, y));
403 } else if (!strcmp(name, "tape")) {
404 item = static_cast<Item *>(new Tape(this, n, x, y));
405 } else if (!strcmp(name, "dial")) {
406 item = static_cast<Item *>(new Dial(this, n, x, y));
407 } else if (!strcmp(name, "turn-bank-indicator")) {
408 item = static_cast<Item *>(new TurnBankIndicator(this, n, x, y));
409 } else if (!strcmp(name, "ladder")) {
410 item = static_cast<Item *>(new Ladder(this, n, x, y));
411 _ladders.insert(_ladders.begin(), item);
413 } else if (!strcmp(name, "runway")) {
414 item = static_cast<Item *>(new Runway(this, n, x, y));
415 } else if (!strcmp(name, "aiming-reticle")) {
416 item = static_cast<Item *>(new AimingReticle(this, n, x, y));
418 SG_LOG(SG_INPUT, TREE, indent << " \\...unsupported!");
421 _items.insert(_items.begin(), item);
424 SG_LOG(SG_INPUT, TREE, indent);
429 void HUD::valueChanged(SGPropertyNode *node)
431 if (_listener_active)
433 _listener_active = true;
435 bool loadNow = false;
436 _visible = _visibility->getBoolValue();
437 if (_visible && !_loaded) {
441 if (!strcmp(node->getName(), "current-path") && _visible) {
446 int pathIndex = _currentPath->getIntValue();
447 SGPropertyNode* pathNode = fgGetNode("/sim/hud/path", pathIndex);
448 std::string path("Huds/default.xml");
449 if (pathNode && pathNode->hasValue()) {
450 path = pathNode->getStringValue();
451 SG_LOG(SG_INSTR, SG_INFO, "will load Hud from " << path);
458 if (!strcmp(node->getName(), "current-color")) {
459 currentColorChanged();
462 _scr_width = _scr_widthN->getIntValue();
463 _scr_height = _scr_heightN->getIntValue();
466 _3Denabled = _3DenabledN->getBoolValue();
467 _transparent = _transparency->getBoolValue();
468 _antialiased = _antialiasing->getBoolValue();
469 float brt = _brightness->getFloatValue();
470 _r = clamp(brt * _red->getFloatValue());
471 _g = clamp(brt * _green->getFloatValue());
472 _b = clamp(brt * _blue->getFloatValue());
473 _a = clamp(_alpha->getFloatValue());
474 _cl = clamp(_alpha_clamp->getFloatValue());
476 _units = strcmp(_unitsN->getStringValue(), "feet") ? METER : FEET;
477 _listener_active = false;
480 void HUD::currentColorChanged()
482 SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
483 int index = _currentColor->getIntValue();
488 n = n->getChild("color", index, false);
493 if (n->hasValue("red"))
494 _red->setFloatValue(n->getFloatValue("red", 1.0));
495 if (n->hasValue("green"))
496 _green->setFloatValue(n->getFloatValue("green", 1.0));
497 if (n->hasValue("blue"))
498 _blue->setFloatValue(n->getFloatValue("blue", 1.0));
499 if (n->hasValue("alpha"))
500 _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
501 if (n->hasValue("alpha-clamp"))
502 _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
503 if (n->hasValue("brightness"))
504 _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
505 if (n->hasValue("antialiased"))
506 _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
507 if (n->hasValue("transparent"))
508 _transparency->setBoolValue(n->getBoolValue("transparent", false));
511 void HUD::setColor() const
514 glColor4f(_r, _g, _b, _a);
516 glColor3f(_r, _g, _b);
520 void HUD::textAlign(fntRenderer *rend, const char *s, int align,
521 float *x, float *y, float *l, float *r, float *b, float *t)
523 fntFont *font = rend->getFont();
524 float gap = font->getGap();
525 float left, right, bot, top;
526 font->getBBox(s, rend->getPointSize(), rend->getSlant(), &left, &right, &bot, &top);
528 if (align & HUD::HCENTER)
529 *x -= left - gap + (right - left - gap) / 2.0;
530 else if (align & HUD::RIGHT)
532 else if (align & HUD::LEFT)
535 if (align & HUD::VCENTER)
536 *y -= bot + (top - bot) / 2.0;
537 else if (align & HUD::TOP)
539 else if (align & HUD::BOTTOM)
551 // HUDText -- text container for TextList vector
554 HUDText::HUDText(fntRenderer *fnt, float x, float y, const char *s, int align, int d) :
560 strncpy(_msg, s, BUFSIZE);
564 HUD::textAlign(fnt, s, align, &_x, &_y, &ign, &ign, &ign, &ign);
570 if (!_digits) { // show all digits in same size
571 _fnt->start2f(_x, _y);
577 // this code is changed to display Numbers with big/small digits
578 // according to MIL Standards for example Altitude above 10000 ft
579 // is shown as 10ooo.
586 //if negative value then increase the c and p values
588 c++; // was moved to the comment. Unintentionally? TODO
592 while (tmp[i] != '\0') {
593 if ((tmp[i] >= '0') && (tmp[i] <= '9'))
598 float orig_size = _fnt->getPointSize();
600 _fnt->setPointSize(orig_size * 0.8);
602 char *tmp1 = _msg + p1;
605 _fnt->start2f(_x + p2, _y);
608 _fnt->setPointSize(orig_size * 1.2);
610 strncpy(tmp2, _msg, p1);
613 _fnt->start2f(_x, _y);
616 _fnt->setPointSize(orig_size * 1.2);
617 _fnt->start2f(_x, _y);
620 _fnt->setPointSize(orig_size);
624 void TextList::align(const char *s, int align, float *x, float *y,
625 float *l, float *r, float *b, float *t) const
627 HUD::textAlign(_font, s, align, x, y, l, r, b, t);
631 void TextList::draw()
636 glPushAttrib(GL_COLOR_BUFFER_BIT);
640 vector<HUDText>::iterator it, end = _list.end();
641 for (it = _list.begin(); it != end; ++it)
645 glDisable(GL_TEXTURE_2D);
650 ClipBox::ClipBox(const SGPropertyNode *n, float xoffset, float yoffset) :
658 // const_cast is necessary because ATM there's no matching getChild(const ...)
659 // prototype and getNode(const ..., <bool>) is wrongly interpreted as
660 // getNode(const ..., <int>)
661 _top_node = (const_cast<SGPropertyNode *>(n))->getChild("top", 0, true);
662 _bot_node = (const_cast<SGPropertyNode *>(n))->getChild("bottom", 0, true);
663 _left_node = (const_cast<SGPropertyNode *>(n))->getChild("left", 0, true);
664 _right_node = (const_cast<SGPropertyNode *>(n))->getChild("right", 0, true);
666 _left[0] = 1.0, _left[1] = _left[2] = 0.0;
667 _right[0] = -1.0, _right[1] = _right[2] = 0.0;
668 _top[0] = 0.0, _top[1] = -1.0, _top[2] = 0.0;
669 _bot[0] = 0.0, _bot[1] = 1.0, _bot[2] = 0.0;
679 _left[3] = -_left_node->getDoubleValue() - _xoffs;
680 _right[3] = _right_node->getDoubleValue() + _xoffs;
681 _bot[3] = -_bot_node->getDoubleValue() - _yoffs;
682 _top[3] = _top_node->getDoubleValue() + _yoffs;
684 glClipPlane(GL_CLIP_PLANE0, _top);
685 glEnable(GL_CLIP_PLANE0);
686 glClipPlane(GL_CLIP_PLANE1, _bot);
687 glEnable(GL_CLIP_PLANE1);
688 glClipPlane(GL_CLIP_PLANE2, _left);
689 glEnable(GL_CLIP_PLANE2);
690 glClipPlane(GL_CLIP_PLANE3, _right);
691 glEnable(GL_CLIP_PLANE3);
695 void ClipBox::unset()
698 glDisable(GL_CLIP_PLANE0);
699 glDisable(GL_CLIP_PLANE1);
700 glDisable(GL_CLIP_PLANE2);
701 glDisable(GL_CLIP_PLANE3);