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/scene/model/location.hxx>
47 #include <plib/sg.h> // plib include
49 #define FG_FOV_MIN 0.1
50 #define FG_FOV_MAX 179.9
57 // Define a structure containing view information
58 class FGViewer : public SGSubsystem {
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
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 );
81 virtual ~FGViewer( void );
83 //////////////////////////////////////////////////////////////////////
84 // Part 1: standard SGSubsystem implementation.
85 //////////////////////////////////////////////////////////////////////
89 virtual void unbind ();
90 void update (double dt);
93 //////////////////////////////////////////////////////////////////////
94 // Part 2: user settings.
95 //////////////////////////////////////////////////////////////////////
97 virtual fgViewType getType() const { return _type; }
98 virtual void setType( int type );
100 virtual bool getInternal() const { return _internal; }
101 virtual void setInternal( bool internal );
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);
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);
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
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,
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);
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
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);
213 //////////////////////////////////////////////////////////////////////
214 // Part 3: output vectors and matrices in FlightGear coordinates.
215 //////////////////////////////////////////////////////////////////////
217 // Vectors and positions...
219 const SGVec3d& get_view_pos() { if ( _dirty ) { recalc(); } return _absolute_view_pos; }
220 const SGVec3d& getViewPosition() { if ( _dirty ) { recalc(); } return _absolute_view_pos; }
221 const SGQuatd& getViewOrientation() { if ( _dirty ) { recalc(); } return mViewOrientation; }
223 //////////////////////////////////////////////////////////////////////
224 // Part 4: View and frustrum data setters and getters
225 //////////////////////////////////////////////////////////////////////
227 virtual void set_fov( double fov_deg ) {
230 virtual double get_fov() const { return _fov_deg; }
231 virtual double get_h_fov(); // Get horizontal fov, in degrees.
232 virtual double get_v_fov(); // Get vertical fov, in degrees.
234 virtual void set_aspect_ratio( double r ) {
237 virtual double get_aspect_ratio() const { return _aspect_ratio; }
239 virtual void set_aspect_ratio_multiplier( double m ) {
240 _aspect_ratio_multiplier = m;
242 virtual double get_aspect_ratio_multiplier() const {
243 return _aspect_ratio_multiplier;
246 virtual double getNear_m () const { return _ground_level_nearplane_m; }
247 inline void setNear_m (double near_m) {
248 _ground_level_nearplane_m = near_m;
251 //////////////////////////////////////////////////////////////////////
252 // Part 5: misc setters and getters
253 //////////////////////////////////////////////////////////////////////
255 inline void set_dirty() { _dirty = true; }
256 inline void set_clean() { _dirty = false; }
258 // return eye location...
259 virtual SGLocation * getSGLocation () const { return _location; }
264 //////////////////////////////////////////////////////////////////
266 //////////////////////////////////////////////////////////////////
268 // flag forcing a recalc of derived view parameters
271 SGQuatd mViewOrientation;
272 SGVec3d _absolute_view_pos;
277 double _target_lon_deg;
278 double _target_lat_deg;
279 double _target_alt_ft;
284 double _target_roll_deg;
285 double _target_pitch_deg;
286 double _target_heading_deg;
291 double _damp_heading;
293 double _damped_roll_deg;
294 double _damped_pitch_deg;
295 double _damped_heading_deg;
297 // Position offsets from FDM origin. The X axis is positive
298 // out the tail, Y is out the right wing, and Z is positive up.
299 // distance in meters
302 // Target offsets from FDM origin (for "lookat" targets) The X
303 // axis is positive out the tail, Y is out the right wing, and Z
304 // is positive up. distance in meters
305 SGVec3d _target_offset_m;
308 // orientation offsets from reference (_goal* are for smoothed transitions)
309 double _roll_offset_deg;
310 double _pitch_offset_deg;
311 double _heading_offset_deg;
312 double _goal_roll_offset_deg;
313 double _goal_pitch_offset_deg;
314 double _goal_heading_offset_deg;
316 // used to set nearplane when at ground level for this view
317 double _ground_level_nearplane_m;
320 fgScalingType _scaling_type;
322 // internal view (e.g. cockpit) flag
325 // view is looking from a model
327 int _from_model_index; // number of model (for multi model)
329 // view is looking at a model
331 int _at_model_index; // number of model (for multi model)
333 SGLocation * _location;
334 SGLocation * _target_location;
336 // the nominal field of view (angle, in degrees)
339 // Ratio of window width and height; height = width *
340 // aspect_ratio. This value is automatically calculated based on
341 // window dimentions.
342 double _aspect_ratio;
344 // default = 1.0, this value is user configurable and is
345 // multiplied into the aspect_ratio to get the actual vertical fov
346 double _aspect_ratio_multiplier;
348 // camera group controled by this view
349 osg::ref_ptr<flightgear::CameraGroup> _cameraGroup;
350 //////////////////////////////////////////////////////////////////
351 // private functions //
352 //////////////////////////////////////////////////////////////////
355 void recalcLookFrom();
357 void dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg);
359 // add to _heading_offset_deg
360 inline void incHeadingOffset_deg( double amt ) {
362 _heading_offset_deg += amt;
365 // add to _pitch_offset_deg
366 inline void incPitchOffset_deg( double amt ) {
368 _pitch_offset_deg += amt;
371 // add to _roll_offset_deg
372 inline void incRollOffset_deg( double amt ) {
374 _roll_offset_deg += amt;
380 #endif // _VIEWER_HXX