1 // HUD.cxx -- Head Up Display
2 //
3 // Written by Michele America, started September 1997.
4 //
5 // Copyright (C) 1997  Michele F. America  [micheleamerica#geocities:com]
6 // Copyright (C) 2006  Melchior FRANZ  [mfranz#aon:at]
7 //
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.
12 //
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.
17 //
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.
21 
22 #include <simgear/compiler.h>
23 #include <simgear/structure/exception.hxx>
24 
25 #include <string>
26 #include <fstream>
27 
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31 
32 #include <simgear/constants.h>
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/io/iostreams/sgstream.hxx>
35 #include <simgear/props/props_io.hxx>
36 #include <osg/GLU>
37 
38 #include "fnt.h"
39 
40 #include <Main/globals.hxx>
41 #include <Main/fg_props.hxx>
42 #include <Viewer/viewmgr.hxx>
43 #include <Viewer/view.hxx>
44 #include <GUI/FGFontCache.hxx>
45 #include <GUI/gui.h> // for guiErrorMessage
46 
47 #include "HUD.hxx"
48 #include "HUD_private.hxx"
49 
50 using std::endl;
51 using std::ifstream;
52 using std::string;
53 using std::deque;
54 using std::vector;
55 
clamp(float f)56 static float clamp(float f)
57 {
58     return f < 0.0f ? 0.0f : f > 1.0f ? 1.0f : f;
59 }
60 
Input(const SGPropertyNode * n,float factor,float offset,float min,float max)61 HUD::Input::Input(const SGPropertyNode *n, float factor, float offset,
62       float min, float max) :
63   _valid(false),
64   _property(0),
65   _damped(SGLimitsf::max())
66 {
67   if (!n)
68     return;
69   _factor = n->getFloatValue("factor", factor);
70   _offset = n->getFloatValue("offset", offset);
71   _min = n->getFloatValue("min", min);
72   _max = n->getFloatValue("max", max);
73   _coeff = 1.0 - 1.0 / powf(10, fabs(n->getFloatValue("damp", 0.0)));
74   SGPropertyNode *p = ((SGPropertyNode *)n)->getNode("property", false);
75   if (p) {
76     const char *path = p->getStringValue();
77     if (path && path[0]) {
78       _property = fgGetNode(path, true);
79       _valid = true;
80     }
81   }
82 }
83 
HUD()84 HUD::HUD() :
85     _currentPath(fgGetNode("/sim/hud/current-path", true)),
86     _currentColor(fgGetNode("/sim/hud/current-color", true)),
87     _visibility(fgGetNode("/sim/hud/visibility[1]", true)),
88     _3DenabledN(fgGetNode("/sim/hud/enable3d[1]", true)),
89     _antialiasing(fgGetNode("/sim/hud/color/antialiased", true)),
90     _transparency(fgGetNode("/sim/hud/color/transparent", true)),
91     _red(fgGetNode("/sim/hud/color/red", true)),
92     _green(fgGetNode("/sim/hud/color/green", true)),
93     _blue(fgGetNode("/sim/hud/color/blue", true)),
94     _alpha(fgGetNode("/sim/hud/color/alpha", true)),
95     _alpha_clamp(fgGetNode("/sim/hud/color/alpha-clamp", true)),
96     _brightness(fgGetNode("/sim/hud/color/brightness", true)),
97     _visible(false),
98     _loaded(false),
99     _antialiased(false),
100     _transparent(false),
101     _a(0.67),									// FIXME better names
102     _cl(0.01),
103     //
104     _scr_widthN(fgGetNode("/sim/startup/xsize", true)),
105     _scr_heightN(fgGetNode("/sim/startup/ysize", true)),
106     _unitsN(fgGetNode("/sim/startup/units", true)),
107     _timer(0.0),
108     //
109     _font_renderer(new fntRenderer()),
110     _font(0),
111     _font_size(0.0),
112     _style(0),
113     _listener_active(false),
114     _clip_box(0)
115 {
116     SG_LOG(SG_COCKPIT, SG_INFO, "Initializing HUD Instrument");
117 
118     SGPropertyNode* hud = fgGetNode("/sim/hud");
119     hud->addChangeListener(this);
120 }
121 
122 
~HUD()123 HUD::~HUD()
124 {
125     SGPropertyNode* hud = fgGetNode("/sim/hud");
126     hud->removeChangeListener(this);
127 
128     deinit();
129 }
130 
131 
init()132 void HUD::init()
133 {
134     std::string fontName;
135     if (!_font) {
136         fontName = fgGetString("/sim/hud/font/name", "Helvetica.txf");
137         _font = FGFontCache::instance()->getTexFont(fontName);
138     }
139     if (!_font)
140         throw sg_io_exception("/sim/hud/font/name is not a texture font",
141                               sg_location(fontName));
142 
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);
147     _loaded = false;
148 
149     currentColorChanged();
150     _currentPath->fireValueChanged();
151 }
152 
deinit()153 void HUD::deinit()
154 {
155   deque<Item *>::const_iterator it, end = _items.end();
156     for (it = _items.begin(); it != end; ++it)
157         delete *it;
158     end = _ladders.end();
159     for (it = _ladders.begin(); it != end; ++it)
160         delete *it;
161 
162   _items.clear();
163   _ladders.clear();
164 
165   delete _clip_box;
166   _clip_box = NULL;
167 
168   _loaded = false;
169 }
170 
reinit()171 void HUD::reinit()
172 {
173     deinit();
174     _currentPath->fireValueChanged();
175 }
176 
update(double dt)177 void HUD::update(double dt)
178 {
179     _timer += dt;
180 }
181 
182 
draw(osg::State &)183 void HUD::draw(osg::State&)
184 {
185     if (!isVisible())
186         return;
187 
188     if (_items.empty() && _ladders.empty())
189         return;
190 
191     if (is3D()) {
192         draw3D();
193         return;
194     }
195 
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);
203 
204     } else {
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);
208     }
209 
210     glViewport(0, 0, _scr_width, _scr_height);
211 }
212 
213 
draw3D()214 void HUD::draw3D()
215 {
216     using namespace osg;
217     flightgear::View* view = globals->get_current_view();
218 
219     // Standard fgfs projection, with essentially meaningless clip
220     // planes (we'll map the whole HUD plane to z=-1)
221     glMatrixMode(GL_PROJECTION);
222     glPushMatrix();
223     Matrixf proj
224         = Matrixf::perspective(view->get_v_fov(), 1/view->get_aspect_ratio(),
225                                0.1, 10);
226     glLoadMatrix(proj.ptr());
227 
228     glMatrixMode(GL_MODELVIEW);
229     glPushMatrix();
230 
231     // Standard fgfs view direction computation
232     Vec3f lookat;
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());
241 
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
247     float m[16];
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;
252     glMultMatrixf(m);
253 
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);
258 
259     common_draw();
260 
261     glMatrixMode(GL_PROJECTION);
262     glPopMatrix();
263     glMatrixMode(GL_MODELVIEW);
264     glPopMatrix();
265 }
266 
267 
draw2D(GLfloat x_start,GLfloat y_start,GLfloat x_end,GLfloat y_end)268 void HUD::draw2D(GLfloat x_start, GLfloat y_start, GLfloat x_end, GLfloat y_end)
269 {
270     using namespace osg;
271     glMatrixMode(GL_PROJECTION);
272     glPushMatrix();
273     Matrixf proj = Matrixf::ortho2D(x_start, x_end, y_start, y_end);
274     glLoadMatrix(proj.ptr());
275 
276     glMatrixMode(GL_MODELVIEW);
277     glPushMatrix();
278     glLoadIdentity();
279 
280     common_draw();
281 
282     glMatrixMode(GL_PROJECTION);
283     glPopMatrix();
284     glMatrixMode(GL_MODELVIEW);
285     glPopMatrix();
286 }
287 
288 
common_draw()289 void HUD::common_draw()
290 {
291     _text_list.erase();
292     _line_list.erase();
293     _stipple_line_list.erase();
294 
295     glDisable(GL_DEPTH_TEST);
296     glDisable(GL_LIGHTING);
297 
298     glEnable(GL_BLEND);
299     if (isTransparent())
300         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
301     else
302         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
303 
304     if (isAntialiased()) {
305         glEnable(GL_LINE_SMOOTH);
306         glAlphaFunc(GL_GREATER, alphaClamp());
307         glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
308         //glLineWidth(1.5);
309     } else {
310         //glLineWidth(1.0);
311     }
312 
313     setColor();
314     _clip_box->set();
315 
316     deque<Item *>::const_iterator it, end = _items.end();
317     for (it = _items.begin(); it != end; ++it)
318         if ((*it)->isEnabled())
319             (*it)->draw();
320 
321     _text_list.draw();
322     _line_list.draw();
323 
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);
329     }
330 
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())
335             (*it)->draw();
336 
337     _clip_box->unset();
338 
339     if (isAntialiased()) {
340         glDisable(GL_ALPHA_TEST);
341         glDisable(GL_LINE_SMOOTH);
342         //glLineWidth(1.0);
343     }
344 
345     if (isTransparent())
346         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
347 
348     glEnable(GL_DEPTH_TEST);
349     glEnable(GL_LIGHTING);
350 }
351 
352 
load(const char * file,float x,float y,int level,const string & indent)353 int HUD::load(const char *file, float x, float y, int level, const string& indent)
354 {
355     const sgDebugPriority TREE = SG_INFO;
356     const int MAXNEST = 10;
357 
358     SGPath path(globals->resolve_maybe_aircraft_path(file));
359     if (path.isNull())
360     {
361         SG_LOG(SG_INPUT, SG_ALERT, "HUD: Cannot find configuration file '" << file << "'.");
362         return 0x2;
363     }
364 
365     if (!level) {
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");
371         return 0x1;
372     } else if (!file || !file[0]) {
373         SG_LOG(SG_INPUT, SG_ALERT, "HUD: invalid filename ");
374         return 0x2;
375     }
376 
377     int ret = 0;
378     sg_ifstream input(path);
379     if (!input.good()) {
380         SG_LOG(SG_INPUT, SG_ALERT, "HUD: Cannot read configuration from '" << path << "'");
381         return 0x4;
382     }
383 
384     SGPropertyNode root;
385     try {
386         readProperties(input, &root);
387     } catch (const sg_exception &e) {
388         input.close();
389         guiErrorMessage("HUD: Error ", e);
390         return 0x8;
391     }
392 
393     delete _clip_box;
394     _clip_box = new ClipBox(fgGetNode("/sim/hud/clipping"), x, y);
395 
396     for (int i = 0; i < root.nChildren(); i++) {
397         SGPropertyNode *n = root.getChild(i);
398         const char *d = n->getStringValue("name", 0);
399         string desc;
400         if (d)
401             desc = string(": \"") + d + '"';
402 
403         const char *name = n->getName();
404         if (!strcmp(name, "name")) {
405             continue;
406 
407         } else if (!strcmp(name, "enable3d")) {
408             // set in the tree so that valueChanged() picks it up
409             _3DenabledN->setBoolValue(n->getBoolValue());
410             continue;
411 
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);
416 
417             SG_LOG(SG_INPUT, TREE, indent << "|__import " << fn << desc);
418 
419             string ind = indent + string(i + 1 < root.nChildren() ? "|    " : "     ");
420             ret |= load(fn, x + xoffs, y + yoffs, level + 1, ind);
421             continue;
422         }
423 
424         SG_LOG(SG_INPUT, TREE, indent << "|__" << name << desc);
425 
426         Item *item;
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);
440             continue;
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));
445         } else {
446             SG_LOG(SG_INPUT, TREE, indent << "      \\...unsupported!");
447             continue;
448         }
449         _items.insert(_items.begin(), item);
450     }
451     input.close();
452     SG_LOG(SG_INPUT, TREE, indent);
453     return ret;
454 }
455 
456 
valueChanged(SGPropertyNode * node)457 void HUD::valueChanged(SGPropertyNode *node)
458 {
459     if (_listener_active)
460         return;
461     _listener_active = true;
462 
463     bool loadNow = false;
464     _visible = _visibility->getBoolValue();
465     if (_visible && !_loaded) {
466       loadNow = true;
467     }
468 
469     if (!strcmp(node->getName(), "current-path") && _visible) {
470       loadNow = true;
471     }
472 
473     if (loadNow) {
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);
480       }
481 
482       _loaded = true;
483       load(path.c_str());
484     }
485 
486     if (!strcmp(node->getName(), "current-color")) {
487         currentColorChanged();
488     }
489 
490     _scr_width = _scr_widthN->getIntValue();
491     _scr_height = _scr_heightN->getIntValue();
492 
493 
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());
503 
504     _units = strcmp(_unitsN->getStringValue(), "feet") ? METER : FEET;
505     _listener_active = false;
506 }
507 
currentColorChanged()508 void HUD::currentColorChanged()
509 {
510   SGPropertyNode *n = fgGetNode("/sim/hud/palette", true);
511   int index = _currentColor->getIntValue();
512   if (index < 0) {
513     index = 0;
514   }
515 
516   n = n->getChild("color", index, false);
517   if (!n) {
518     return;
519   }
520 
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));
537 }
538 
setColor() const539 void HUD::setColor() const
540 {
541     if (_antialiased)
542         glColor4f(_r, _g, _b, _a);
543     else
544         glColor3f(_r, _g, _b);
545 }
546 
547 
textAlign(fntRenderer * rend,const char * s,int align,float * x,float * y,float * l,float * r,float * b,float * t)548 void HUD::textAlign(fntRenderer *rend, const char *s, int align,
549         float *x, float *y, float *l, float *r, float *b, float *t)
550 {
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);
555 
556     if (align & HUD::HCENTER)
557         *x -= left - gap + (right - left - gap) / 2.0;
558     else if (align & HUD::RIGHT)
559         *x -= right;
560     else if (align & HUD::LEFT)
561         *x -= left;
562 
563     if (align & HUD::VCENTER)
564         *y -= bot + (top - bot) / 2.0;
565     else if (align & HUD::TOP)
566         *y -= top;
567     else if (align & HUD::BOTTOM)
568         *y -= bot;
569 
570     *l = *x + left;
571     *r = *x + right;
572     *b = *y + bot;
573     *t = *y + top;
574 }
575 
576 
577 
578 
579 // HUDText -- text container for TextList vector
580 
581 
HUDText(fntRenderer * fnt,float x,float y,const char * s,int align,int d)582 HUDText::HUDText(fntRenderer *fnt, float x, float y, const char *s, int align, int d) :
583     _fnt(fnt),
584     _x(x),
585     _y(y),
586     _digits(d)
587 {
588     strncpy(_msg, s, BUFSIZE);
589     if (!align || !s[0])
590         return;
591     float ign;
592     HUD::textAlign(fnt, s, align, &_x, &_y, &ign, &ign, &ign, &ign);
593 }
594 
595 
draw()596 void HUDText::draw()
597 {
598     if (!_digits) { // show all digits in same size
599         _fnt->start2f(_x, _y);
600         _fnt->puts(_msg);
601         return;
602     }
603 
604     // FIXME
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.
608 
609     int c = 0, i = 0;
610     char *t = _msg;
611     int p = 4;
612 
613     if (t[0] == '-') {
614         //if negative value then increase the c and p values
615         //for '-' sign.
616         c++; // was moved to the comment. Unintentionally?   TODO
617         p++;
618     }
619     char *tmp = _msg;
620     while (tmp[i] != '\0') {
621         if ((tmp[i] >= '0') && (tmp[i] <= '9'))
622             c++;
623         i++;
624     }
625 
626     float orig_size = _fnt->getPointSize();
627     if (c > p) {
628         _fnt->setPointSize(orig_size * 0.8);
629         int p1 = c - 3;
630         char *tmp1 = _msg + p1;
631         int p2 = p1 * 8;
632 
633         _fnt->start2f(_x + p2, _y);
634         _fnt->puts(tmp1);
635 
636         _fnt->setPointSize(orig_size * 1.2);
637         char tmp2[BUFSIZE];
638         strncpy(tmp2, _msg, p1);
639         tmp2[p1] = '\0';
640 
641         _fnt->start2f(_x, _y);
642         _fnt->puts(tmp2);
643     } else {
644         _fnt->setPointSize(orig_size * 1.2);
645         _fnt->start2f(_x, _y);
646         _fnt->puts(tmp);
647     }
648     _fnt->setPointSize(orig_size);
649 }
650 
651 
652 // Register the subsystem.
653 SGSubsystemMgr::Registrant<HUD> registrantHUD;
654 
655 
align(const char * s,int align,float * x,float * y,float * l,float * r,float * b,float * t) const656 void TextList::align(const char *s, int align, float *x, float *y,
657         float *l, float *r, float *b, float *t) const
658 {
659     HUD::textAlign(_font, s, align, x, y, l, r, b, t);
660 }
661 
662 
draw()663 void TextList::draw()
664 {
665     assert(_font);
666 
667     // FIXME
668     glPushAttrib(GL_COLOR_BUFFER_BIT);
669     glEnable(GL_BLEND);
670 
671     _font->begin();
672     vector<HUDText>::iterator it, end = _list.end();
673     for (it = _list.begin(); it != end; ++it)
674         it->draw();
675     _font->end();
676 
677     glDisable(GL_TEXTURE_2D);
678     glPopAttrib();
679 }
680 
681 
ClipBox(const SGPropertyNode * n,float xoffset,float yoffset)682 ClipBox::ClipBox(const SGPropertyNode *n, float xoffset, float yoffset) :
683     _active(false),
684     _xoffs(xoffset),
685     _yoffs(yoffset)
686 {
687     if (!n)
688         return;
689 
690     // const_cast is necessary because ATM there's no matching getChild(const ...)
691     // prototype and getNode(const ..., <bool>) is wrongly interpreted as
692     // getNode(const ..., <int>)
693     _top_node = (const_cast<SGPropertyNode *>(n))->getChild("top", 0, true);
694     _bot_node = (const_cast<SGPropertyNode *>(n))->getChild("bottom", 0, true);
695     _left_node = (const_cast<SGPropertyNode *>(n))->getChild("left", 0, true);
696     _right_node = (const_cast<SGPropertyNode *>(n))->getChild("right", 0, true);
697 
698     _left[0] = 1.0, _left[1] = _left[2] = 0.0;
699     _right[0] = -1.0, _right[1] = _right[2] = 0.0;
700     _top[0] = 0.0, _top[1] = -1.0, _top[2] = 0.0;
701     _bot[0] = 0.0, _bot[1] = 1.0, _bot[2] = 0.0;
702     _active = true;
703 }
704 
705 
set()706 void ClipBox::set()
707 {
708     if (!_active)
709         return;
710 
711     _left[3] = -_left_node->getDoubleValue() - _xoffs;
712     _right[3] = _right_node->getDoubleValue() + _xoffs;
713     _bot[3] = -_bot_node->getDoubleValue() - _yoffs;
714     _top[3] = _top_node->getDoubleValue() + _yoffs;
715 
716     glClipPlane(GL_CLIP_PLANE0, _top);
717     glEnable(GL_CLIP_PLANE0);
718     glClipPlane(GL_CLIP_PLANE1, _bot);
719     glEnable(GL_CLIP_PLANE1);
720     glClipPlane(GL_CLIP_PLANE2, _left);
721     glEnable(GL_CLIP_PLANE2);
722     glClipPlane(GL_CLIP_PLANE3, _right);
723     glEnable(GL_CLIP_PLANE3);
724 }
725 
726 
unset()727 void ClipBox::unset()
728 {
729     if (_active) {
730         glDisable(GL_CLIP_PLANE0);
731         glDisable(GL_CLIP_PLANE1);
732         glDisable(GL_CLIP_PLANE2);
733         glDisable(GL_CLIP_PLANE3);
734     }
735 }
736