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>
38 #include <simgear/constants.h>
39 #include <simgear/misc/sg_path.hxx>
41 #include <Main/globals.hxx>
42 #include <Main/viewmgr.hxx>
47 static float clamp(float f)
49 return f < 0.0f ? 0.0f : f > 1.0f ? 1.0f : f;
54 _current(fgGetNode("/sim/hud/current-color", true)),
55 _visibility(fgGetNode("/sim/hud/visibility[1]", true)),
56 _3DenabledN(fgGetNode("/sim/hud/enable3d[1]", true)),
57 _antialiasing(fgGetNode("/sim/hud/color/antialiased", true)),
58 _transparency(fgGetNode("/sim/hud/color/transparent", true)),
59 _red(fgGetNode("/sim/hud/color/red", true)),
60 _green(fgGetNode("/sim/hud/color/green", true)),
61 _blue(fgGetNode("/sim/hud/color/blue", true)),
62 _alpha(fgGetNode("/sim/hud/color/alpha", true)),
63 _alpha_clamp(fgGetNode("/sim/hud/color/alpha-clamp", true)),
64 _brightness(fgGetNode("/sim/hud/color/brightness", true)),
68 _a(0.67), // FIXME better names
71 _scr_widthN(fgGetNode("/sim/startup/xsize", true)),
72 _scr_heightN(fgGetNode("/sim/startup/ysize", true)),
73 _unitsN(fgGetNode("/sim/startup/units", true)),
76 _font_renderer(new fntRenderer()),
81 SG_LOG(SG_COCKPIT, SG_INFO, "Initializing HUD Instrument");
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 _visibility->removeChangeListener(this);
103 _3DenabledN->removeChangeListener(this);
104 _antialiasing->removeChangeListener(this);
105 _transparency->removeChangeListener(this);
106 _red->removeChangeListener(this);
107 _green->removeChangeListener(this);
108 _blue->removeChangeListener(this);
109 _alpha->removeChangeListener(this);
110 _alpha_clamp->removeChangeListener(this);
111 _brightness->removeChangeListener(this);
112 _current->removeChangeListener(this);
113 _scr_widthN->removeChangeListener(this);
114 _scr_heightN->removeChangeListener(this);
115 _unitsN->removeChangeListener(this);
116 delete _font_renderer;
118 deque<Item *>::const_iterator it, end = _items.end();
119 for (it = _items.begin(); it != end; ++it)
126 _font_cache = globals->get_fontcache();
128 _font = _font_cache->getTexFont(fgGetString("/sim/hud/font/name", "Helvetica.txf"));
130 throw sg_throwable(string("/sim/hud/font/name is not a texture font"));
132 _font_size = fgGetFloat("/sim/hud/font/size", 8);
133 _font_renderer->setFont(_font);
134 _font_renderer->setPointSize(_font_size);
135 _text_list.setFont(_font_renderer);
137 load(fgGetString("/sim/hud/path[1]", "Huds/default.xml"));
141 void HUD::update(double dt)
147 void HUD::draw(osg::State&)
160 const float normal_aspect = 640.0f / 480.0f;
161 // note: aspect_ratio is Y/X
162 float current_aspect = 1.0f / globals->get_current_view()->get_aspect_ratio();
163 if (current_aspect > normal_aspect) {
164 float aspect_adjust = current_aspect / normal_aspect;
165 float adjust = 320.0f * aspect_adjust - 320.0f;
166 draw2D(-adjust, 0.0f, 640.0f + adjust, 480.0f);
169 float aspect_adjust = normal_aspect / current_aspect;
170 float adjust = 240.0f * aspect_adjust - 240.0f;
171 draw2D(0.0f, -adjust, 640.0f, 480.0f + adjust);
174 glViewport(0, 0, _scr_width, _scr_height);
180 FGViewer* view = globals->get_current_view();
182 // Standard fgfs projection, with essentially meaningless clip
183 // planes (we'll map the whole HUD plane to z=-1)
184 glMatrixMode(GL_PROJECTION);
187 gluPerspective(view->get_v_fov(), 1.0 / view->get_aspect_ratio(), 0.1, 10);
189 glMatrixMode(GL_MODELVIEW);
193 // Standard fgfs view direction computation
195 lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
196 lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
197 lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
198 if (fabs(lookat[1]) > 9999)
199 lookat[1] = 9999; // FPU sanity
200 gluLookAt(0, 0, 0, lookat[0], lookat[1], lookat[2], 0, 1, 0);
202 // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
203 // This is the default fgfs field of view, which the HUD files are
204 // written to assume.
205 float dx = 0.52056705; // tan(55/2)
206 float dy = dx * 0.75; // assumes 4:3 aspect ratio
208 m[0] = dx, m[4] = 0, m[ 8] = 0, m[12] = 0;
209 m[1] = 0, m[5] = dy, m[ 9] = 0, m[13] = 0;
210 m[2] = 0, m[6] = 0, m[10] = 1, m[14] = 0;
211 m[3] = 0, m[7] = 0, m[11] = 0, m[15] = 1;
214 // Convert the 640x480 "HUD standard" coordinate space to a square
215 // about the origin in the range [-1:1] at depth of -1
216 glScalef(1.0 / 320, 1.0 / 240, 1);
217 glTranslatef(-320, -240, -1);
221 glMatrixMode(GL_PROJECTION);
223 glMatrixMode(GL_MODELVIEW);
228 void HUD::draw2D(GLfloat x_start, GLfloat y_start, GLfloat x_end, GLfloat y_end)
230 glMatrixMode(GL_PROJECTION);
233 gluOrtho2D(x_start, x_end, y_start, y_end);
235 glMatrixMode(GL_MODELVIEW);
241 glMatrixMode(GL_PROJECTION);
243 glMatrixMode(GL_MODELVIEW);
248 void HUD::common_draw()
252 _stipple_line_list.erase();
254 glDisable(GL_DEPTH_TEST);
255 glDisable(GL_LIGHTING);
259 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
261 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
263 if (isAntialiased()) {
264 glEnable(GL_LINE_SMOOTH);
265 glAlphaFunc(GL_GREATER, alphaClamp());
266 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
273 deque<Item *>::const_iterator it, end = _items.end();
274 for (it = _items.begin(); it != end; ++it)
275 if ((*it)->isEnabled())
281 if (_stipple_line_list.size()) {
282 glEnable(GL_LINE_STIPPLE);
283 glLineStipple(1, 0x00FF);
284 _stipple_line_list.draw();
285 glDisable(GL_LINE_STIPPLE);
288 if (isAntialiased()) {
289 glDisable(GL_ALPHA_TEST);
290 glDisable(GL_LINE_SMOOTH);
295 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
297 glEnable(GL_DEPTH_TEST);
298 glEnable(GL_LIGHTING);
302 int HUD::load(const char *file, float x, float y, int level, const string& indent)
304 const sgDebugPriority TREE = SG_INFO;
305 const int MAXNEST = 10;
307 SGPath path(globals->get_fg_root());
311 SG_LOG(SG_INPUT, TREE, endl << "load " << file);
312 _items.erase(_items.begin(), _items.end());
313 } else if (level > MAXNEST) {
314 SG_LOG(SG_INPUT, SG_ALERT, "HUD: files nested more than " << MAXNEST << " levels");
316 } else if (!file || !file[0]) {
317 SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid filename ");
322 ifstream input(path.c_str());
324 SG_LOG(SG_INPUT, SG_ALERT, "HUD: Cannot read configuration from " << path.str());
330 readProperties(input, &root);
331 } catch (const sg_exception &e) {
333 guiErrorMessage("HUD: Error ", e);
337 for (int i = 0; i < root.nChildren(); i++) {
338 SGPropertyNode *n = root.getChild(i);
339 const char *d = n->getStringValue("name", 0);
342 desc = string(": \"") + d + '"';
344 const char *name = n->getName();
345 if (!strcmp(name, "name")) {
348 } else if (!strcmp(name, "enable3d")) {
349 // set in the tree so that valueChanged() picks it up
350 _3DenabledN->setBoolValue(n->getBoolValue());
353 } else if (!strcmp(name, "import")) {
354 const char *fn = n->getStringValue("path", "");
355 float xoffs = n->getFloatValue("x-offset", 0.0f);
356 float yoffs = n->getFloatValue("y-offset", 0.0f);
358 SG_LOG(SG_INPUT, TREE, indent << "|__import " << fn << desc);
360 string ind = indent + string(i + 1 < root.nChildren() ? "| " : " ");
361 ret |= load(fn, x + xoffs, y + yoffs, level + 1, ind);
365 SG_LOG(SG_INPUT, TREE, indent << "|__" << name << desc);
368 if (!strcmp(name, "label")) {
369 item = static_cast<Item *>(new Label(this, n, x, y));
370 } else if (!strcmp(name, "gauge")) {
371 item = static_cast<Item *>(new Gauge(this, n, x, y));
372 } else if (!strcmp(name, "tape")) {
373 item = static_cast<Item *>(new Tape(this, n, x, y));
374 } else if (!strcmp(name, "dial")) {
375 item = static_cast<Item *>(new Dial(this, n, x, y));
376 } else if (!strcmp(name, "turn-bank-indicator")) {
377 item = static_cast<Item *>(new TurnBankIndicator(this, n, x, y));
378 } else if (!strcmp(name, "ladder")) {
379 item = static_cast<Item *>(new Ladder(this, n, x, y));
380 } else if (!strcmp(name, "runway")) {
381 item = static_cast<Item *>(new Runway(this, n, x, y));
382 } else if (!strcmp(name, "aiming-reticle")) {
383 item = static_cast<Item *>(new AimingReticle(this, n, x, y));
385 SG_LOG(SG_INPUT, TREE, indent << " \\...unsupported!");
388 _items.insert(_items.begin(), item);
391 SG_LOG(SG_INPUT, TREE, indent);
396 void HUD::valueChanged(SGPropertyNode *node)
398 if (!strcmp(node->getName(), "current-color")) {
399 int i = node->getIntValue();
402 SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
403 if ((n = n->getChild("color", i, false))) {
404 if (n->hasValue("red"))
405 _red->setFloatValue(n->getFloatValue("red", 1.0));
406 if (n->hasValue("green"))
407 _green->setFloatValue(n->getFloatValue("green", 1.0));
408 if (n->hasValue("blue"))
409 _blue->setFloatValue(n->getFloatValue("blue", 1.0));
410 if (n->hasValue("alpha"))
411 _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
412 if (n->hasValue("alpha-clamp"))
413 _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
414 if (n->hasValue("brightness"))
415 _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
416 if (n->hasValue("antialiased"))
417 _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
418 if (n->hasValue("transparent"))
419 _transparency->setBoolValue(n->getBoolValue("transparent", false));
422 _scr_width = _scr_widthN->getIntValue();
423 _scr_height = _scr_heightN->getIntValue();
425 _visible = _visibility->getBoolValue();
426 _3Denabled = _3DenabledN->getBoolValue();
427 _transparent = _transparency->getBoolValue();
428 _antialiased = _antialiasing->getBoolValue();
429 float brt = _brightness->getFloatValue();
430 _r = clamp(brt * _red->getFloatValue());
431 _g = clamp(brt * _green->getFloatValue());
432 _b = clamp(brt * _blue->getFloatValue());
433 _a = clamp(_alpha->getFloatValue());
434 _cl = clamp(_alpha_clamp->getFloatValue());
436 _units = strcmp(_unitsN->getStringValue(), "feet") ? METER : FEET;
440 void HUD::setColor() const
443 glColor4f(_r, _g, _b, _a);
445 glColor3f(_r, _g, _b);
451 void HUD::textAlign(fntRenderer *rend, const char *s, int align,
452 float *x, float *y, float *l, float *r, float *t, float *b)
454 fntFont *font = rend->getFont();
455 float gap = font->getGap();
456 float left, right, bot, top;
457 font->getBBox(s, rend->getPointSize(), rend->getSlant(), &left, &right, &bot, &top);
459 if (align & HUD::HCENTER)
460 *x -= left - gap + (right - left - gap) / 2.0;
461 else if (align & HUD::RIGHT)
463 else if (align & HUD::LEFT)
466 if (align & HUD::VCENTER)
467 *y -= bot + (top - bot) / 2.0;
468 else if (align & HUD::TOP)
470 else if (align & HUD::BOTTOM)
482 // HUDText -- text container for TextList vector
485 HUDText::HUDText(fntRenderer *fnt, float x, float y, const char *s, int align, int d) :
491 strncpy(_msg, s, BUFSIZE);
495 HUD::textAlign(fnt, s, align, &_x, &_y, &ign, &ign, &ign, &ign);
501 if (!_digits) { // show all digits in same size
502 _fnt->start2f(_x, _y);
508 // this code is changed to display Numbers with big/small digits
509 // according to MIL Standards for example Altitude above 10000 ft
510 // is shown as 10ooo.
517 //if negative value then increase the c and p values
519 c++; // was moved to the comment. Unintentionally? TODO
523 while (tmp[i] != '\0') {
524 if ((tmp[i] >= '0') && (tmp[i] <= '9'))
529 float orig_size = _fnt->getPointSize();
531 _fnt->setPointSize(orig_size * 0.8);
533 char *tmp1 = _msg + p1;
536 _fnt->start2f(_x + p2, _y);
539 _fnt->setPointSize(orig_size * 1.2);
541 strncpy(tmp2, _msg, p1);
544 _fnt->start2f(_x, _y);
547 _fnt->setPointSize(orig_size * 1.2);
548 _fnt->start2f(_x, _y);
551 _fnt->setPointSize(orig_size);
555 void TextList::align(const char *s, int align, float *x, float *y,
556 float *l, float *r, float *b, float *t) const
558 HUD::textAlign(_font, s, align, x, y, l, r, b, t);
562 void TextList::draw()
567 glPushAttrib(GL_COLOR_BUFFER_BIT);
571 vector<HUDText>::iterator it, end = _list.end();
572 for (it = _list.begin(); it != end; ++it)
576 glDisable(GL_TEXTURE_2D);