]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.hxx
proper sound orientation(?) and comment out velocity since it messes with OpenAL...
[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 namespace flightgear
31 {
32 class CameraGroup;
33 }
34
35 #include <osg/ref_ptr>
36
37 #include <simgear/compiler.h>
38 #include <simgear/constants.h>
39 #include <simgear/structure/subsystem_mgr.hxx>
40 #include <simgear/math/SGMath.hxx>
41
42 #define FG_FOV_MIN 0.1
43 #define FG_FOV_MAX 179.9
44
45 enum fgViewType {
46  FG_LOOKFROM = 0,
47  FG_LOOKAT = 1
48 };
49
50 // Define a structure containing view information
51 class FGViewer : public SGSubsystem {
52
53 public:
54
55     enum fgScalingType {  // nominal Field Of View actually applies to ...
56         FG_SCALING_WIDTH,       // window width
57         FG_SCALING_MAX          // max(width, height)
58         // FG_SCALING_G_MEAN,      // geometric_mean(width, height)
59         // FG_SCALING_INDEPENDENT  // whole screen
60     };
61
62     // Constructor
63     FGViewer( fgViewType Type, bool from_model, int from_model_index,
64               bool at_model, int at_model_index,
65               double damp_roll, double damp_pitch, double damp_heading,
66               double x_offset_m, double y_offset_m, double z_offset_m,
67               double heading_offset_deg, double pitch_offset_deg,
68               double roll_offset_deg,
69               double fov_deg, double aspect_ratio_multiplier,
70               double target_x_offset_m, double target_y_offset_m,
71               double target_z_offset_m, double near_m, bool internal );
72
73     // Destructor
74     virtual ~FGViewer( void );
75
76     //////////////////////////////////////////////////////////////////////
77     // Part 1: standard SGSubsystem implementation.
78     //////////////////////////////////////////////////////////////////////
79
80     virtual void init ();
81     virtual void bind ();
82     virtual void unbind ();
83     void update (double dt);
84
85
86     //////////////////////////////////////////////////////////////////////
87     // Part 2: user settings.
88     //////////////////////////////////////////////////////////////////////
89
90     virtual fgViewType getType() const { return _type; }
91     virtual void setType( int type );
92
93     virtual bool getInternal() const { return _internal; }
94     virtual void setInternal( bool internal );
95
96     // Reference geodetic position of view from position...
97     //   These are the actual aircraft position (pilot in
98     //   pilot view, model in model view).
99     //   FIXME: the model view position (ie target positions) 
100     //   should be in the model class.
101     virtual void setPosition (double lon_deg, double lat_deg, double alt_ft);
102     const SGGeod& getPosition() const { return _position; }
103
104     // Reference geodetic target position...
105     virtual void setTargetPosition (double lon_deg, double lat_deg, double alt_ft);
106     const SGGeod& getTargetPosition() const { return _target; }
107
108
109
110     // Position offsets from reference
111     //   These offsets position they "eye" in the scene according to a given
112     //   location.  For example in pilot view they are used to position the 
113     //   head inside the aircraft.
114     //   Note that in pilot view these are applied "before" the orientation 
115     //   rotations (see below) so that the orientation rotations have the 
116     //   effect of the pilot staying in his seat and "looking out" in 
117     //   different directions.
118     //   In chase view these are applied "after" the application of the 
119     //   orientation rotations listed below.  This has the effect of the 
120     //   eye moving around and "looking at" the object (model) from 
121     //   different angles.
122     virtual double getXOffset_m () const { return _offset_m.x(); }
123     virtual double getYOffset_m () const { return _offset_m.y(); }
124     virtual double getZOffset_m () const { return _offset_m.z(); }
125     virtual double getTargetXOffset_m () const { return _target_offset_m.x(); }
126     virtual double getTargetYOffset_m () const { return _target_offset_m.y(); }
127     virtual double getTargetZOffset_m () const { return _target_offset_m.z(); }
128     virtual void setXOffset_m (double x_offset_m);
129     virtual void setYOffset_m (double y_offset_m);
130     virtual void setZOffset_m (double z_offset_m);
131     virtual void setTargetXOffset_m (double x_offset_m);
132     virtual void setTargetYOffset_m (double y_offset_m);
133     virtual void setTargetZOffset_m (double z_offset_m);
134     virtual void setPositionOffsets (double x_offset_m,
135                                      double y_offset_m,
136                                      double z_offset_m);
137
138
139
140
141     // Reference orientation rotations...
142     //   These are rotations that represent the plane attitude effect on
143     //   the view (in Pilot view).  IE The view frustrum rotates as the plane
144     //   turns, pitches, and rolls.
145     //   In model view (lookat/chaseview) these end up changing the angle that
146     //   the eye is looking at the ojbect (ie the model).
147     //   FIXME: the FGModel class should have its own version of these so that
148     //   it can generate it's own model rotations.
149     virtual double getRoll_deg () const { return _roll_deg; }
150     virtual double getPitch_deg () const {return _pitch_deg; }
151     virtual double getHeading_deg () const {return _heading_deg; }
152     virtual void setRoll_deg (double roll_deg);
153     virtual void setPitch_deg (double pitch_deg);
154     virtual void setHeading_deg (double heading_deg);
155     virtual void setOrientation (double roll_deg, double pitch_deg, double heading_deg);
156     virtual double getTargetRoll_deg () const { return _target_roll_deg; }
157     virtual double getTargetPitch_deg () const {return _target_pitch_deg; }
158     virtual double getTargetHeading_deg () const {return _target_heading_deg; }
159     virtual void setTargetRoll_deg (double roll_deg);
160     virtual void setTargetPitch_deg (double pitch_deg);
161     virtual void setTargetHeading_deg (double heading_deg);
162     virtual void setTargetOrientation (double roll_deg, double pitch_deg, double heading_deg);
163
164
165
166
167     // Orientation offsets rotations from reference orientation.
168     // Goal settings are for smooth transition from prior 
169     // offset when changing view direction.
170     //   These offsets are in ADDITION to the orientation rotations listed 
171     //   above.
172     //   In pilot view they are applied after the position offsets in order to
173     //   give the effect of the pilot looking around.
174     //   In lookat view they are applied before the position offsets so that
175     //   the effect is the eye moving around looking at the object (ie the model)
176     //   from different angles.
177     virtual double getRollOffset_deg () const { return _roll_offset_deg; }
178     virtual double getPitchOffset_deg () const { return _pitch_offset_deg; }
179     virtual double getHeadingOffset_deg () const { return _heading_offset_deg; }
180     virtual double getGoalRollOffset_deg () const { return _goal_roll_offset_deg; }
181     virtual double getGoalPitchOffset_deg () const { return _goal_pitch_offset_deg; }
182     virtual double getGoalHeadingOffset_deg () const {return _goal_heading_offset_deg; }
183     virtual void setRollOffset_deg (double roll_offset_deg);
184     virtual void setPitchOffset_deg (double pitch_offset_deg);
185     virtual void setHeadingOffset_deg (double heading_offset_deg);
186     virtual void setGoalRollOffset_deg (double goal_roll_offset_deg);
187     virtual void setGoalPitchOffset_deg (double goal_pitch_offset_deg);
188     virtual void setGoalHeadingOffset_deg (double goal_heading_offset_deg);
189     virtual void setOrientationOffsets (double roll_offset_deg,
190                                      double heading_offset_deg,
191                                      double pitch_offset_deg);
192
193
194
195     //////////////////////////////////////////////////////////////////////
196     // Part 3: output vectors and matrices in FlightGear coordinates.
197     //////////////////////////////////////////////////////////////////////
198
199     // Vectors and positions...
200
201     const SGVec3d& get_view_pos() { if ( _dirty ) { recalc(); } return _absolute_view_pos; }
202     const SGVec3d& getViewPosition() { if ( _dirty ) { recalc(); } return _absolute_view_pos; }
203     const SGQuatd& getViewOrientation() { if ( _dirty ) { recalc(); } return mViewOrientation; }
204     const SGQuatd& getViewOrientationOffset() { if ( _dirty ) { recalc(); } return mViewOffsetOr; }
205
206     //////////////////////////////////////////////////////////////////////
207     // Part 4: View and frustrum data setters and getters
208     //////////////////////////////////////////////////////////////////////
209
210     virtual void set_fov( double fov_deg ) {
211         _fov_deg = fov_deg;
212     }
213     virtual double get_fov() const { return _fov_deg; }
214     virtual double get_h_fov();    // Get horizontal fov, in degrees.
215     virtual double get_v_fov();    // Get vertical fov, in degrees.
216
217     virtual void set_aspect_ratio( double r ) {
218         _aspect_ratio = r;
219     }
220     virtual double get_aspect_ratio() const { return _aspect_ratio; }
221
222     virtual void set_aspect_ratio_multiplier( double m ) {
223         _aspect_ratio_multiplier = m;
224     }
225     virtual double get_aspect_ratio_multiplier() const {
226         return _aspect_ratio_multiplier;
227     }
228
229     virtual double getNear_m () const { return _ground_level_nearplane_m; }
230     inline void setNear_m (double near_m) {
231         _ground_level_nearplane_m = near_m;
232     }
233
234     //////////////////////////////////////////////////////////////////////
235     // Part 5: misc setters and getters
236     //////////////////////////////////////////////////////////////////////
237
238     inline void set_dirty() { _dirty = true; }
239     inline void set_clean() { _dirty = false; }
240     
241 private:
242
243     //////////////////////////////////////////////////////////////////
244     // private data                                                 //
245     //////////////////////////////////////////////////////////////////
246
247     // flag forcing a recalc of derived view parameters
248     bool _dirty;
249
250     SGQuatd mViewOrientation;
251     SGQuatd mViewOffsetOr;
252     SGVec3d _absolute_view_pos;
253
254     SGGeod _position;
255     SGGeod _target;
256
257     double _roll_deg;
258     double _pitch_deg;
259     double _heading_deg;
260     double _target_roll_deg;
261     double _target_pitch_deg;
262     double _target_heading_deg;
263
264     double _damp_sync;
265     double _damp_roll;
266     double _damp_pitch;
267     double _damp_heading;
268
269     double _damped_roll_deg;
270     double _damped_pitch_deg;
271     double _damped_heading_deg;
272
273     // Position offsets from FDM origin.  The X axis is positive
274     // out the tail, Y is out the right wing, and Z is positive up.
275     // distance in meters
276     SGVec3d _offset_m;
277
278     // Target offsets from FDM origin (for "lookat" targets) The X
279     // axis is positive out the tail, Y is out the right wing, and Z
280     // is positive up.  distance in meters
281     SGVec3d _target_offset_m;
282
283
284     // orientation offsets from reference (_goal* are for smoothed transitions)
285     double _roll_offset_deg;
286     double _pitch_offset_deg;
287     double _heading_offset_deg;
288     double _goal_roll_offset_deg;
289     double _goal_pitch_offset_deg;
290     double _goal_heading_offset_deg;
291
292     // used to set nearplane when at ground level for this view
293     double _ground_level_nearplane_m;
294
295     fgViewType _type;
296     fgScalingType _scaling_type;
297
298     // internal view (e.g. cockpit) flag
299     bool _internal;
300
301     // view is looking from a model
302     bool _from_model;
303     int _from_model_index;  // number of model (for multi model)
304
305     // view is looking at a model
306     bool _at_model;
307     int _at_model_index;  // number of model (for multi model)
308
309     // the nominal field of view (angle, in degrees)
310     double _fov_deg;
311
312     // Ratio of window width and height; height = width *
313     // aspect_ratio.  This value is automatically calculated based on
314     // window dimentions.
315     double _aspect_ratio;
316
317     // default = 1.0, this value is user configurable and is
318     // multiplied into the aspect_ratio to get the actual vertical fov
319     double _aspect_ratio_multiplier;
320
321     // camera group controled by this view
322     osg::ref_ptr<flightgear::CameraGroup> _cameraGroup;
323     //////////////////////////////////////////////////////////////////
324     // private functions                                            //
325     //////////////////////////////////////////////////////////////////
326
327     void recalc ();
328     void recalcLookFrom();
329     void recalcLookAt();
330     void dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg);
331
332     // add to _heading_offset_deg
333     inline void incHeadingOffset_deg( double amt ) {
334         set_dirty();
335         _heading_offset_deg += amt;
336     }
337
338     // add to _pitch_offset_deg
339     inline void incPitchOffset_deg( double amt ) {
340         set_dirty();
341         _pitch_offset_deg += amt;
342     }
343
344     // add to _roll_offset_deg
345     inline void incRollOffset_deg( double amt ) {
346         set_dirty();
347         _roll_offset_deg += amt;
348     }
349
350 };
351
352
353 #endif // _VIEWER_HXX