1 // viewer.hxx -- class for managing a viewer in the flightgear world.
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
8 // Copyright (C) 1997 - 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
32 # error This library requires C++
40 #include <osg/ref_ptr>
42 #include <simgear/compiler.h>
43 #include <simgear/constants.h>
44 #include <simgear/structure/subsystem_mgr.hxx>
45 #include <simgear/math/SGMath.hxx>
47 #define FG_FOV_MIN 0.1
48 #define FG_FOV_MAX 179.9
55 // Define a structure containing view information
56 class FGViewer : public SGSubsystem {
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
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 );
79 virtual ~FGViewer( void );
81 //////////////////////////////////////////////////////////////////////
82 // Part 1: standard SGSubsystem implementation.
83 //////////////////////////////////////////////////////////////////////
87 virtual void unbind ();
88 void update (double dt);
91 //////////////////////////////////////////////////////////////////////
92 // Part 2: user settings.
93 //////////////////////////////////////////////////////////////////////
95 virtual fgViewType getType() const { return _type; }
96 virtual void setType( int type );
98 virtual bool getInternal() const { return _internal; }
99 virtual void setInternal( bool internal );
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 _position.getLongitudeDeg(); }
107 virtual double getLatitude_deg () const { return _position.getLatitudeDeg(); }
109 virtual void setPosition (double lon_deg, double lat_deg, double alt_ft);
110 const SGGeod& getPosition() const { return _position; }
113 // Reference geodetic target position...
114 virtual void setTargetPosition (double lon_deg, double lat_deg, double alt_ft);
115 const SGGeod& getTargetPosition() const { return _target; }
119 // Position offsets from reference
120 // These offsets position they "eye" in the scene according to a given
121 // location. For example in pilot view they are used to position the
122 // head inside the aircraft.
123 // Note that in pilot view these are applied "before" the orientation
124 // rotations (see below) so that the orientation rotations have the
125 // effect of the pilot staying in his seat and "looking out" in
126 // different directions.
127 // In chase view these are applied "after" the application of the
128 // orientation rotations listed below. This has the effect of the
129 // eye moving around and "looking at" the object (model) from
131 virtual double getXOffset_m () const { return _offset_m.x(); }
132 virtual double getYOffset_m () const { return _offset_m.y(); }
133 virtual double getZOffset_m () const { return _offset_m.z(); }
134 virtual double getTargetXOffset_m () const { return _target_offset_m.x(); }
135 virtual double getTargetYOffset_m () const { return _target_offset_m.y(); }
136 virtual double getTargetZOffset_m () const { return _target_offset_m.z(); }
137 virtual void setXOffset_m (double x_offset_m);
138 virtual void setYOffset_m (double y_offset_m);
139 virtual void setZOffset_m (double z_offset_m);
140 virtual void setTargetXOffset_m (double x_offset_m);
141 virtual void setTargetYOffset_m (double y_offset_m);
142 virtual void setTargetZOffset_m (double z_offset_m);
143 virtual void setPositionOffsets (double x_offset_m,
150 // Reference orientation rotations...
151 // These are rotations that represent the plane attitude effect on
152 // the view (in Pilot view). IE The view frustrum rotates as the plane
153 // turns, pitches, and rolls.
154 // In model view (lookat/chaseview) these end up changing the angle that
155 // the eye is looking at the ojbect (ie the model).
156 // FIXME: the FGModel class should have its own version of these so that
157 // it can generate it's own model rotations.
158 virtual double getRoll_deg () const { return _roll_deg; }
159 virtual double getPitch_deg () const {return _pitch_deg; }
160 virtual double getHeading_deg () const {return _heading_deg; }
161 virtual void setRoll_deg (double roll_deg);
162 virtual void setPitch_deg (double pitch_deg);
163 virtual void setHeading_deg (double heading_deg);
164 virtual void setOrientation (double roll_deg, double pitch_deg, double heading_deg);
165 virtual double getTargetRoll_deg () const { return _target_roll_deg; }
166 virtual double getTargetPitch_deg () const {return _target_pitch_deg; }
167 virtual double getTargetHeading_deg () const {return _target_heading_deg; }
168 virtual void setTargetRoll_deg (double roll_deg);
169 virtual void setTargetPitch_deg (double pitch_deg);
170 virtual void setTargetHeading_deg (double heading_deg);
171 virtual void setTargetOrientation (double roll_deg, double pitch_deg, double heading_deg);
176 // Orientation offsets rotations from reference orientation.
177 // Goal settings are for smooth transition from prior
178 // offset when changing view direction.
179 // These offsets are in ADDITION to the orientation rotations listed
181 // In pilot view they are applied after the position offsets in order to
182 // give the effect of the pilot looking around.
183 // In lookat view they are applied before the position offsets so that
184 // the effect is the eye moving around looking at the object (ie the model)
185 // from different angles.
186 virtual double getRollOffset_deg () const { return _roll_offset_deg; }
187 virtual double getPitchOffset_deg () const { return _pitch_offset_deg; }
188 virtual double getHeadingOffset_deg () const { return _heading_offset_deg; }
189 virtual double getGoalRollOffset_deg () const { return _goal_roll_offset_deg; }
190 virtual double getGoalPitchOffset_deg () const { return _goal_pitch_offset_deg; }
191 virtual double getGoalHeadingOffset_deg () const {return _goal_heading_offset_deg; }
192 virtual void setRollOffset_deg (double roll_offset_deg);
193 virtual void setPitchOffset_deg (double pitch_offset_deg);
194 virtual void setHeadingOffset_deg (double heading_offset_deg);
195 virtual void setGoalRollOffset_deg (double goal_roll_offset_deg);
196 virtual void setGoalPitchOffset_deg (double goal_pitch_offset_deg);
197 virtual void setGoalHeadingOffset_deg (double goal_heading_offset_deg);
198 virtual void setOrientationOffsets (double roll_offset_deg,
199 double heading_offset_deg,
200 double pitch_offset_deg);
204 //////////////////////////////////////////////////////////////////////
205 // Part 3: output vectors and matrices in FlightGear coordinates.
206 //////////////////////////////////////////////////////////////////////
208 // Vectors and positions...
210 const SGVec3d& get_view_pos() { if ( _dirty ) { recalc(); } return _absolute_view_pos; }
211 const SGVec3d& getViewPosition() { if ( _dirty ) { recalc(); } return _absolute_view_pos; }
212 const SGQuatd& getViewOrientation() { if ( _dirty ) { recalc(); } return mViewOrientation; }
214 //////////////////////////////////////////////////////////////////////
215 // Part 4: View and frustrum data setters and getters
216 //////////////////////////////////////////////////////////////////////
218 virtual void set_fov( double fov_deg ) {
221 virtual double get_fov() const { return _fov_deg; }
222 virtual double get_h_fov(); // Get horizontal fov, in degrees.
223 virtual double get_v_fov(); // Get vertical fov, in degrees.
225 virtual void set_aspect_ratio( double r ) {
228 virtual double get_aspect_ratio() const { return _aspect_ratio; }
230 virtual void set_aspect_ratio_multiplier( double m ) {
231 _aspect_ratio_multiplier = m;
233 virtual double get_aspect_ratio_multiplier() const {
234 return _aspect_ratio_multiplier;
237 virtual double getNear_m () const { return _ground_level_nearplane_m; }
238 inline void setNear_m (double near_m) {
239 _ground_level_nearplane_m = near_m;
242 //////////////////////////////////////////////////////////////////////
243 // Part 5: misc setters and getters
244 //////////////////////////////////////////////////////////////////////
246 inline void set_dirty() { _dirty = true; }
247 inline void set_clean() { _dirty = false; }
251 //////////////////////////////////////////////////////////////////
253 //////////////////////////////////////////////////////////////////
255 // flag forcing a recalc of derived view parameters
258 SGQuatd mViewOrientation;
259 SGVec3d _absolute_view_pos;
267 double _target_roll_deg;
268 double _target_pitch_deg;
269 double _target_heading_deg;
274 double _damp_heading;
276 double _damped_roll_deg;
277 double _damped_pitch_deg;
278 double _damped_heading_deg;
280 // Position offsets from FDM origin. The X axis is positive
281 // out the tail, Y is out the right wing, and Z is positive up.
282 // distance in meters
285 // Target offsets from FDM origin (for "lookat" targets) The X
286 // axis is positive out the tail, Y is out the right wing, and Z
287 // is positive up. distance in meters
288 SGVec3d _target_offset_m;
291 // orientation offsets from reference (_goal* are for smoothed transitions)
292 double _roll_offset_deg;
293 double _pitch_offset_deg;
294 double _heading_offset_deg;
295 double _goal_roll_offset_deg;
296 double _goal_pitch_offset_deg;
297 double _goal_heading_offset_deg;
299 // used to set nearplane when at ground level for this view
300 double _ground_level_nearplane_m;
303 fgScalingType _scaling_type;
305 // internal view (e.g. cockpit) flag
308 // view is looking from a model
310 int _from_model_index; // number of model (for multi model)
312 // view is looking at a model
314 int _at_model_index; // number of model (for multi model)
316 // the nominal field of view (angle, in degrees)
319 // Ratio of window width and height; height = width *
320 // aspect_ratio. This value is automatically calculated based on
321 // window dimentions.
322 double _aspect_ratio;
324 // default = 1.0, this value is user configurable and is
325 // multiplied into the aspect_ratio to get the actual vertical fov
326 double _aspect_ratio_multiplier;
328 // camera group controled by this view
329 osg::ref_ptr<flightgear::CameraGroup> _cameraGroup;
330 //////////////////////////////////////////////////////////////////
331 // private functions //
332 //////////////////////////////////////////////////////////////////
335 void recalcLookFrom();
337 void dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg);
339 // add to _heading_offset_deg
340 inline void incHeadingOffset_deg( double amt ) {
342 _heading_offset_deg += amt;
345 // add to _pitch_offset_deg
346 inline void incPitchOffset_deg( double amt ) {
348 _pitch_offset_deg += amt;
351 // add to _roll_offset_deg
352 inline void incRollOffset_deg( double amt ) {
354 _roll_offset_deg += amt;
360 #endif // _VIEWER_HXX