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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
32 # error This library requires C++
35 #include <simgear/compiler.h>
36 #include <simgear/constants.h>
38 #include <plib/sg.h> // plib include
43 #define FG_FOV_MIN 0.1
44 #define FG_FOV_MAX 179.9
47 // Define a structure containing view information
48 class FGViewer : public FGSubsystem {
58 enum fgScalingType { // nominal Field Of View actually applies to ...
59 FG_SCALING_WIDTH, // window width
60 FG_SCALING_MAX, // max(width, height)
61 // FG_SCALING_G_MEAN, // geometric_mean(width, height)
62 // FG_SCALING_INDEPENDENT // whole screen
69 virtual ~FGViewer( void );
71 //////////////////////////////////////////////////////////////////////
72 // Part 1: standard FGSubsystem implementation.
73 //////////////////////////////////////////////////////////////////////
77 virtual void unbind ();
81 //////////////////////////////////////////////////////////////////////
82 // Part 2: user settings.
83 //////////////////////////////////////////////////////////////////////
85 virtual fgViewType getType() const { return _type; }
86 virtual void setType( int type );
88 // Reference geodetic position of view from position...
89 // These are the actual aircraft position (pilot in
90 // pilot view, model in model view).
91 // FIXME: the model view position (ie target positions)
92 // should be in the model class.
93 virtual double getLongitude_deg () const { return _lon_deg; }
94 virtual double getLatitude_deg () const { return _lat_deg; }
95 virtual double getAltitudeASL_ft () const { return _alt_ft; }
96 virtual void setLongitude_deg (double lon_deg);
97 virtual void setLatitude_deg (double lat_deg);
98 virtual void setAltitude_ft (double alt_ft);
99 virtual void setPosition (double lon_deg, double lat_deg, double alt_ft);
101 // Reference geodetic target position...
102 virtual double getTargetLongitude_deg () const { return _target_lon_deg; }
103 virtual double getTargetLatitude_deg () const { return _target_lat_deg; }
104 virtual double getTargetAltitudeASL_ft () const { return _target_alt_ft; }
105 virtual void setTargetLongitude_deg (double lon_deg);
106 virtual void setTargetLatitude_deg (double lat_deg);
107 virtual void setTargetAltitude_ft (double alt_ft);
108 virtual void setTargetPosition (double lon_deg, double lat_deg, double alt_ft);
113 // Position offsets from reference
114 // These offsets position they "eye" in the scene according to a given
115 // location. For example in pilot view they are used to position the
116 // head inside the aircraft.
117 // Note that in pilot view these are applied "before" the orientation
118 // rotations (see below) so that the orientation rotations have the
119 // effect of the pilot staying in his seat and "looking out" in
120 // different directions.
121 // In chase view these are applied "after" the application of the
122 // orientation rotations listed below. This has the effect of the
123 // eye moving around and "looking at" the object (model) from
125 virtual double getXOffset_m () const { return _x_offset_m; }
126 virtual double getYOffset_m () const { return _y_offset_m; }
127 virtual double getZOffset_m () const { return _z_offset_m; }
128 virtual void setXOffset_m (double x_offset_m);
129 virtual void setYOffset_m (double y_offset_m);
130 virtual void setZOffset_m (double z_offset_m);
131 virtual void setPositionOffsets (double x_offset_m,
138 // Reference orientation rotations...
139 // These are rotations that represent the plane attitude effect on
140 // the view (in Pilot view). IE The view frustrum rotates as the plane
141 // turns, pitches, and rolls.
142 // In model view (lookat/chaseview) these end up changing the angle that
143 // the eye is looking at the ojbect (ie the model).
144 // FIXME: the FGModel class should have its own version of these so that
145 // it can generate it's own model rotations.
146 virtual double getRoll_deg () const { return _roll_deg; }
147 virtual double getPitch_deg () const {return _pitch_deg; }
148 virtual double getHeading_deg () const {return _heading_deg; }
149 virtual void setRoll_deg (double roll_deg);
150 virtual void setPitch_deg (double pitch_deg);
151 virtual void setHeading_deg (double heading_deg);
152 virtual void setOrientation (double roll_deg, double pitch_deg, double heading_deg);
153 virtual double getTargetRoll_deg () const { return _target_roll_deg; }
154 virtual double getTargetPitch_deg () const {return _target_pitch_deg; }
155 virtual double getTargetHeading_deg () const {return _target_heading_deg; }
156 virtual void setTargetRoll_deg (double roll_deg);
157 virtual void setTargetPitch_deg (double pitch_deg);
158 virtual void setTargetHeading_deg (double heading_deg);
159 virtual void setTargetOrientation (double roll_deg, double pitch_deg, double heading_deg);
164 // Orientation offsets rotations from reference orientation.
165 // Goal settings are for smooth transition from prior
166 // offset when changing view direction.
167 // These offsets are in ADDITION to the orientation rotations listed
169 // In pilot view they are applied after the position offsets in order to
170 // give the effect of the pilot looking around.
171 // In lookat view they are applied before the position offsets so that
172 // the effect is the eye moving around looking at the object (ie the model)
173 // from different angles.
174 virtual double getRollOffset_deg () const { return _roll_offset_deg; }
175 virtual double getPitchOffset_deg () const { return _pitch_offset_deg; }
176 virtual double getHeadingOffset_deg () const { return _heading_offset_deg; }
177 virtual double getGoalRollOffset_deg () const { return _goal_roll_offset_deg; }
178 virtual double getGoalPitchOffset_deg () const { return _goal_pitch_offset_deg; }
179 virtual double getGoalHeadingOffset_deg () const {return _goal_heading_offset_deg; }
180 virtual void setRollOffset_deg (double roll_offset_deg);
181 virtual void setPitchOffset_deg (double pitch_offset_deg);
182 virtual void setHeadingOffset_deg (double heading_offset_deg);
183 virtual void setGoalRollOffset_deg (double goal_roll_offset_deg);
184 virtual void setGoalPitchOffset_deg (double goal_pitch_offset_deg);
185 virtual void setGoalHeadingOffset_deg (double goal_heading_offset_deg);
186 virtual void setOrientationOffsets (double roll_offset_deg,
187 double heading_offset_deg,
188 double pitch_offset_deg);
192 //////////////////////////////////////////////////////////////////////
193 // Part 3: output vectors and matrices in FlightGear coordinates.
194 //////////////////////////////////////////////////////////////////////
196 // Vectors and positions...
199 virtual float * get_view_pos() {if ( _dirty ) { recalc(); } return _view_pos; }
200 // Get the absolute view position in fgfs coordinates.
201 virtual double * get_absolute_view_pos ();
203 virtual float * get_zero_elev() {if ( _dirty ) { recalc(); } return _zero_elev; }
204 // Get world up vector
205 virtual float *get_world_up() {if ( _dirty ) { recalc(); } return _world_up; }
206 // Get the relative (to scenery center) view position in fgfs coordinates.
207 virtual float * getRelativeViewPos ();
208 // Get the absolute zero-elevation view position in fgfs coordinates.
209 virtual float * getZeroElevViewPos ();
210 // Get surface east vector
211 virtual float *get_surface_east() { if ( _dirty ) { recalc(); } return _surface_east; }
212 // Get surface south vector
213 virtual float *get_surface_south() {if ( _dirty ) { recalc(); } return _surface_south; }
216 virtual const sgVec4 *get_VIEW() { if ( _dirty ) { recalc(); } return VIEW; }
217 virtual const sgVec4 *get_VIEW_ROT() { if ( _dirty ) { recalc(); } return VIEW_ROT; }
218 virtual const sgVec4 *get_LOCAL_ROT() { if ( _dirty ) { recalc(); } return LOCAL_ROT; }
219 virtual const sgVec4 *get_UP() { if ( _dirty ) { recalc(); } return UP; }
221 //////////////////////////////////////////////////////////////////////
222 // Part 4: frustrum data setters and getters
223 //////////////////////////////////////////////////////////////////////
225 virtual void set_fov( double fov_deg ) {
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.
232 virtual void set_aspect_ratio( double r ) {
235 virtual double get_aspect_ratio() const { return _aspect_ratio; }
239 //////////////////////////////////////////////////////////////////
241 //////////////////////////////////////////////////////////////////
243 // flag forcing a recalc of derived view parameters
246 mutable sgdVec3 _absolute_view_pos;
247 mutable sgVec3 _relative_view_pos;
248 mutable sgVec3 _zero_elev_view_pos;
253 double _target_lon_deg;
254 double _target_lat_deg;
255 double _target_alt_ft;
260 double _target_roll_deg;
261 double _target_pitch_deg;
262 double _target_heading_deg;
264 // Position offsets from center of gravity. The X axis is positive
265 // out the tail, Y is out the right wing, and Z is positive up.
266 // distance in meters
271 // orientation offsets from reference (_goal* are for smoothed transitions)
272 double _roll_offset_deg;
273 double _pitch_offset_deg;
274 double _heading_offset_deg;
275 double _goal_roll_offset_deg;
276 double _goal_pitch_offset_deg;
277 double _goal_heading_offset_deg;
280 fgScalingType _scaling_type;
282 // the nominal field of view (angle, in degrees)
285 // ratio of window width and height; height = width * aspect_ratio
286 double _aspect_ratio;
288 bool _reverse_view_offset;
290 // view position in opengl world coordinates (this is the
291 // abs_view_pos translated to scenery.center)
294 // cartesion coordinates of current lon/lat if at sea level
295 // translated to scenery.center
298 // surface vector heading south
299 sgVec3 _surface_south;
301 // surface vector heading east (used to unambiguously align sky
303 sgVec3 _surface_east;
305 // world up vector (normal to the plane tangent to the earth's
306 // surface at the spot we are directly above
309 // up vector for the view (usually point straight up through the
310 // top of the aircraft
313 // // the vector pointing straight out the nose of the aircraft
314 // sgVec3 _view_forward;
316 // sg versions of our friendly matrices
317 sgMat4 VIEW, VIEW_ROT, UP, LOCAL_ROT;
318 sgMat4 LOCAL, TRANS, LARC_TO_SSG;
320 // Transformation matrix for the view direction offset relative to
321 // the AIRCRAFT matrix
324 //////////////////////////////////////////////////////////////////
325 // private functions //
326 //////////////////////////////////////////////////////////////////
329 void recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const;
331 inline void set_dirty() { _dirty = true; }
332 inline void set_clean() { _dirty = false; }
334 // add to _heading_offset_deg
335 inline void incHeadingOffset_deg( double amt ) {
337 _heading_offset_deg += amt;
340 // add to _pitch_offset_deg
341 inline void incPitchOffset_deg( double amt ) {
343 _pitch_offset_deg += amt;
346 inline void set_reverse_view_offset( bool val ) {
347 _reverse_view_offset = val;
353 #endif // _VIEWER_HXX