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++
35 #include <simgear/compiler.h>
36 #include <simgear/constants.h>
37 #include <simgear/structure/subsystem_mgr.hxx>
38 #include <simgear/scene/model/location.hxx>
40 #include <plib/sg.h> // plib include
42 #define FG_FOV_MIN 0.1
43 #define FG_FOV_MAX 179.9
50 // Define a structure containing view information
51 class FGViewer : public SGSubsystem {
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
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 );
74 virtual ~FGViewer( void );
76 //////////////////////////////////////////////////////////////////////
77 // Part 1: standard SGSubsystem implementation.
78 //////////////////////////////////////////////////////////////////////
82 virtual void unbind ();
83 void update (double dt);
86 //////////////////////////////////////////////////////////////////////
87 // Part 2: user settings.
88 //////////////////////////////////////////////////////////////////////
90 virtual fgViewType getType() const { return _type; }
91 virtual void setType( int type );
93 virtual bool getInternal() const { return _internal; }
94 virtual void setInternal( bool internal );
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 double getLongitude_deg () const { return _lon_deg; }
102 virtual double getLatitude_deg () const { return _lat_deg; }
103 virtual double getAltitudeASL_ft () const { return _alt_ft; }
104 virtual void setLongitude_deg (double lon_deg);
105 virtual void setLatitude_deg (double lat_deg);
106 virtual void setAltitude_ft (double alt_ft);
107 virtual void setPosition (double lon_deg, double lat_deg, double alt_ft);
109 // Reference geodetic target position...
110 virtual double getTargetLongitude_deg () const { return _target_lon_deg; }
111 virtual double getTargetLatitude_deg () const { return _target_lat_deg; }
112 virtual double getTargetAltitudeASL_ft () const { return _target_alt_ft; }
113 virtual void setTargetLongitude_deg (double lon_deg);
114 virtual void setTargetLatitude_deg (double lat_deg);
115 virtual void setTargetAltitude_ft (double alt_ft);
116 virtual void setTargetPosition (double lon_deg, double lat_deg, double alt_ft);
121 // Position offsets from reference
122 // These offsets position they "eye" in the scene according to a given
123 // location. For example in pilot view they are used to position the
124 // head inside the aircraft.
125 // Note that in pilot view these are applied "before" the orientation
126 // rotations (see below) so that the orientation rotations have the
127 // effect of the pilot staying in his seat and "looking out" in
128 // different directions.
129 // In chase view these are applied "after" the application of the
130 // orientation rotations listed below. This has the effect of the
131 // eye moving around and "looking at" the object (model) from
133 virtual double getXOffset_m () const { return _offset_m.x(); }
134 virtual double getYOffset_m () const { return _offset_m.y(); }
135 virtual double getZOffset_m () const { return _offset_m.z(); }
136 virtual double getTargetXOffset_m () const { return _target_offset_m.x(); }
137 virtual double getTargetYOffset_m () const { return _target_offset_m.y(); }
138 virtual double getTargetZOffset_m () const { return _target_offset_m.z(); }
139 virtual void setXOffset_m (double x_offset_m);
140 virtual void setYOffset_m (double y_offset_m);
141 virtual void setZOffset_m (double z_offset_m);
142 virtual void setTargetXOffset_m (double x_offset_m);
143 virtual void setTargetYOffset_m (double y_offset_m);
144 virtual void setTargetZOffset_m (double z_offset_m);
145 virtual void setPositionOffsets (double x_offset_m,
152 // Reference orientation rotations...
153 // These are rotations that represent the plane attitude effect on
154 // the view (in Pilot view). IE The view frustrum rotates as the plane
155 // turns, pitches, and rolls.
156 // In model view (lookat/chaseview) these end up changing the angle that
157 // the eye is looking at the ojbect (ie the model).
158 // FIXME: the FGModel class should have its own version of these so that
159 // it can generate it's own model rotations.
160 virtual double getRoll_deg () const { return _roll_deg; }
161 virtual double getPitch_deg () const {return _pitch_deg; }
162 virtual double getHeading_deg () const {return _heading_deg; }
163 virtual void setRoll_deg (double roll_deg);
164 virtual void setPitch_deg (double pitch_deg);
165 virtual void setHeading_deg (double heading_deg);
166 virtual void setOrientation (double roll_deg, double pitch_deg, double heading_deg);
167 virtual double getTargetRoll_deg () const { return _target_roll_deg; }
168 virtual double getTargetPitch_deg () const {return _target_pitch_deg; }
169 virtual double getTargetHeading_deg () const {return _target_heading_deg; }
170 virtual void setTargetRoll_deg (double roll_deg);
171 virtual void setTargetPitch_deg (double pitch_deg);
172 virtual void setTargetHeading_deg (double heading_deg);
173 virtual void setTargetOrientation (double roll_deg, double pitch_deg, double heading_deg);
178 // Orientation offsets rotations from reference orientation.
179 // Goal settings are for smooth transition from prior
180 // offset when changing view direction.
181 // These offsets are in ADDITION to the orientation rotations listed
183 // In pilot view they are applied after the position offsets in order to
184 // give the effect of the pilot looking around.
185 // In lookat view they are applied before the position offsets so that
186 // the effect is the eye moving around looking at the object (ie the model)
187 // from different angles.
188 virtual double getRollOffset_deg () const { return _roll_offset_deg; }
189 virtual double getPitchOffset_deg () const { return _pitch_offset_deg; }
190 virtual double getHeadingOffset_deg () const { return _heading_offset_deg; }
191 virtual double getGoalRollOffset_deg () const { return _goal_roll_offset_deg; }
192 virtual double getGoalPitchOffset_deg () const { return _goal_pitch_offset_deg; }
193 virtual double getGoalHeadingOffset_deg () const {return _goal_heading_offset_deg; }
194 virtual void setRollOffset_deg (double roll_offset_deg);
195 virtual void setPitchOffset_deg (double pitch_offset_deg);
196 virtual void setHeadingOffset_deg (double heading_offset_deg);
197 virtual void setGoalRollOffset_deg (double goal_roll_offset_deg);
198 virtual void setGoalPitchOffset_deg (double goal_pitch_offset_deg);
199 virtual void setGoalHeadingOffset_deg (double goal_heading_offset_deg);
200 virtual void setOrientationOffsets (double roll_offset_deg,
201 double heading_offset_deg,
202 double pitch_offset_deg);
206 //////////////////////////////////////////////////////////////////////
207 // Part 3: output vectors and matrices in FlightGear coordinates.
208 //////////////////////////////////////////////////////////////////////
210 // Vectors and positions...
213 const SGVec3f& get_view_pos() {if ( _dirty ) { recalc(); } return _view_pos; }
214 // Get the absolute view position in fgfs coordinates.
215 virtual double * get_absolute_view_pos ();
217 const SGVec3f& get_zero_elev() {if ( _dirty ) { recalc(); } return _zero_elev; }
218 // Get world up vector
219 const SGVec3f& get_world_up() {if ( _dirty ) { recalc(); } return _world_up; }
220 // Get surface east vector
221 const SGVec3f& get_surface_east() { if ( _dirty ) { recalc(); } return _surface_east; }
222 // Get surface south vector
223 const SGVec3f& get_surface_south() {if ( _dirty ) { recalc(); } return _surface_south; }
225 const SGVec3d& getViewPosition() { if ( _dirty ) { recalc(); } return _absolute_view_pos; }
226 const SGQuatd& getViewOrientation() { if ( _dirty ) { recalc(); } return mViewOrientation; }
228 //////////////////////////////////////////////////////////////////////
229 // Part 4: View and frustrum data setters and getters
230 //////////////////////////////////////////////////////////////////////
232 virtual void set_fov( double fov_deg ) {
235 virtual double get_fov() const { return _fov_deg; }
236 virtual double get_h_fov(); // Get horizontal fov, in degrees.
237 virtual double get_v_fov(); // Get vertical fov, in degrees.
239 virtual void set_aspect_ratio( double r ) {
242 virtual double get_aspect_ratio() const { return _aspect_ratio; }
244 virtual void set_aspect_ratio_multiplier( double m ) {
245 _aspect_ratio_multiplier = m;
247 virtual double get_aspect_ratio_multiplier() const {
248 return _aspect_ratio_multiplier;
251 virtual double getNear_m () const { return _ground_level_nearplane_m; }
252 inline void setNear_m (double near_m) {
253 _ground_level_nearplane_m = near_m;
256 //////////////////////////////////////////////////////////////////////
257 // Part 5: misc setters and getters
258 //////////////////////////////////////////////////////////////////////
260 inline void set_dirty() { _dirty = true; }
261 inline void set_clean() { _dirty = false; }
263 // return eye location...
264 virtual SGLocation * getSGLocation () const { return _location; }
269 //////////////////////////////////////////////////////////////////
271 //////////////////////////////////////////////////////////////////
273 // flag forcing a recalc of derived view parameters
276 SGQuatd mViewOrientation;
277 SGVec3d _absolute_view_pos;
278 SGVec3f _relative_view_pos;
283 double _target_lon_deg;
284 double _target_lat_deg;
285 double _target_alt_ft;
290 double _target_roll_deg;
291 double _target_pitch_deg;
292 double _target_heading_deg;
297 double _damp_heading;
299 double _damped_roll_deg;
300 double _damped_pitch_deg;
301 double _damped_heading_deg;
303 // Position offsets from FDM origin. The X axis is positive
304 // out the tail, Y is out the right wing, and Z is positive up.
305 // distance in meters
308 // Target offsets from FDM origin (for "lookat" targets) The X
309 // axis is positive out the tail, Y is out the right wing, and Z
310 // is positive up. distance in meters
311 SGVec3d _target_offset_m;
314 // orientation offsets from reference (_goal* are for smoothed transitions)
315 double _roll_offset_deg;
316 double _pitch_offset_deg;
317 double _heading_offset_deg;
318 double _goal_roll_offset_deg;
319 double _goal_pitch_offset_deg;
320 double _goal_heading_offset_deg;
322 // used to set nearplane when at ground level for this view
323 double _ground_level_nearplane_m;
326 fgScalingType _scaling_type;
328 // internal view (e.g. cockpit) flag
331 // view is looking from a model
333 int _from_model_index; // number of model (for multi model)
335 // view is looking at a model
337 int _at_model_index; // number of model (for multi model)
339 SGLocation * _location;
340 SGLocation * _target_location;
342 // the nominal field of view (angle, in degrees)
345 // Ratio of window width and height; height = width *
346 // aspect_ratio. This value is automatically calculated based on
347 // window dimentions.
348 double _aspect_ratio;
350 // default = 1.0, this value is user configurable and is
351 // multiplied into the aspect_ratio to get the actual vertical fov
352 double _aspect_ratio_multiplier;
354 // view position in opengl world coordinates (this is the
355 // abs_view_pos translated to scenery.center)
358 // cartesion coordinates of current lon/lat if at sea level
359 // translated to scenery.center
362 // surface vector heading south
363 SGVec3f _surface_south;
365 // surface vector heading east (used to unambiguously align sky
367 SGVec3f _surface_east;
369 // world up vector (normal to the plane tangent to the earth's
370 // surface at the spot we are directly above
373 //////////////////////////////////////////////////////////////////
374 // private functions //
375 //////////////////////////////////////////////////////////////////
378 void recalcLookFrom();
380 void dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg);
382 // add to _heading_offset_deg
383 inline void incHeadingOffset_deg( double amt ) {
385 _heading_offset_deg += amt;
388 // add to _pitch_offset_deg
389 inline void incPitchOffset_deg( double amt ) {
391 _pitch_offset_deg += amt;
394 // add to _roll_offset_deg
395 inline void incRollOffset_deg( double amt ) {
397 _roll_offset_deg += amt;
403 #endif // _VIEWER_HXX