]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.hxx
Merge branch 'maint' into next
[flightgear.git] / src / Main / viewer.hxx
1 // viewer.hxx -- class for managing a viewer in the flightgear world.
2 //
3 // Written by Curtis Olson, started August 1997.
4 //                          overhaul started October 2000.
5 //   partially rewritten by Jim Wilson jim@kelcomaine.com using interface
6 //                          by David Megginson March 2002
7 //
8 // Copyright (C) 1997 - 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26
27 #ifndef _VIEWER_HXX
28 #define _VIEWER_HXX
29
30
31 #ifndef __cplusplus                                                          
32 # error This library requires C++
33 #endif                                   
34
35 namespace flightgear
36 {
37 class CameraGroup;
38 }
39
40 #include <osg/ref_ptr>
41
42 #include <simgear/compiler.h>
43 #include <simgear/constants.h>
44 #include <simgear/structure/subsystem_mgr.hxx>
45 #include <simgear/scene/model/location.hxx>
46
47 #include <plib/sg.h>            // plib include
48
49 #define FG_FOV_MIN 0.1
50 #define FG_FOV_MAX 179.9
51
52 enum fgViewType {
53  FG_LOOKFROM = 0,
54  FG_LOOKAT = 1
55 };
56
57 // Define a structure containing view information
58 class FGViewer : public SGSubsystem {
59
60 public:
61
62     enum fgScalingType {  // nominal Field Of View actually applies to ...
63         FG_SCALING_WIDTH,       // window width
64         FG_SCALING_MAX          // max(width, height)
65         // FG_SCALING_G_MEAN,      // geometric_mean(width, height)
66         // FG_SCALING_INDEPENDENT  // whole screen
67     };
68
69     // Constructor
70     FGViewer( fgViewType Type, bool from_model, int from_model_index,
71               bool at_model, int at_model_index,
72               double damp_roll, double damp_pitch, double damp_heading,
73               double x_offset_m, double y_offset_m, double z_offset_m,
74               double heading_offset_deg, double pitch_offset_deg,
75               double roll_offset_deg,
76               double fov_deg, double aspect_ratio_multiplier,
77               double target_x_offset_m, double target_y_offset_m,
78               double target_z_offset_m, double near_m, bool internal );
79
80     // Destructor
81     virtual ~FGViewer( void );
82
83     //////////////////////////////////////////////////////////////////////
84     // Part 1: standard SGSubsystem implementation.
85     //////////////////////////////////////////////////////////////////////
86
87     virtual void init ();
88     virtual void bind ();
89     virtual void unbind ();
90     void update (double dt);
91
92
93     //////////////////////////////////////////////////////////////////////
94     // Part 2: user settings.
95     //////////////////////////////////////////////////////////////////////
96
97     virtual fgViewType getType() const { return _type; }
98     virtual void setType( int type );
99
100     virtual bool getInternal() const { return _internal; }
101     virtual void setInternal( bool internal );
102
103     // Reference geodetic position of view from position...
104     //   These are the actual aircraft position (pilot in
105     //   pilot view, model in model view).
106     //   FIXME: the model view position (ie target positions) 
107     //   should be in the model class.
108     virtual double getLongitude_deg () const { return _lon_deg; }
109     virtual double getLatitude_deg () const { return _lat_deg; }
110     virtual double getAltitudeASL_ft () const { return _alt_ft; }
111     virtual void setLongitude_deg (double lon_deg);
112     virtual void setLatitude_deg (double lat_deg);
113     virtual void setAltitude_ft (double alt_ft);
114     virtual void setPosition (double lon_deg, double lat_deg, double alt_ft);
115
116     // Reference geodetic target position...
117     virtual double getTargetLongitude_deg () const { return _target_lon_deg; }
118     virtual double getTargetLatitude_deg () const { return _target_lat_deg; }
119     virtual double getTargetAltitudeASL_ft () const { return _target_alt_ft; }
120     virtual void setTargetLongitude_deg (double lon_deg);
121     virtual void setTargetLatitude_deg (double lat_deg);
122     virtual void setTargetAltitude_ft (double alt_ft);
123     virtual void setTargetPosition (double lon_deg, double lat_deg, double alt_ft);
124
125
126
127
128     // Position offsets from reference
129     //   These offsets position they "eye" in the scene according to a given
130     //   location.  For example in pilot view they are used to position the 
131     //   head inside the aircraft.
132     //   Note that in pilot view these are applied "before" the orientation 
133     //   rotations (see below) so that the orientation rotations have the 
134     //   effect of the pilot staying in his seat and "looking out" in 
135     //   different directions.
136     //   In chase view these are applied "after" the application of the 
137     //   orientation rotations listed below.  This has the effect of the 
138     //   eye moving around and "looking at" the object (model) from 
139     //   different angles.
140     virtual double getXOffset_m () const { return _offset_m.x(); }
141     virtual double getYOffset_m () const { return _offset_m.y(); }
142     virtual double getZOffset_m () const { return _offset_m.z(); }
143     virtual double getTargetXOffset_m () const { return _target_offset_m.x(); }
144     virtual double getTargetYOffset_m () const { return _target_offset_m.y(); }
145     virtual double getTargetZOffset_m () const { return _target_offset_m.z(); }
146     virtual void setXOffset_m (double x_offset_m);
147     virtual void setYOffset_m (double y_offset_m);
148     virtual void setZOffset_m (double z_offset_m);
149     virtual void setTargetXOffset_m (double x_offset_m);
150     virtual void setTargetYOffset_m (double y_offset_m);
151     virtual void setTargetZOffset_m (double z_offset_m);
152     virtual void setPositionOffsets (double x_offset_m,
153                                      double y_offset_m,
154                                      double z_offset_m);
155
156
157
158
159     // Reference orientation rotations...
160     //   These are rotations that represent the plane attitude effect on
161     //   the view (in Pilot view).  IE The view frustrum rotates as the plane
162     //   turns, pitches, and rolls.
163     //   In model view (lookat/chaseview) these end up changing the angle that
164     //   the eye is looking at the ojbect (ie the model).
165     //   FIXME: the FGModel class should have its own version of these so that
166     //   it can generate it's own model rotations.
167     virtual double getRoll_deg () const { return _roll_deg; }
168     virtual double getPitch_deg () const {return _pitch_deg; }
169     virtual double getHeading_deg () const {return _heading_deg; }
170     virtual void setRoll_deg (double roll_deg);
171     virtual void setPitch_deg (double pitch_deg);
172     virtual void setHeading_deg (double heading_deg);
173     virtual void setOrientation (double roll_deg, double pitch_deg, double heading_deg);
174     virtual double getTargetRoll_deg () const { return _target_roll_deg; }
175     virtual double getTargetPitch_deg () const {return _target_pitch_deg; }
176     virtual double getTargetHeading_deg () const {return _target_heading_deg; }
177     virtual void setTargetRoll_deg (double roll_deg);
178     virtual void setTargetPitch_deg (double pitch_deg);
179     virtual void setTargetHeading_deg (double heading_deg);
180     virtual void setTargetOrientation (double roll_deg, double pitch_deg, double heading_deg);
181
182
183
184
185     // Orientation offsets rotations from reference orientation.
186     // Goal settings are for smooth transition from prior 
187     // offset when changing view direction.
188     //   These offsets are in ADDITION to the orientation rotations listed 
189     //   above.
190     //   In pilot view they are applied after the position offsets in order to
191     //   give the effect of the pilot looking around.
192     //   In lookat view they are applied before the position offsets so that
193     //   the effect is the eye moving around looking at the object (ie the model)
194     //   from different angles.
195     virtual double getRollOffset_deg () const { return _roll_offset_deg; }
196     virtual double getPitchOffset_deg () const { return _pitch_offset_deg; }
197     virtual double getHeadingOffset_deg () const { return _heading_offset_deg; }
198     virtual double getGoalRollOffset_deg () const { return _goal_roll_offset_deg; }
199     virtual double getGoalPitchOffset_deg () const { return _goal_pitch_offset_deg; }
200     virtual double getGoalHeadingOffset_deg () const {return _goal_heading_offset_deg; }
201     virtual void setRollOffset_deg (double roll_offset_deg);
202     virtual void setPitchOffset_deg (double pitch_offset_deg);
203     virtual void setHeadingOffset_deg (double heading_offset_deg);
204     virtual void setGoalRollOffset_deg (double goal_roll_offset_deg);
205     virtual void setGoalPitchOffset_deg (double goal_pitch_offset_deg);
206     virtual void setGoalHeadingOffset_deg (double goal_heading_offset_deg);
207     virtual void setOrientationOffsets (double roll_offset_deg,
208                                      double heading_offset_deg,
209                                      double pitch_offset_deg);
210
211
212
213     //////////////////////////////////////////////////////////////////////
214     // Part 3: output vectors and matrices in FlightGear coordinates.
215     //////////////////////////////////////////////////////////////////////
216
217     // Vectors and positions...
218
219     // Get zero elev
220     const SGVec3d& get_zero_elev() {if ( _dirty ) { recalc(); } return _zero_elev; }
221     // Get world up vector
222     const SGVec3f& get_world_up() {if ( _dirty ) { recalc(); } return _world_up; }
223     // Get surface east vector
224     const SGVec3f& get_surface_east() { if ( _dirty ) { recalc(); } return _surface_east; }
225     // Get surface south vector
226     const SGVec3f& get_surface_south() {if ( _dirty ) { recalc(); } return _surface_south; }
227
228     const SGVec3d& get_view_pos() { if ( _dirty ) { recalc(); } return _absolute_view_pos; }
229     const SGVec3d& getViewPosition() { if ( _dirty ) { recalc(); } return _absolute_view_pos; }
230     const SGQuatd& getViewOrientation() { if ( _dirty ) { recalc(); } return mViewOrientation; }
231
232     //////////////////////////////////////////////////////////////////////
233     // Part 4: View and frustrum data setters and getters
234     //////////////////////////////////////////////////////////////////////
235
236     virtual void set_fov( double fov_deg ) {
237         _fov_deg = fov_deg;
238     }
239     virtual double get_fov() const { return _fov_deg; }
240     virtual double get_h_fov();    // Get horizontal fov, in degrees.
241     virtual double get_v_fov();    // Get vertical fov, in degrees.
242
243     virtual void set_aspect_ratio( double r ) {
244         _aspect_ratio = r;
245     }
246     virtual double get_aspect_ratio() const { return _aspect_ratio; }
247
248     virtual void set_aspect_ratio_multiplier( double m ) {
249         _aspect_ratio_multiplier = m;
250     }
251     virtual double get_aspect_ratio_multiplier() const {
252         return _aspect_ratio_multiplier;
253     }
254
255     virtual double getNear_m () const { return _ground_level_nearplane_m; }
256     inline void setNear_m (double near_m) {
257         _ground_level_nearplane_m = near_m;
258     }
259
260     //////////////////////////////////////////////////////////////////////
261     // Part 5: misc setters and getters
262     //////////////////////////////////////////////////////////////////////
263
264     inline void set_dirty() { _dirty = true; }
265     inline void set_clean() { _dirty = false; }
266     
267     // return eye location...
268     virtual SGLocation * getSGLocation () const { return _location; }
269
270
271 private:
272
273     //////////////////////////////////////////////////////////////////
274     // private data                                                 //
275     //////////////////////////////////////////////////////////////////
276
277     // flag forcing a recalc of derived view parameters
278     bool _dirty;
279
280     SGQuatd mViewOrientation;
281     SGVec3d _absolute_view_pos;
282
283     double _lon_deg;
284     double _lat_deg;
285     double _alt_ft;
286     double _target_lon_deg;
287     double _target_lat_deg;
288     double _target_alt_ft;
289
290     double _roll_deg;
291     double _pitch_deg;
292     double _heading_deg;
293     double _target_roll_deg;
294     double _target_pitch_deg;
295     double _target_heading_deg;
296
297     double _damp_sync;
298     double _damp_roll;
299     double _damp_pitch;
300     double _damp_heading;
301
302     double _damped_roll_deg;
303     double _damped_pitch_deg;
304     double _damped_heading_deg;
305
306     // Position offsets from FDM origin.  The X axis is positive
307     // out the tail, Y is out the right wing, and Z is positive up.
308     // distance in meters
309     SGVec3d _offset_m;
310
311     // Target offsets from FDM origin (for "lookat" targets) The X
312     // axis is positive out the tail, Y is out the right wing, and Z
313     // is positive up.  distance in meters
314     SGVec3d _target_offset_m;
315
316
317     // orientation offsets from reference (_goal* are for smoothed transitions)
318     double _roll_offset_deg;
319     double _pitch_offset_deg;
320     double _heading_offset_deg;
321     double _goal_roll_offset_deg;
322     double _goal_pitch_offset_deg;
323     double _goal_heading_offset_deg;
324
325     // used to set nearplane when at ground level for this view
326     double _ground_level_nearplane_m;
327
328     fgViewType _type;
329     fgScalingType _scaling_type;
330
331     // internal view (e.g. cockpit) flag
332     bool _internal;
333
334     // view is looking from a model
335     bool _from_model;
336     int _from_model_index;  // number of model (for multi model)
337
338     // view is looking at a model
339     bool _at_model;
340     int _at_model_index;  // number of model (for multi model)
341
342     SGLocation * _location;
343     SGLocation * _target_location;
344
345     // the nominal field of view (angle, in degrees)
346     double _fov_deg;
347
348     // Ratio of window width and height; height = width *
349     // aspect_ratio.  This value is automatically calculated based on
350     // window dimentions.
351     double _aspect_ratio;
352
353     // default = 1.0, this value is user configurable and is
354     // multiplied into the aspect_ratio to get the actual vertical fov
355     double _aspect_ratio_multiplier;
356
357     // cartesion coordinates of current lon/lat if at sea level
358     SGVec3d _zero_elev;
359
360     // surface vector heading south
361     SGVec3f _surface_south;
362
363     // surface vector heading east (used to unambiguously align sky
364     // with sun)
365     SGVec3f _surface_east;
366
367     // world up vector (normal to the plane tangent to the earth's
368     // surface at the spot we are directly above
369     SGVec3f _world_up;
370
371     // camera group controled by this view
372     osg::ref_ptr<flightgear::CameraGroup> _cameraGroup;
373     //////////////////////////////////////////////////////////////////
374     // private functions                                            //
375     //////////////////////////////////////////////////////////////////
376
377     void recalc ();
378     void recalcLookFrom();
379     void recalcLookAt();
380     void dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg);
381
382     // add to _heading_offset_deg
383     inline void incHeadingOffset_deg( double amt ) {
384         set_dirty();
385         _heading_offset_deg += amt;
386     }
387
388     // add to _pitch_offset_deg
389     inline void incPitchOffset_deg( double amt ) {
390         set_dirty();
391         _pitch_offset_deg += amt;
392     }
393
394     // add to _roll_offset_deg
395     inline void incRollOffset_deg( double amt ) {
396         set_dirty();
397         _roll_offset_deg += amt;
398     }
399
400 };
401
402
403 #endif // _VIEWER_HXX