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 virtual double getLongitude_deg () const { return _lon_deg; }
90 virtual double getLatitude_deg () const { return _lat_deg; }
91 virtual double getAltitudeASL_ft () const { return _alt_ft; }
92 // Set individual coordinates for the view point position.
93 virtual void setLongitude_deg (double lon_deg);
94 virtual void setLatitude_deg (double lat_deg);
95 virtual void setAltitude_ft (double alt_ft);
96 // Set the geodetic position for the view point.
97 virtual void setPosition (double lon_deg, double lat_deg, double alt_ft);
99 // Reference geodetic target position...
100 virtual double getTargetLongitude_deg () const { return _target_lon_deg; }
101 virtual double getTargetLatitude_deg () const { return _target_lat_deg; }
102 virtual double getTargetAltitudeASL_ft () const { return _target_alt_ft; }
103 // Set individual coordinates for the Target point position.
104 virtual void setTargetLongitude_deg (double lon_deg);
105 virtual void setTargetLatitude_deg (double lat_deg);
106 virtual void setTargetAltitude_ft (double alt_ft);
107 // Set the geodetic position for the Target point.
108 virtual void setTargetPosition (double lon_deg, double lat_deg, double alt_ft);
110 // Refence orientation...
111 virtual double getRoll_deg () const { return _roll_deg; }
112 virtual double getPitch_deg () const {return _pitch_deg; }
113 virtual double getHeading_deg () const {return _heading_deg; }
114 virtual void setRoll_deg (double roll_deg);
115 virtual void setPitch_deg (double pitch_deg);
116 virtual void setHeading_deg (double heading_deg);
117 virtual void setOrientation (double roll_deg, double pitch_deg, double heading_deg);
119 // Position offsets from reference
120 virtual double getXOffset_m () const { return _x_offset_m; }
121 virtual double getYOffset_m () const { return _y_offset_m; }
122 virtual double getZOffset_m () const { return _z_offset_m; }
123 virtual void setXOffset_m (double x_offset_m);
124 virtual void setYOffset_m (double y_offset_m);
125 virtual void setZOffset_m (double z_offset_m);
126 virtual void setPositionOffsets (double x_offset_m,
130 // Orientation offsets from reference
131 // Goal settings are for smooth transition from prior
132 // offset when changing view direction.
133 virtual double getRollOffset_deg () const { return _roll_offset_deg; }
134 virtual double getPitchOffset_deg () const { return _pitch_offset_deg; }
135 virtual double getHeadingOffset_deg () const { return _heading_offset_deg; }
136 virtual double getGoalRollOffset_deg () const { return _goal_roll_offset_deg; }
137 virtual double getGoalPitchOffset_deg () const { return _goal_pitch_offset_deg; }
138 virtual double getGoalHeadingOffset_deg () const {return _goal_heading_offset_deg; }
139 virtual void setRollOffset_deg (double roll_offset_deg);
140 virtual void setPitchOffset_deg (double pitch_offset_deg);
141 virtual void setHeadingOffset_deg (double heading_offset_deg);
142 virtual void setGoalRollOffset_deg (double goal_roll_offset_deg);
143 virtual void setGoalPitchOffset_deg (double goal_pitch_offset_deg);
144 virtual void setGoalHeadingOffset_deg (double goal_heading_offset_deg);
145 virtual void setOrientationOffsets (double roll_offset_deg,
146 double heading_offset_deg,
147 double pitch_offset_deg);
151 //////////////////////////////////////////////////////////////////////
152 // Part 3: output vectors and matrices in FlightGear coordinates.
153 //////////////////////////////////////////////////////////////////////
155 // Vectors and positions...
158 virtual float * get_view_pos() {if ( _dirty ) { recalc(); } return view_pos; }
159 // Get the absolute view position in fgfs coordinates.
160 virtual double * get_absolute_view_pos ();
162 virtual float * get_zero_elev() {if ( _dirty ) { recalc(); } return zero_elev; }
163 // Get world up vector
164 virtual float *get_world_up() {if ( _dirty ) { recalc(); } return world_up; }
165 // Get the relative (to scenery center) view position in fgfs coordinates.
166 virtual float * getRelativeViewPos ();
167 // Get the absolute zero-elevation view position in fgfs coordinates.
168 virtual float * getZeroElevViewPos ();
169 // Get surface east vector
170 virtual float *get_surface_east() { if ( _dirty ) { recalc(); } return surface_east; }
171 // Get surface south vector
172 virtual float *get_surface_south() {if ( _dirty ) { recalc(); } return surface_south; }
175 virtual const sgVec4 *get_VIEW() { if ( _dirty ) { recalc(); } return VIEW; }
176 virtual const sgVec4 *get_VIEW_ROT() { if ( _dirty ) { recalc(); } return VIEW_ROT; }
177 virtual const sgVec4 *get_UP() { if ( _dirty ) { recalc(); } return UP; }
179 // virtual double get_ground_elev() { if ( _dirty ) { recalc(); } return ground_elev; }
184 // flag forcing a recalc of derived view parameters
188 void recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const;
190 mutable sgdVec3 _absolute_view_pos;
191 mutable sgVec3 _relative_view_pos;
192 mutable sgVec3 _zero_elev_view_pos;
197 double _target_lon_deg;
198 double _target_lat_deg;
199 double _target_alt_ft;
205 // Position offsets from center of gravity. The X axis is positive
206 // out the tail, Y is out the right wing, and Z is positive up.
207 // distance in meters
212 // orientation offsets from reference (_goal* are for smoothed transitions)
213 double _roll_offset_deg;
214 double _pitch_offset_deg;
215 double _heading_offset_deg;
216 double _goal_roll_offset_deg;
217 double _goal_pitch_offset_deg;
218 double _goal_heading_offset_deg;
223 fgScalingType scalingType;
225 // the nominal field of view (angle, in degrees)
228 // ratio of window width and height; height = width * aspect_ratio
231 bool reverse_view_offset;
233 // view position in opengl world coordinates (this is the
234 // abs_view_pos translated to scenery.center)
237 // radius to sea level from center of the earth (m)
238 double sea_level_radius;
240 // cartesion coordinates of current lon/lat if at sea level
241 // translated to scenery.center
244 // height ASL of the terrain for our current view position
245 // (future?) double ground_elev;
247 // surface vector heading south
248 sgVec3 surface_south;
250 // surface vector heading east (used to unambiguously align sky
254 // world up vector (normal to the plane tangent to the earth's
255 // surface at the spot we are directly above
258 // sg versions of our friendly matrices
259 sgMat4 VIEW, VIEW_ROT, UP;
261 // up vector for the view (usually point straight up through the
262 // top of the aircraft
265 // the vector pointing straight out the nose of the aircraft
268 // Transformation matrix for the view direction offset relative to
269 // the AIRCRAFT matrix
272 // sg versions of our friendly matrices (from lookat)
273 sgMat4 LOCAL, TRANS, LARC_TO_SSG;
275 inline void set_dirty() { _dirty = true; }
276 inline void set_clean() { _dirty = false; }
279 void fgMakeLookAtMat4 ( sgMat4 dst, const sgVec3 eye, const sgVec3 center,
283 void fgMakeViewRot( sgMat4 dst, const sgMat4 m1, const sgMat4 m2 );
284 void fgMakeLOCAL( sgMat4 dst, const double Theta,
285 const double Phi, const double Psi);
291 //////////////////////////////////////////////////////////////////////
293 //////////////////////////////////////////////////////////////////////
295 inline void set_fov( double fov_deg ) {
299 inline void set_aspect_ratio( double r ) {
302 inline void inc_view_offset( double amt ) {
304 _heading_offset_deg += amt;
306 inline void set_reverse_view_offset( bool val ) {
307 reverse_view_offset = val;
309 inline void inc_view_tilt( double amt ) {
311 _pitch_offset_deg += amt;
313 inline void set_sea_level_radius( double r ) {
314 // data should be in meters from the center of the earth
316 sea_level_radius = r;
320 inline void set_view_forward( sgVec3 vf ) {
322 sgCopyVec3( view_forward, vf );
324 inline void set_view_up( sgVec3 vf ) {
326 sgCopyVec3( view_up, vf );
328 /* end from lookat */
331 //////////////////////////////////////////////////////////////////////
332 // accessor functions
333 //////////////////////////////////////////////////////////////////////
334 inline int get_type() const { return _type ; }
335 inline int is_a( int t ) const { return get_type() == t ; }
336 inline bool is_dirty() const { return _dirty; }
337 inline double get_fov() const { return fov; }
338 inline double get_aspect_ratio() const { return aspect_ratio; }
339 inline bool get_reverse_view_offset() const { return reverse_view_offset; }
340 inline double get_sea_level_radius() const { return sea_level_radius; }
341 // Get horizontal field of view angle, in degrees.
343 // Get vertical field of view angle, in degrees.
347 inline float *get_view_forward() { return view_forward; }
348 inline float *get_view_up() { return view_up; }
349 /* end from lookat */
351 //////////////////////////////////////////////////////////////////////
352 // derived values accessor functions
353 //////////////////////////////////////////////////////////////////////
358 #endif // _VIEWER_HXX