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.
31 #include <simgear/compiler.h>
32 #include <simgear/constants.h>
34 #include <simgear/props/props.hxx>
35 #include <simgear/structure/subsystem_mgr.hxx>
36 #include <simgear/math/SGMath.hxx>
38 #define FG_FOV_MIN 0.1
39 #define FG_FOV_MAX 179.9
44 // Define a structure containing view information
45 class View : public SGSubsystem {
49 enum ScalingType { // nominal Field Of View actually applies to ...
50 FG_SCALING_WIDTH, // window width
51 FG_SCALING_MAX // max(width, height)
52 // FG_SCALING_G_MEAN, // geometric_mean(width, height)
53 // FG_SCALING_INDEPENDENT // whole screen
61 static View* createFromProperties(SGPropertyNode_ptr props);
66 //////////////////////////////////////////////////////////////////////
67 // Part 1: standard SGSubsystem implementation.
68 //////////////////////////////////////////////////////////////////////
72 virtual void unbind ();
73 virtual void update (double dt);
76 //////////////////////////////////////////////////////////////////////
77 // Part 2: user settings.
78 //////////////////////////////////////////////////////////////////////
80 ViewType getType() const { return _type; }
81 void setType( int type );
83 bool getInternal() const { return _internal; }
84 void setInternal( bool internal );
86 // Reference geodetic position of view from position...
87 // These are the actual aircraft position (pilot in
88 // pilot view, model in model view).
89 // FIXME: the model view position (ie target positions)
90 // should be in the model class.
91 void setPosition (double lon_deg, double lat_deg, double alt_ft);
92 const SGGeod& getPosition() const { return _position; }
94 // Reference geodetic target position...
95 void setTargetPosition (double lon_deg, double lat_deg, double alt_ft);
96 const SGGeod& getTargetPosition() const { return _target; }
100 // Position offsets from reference
101 // These offsets position they "eye" in the scene according to a given
102 // location. For example in pilot view they are used to position the
103 // head inside the aircraft.
104 // Note that in pilot view these are applied "before" the orientation
105 // rotations (see below) so that the orientation rotations have the
106 // effect of the pilot staying in his seat and "looking out" in
107 // different directions.
108 // In chase view these are applied "after" the application of the
109 // orientation rotations listed below. This has the effect of the
110 // eye moving around and "looking at" the object (model) from
112 SGVec3d getOffset_m () const { return _offset_m; }
113 double getXOffset_m () const { return _offset_m.x(); }
114 double getYOffset_m () const { return _offset_m.y(); }
115 double getZOffset_m () const { return _offset_m.z(); }
116 double getTargetXOffset_m () const { return _target_offset_m.x(); }
117 double getTargetYOffset_m () const { return _target_offset_m.y(); }
118 double getTargetZOffset_m () const { return _target_offset_m.z(); }
119 void setXOffset_m (double x_offset_m);
120 void setYOffset_m (double y_offset_m);
121 void setZOffset_m (double z_offset_m);
122 void setTargetXOffset_m (double x_offset_m);
123 void setTargetYOffset_m (double y_offset_m);
124 void setTargetZOffset_m (double z_offset_m);
125 void setPositionOffsets (double x_offset_m,
132 // Reference orientation rotations...
133 // These are rotations that represent the plane attitude effect on
134 // the view (in Pilot view). IE The view frustrum rotates as the plane
135 // turns, pitches, and rolls.
136 // In model view (lookat/chaseview) these end up changing the angle that
137 // the eye is looking at the ojbect (ie the model).
138 // FIXME: the FGModel class should have its own version of these so that
139 // it can generate it's own model rotations.
140 double getRoll_deg () const { return _roll_deg; }
141 double getPitch_deg () const {return _pitch_deg; }
142 double getHeading_deg () const {return _heading_deg; }
143 void setRoll_deg (double roll_deg);
144 void setPitch_deg (double pitch_deg);
145 void setHeading_deg (double heading_deg);
146 void setOrientation (double roll_deg, double pitch_deg, double heading_deg);
147 double getTargetRoll_deg () const { return _target_roll_deg; }
148 double getTargetPitch_deg () const {return _target_pitch_deg; }
149 double getTargetHeading_deg () const {return _target_heading_deg; }
150 void setTargetRoll_deg (double roll_deg);
151 void setTargetPitch_deg (double pitch_deg);
152 void setTargetHeading_deg (double heading_deg);
153 void setTargetOrientation (double roll_deg, double pitch_deg, double heading_deg);
158 // Orientation offsets rotations from reference orientation.
159 // Goal settings are for smooth transition from prior
160 // offset when changing view direction.
161 // These offsets are in ADDITION to the orientation rotations listed
163 // In pilot view they are applied after the position offsets in order to
164 // give the effect of the pilot looking around.
165 // In lookat view they are applied before the position offsets so that
166 // the effect is the eye moving around looking at the object (ie the model)
167 // from different angles.
168 double getRollOffset_deg () const { return _roll_offset_deg; }
169 double getPitchOffset_deg () const { return _pitch_offset_deg; }
170 double getHeadingOffset_deg () const { return _heading_offset_deg; }
171 double getGoalRollOffset_deg () const { return _goal_roll_offset_deg; }
172 double getGoalPitchOffset_deg () const { return _goal_pitch_offset_deg; }
173 double getGoalHeadingOffset_deg () const {return _goal_heading_offset_deg; }
174 void setRollOffset_deg (double roll_offset_deg);
175 void setPitchOffset_deg (double pitch_offset_deg);
176 void setHeadingOffset_deg (double heading_offset_deg);
177 void setGoalRollOffset_deg (double goal_roll_offset_deg);
178 void setGoalPitchOffset_deg (double goal_pitch_offset_deg);
179 void setGoalHeadingOffset_deg (double goal_heading_offset_deg);
180 void setOrientationOffsets (double roll_offset_deg,
181 double heading_offset_deg,
182 double pitch_offset_deg);
186 //////////////////////////////////////////////////////////////////////
187 // Part 3: output vectors and matrices in FlightGear coordinates.
188 //////////////////////////////////////////////////////////////////////
190 // Vectors and positions...
192 const SGVec3d& get_view_pos() { if ( _dirty ) { recalc(); } return _absolute_view_pos; }
193 const SGVec3d& getViewPosition() { if ( _dirty ) { recalc(); } return _absolute_view_pos; }
194 const SGQuatd& getViewOrientation() { if ( _dirty ) { recalc(); } return mViewOrientation; }
195 const SGQuatd& getViewOrientationOffset() { if ( _dirty ) { recalc(); } return mViewOffsetOr; }
197 //////////////////////////////////////////////////////////////////////
198 // Part 4: View and frustrum data setters and getters
199 //////////////////////////////////////////////////////////////////////
201 void set_fov( double fov_deg ) {
205 double get_fov() const { return _fov_deg; }
206 double get_h_fov(); // Get horizontal fov, in degrees.
207 double get_v_fov(); // Get vertical fov, in degrees.
209 // this is currently just a wrapper for the default CameraGroups' aspect
210 double get_aspect_ratio() const;
212 void set_aspect_ratio_multiplier( double m ) {
213 _aspect_ratio_multiplier = m;
215 double get_aspect_ratio_multiplier() const {
216 return _aspect_ratio_multiplier;
219 double getNear_m () const { return _ground_level_nearplane_m; }
220 void setNear_m (double near_m) {
221 _ground_level_nearplane_m = near_m;
224 //////////////////////////////////////////////////////////////////////
225 // Part 5: misc setters and getters
226 //////////////////////////////////////////////////////////////////////
228 void set_dirty() { _dirty = true; }
229 void set_clean() { _dirty = false; }
234 View( ViewType Type, bool from_model, int from_model_index,
235 bool at_model, int at_model_index,
236 double damp_roll, double damp_pitch, double damp_heading,
237 double x_offset_m, double y_offset_m, double z_offset_m,
238 double heading_offset_deg, double pitch_offset_deg,
239 double roll_offset_deg,
240 double fov_deg, double aspect_ratio_multiplier,
241 double target_x_offset_m, double target_y_offset_m,
242 double target_z_offset_m, double near_m, bool internal );
245 //////////////////////////////////////////////////////////////////
247 //////////////////////////////////////////////////////////////////
249 // flag forcing a recalc of derived view parameters
252 SGQuatd mViewOrientation;
253 SGQuatd mViewOffsetOr;
254 SGVec3d _absolute_view_pos;
262 double _target_roll_deg;
263 double _target_pitch_deg;
264 double _target_heading_deg;
266 SGVec3d _dampTarget; ///< current target value we are damping towards
267 SGVec3d _dampOutput; ///< current output of damping filter
268 SGVec3d _dampFactor; ///< weighting of the damping filter
270 // Position offsets from FDM origin. The X axis is positive
271 // out the tail, Y is out the right wing, and Z is positive up.
272 // distance in meters
275 // Target offsets from FDM origin (for "lookat" targets) The X
276 // axis is positive out the tail, Y is out the right wing, and Z
277 // is positive up. distance in meters
278 SGVec3d _target_offset_m;
281 // orientation offsets from reference (_goal* are for smoothed transitions)
282 double _roll_offset_deg;
283 double _pitch_offset_deg;
284 double _heading_offset_deg;
285 double _goal_roll_offset_deg;
286 double _goal_pitch_offset_deg;
287 double _goal_heading_offset_deg;
289 // used to set nearplane when at ground level for this view
290 double _ground_level_nearplane_m;
293 ScalingType _scaling_type;
295 // internal view (e.g. cockpit) flag
298 // view is looking from a model
300 int _from_model_index; // number of model (for multi model)
302 // view is looking at a model
304 int _at_model_index; // number of model (for multi model)
306 // the nominal field of view (angle, in degrees)
309 // default = 1.0, this value is user configurable and is
310 // multiplied into the aspect_ratio to get the actual vertical fov
311 double _aspect_ratio_multiplier;
313 //////////////////////////////////////////////////////////////////
314 // private functions //
315 //////////////////////////////////////////////////////////////////
318 void recalcLookFrom();
321 void setDampTarget(double h, double p, double r);
322 void getDampOutput(double& roll, double& pitch, double& heading);
324 void updateDampOutput(double dt);
326 // add to _heading_offset_deg
327 inline void incHeadingOffset_deg( double amt ) {
329 _heading_offset_deg += amt;
332 // add to _pitch_offset_deg
333 inline void incPitchOffset_deg( double amt ) {
335 _pitch_offset_deg += amt;
338 // add to _roll_offset_deg
339 inline void incRollOffset_deg( double amt ) {
341 _roll_offset_deg += amt;
346 } // of namespace flightgear
349 #endif // _VIEWER_HXX