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>
34 #include <simgear/constants.h>
35 #include <simgear/misc/sg_path.hxx>
37 #include <Main/globals.hxx>
38 #include <Main/viewmgr.hxx>
43 static float clamp(float f)
45 return f < 0.0f ? 0.0f : f > 1.0f ? 1.0f : f;
50 _current(fgGetNode("/sim/hud/current-color", true)),
51 _visibility(fgGetNode("/sim/hud/visibility[1]", true)),
52 _3DenabledN(fgGetNode("/sim/hud/enable3d", true)),
53 _antialiasing(fgGetNode("/sim/hud/color/antialiased", true)),
54 _transparency(fgGetNode("/sim/hud/color/transparent", true)),
55 _red(fgGetNode("/sim/hud/color/red", true)),
56 _green(fgGetNode("/sim/hud/color/green", true)),
57 _blue(fgGetNode("/sim/hud/color/blue", true)),
58 _alpha(fgGetNode("/sim/hud/color/alpha", true)),
59 _alpha_clamp(fgGetNode("/sim/hud/color/alpha-clamp", true)),
60 _brightness(fgGetNode("/sim/hud/color/brightness", true)),
64 _a(0.67), // FIXME better names
67 _scr_widthN(fgGetNode("/sim/startup/xsize", true)),
68 _scr_heightN(fgGetNode("/sim/startup/ysize", true)),
69 _unitsN(fgGetNode("/sim/startup/units", true)),
72 _font_renderer(new fntRenderer()),
77 SG_LOG(SG_COCKPIT, SG_INFO, "Initializing HUD Instrument");
79 _visibility->addChangeListener(this);
80 _3DenabledN->addChangeListener(this);
81 _antialiasing->addChangeListener(this);
82 _transparency->addChangeListener(this);
83 _red->addChangeListener(this);
84 _green->addChangeListener(this);
85 _blue->addChangeListener(this);
86 _alpha->addChangeListener(this);
87 _alpha_clamp->addChangeListener(this);
88 _brightness->addChangeListener(this);
89 _current->addChangeListener(this);
90 _scr_widthN->addChangeListener(this);
91 _scr_heightN->addChangeListener(this);
92 _unitsN->addChangeListener(this, true);
98 _visibility->removeChangeListener(this);
99 _3DenabledN->removeChangeListener(this);
100 _antialiasing->removeChangeListener(this);
101 _transparency->removeChangeListener(this);
102 _red->removeChangeListener(this);
103 _green->removeChangeListener(this);
104 _blue->removeChangeListener(this);
105 _alpha->removeChangeListener(this);
106 _alpha_clamp->removeChangeListener(this);
107 _brightness->removeChangeListener(this);
108 _current->removeChangeListener(this);
109 _scr_widthN->removeChangeListener(this);
110 _scr_heightN->removeChangeListener(this);
111 _unitsN->removeChangeListener(this);
112 delete _font_renderer;
114 deque<Item *>::const_iterator it, end = _items.end();
115 for (it = _items.begin(); it != end; ++it)
122 _font_cache = globals->get_fontcache();
124 _font = _font_cache->getTexFont(fgGetString("/sim/hud/font/name", "Helvetica.txf"));
126 throw sg_throwable(string("/sim/hud/font/name is not a texture font"));
128 _font_size = fgGetFloat("/sim/hud/font/size", 10);
129 _font_renderer->setFont(_font);
130 _font_renderer->setPointSize(_font_size);
131 _text_list.setFont(_font_renderer);
133 load(fgGetString("/hud", "Huds/default.xml"));
137 void HUD::update(double dt)
156 const float normal_aspect = 640.0f / 480.0f;
157 // note: aspect_ratio is Y/X
158 float current_aspect = 1.0f / globals->get_current_view()->get_aspect_ratio();
159 if (current_aspect > normal_aspect) {
160 float aspect_adjust = current_aspect / normal_aspect;
161 float adjust = 320.0f * aspect_adjust - 320.0f;
162 draw2D(-adjust, 0.0f, 640.0f + adjust, 480.0f);
165 float aspect_adjust = normal_aspect / current_aspect;
166 float adjust = 240.0f * aspect_adjust - 240.0f;
167 draw2D(0.0f, -adjust, 640.0f, 480.0f + adjust);
170 glViewport(0, 0, _scr_width, _scr_height);
176 FGViewer* view = globals->get_current_view();
178 // Standard fgfs projection, with essentially meaningless clip
179 // planes (we'll map the whole HUD plane to z=-1)
180 glMatrixMode(GL_PROJECTION);
183 gluPerspective(view->get_v_fov(), 1.0 / view->get_aspect_ratio(), 0.1, 10);
185 glMatrixMode(GL_MODELVIEW);
189 // Standard fgfs view direction computation
191 lookat[0] = -sin(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
192 lookat[1] = tan(SG_DEGREES_TO_RADIANS * view->getPitchOffset_deg());
193 lookat[2] = -cos(SG_DEGREES_TO_RADIANS * view->getHeadingOffset_deg());
194 if (fabs(lookat[1]) > 9999)
195 lookat[1] = 9999; // FPU sanity
196 gluLookAt(0, 0, 0, lookat[0], lookat[1], lookat[2], 0, 1, 0);
198 // Map the -1:1 square to a 55.0x41.25 degree wide patch at z=1.
199 // This is the default fgfs field of view, which the HUD files are
200 // written to assume.
201 float dx = 0.52056705; // tan(55/2)
202 float dy = dx * 0.75; // assumes 4:3 aspect ratio
204 m[0] = dx; m[4] = 0; m[ 8] = 0; m[12] = 0;
205 m[1] = 0; m[5] = dy; m[ 9] = 0; m[13] = 0;
206 m[2] = 0; m[6] = 0; m[10] = 1; m[14] = 0;
207 m[3] = 0; m[7] = 0; m[11] = 0; m[15] = 1;
210 // Convert the 640x480 "HUD standard" coordinate space to a square
211 // about the origin in the range [-1:1] at depth of -1
212 glScalef(1.0 / 320, 1.0 / 240, 1);
213 glTranslatef(-320, -240, -1);
217 glMatrixMode(GL_PROJECTION);
219 glMatrixMode(GL_MODELVIEW);
224 void HUD::draw2D( GLfloat x_start, GLfloat y_start,
225 GLfloat x_end, GLfloat y_end )
227 glMatrixMode(GL_PROJECTION);
230 gluOrtho2D(x_start, x_end, y_start, y_end);
232 glMatrixMode(GL_MODELVIEW);
238 glMatrixMode(GL_PROJECTION);
240 glMatrixMode(GL_MODELVIEW);
245 void HUD::common_draw()
249 _stipple_line_list.erase();
251 glDisable(GL_DEPTH_TEST);
252 glDisable(GL_LIGHTING);
256 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
258 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
260 if (isAntialiased()) {
261 glEnable(GL_LINE_SMOOTH);
262 glAlphaFunc(GL_GREATER, alphaClamp());
263 glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
270 deque<Item *>::const_iterator it, end = _items.end();
271 for (it = _items.begin(); it != end; ++it)
272 if ((*it)->isEnabled())
278 if (_stipple_line_list.size()) {
279 glEnable(GL_LINE_STIPPLE);
280 glLineStipple(1, 0x00FF);
281 _stipple_line_list.draw();
282 glDisable(GL_LINE_STIPPLE);
285 if (isAntialiased()) {
286 glDisable(GL_ALPHA_TEST);
287 glDisable(GL_LINE_SMOOTH);
292 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
294 glEnable(GL_DEPTH_TEST);
295 glEnable(GL_LIGHTING);
299 int HUD::load(const char *file, float x, float y, int level, const string& indent)
301 const sgDebugPriority TREE = SG_INFO;
302 const int MAXNEST = 10;
304 SGPath path(globals->get_fg_root());
308 SG_LOG(SG_INPUT, TREE, endl << "load " << file);
309 _items.erase(_items.begin(), _items.end());
310 } else if (level > MAXNEST) {
311 SG_LOG(SG_INPUT, SG_ALERT, "HUD: files nested more than " << MAXNEST << " levels");
313 } else if (!file || !file[0]) {
314 SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid filename ");
319 ifstream input(path.c_str());
321 SG_LOG(SG_INPUT, SG_ALERT, "HUD: Cannot read configuration from " << path.str());
327 readProperties(input, &root);
328 } catch (const sg_exception &e) {
330 guiErrorMessage("HUD: Error ", e);
334 for (int i = 0; i < root.nChildren(); i++) {
335 SGPropertyNode *n = root.getChild(i);
336 const char *d = n->getStringValue("name", 0);
339 desc = string(": \"") + d + '"';
341 const char *name = n->getName();
342 if (!strcmp(name, "name")) {
345 } else if (!strcmp(name, "enable3d")) {
346 // set in the tree so that valueChanged() picks it up
347 fgSetBool("/sim/hud/enable3d", n->getBoolValue());
350 } else if (!strcmp(name, "import")) {
351 const char *fn = n->getStringValue("path", "");
352 float xoffs = n->getFloatValue("x-offset", 0.0f);
353 float yoffs = n->getFloatValue("y-offset", 0.0f);
355 SG_LOG(SG_INPUT, TREE, indent << "|__import " << fn << desc);
357 string ind = indent + string(i + 1 < root.nChildren() ? "| " : " ");
358 ret |= load(fn, x + xoffs, y + yoffs, level + 1, ind);
362 SG_LOG(SG_INPUT, TREE, indent << "|__" << name << desc);
365 if (!strcmp(name, "label")) {
366 item = static_cast<Item *>(new Label(this, n, x, y));
367 } else if (!strcmp(name, "gauge")) {
368 item = static_cast<Item *>(new Gauge(this, n, x, y));
369 } else if (!strcmp(name, "tape")) {
370 item = static_cast<Item *>(new Tape(this, n, x, y));
371 } else if (!strcmp(name, "dial")) {
372 item = static_cast<Item *>(new Dial(this, n, x, y));
373 } else if (!strcmp(name, "turn-bank-indicator")) {
374 item = static_cast<Item *>(new TurnBankIndicator(this, n, x, y));
375 } else if (!strcmp(name, "ladder")) {
376 item = static_cast<Item *>(new Ladder(this, n, x, y));
377 } else if (!strcmp(name, "runway")) {
378 item = static_cast<Item *>(new Runway(this, n, x, y));
380 SG_LOG(SG_INPUT, TREE, indent << " \\...unsupported!");
383 _items.insert(_items.begin(), item);
386 SG_LOG(SG_INPUT, TREE, indent);
391 void HUD::valueChanged(SGPropertyNode *node)
393 if (!strcmp(node->getName(), "current-color")) {
394 int i = node->getIntValue();
397 SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
398 if ((n = n->getChild("color", i, false))) {
399 if (n->hasValue("red"))
400 _red->setFloatValue(n->getFloatValue("red", 1.0));
401 if (n->hasValue("green"))
402 _green->setFloatValue(n->getFloatValue("green", 1.0));
403 if (n->hasValue("blue"))
404 _blue->setFloatValue(n->getFloatValue("blue", 1.0));
405 if (n->hasValue("alpha"))
406 _alpha->setFloatValue(n->getFloatValue("alpha", 0.67));
407 if (n->hasValue("alpha-clamp"))
408 _alpha_clamp->setFloatValue(n->getFloatValue("alpha-clamp", 0.01));
409 if (n->hasValue("brightness"))
410 _brightness->setFloatValue(n->getFloatValue("brightness", 0.75));
411 if (n->hasValue("antialiased"))
412 _antialiasing->setBoolValue(n->getBoolValue("antialiased", false));
413 if (n->hasValue("transparent"))
414 _transparency->setBoolValue(n->getBoolValue("transparent", false));
417 _scr_width = _scr_widthN->getIntValue();
418 _scr_height = _scr_heightN->getIntValue();
420 _visible = _visibility->getBoolValue();
421 _3Denabled = _3DenabledN->getBoolValue();
422 _transparent = _transparency->getBoolValue();
423 _antialiased = _antialiasing->getBoolValue();
424 float brt = _brightness->getFloatValue();
425 _r = clamp(brt * _red->getFloatValue());
426 _g = clamp(brt * _green->getFloatValue());
427 _b = clamp(brt * _blue->getFloatValue());
428 _a = clamp(_alpha->getFloatValue());
429 _cl = clamp(_alpha_clamp->getFloatValue());
431 _units = strcmp(_unitsN->getStringValue(), "feet") ? METER : FEET;
435 void HUD::setColor() const
438 glColor4f(_r, _g, _b, _a);
440 glColor3f(_r, _g, _b);