]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/HUD/HUD.hxx
- remove the SG_GLxxxx_H #defines, since OSG provides its own versions
[flightgear.git] / src / Instrumentation / HUD / HUD.hxx
1 // HUD.hxx -- 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 #ifndef _HUD_HXX
23 #define _HUD_HXX
24
25 #include <simgear/compiler.h>
26 #include <simgear/props/condition.hxx>
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include <vector>
33 #include <deque>
34 #include <fstream>
35
36 using std::deque;
37 using std::vector;
38
39 #include <osg/State>
40
41 #include <plib/sg.h>
42
43 #include <simgear/math/SGLimits.hxx>
44 #include <simgear/constants.h>
45 #include <simgear/structure/subsystem_mgr.hxx>
46
47 #include <Airports/runways.hxx>     // FGRunway
48 #include <GUI/gui.h>                // fntRenderer ?   guiErrorMessage()
49 #include <GUI/new_gui.hxx>          // FGFontCache, FGColor
50 #include <Include/fg_typedefs.h>
51 #include <Main/fg_props.hxx>
52
53
54 class FGViewer;
55
56
57 class LineSegment {
58 public:
59     LineSegment(GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1)
60         : _x0(x0), _y0(y0), _x1(x1), _y1(y1) {}
61
62     void draw() const {
63         glVertex2f(_x0, _y0);
64         glVertex2f(_x1, _y1);
65     }
66
67 private:
68     GLfloat _x0, _y0, _x1, _y1;
69 };
70
71
72
73 class LineList {
74 public:
75     void add(const LineSegment& seg) { _list.push_back(seg); }
76     void erase() { _list.erase(_list.begin(), _list.end()); }
77     inline unsigned int size() const { return _list.size(); }
78     void draw() {
79         glBegin(GL_LINES);
80         vector<LineSegment>::const_iterator it, end = _list.end();
81         for (it = _list.begin(); it != end; ++it)
82             it->draw();
83         glEnd();
84     }
85
86 private:
87     vector<LineSegment> _list;
88 };
89
90
91
92 class HUDText {
93 public:
94     HUDText(fntRenderer *f, float x, float y, const char *s, int align = 0, int digits = 0);
95     void draw();
96
97 private:
98     fntRenderer *_fnt;
99     float _x, _y;
100     int _digits;
101     static const int BUFSIZE = 64;
102     char _msg[BUFSIZE];
103 };
104
105
106
107 class TextList {
108 public:
109     TextList() { _font = 0; }
110
111     void setFont(fntRenderer *Renderer) { _font = Renderer; }
112     void add(float x, float y, const char *s, int align = 0, int digit = 0) {
113         _list.push_back(HUDText(_font, x, y, s, align, digit));
114     }
115     void erase() { _list.erase(_list.begin(), _list.end()); }
116     void align(const char *s, int align, float *x, float *y,
117             float *l, float *r, float *b, float *t) const;
118     void draw();
119
120 private:
121     fntRenderer *_font;
122     vector<HUDText> _list;
123 };
124
125
126
127
128
129
130 class HUD : public SGSubsystem, public SGPropertyChangeListener {
131 public:
132     HUD();
133     ~HUD();
134     void init();
135     void update(double);
136
137     // called from Main/renderer.cxx to draw 2D and 3D HUD
138     void draw(osg::State&);
139
140     // listener callback to read various HUD related properties
141     void valueChanged(SGPropertyNode *);
142     // set current glColor
143     void setColor() const;
144     inline bool isVisible() const { return _visible; }
145     inline bool isAntialiased() const { return _antialiased; }
146     inline bool isTransparent() const { return _transparent; }
147     inline bool is3D() const { return _3Denabled; }
148     inline float alphaClamp() const { return _cl; }
149     inline double timer() const { return _timer; }
150     static void textAlign(fntRenderer *rend, const char *s, int align, float *x, float *y,
151             float *l, float *r, float *b, float *t);
152
153     enum Units { FEET, METER };
154     Units getUnits() const { return _units; }
155
156     enum {
157         HORIZONTAL = 0x0000,  // keep that at zero?
158         VERTICAL   = 0x0001,
159         TOP        = 0x0002,
160         BOTTOM     = 0x0004,
161         LEFT       = 0x0008,
162         RIGHT      = 0x0010,
163         NOTICKS    = 0x0020,
164         NOTEXT     = 0x0040,
165         BOTH       = (LEFT|RIGHT),
166
167         // for alignment (with LEFT, RIGHT, TOP, BOTTOM)
168         HCENTER    = 0x0080,
169         VCENTER    = 0x0100,
170         CENTER     = (HCENTER|VCENTER),
171     };
172
173 protected:
174     void common_draw();
175     int load(const char *, float x = 320.0f, float y = 240.0f,
176             int level = 0, const std::string& indent = "");
177
178 private:
179     void draw3D();
180     void draw2D(GLfloat, GLfloat, GLfloat, GLfloat);
181
182     class Input;
183     class Item;
184     class Label;
185     class Scale;
186     class Gauge;
187     class Tape;
188     class Dial;
189     class TurnBankIndicator;
190     class Ladder;
191     class Runway;
192     class AimingReticle;
193
194     deque<Item *> _items;
195
196     SGPropertyNode_ptr _current;
197     SGPropertyNode_ptr _visibility;
198     SGPropertyNode_ptr _3DenabledN;
199     SGPropertyNode_ptr _antialiasing;
200     SGPropertyNode_ptr _transparency;
201     SGPropertyNode_ptr _red, _green, _blue, _alpha;
202     SGPropertyNode_ptr _alpha_clamp;
203     SGPropertyNode_ptr _brightness;
204     bool _visible;
205     bool _3Denabled;
206     bool _antialiased;
207     bool _transparent;
208     float _r, _g, _b, _a, _cl;
209
210     SGPropertyNode_ptr _scr_widthN, _scr_heightN;
211     int _scr_width, _scr_height;
212     SGPropertyNode_ptr _unitsN;
213     Units _units;
214     double _timer;
215
216     fntRenderer *_font_renderer;
217     FGFontCache *_font_cache;
218     fntTexFont *_font;
219     float _font_size;
220     int _style;
221
222     TextList _text_list;
223     LineList _line_list;
224     LineList _stipple_line_list;
225 };
226
227
228
229 class HUD::Input {
230 public:
231     Input(const SGPropertyNode *n, float factor = 1.0, float offset = 0.0,
232             float min = -SGLimitsf::max(), float max = SGLimitsf::max()) :
233         _valid(false),
234         _property(0),
235         _damped(SGLimitsf::max())
236     {
237         if (!n)
238             return;
239         _factor = n->getFloatValue("factor", factor);
240         _offset = n->getFloatValue("offset", offset);
241         _min = n->getFloatValue("min", min);
242         _max = n->getFloatValue("max", max);
243         _coeff = 1.0 - 1.0 / powf(10, fabsf(n->getFloatValue("damp", 0.0)));
244         SGPropertyNode *p = ((SGPropertyNode *)n)->getNode("property", false);
245         if (p) {
246             const char *path = p->getStringValue();
247             if (path && path[0]) {
248                 _property = fgGetNode(path, true);
249                 _valid = true;
250             }
251         }
252     }
253
254     bool getBoolValue() const {
255         assert(_property);
256         return _property->getBoolValue();
257     }
258
259     const char *getStringValue() const {
260         assert(_property);
261         return _property->getStringValue();
262     }
263
264     float getFloatValue() {
265         assert(_property);
266         float f = _property->getFloatValue() * _factor + _offset;
267         if (_damped == SGLimitsf::max())
268             _damped = f;
269         if (_coeff > 0.0f)
270             f = _damped = f * (1.0f - _coeff) + _damped * _coeff;
271         return clamp(f);
272     }
273
274     inline float isValid() const { return _valid; }
275     inline float min() const { return _min; }
276     inline float max() const { return _max; }
277     inline float factor() const { return _factor; }
278     float clamp(float v) const { return v < _min ? _min : v > _max ? _max : v; }
279
280     void set_min(float m, bool force = true) {
281         if (force || _min == -SGLimitsf::max())
282             _min = m;
283     }
284     void set_max(float m, bool force = true) {
285         if (force || _max == SGLimitsf::max())
286             _max = m;
287     }
288
289 private:
290     bool _valid;
291     SGConstPropertyNode_ptr _property;
292     float _factor;
293     float _offset;
294     float _min;
295     float _max;
296     float _coeff;
297     float _damped;
298 };
299
300
301
302 class HUD::Item {
303 public:
304     Item(HUD *parent, const SGPropertyNode *, float x = 0.0f, float y = 0.0f);
305     virtual ~Item () {}
306     virtual void draw() = 0;
307     virtual bool isEnabled();
308
309 protected:
310     enum Format {
311         INVALID,
312         NONE,
313         INT,
314         LONG,
315         FLOAT,
316         DOUBLE,
317         STRING,
318     };
319
320     Format  check_format(const char *) const;
321     inline float get_span()       const { return _scr_span; }
322     inline int   get_digits()     const { return _digits; }
323
324     inline bool option_vert()    const { return (_options & VERTICAL) == VERTICAL; }
325     inline bool option_left()    const { return (_options & LEFT) == LEFT; }
326     inline bool option_right()   const { return (_options & RIGHT) == RIGHT; }
327     inline bool option_both()    const { return (_options & BOTH) == BOTH; }
328     inline bool option_noticks() const { return (_options & NOTICKS) == NOTICKS; }
329     inline bool option_notext()  const { return (_options & NOTEXT) == NOTEXT; }
330     inline bool option_top()     const { return (_options & TOP) == TOP; }
331     inline bool option_bottom()  const { return (_options & BOTTOM) == BOTTOM; }
332
333     void draw_line(float x1, float y1, float x2, float y2);
334     void draw_stipple_line(float x1, float y1, float x2, float y2);
335     void draw_text(float x, float y, char *msg, int align = 0, int digit = 0);
336     void draw_circle(float x1, float y1, float r) const;
337     void draw_bullet(float, float, float);
338
339     HUD         *_hud;
340     std::string      _name;
341     int         _options;
342     float       _x, _y, _w, _h;
343     float       _center_x, _center_y;
344
345 private:
346     SGSharedPtr<SGCondition> _condition;
347     float       _disp_factor;   // Multiply by to get numbers shown on scale.
348     float       _scr_span;      // Working values for draw;
349     int         _digits;
350 };
351
352
353
354 class HUD::Label : public Item {
355 public:
356     Label(HUD *parent, const SGPropertyNode *, float x, float y);
357     virtual void draw();
358
359 private:
360     bool    blink();
361
362     Input   _input;
363     Format  _mode;
364     std::string  _format;
365     int     _halign;    // HUDText alignment
366     int     _blink;
367     bool    _box;
368     float   _text_y;
369     float   _pointer_width;
370     float   _pointer_length;
371
372     SGSharedPtr<SGCondition> _blink_condition;
373     double  _blink_interval;
374     double  _blink_target;  // time for next blink state change
375     bool    _blink_state;
376 };
377
378
379
380 // abstract base class for both moving scale and moving needle (fixed scale)
381 // indicators.
382 //
383 class HUD::Scale : public Item {
384 public:
385     Scale(HUD *parent, const SGPropertyNode *, float x, float y);
386     virtual void draw    ( void ) {}  // No-op here. Defined in derived classes.
387
388 protected:
389     inline float factor() const { return _display_factor; }
390     inline float range_to_show() const { return _range_shown; }
391
392     Input _input;
393     float _major_divs;      // major division marker units
394     float _minor_divs;      // minor division marker units
395     unsigned int _modulo;   // Roll over point
396
397 private:
398     float _range_shown;     // Width Units.
399     float _display_factor;  // factor => screen units/range values.
400 };
401
402
403 class HUD::Gauge : public Scale {
404 public:
405     Gauge(HUD *parent, const SGPropertyNode *, float x, float y);
406     virtual void draw();
407 };
408
409
410
411 // displays the indicated quantity on a scale that moves past the
412 // pointer. It may be horizontal or vertical.
413 //
414 class HUD::Tape : public Scale {
415 public:
416     Tape(HUD *parent, const SGPropertyNode *, float x, float y);
417     virtual void draw();
418
419 protected:
420     void draw_vertical(float);
421     void draw_horizontal(float);
422     void draw_fixed_pointer(float, float, float, float, float, float);
423     char *format_value(float);
424
425 private:
426     float  _val_span;
427     float  _half_width_units;
428     bool   _draw_tick_bottom;
429     bool   _draw_tick_top;
430     bool   _draw_tick_right;
431     bool   _draw_tick_left;
432     bool   _draw_cap_bottom;
433     bool   _draw_cap_top;
434     bool   _draw_cap_right;
435     bool   _draw_cap_left;
436     float  _marker_offset;
437     float  _label_gap;
438     bool   _pointer;
439     Format _label_fmt;
440     std::string _format;
441     int    _div_ratio;          // _major_divs/_minor_divs
442     bool   _odd_type;           // whether to put numbers at 0/2/4 or 1/3/5
443
444     enum { BUFSIZE = 64 };
445     char   _buf[BUFSIZE];
446
447     enum PointerType { FIXED, MOVING } _pointer_type;
448     enum TickType { LINE, CIRCLE } _tick_type;
449     enum TickLength { VARIABLE, CONSTANT } _tick_length;
450 };
451
452
453
454 class HUD::Dial : public Scale {
455 public:
456     Dial(HUD *parent, const SGPropertyNode *, float x, float y);
457     virtual void draw();
458
459 private:
460     float  _radius;
461     int    _divisions;
462 };
463
464
465
466 class HUD::TurnBankIndicator : public Item {
467 public:
468     TurnBankIndicator(HUD *parent, const SGPropertyNode *, float x, float y);
469     virtual void draw();
470
471 private:
472     void draw_scale();
473     void draw_tee();
474     void draw_line(float, float, float, float);
475     void draw_tick(float angle, float r1, float r2, int side);
476
477     Input _bank;
478     Input _sideslip;
479
480     float _gap_width;
481     bool  _bank_scale;
482 };
483
484
485
486 class HUD::Ladder : public Item {
487 public:
488     Ladder(HUD *parent, const SGPropertyNode *, float x, float y);
489     virtual void draw();
490
491 private:
492     void draw_zenith(float, float);
493     void draw_nadir(float, float);
494
495     void draw_text(float x, float y, char *s, int align = 0) {
496         _locTextList.add(x, y, s, align, 0);
497     }
498
499     void draw_line(float x1, float y1, float x2, float y2) {
500         _locLineList.add(LineSegment(x1, y1, x2, y2));
501     }
502
503     void draw_stipple_line(float x1, float y1, float x2, float y2) {
504         _locStippleLineList.add(LineSegment(x1, y1, x2, y2));
505     }
506
507     enum   Type { PITCH, CLIMB_DIVE } _type;
508     Input  _pitch;
509     Input  _roll;
510     float  _width_units;
511     int    _div_units;
512     unsigned int _scr_hole;
513     float  _vmax;
514     float  _vmin;
515     float  _compression;
516     bool   _dynamic_origin;
517     bool   _clip_plane;
518     bool   _frl;               // fuselage reference line
519     bool   _target_spot;
520     bool   _target_markers;
521     bool   _velocity_vector;
522     bool   _drift_marker;
523     bool   _alpha_bracket;
524     bool   _energy_marker;
525     bool   _climb_dive_marker;
526     bool   _glide_slope_marker;
527     float  _glide_slope;
528     bool   _energy_worm;
529     bool   _waypoint_marker;
530     bool   _zenith;
531     bool   _nadir;
532     bool   _hat;
533
534     // The Ladder has its own temporary display lists
535     TextList _locTextList;
536     LineList _locLineList;
537     LineList _locStippleLineList;
538 };
539
540
541
542 // responsible for rendering the active runway in the hud (if visible).
543 //
544 class HUD::Runway : public Item {
545 public:
546     Runway(HUD *parent, const SGPropertyNode *, float x, float y);
547     virtual void draw();
548
549 private:
550     void boundPoint(const sgdVec3& v, sgdVec3& m);
551     bool boundOutsidePoints(sgdVec3& v, sgdVec3& m);
552     bool drawLine(const sgdVec3& a1, const sgdVec3& a2, const sgdVec3& p1, const sgdVec3& p2);
553     void drawArrow();
554     bool get_active_runway(FGRunway& rwy);
555     void get_rwy_points(sgdVec3 *points);
556     void setLineWidth();
557
558     SGPropertyNode_ptr _agl;
559     sgdVec3 _points3d[6], _points2d[6];
560     double _mm[16];
561     double _pm[16];
562     double _arrow_scale;  // scales of runway indication arrow
563     double _arrow_radius;
564     double _line_scale;   // maximum line scale
565     double _scale_dist;   // distance where to start scaling the lines
566     double _default_pitch;
567     double _default_heading;
568     GLint  _view[4];
569     FGRunway _runway;
570     FGViewer* _cockpit_view;
571     unsigned short _stipple_out;    // stipple pattern of the outline of the runway
572     unsigned short _stipple_center; // stipple pattern of the center line of the runway
573     bool   _draw_arrow;             // draw arrow when runway is not visible in HUD
574     bool   _draw_arrow_always;      // always draws arrow
575     float  _left, _right, _top, _bottom;
576 };
577
578
579 class HUD::AimingReticle : public Item {
580 public:
581     AimingReticle(HUD *parent, const SGPropertyNode *, float x, float y);
582     virtual void draw();
583
584 private:
585     SGSharedPtr<SGCondition> _active_condition;  // stadiametric (true) or standby (false)
586     Input   _diameter;               // inner/outer radius relation
587     float   _bullet_size;
588     float   _inner_radius;
589 };
590
591
592
593 #endif // _HUD_HXX