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