#include <simgear/compiler.h>
#include <simgear/structure/exception.hxx>
-#include STL_STRING
-#include STL_FSTREAM
+#include <string>
+#include <fstream>
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
-#include SG_GLU_H
-
#include <simgear/constants.h>
#include <simgear/misc/sg_path.hxx>
+#include <osg/GLU>
#include <Main/globals.hxx>
#include <Main/viewmgr.hxx>
HUD::HUD() :
_current(fgGetNode("/sim/hud/current-color", true)),
_visibility(fgGetNode("/sim/hud/visibility[1]", true)),
- _3DenabledN(fgGetNode("/sim/hud/enable3d", true)),
+ _3DenabledN(fgGetNode("/sim/hud/enable3d[1]", true)),
_antialiasing(fgGetNode("/sim/hud/color/antialiased", true)),
_transparency(fgGetNode("/sim/hud/color/transparent", true)),
_red(fgGetNode("/sim/hud/color/red", true)),
_font_renderer(new fntRenderer()),
_font(0),
_font_size(0.0),
- _style(0)
+ _style(0),
+ _clip_box(0)
{
SG_LOG(SG_COCKPIT, SG_INFO, "Initializing HUD Instrument");
_scr_heightN->removeChangeListener(this);
_unitsN->removeChangeListener(this);
delete _font_renderer;
+ delete _clip_box;
deque<Item *>::const_iterator it, end = _items.end();
for (it = _items.begin(); it != end; ++it)
delete *it;
+ end = _ladders.end();
+ for (it = _ladders.begin(); it != end; ++it)
+ delete *it;
}
if (!_font)
throw sg_throwable(string("/sim/hud/font/name is not a texture font"));
- _font_size = fgGetFloat("/sim/hud/font/size", 10);
+ _font_size = fgGetFloat("/sim/hud/font/size", 8);
_font_renderer->setFont(_font);
_font_renderer->setPointSize(_font_size);
_text_list.setFont(_font_renderer);
- load(fgGetString("/hud", "Huds/default.xml"));
+ load(fgGetString("/sim/hud/path[1]", "Huds/default.xml"));
}
}
-void HUD::draw()
+void HUD::draw(osg::State&)
{
if (!isVisible())
return;
- if (!_items.size())
+ if (!_items.size() && !_ladders.size())
return;
if (is3D()) {
float dx = 0.52056705; // tan(55/2)
float dy = dx * 0.75; // assumes 4:3 aspect ratio
float m[16];
- m[0] = dx; m[4] = 0; m[ 8] = 0; m[12] = 0;
- m[1] = 0; m[5] = dy; m[ 9] = 0; m[13] = 0;
- m[2] = 0; m[6] = 0; m[10] = 1; m[14] = 0;
- m[3] = 0; m[7] = 0; m[11] = 0; m[15] = 1;
+ m[0] = dx, m[4] = 0, m[ 8] = 0, m[12] = 0;
+ m[1] = 0, m[5] = dy, m[ 9] = 0, m[13] = 0;
+ m[2] = 0, m[6] = 0, m[10] = 1, m[14] = 0;
+ m[3] = 0, m[7] = 0, m[11] = 0, m[15] = 1;
glMultMatrixf(m);
// Convert the 640x480 "HUD standard" coordinate space to a square
}
-void HUD::draw2D( GLfloat x_start, GLfloat y_start,
- GLfloat x_end, GLfloat y_end )
+void HUD::draw2D(GLfloat x_start, GLfloat y_start, GLfloat x_end, GLfloat y_end)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
}
setColor();
+ _clip_box->set();
+
deque<Item *>::const_iterator it, end = _items.end();
for (it = _items.begin(); it != end; ++it)
if ((*it)->isEnabled())
glDisable(GL_LINE_STIPPLE);
}
+ // ladders last, as they can have their own clip planes
+ end = _ladders.end();
+ for (it = _ladders.begin(); it != end; ++it)
+ if ((*it)->isEnabled())
+ (*it)->draw();
+
+ _clip_box->unset();
+
if (isAntialiased()) {
glDisable(GL_ALPHA_TEST);
glDisable(GL_LINE_SMOOTH);
if (!level) {
SG_LOG(SG_INPUT, TREE, endl << "load " << file);
_items.erase(_items.begin(), _items.end());
+ _ladders.erase(_ladders.begin(), _ladders.end());
} else if (level > MAXNEST) {
SG_LOG(SG_INPUT, SG_ALERT, "HUD: files nested more than " << MAXNEST << " levels");
return 0x1;
return 0x8;
}
+ delete _clip_box;
+ _clip_box = new ClipBox(fgGetNode("/sim/hud/clipping"), x, y);
+
for (int i = 0; i < root.nChildren(); i++) {
SGPropertyNode *n = root.getChild(i);
const char *d = n->getStringValue("name", 0);
} else if (!strcmp(name, "enable3d")) {
// set in the tree so that valueChanged() picks it up
- fgSetBool("/sim/hud/enable3d", n->getBoolValue());
+ _3DenabledN->setBoolValue(n->getBoolValue());
continue;
} else if (!strcmp(name, "import")) {
item = static_cast<Item *>(new TurnBankIndicator(this, n, x, y));
} else if (!strcmp(name, "ladder")) {
item = static_cast<Item *>(new Ladder(this, n, x, y));
+ _ladders.insert(_ladders.begin(), item);
+ continue;
} else if (!strcmp(name, "runway")) {
item = static_cast<Item *>(new Runway(this, n, x, y));
+ } else if (!strcmp(name, "aiming-reticle")) {
+ item = static_cast<Item *>(new AimingReticle(this, n, x, y));
} else {
SG_LOG(SG_INPUT, TREE, indent << " \\...unsupported!");
continue;
}
+void HUD::textAlign(fntRenderer *rend, const char *s, int align,
+ float *x, float *y, float *l, float *r, float *b, float *t)
+{
+ fntFont *font = rend->getFont();
+ float gap = font->getGap();
+ float left, right, bot, top;
+ font->getBBox(s, rend->getPointSize(), rend->getSlant(), &left, &right, &bot, &top);
+
+ if (align & HUD::HCENTER)
+ *x -= left - gap + (right - left - gap) / 2.0;
+ else if (align & HUD::RIGHT)
+ *x -= right;
+ else if (align & HUD::LEFT)
+ *x -= left;
+
+ if (align & HUD::VCENTER)
+ *y -= bot + (top - bot) / 2.0;
+ else if (align & HUD::TOP)
+ *y -= top;
+ else if (align & HUD::BOTTOM)
+ *y -= bot;
+
+ *l = *x + left;
+ *r = *x + right;
+ *b = *y + bot;
+ *t = *y + top;
+}
+
+
+
+
+// HUDText -- text container for TextList vector
+
+
+HUDText::HUDText(fntRenderer *fnt, float x, float y, const char *s, int align, int d) :
+ _fnt(fnt),
+ _x(x),
+ _y(y),
+ _digits(d)
+{
+ strncpy(_msg, s, BUFSIZE);
+ if (!align || !s[0])
+ return;
+ float ign;
+ HUD::textAlign(fnt, s, align, &_x, &_y, &ign, &ign, &ign, &ign);
+}
+
+
+void HUDText::draw()
+{
+ if (!_digits) { // show all digits in same size
+ _fnt->start2f(_x, _y);
+ _fnt->puts(_msg);
+ return;
+ }
+
+ // FIXME
+ // this code is changed to display Numbers with big/small digits
+ // according to MIL Standards for example Altitude above 10000 ft
+ // is shown as 10ooo.
+
+ int c = 0, i = 0;
+ char *t = _msg;
+ int p = 4;
+
+ if (t[0] == '-') {
+ //if negative value then increase the c and p values
+ //for '-' sign.
+ c++; // was moved to the comment. Unintentionally? TODO
+ p++;
+ }
+ char *tmp = _msg;
+ while (tmp[i] != '\0') {
+ if ((tmp[i] >= '0') && (tmp[i] <= '9'))
+ c++;
+ i++;
+ }
+
+ float orig_size = _fnt->getPointSize();
+ if (c > p) {
+ _fnt->setPointSize(orig_size * 0.8);
+ int p1 = c - 3;
+ char *tmp1 = _msg + p1;
+ int p2 = p1 * 8;
+
+ _fnt->start2f(_x + p2, _y);
+ _fnt->puts(tmp1);
+
+ _fnt->setPointSize(orig_size * 1.2);
+ char tmp2[BUFSIZE];
+ strncpy(tmp2, _msg, p1);
+ tmp2[p1] = '\0';
+
+ _fnt->start2f(_x, _y);
+ _fnt->puts(tmp2);
+ } else {
+ _fnt->setPointSize(orig_size * 1.2);
+ _fnt->start2f(_x, _y);
+ _fnt->puts(tmp);
+ }
+ _fnt->setPointSize(orig_size);
+}
+
+
+void TextList::align(const char *s, int align, float *x, float *y,
+ float *l, float *r, float *b, float *t) const
+{
+ HUD::textAlign(_font, s, align, x, y, l, r, b, t);
+}
+
+
+void TextList::draw()
+{
+ assert(_font);
+
+ // FIXME
+ glPushAttrib(GL_COLOR_BUFFER_BIT);
+ glEnable(GL_BLEND);
+
+ _font->begin();
+ vector<HUDText>::iterator it, end = _list.end();
+ for (it = _list.begin(); it != end; ++it)
+ it->draw();
+ _font->end();
+
+ glDisable(GL_TEXTURE_2D);
+ glPopAttrib();
+}
+
+
+ClipBox::ClipBox(const SGPropertyNode *n, float xoffset, float yoffset) :
+ _active(false),
+ _xoffs(xoffset),
+ _yoffs(yoffset)
+{
+ if (!n)
+ return;
+
+ // const_cast is necessary because ATM there's no matching getChild(const ...)
+ // prototype and getNode(const ..., <bool>) is wrongly interpreted as
+ // getNode(const ..., <int>)
+ _top_node = (const_cast<SGPropertyNode *>(n))->getChild("top", 0, true);
+ _bot_node = (const_cast<SGPropertyNode *>(n))->getChild("bottom", 0, true);
+ _left_node = (const_cast<SGPropertyNode *>(n))->getChild("left", 0, true);
+ _right_node = (const_cast<SGPropertyNode *>(n))->getChild("right", 0, true);
+
+ _left[0] = 1.0, _left[1] = _left[2] = 0.0;
+ _right[0] = -1.0, _right[1] = _right[2] = 0.0;
+ _top[0] = 0.0, _top[1] = -1.0, _top[2] = 0.0;
+ _bot[0] = 0.0, _bot[1] = 1.0, _bot[2] = 0.0;
+ _active = true;
+}
+
+
+void ClipBox::set()
+{
+ if (!_active)
+ return;
+
+ _left[3] = -_left_node->getDoubleValue() - _xoffs;
+ _right[3] = _right_node->getDoubleValue() + _xoffs;
+ _bot[3] = -_bot_node->getDoubleValue() - _yoffs;
+ _top[3] = _top_node->getDoubleValue() + _yoffs;
+
+ glClipPlane(GL_CLIP_PLANE0, _top);
+ glEnable(GL_CLIP_PLANE0);
+ glClipPlane(GL_CLIP_PLANE1, _bot);
+ glEnable(GL_CLIP_PLANE1);
+ glClipPlane(GL_CLIP_PLANE2, _left);
+ glEnable(GL_CLIP_PLANE2);
+ glClipPlane(GL_CLIP_PLANE3, _right);
+ glEnable(GL_CLIP_PLANE3);
+}
+
+
+void ClipBox::unset()
+{
+ if (_active) {
+ glDisable(GL_CLIP_PLANE0);
+ glDisable(GL_CLIP_PLANE1);
+ glDisable(GL_CLIP_PLANE2);
+ glDisable(GL_CLIP_PLANE3);
+ }
+}