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