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