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., 675 Mass Ave, Cambridge, MA 02139, 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, double fov_deg,
69 double target_x_offset_m, double target_y_offset_m,
70 double target_z_offset_m, double near_m, bool internal );
73 virtual ~FGViewer( void );
75 //////////////////////////////////////////////////////////////////////
76 // Part 1: standard SGSubsystem implementation.
77 //////////////////////////////////////////////////////////////////////
81 virtual void unbind ();
82 void update (double dt);
85 //////////////////////////////////////////////////////////////////////
86 // Part 2: user settings.
87 //////////////////////////////////////////////////////////////////////
89 virtual fgViewType getType() const { return _type; }
90 virtual void setType( int type );
92 virtual bool getInternal() const { return _internal; }
93 virtual void setInternal( bool internal );
95 // Reference geodetic position of view from position...
96 // These are the actual aircraft position (pilot in
97 // pilot view, model in model view).
98 // FIXME: the model view position (ie target positions)
99 // should be in the model class.
100 virtual double getLongitude_deg () const { return _lon_deg; }
101 virtual double getLatitude_deg () const { return _lat_deg; }
102 virtual double getAltitudeASL_ft () const { return _alt_ft; }
103 virtual void setLongitude_deg (double lon_deg);
104 virtual void setLatitude_deg (double lat_deg);
105 virtual void setAltitude_ft (double alt_ft);
106 virtual void setPosition (double lon_deg, double lat_deg, double alt_ft);
108 // Reference geodetic target position...
109 virtual double getTargetLongitude_deg () const { return _target_lon_deg; }
110 virtual double getTargetLatitude_deg () const { return _target_lat_deg; }
111 virtual double getTargetAltitudeASL_ft () const { return _target_alt_ft; }
112 virtual void setTargetLongitude_deg (double lon_deg);
113 virtual void setTargetLatitude_deg (double lat_deg);
114 virtual void setTargetAltitude_ft (double alt_ft);
115 virtual void setTargetPosition (double lon_deg, double lat_deg, double alt_ft);
120 // Position offsets from reference
121 // These offsets position they "eye" in the scene according to a given
122 // location. For example in pilot view they are used to position the
123 // head inside the aircraft.
124 // Note that in pilot view these are applied "before" the orientation
125 // rotations (see below) so that the orientation rotations have the
126 // effect of the pilot staying in his seat and "looking out" in
127 // different directions.
128 // In chase view these are applied "after" the application of the
129 // orientation rotations listed below. This has the effect of the
130 // eye moving around and "looking at" the object (model) from
132 virtual double getXOffset_m () const { return _x_offset_m; }
133 virtual double getYOffset_m () const { return _y_offset_m; }
134 virtual double getZOffset_m () const { return _z_offset_m; }
135 virtual double getTargetXOffset_m () const { return _target_x_offset_m; }
136 virtual double getTargetYOffset_m () const { return _target_y_offset_m; }
137 virtual double getTargetZOffset_m () const { return _target_z_offset_m; }
138 virtual void setXOffset_m (double x_offset_m);
139 virtual void setYOffset_m (double y_offset_m);
140 virtual void setZOffset_m (double z_offset_m);
141 virtual void setTargetXOffset_m (double x_offset_m);
142 virtual void setTargetYOffset_m (double y_offset_m);
143 virtual void setTargetZOffset_m (double z_offset_m);
144 virtual void setPositionOffsets (double x_offset_m,
151 // Reference orientation rotations...
152 // These are rotations that represent the plane attitude effect on
153 // the view (in Pilot view). IE The view frustrum rotates as the plane
154 // turns, pitches, and rolls.
155 // In model view (lookat/chaseview) these end up changing the angle that
156 // the eye is looking at the ojbect (ie the model).
157 // FIXME: the FGModel class should have its own version of these so that
158 // it can generate it's own model rotations.
159 virtual double getRoll_deg () const { return _roll_deg; }
160 virtual double getPitch_deg () const {return _pitch_deg; }
161 virtual double getHeading_deg () const {return _heading_deg; }
162 virtual void setRoll_deg (double roll_deg);
163 virtual void setPitch_deg (double pitch_deg);
164 virtual void setHeading_deg (double heading_deg);
165 virtual void setOrientation (double roll_deg, double pitch_deg, double heading_deg);
166 virtual double getTargetRoll_deg () const { return _target_roll_deg; }
167 virtual double getTargetPitch_deg () const {return _target_pitch_deg; }
168 virtual double getTargetHeading_deg () const {return _target_heading_deg; }
169 virtual void setTargetRoll_deg (double roll_deg);
170 virtual void setTargetPitch_deg (double pitch_deg);
171 virtual void setTargetHeading_deg (double heading_deg);
172 virtual void setTargetOrientation (double roll_deg, double pitch_deg, double heading_deg);
177 // Orientation offsets rotations from reference orientation.
178 // Goal settings are for smooth transition from prior
179 // offset when changing view direction.
180 // These offsets are in ADDITION to the orientation rotations listed
182 // In pilot view they are applied after the position offsets in order to
183 // give the effect of the pilot looking around.
184 // In lookat view they are applied before the position offsets so that
185 // the effect is the eye moving around looking at the object (ie the model)
186 // from different angles.
187 virtual double getRollOffset_deg () const { return _roll_offset_deg; }
188 virtual double getPitchOffset_deg () const { return _pitch_offset_deg; }
189 virtual double getHeadingOffset_deg () const { return _heading_offset_deg; }
190 virtual double getGoalRollOffset_deg () const { return _goal_roll_offset_deg; }
191 virtual double getGoalPitchOffset_deg () const { return _goal_pitch_offset_deg; }
192 virtual double getGoalHeadingOffset_deg () const {return _goal_heading_offset_deg; }
193 virtual void setRollOffset_deg (double roll_offset_deg);
194 virtual void setPitchOffset_deg (double pitch_offset_deg);
195 virtual void setHeadingOffset_deg (double heading_offset_deg);
196 virtual void setGoalRollOffset_deg (double goal_roll_offset_deg);
197 virtual void setGoalPitchOffset_deg (double goal_pitch_offset_deg);
198 virtual void setGoalHeadingOffset_deg (double goal_heading_offset_deg);
199 virtual void setOrientationOffsets (double roll_offset_deg,
200 double heading_offset_deg,
201 double pitch_offset_deg);
205 //////////////////////////////////////////////////////////////////////
206 // Part 3: output vectors and matrices in FlightGear coordinates.
207 //////////////////////////////////////////////////////////////////////
209 // Vectors and positions...
212 virtual float * get_view_pos() {if ( _dirty ) { recalc(); } return _view_pos; }
213 // Get the absolute view position in fgfs coordinates.
214 virtual double * get_absolute_view_pos ();
216 virtual float * get_zero_elev() {if ( _dirty ) { recalc(); } return _zero_elev; }
217 // Get world up vector
218 virtual float *get_world_up() {if ( _dirty ) { recalc(); } return _world_up; }
219 // Get the relative (to scenery center) view position in fgfs coordinates.
220 virtual float * getRelativeViewPos ();
221 // Get the absolute zero-elevation view position in fgfs coordinates.
222 virtual float * getZeroElevViewPos ();
223 // Get surface east vector
224 virtual float *get_surface_east() { if ( _dirty ) { recalc(); } return _surface_east; }
225 // Get surface south vector
226 virtual float *get_surface_south() {if ( _dirty ) { recalc(); } return _surface_south; }
229 virtual const sgVec4 *get_VIEW() { if ( _dirty ) { recalc(); } return VIEW; }
230 virtual const sgVec4 *get_UP() { if ( _dirty ) { recalc(); } return UP; }
232 //////////////////////////////////////////////////////////////////////
233 // Part 4: View and frustrum data setters and getters
234 //////////////////////////////////////////////////////////////////////
236 virtual void set_fov( double fov_deg ) {
239 virtual double get_fov() const { return _fov_deg; }
240 virtual double get_h_fov(); // Get horizontal fov, in degrees.
241 virtual double get_v_fov(); // Get vertical fov, in degrees.
243 virtual void set_aspect_ratio( double r ) {
246 virtual double get_aspect_ratio() const { return _aspect_ratio; }
248 virtual double getNear_m () const { return _ground_level_nearplane_m; }
249 inline void setNear_m (double near_m) {
250 _ground_level_nearplane_m = near_m;
253 //////////////////////////////////////////////////////////////////////
254 // Part 5: misc setters and getters
255 //////////////////////////////////////////////////////////////////////
257 inline void set_dirty() { _dirty = true; }
258 inline void set_clean() { _dirty = false; }
260 // return eye location...
261 virtual SGLocation * getSGLocation () const { return _location; }
266 //////////////////////////////////////////////////////////////////
268 //////////////////////////////////////////////////////////////////
270 // flag forcing a recalc of derived view parameters
273 mutable sgdVec3 _absolute_view_pos;
274 mutable sgVec3 _relative_view_pos;
275 mutable sgVec3 _zero_elev_view_pos;
280 double _target_lon_deg;
281 double _target_lat_deg;
282 double _target_alt_ft;
287 double _target_roll_deg;
288 double _target_pitch_deg;
289 double _target_heading_deg;
294 double _damp_heading;
296 double _damped_roll_deg;
297 double _damped_pitch_deg;
298 double _damped_heading_deg;
300 // Position offsets from FDM origin. The X axis is positive
301 // out the tail, Y is out the right wing, and Z is positive up.
302 // distance in meters
307 // Target offsets from FDM origin (for "lookat" targets) The X
308 // axis is positive out the tail, Y is out the right wing, and Z
309 // is positive up. distance in meters
310 double _target_x_offset_m;
311 double _target_y_offset_m;
312 double _target_z_offset_m;
315 // orientation offsets from reference (_goal* are for smoothed transitions)
316 double _roll_offset_deg;
317 double _pitch_offset_deg;
318 double _heading_offset_deg;
319 double _goal_roll_offset_deg;
320 double _goal_pitch_offset_deg;
321 double _goal_heading_offset_deg;
323 // used to set nearplane when at ground level for this view
324 double _ground_level_nearplane_m;
327 fgScalingType _scaling_type;
329 // internal view (e.g. cockpit) flag
332 // view is looking from a model
334 int _from_model_index; // number of model (for multi model)
336 // view is looking at a model
338 int _at_model_index; // number of model (for multi model)
340 SGLocation * _location;
341 SGLocation * _target_location;
343 // the nominal field of view (angle, in degrees)
346 // ratio of window width and height; height = width * aspect_ratio
347 double _aspect_ratio;
349 bool _reverse_view_offset;
351 // view position in opengl world coordinates (this is the
352 // abs_view_pos translated to scenery.center)
355 // cartesion coordinates of current lon/lat if at sea level
356 // translated to scenery.center
359 // surface vector heading south
360 sgVec3 _surface_south;
362 // surface vector heading east (used to unambiguously align sky
364 sgVec3 _surface_east;
366 // world up vector (normal to the plane tangent to the earth's
367 // surface at the spot we are directly above
370 // up vector for the view (usually point straight up through the
371 // top of the aircraft
374 // sg versions of our friendly matrices
376 sgMat4 LOCAL, ATLOCAL, TRANS, LARC_TO_SSG;
378 // Transformation matrix for the view direction offset relative to
379 // the AIRCRAFT matrix
382 //////////////////////////////////////////////////////////////////
383 // private functions //
384 //////////////////////////////////////////////////////////////////
387 void recalcLookFrom();
389 void copyLocationData();
390 void updateFromModelLocation (SGLocation * location);
391 void updateAtModelLocation (SGLocation * location);
392 void recalcOurOwnLocation (SGLocation * location, double lon_deg, double lat_deg, double alt_ft,
393 double roll_deg, double pitch_deg, double heading_deg);
394 void dampEyeData (double &roll_deg, double &pitch_deg, double &heading_deg);
396 // add to _heading_offset_deg
397 inline void incHeadingOffset_deg( double amt ) {
399 _heading_offset_deg += amt;
402 // add to _pitch_offset_deg
403 inline void incPitchOffset_deg( double amt ) {
405 _pitch_offset_deg += amt;
408 // add to _roll_offset_deg
409 inline void incRollOffset_deg( double amt ) {
411 _roll_offset_deg += amt;
414 inline void set_reverse_view_offset( bool val ) {
415 _reverse_view_offset = val;
421 #endif // _VIEWER_HXX