]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.hxx
2b6a5f52d150cdbf4b0953d60f68595fdb91fff3
[flightgear.git] / src / Main / viewer.hxx
1 // viewer.hxx -- class for managing a viewer in the flightgear world.
2 //
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
7 //
8 // Copyright (C) 1997 - 2000  Curtis L. Olson  - curt@flightgear.org
9 //
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.
14 //
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.
19 //
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.
23 //
24 // $Id$
25
26
27 #ifndef _VIEWER_HXX
28 #define _VIEWER_HXX
29
30
31 #ifndef __cplusplus                                                          
32 # error This library requires C++
33 #endif                                   
34
35 #include <simgear/compiler.h>
36 #include <simgear/constants.h>
37
38 #include <plib/sg.h>            // plib include
39
40 #include "fgfs.hxx"
41
42
43 #define FG_FOV_MIN 0.1
44 #define FG_FOV_MAX 179.9
45
46
47 // Define a structure containing view information
48 class FGViewer : public FGSubsystem {
49
50 public:
51
52     enum fgViewType {
53         FG_RPH = 0,
54         FG_LOOKAT = 1,
55         FG_HPR = 2
56     };
57
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
63     };
64
65     // Constructor
66     FGViewer( void );
67
68     // Destructor
69     virtual ~FGViewer( void );
70
71     //////////////////////////////////////////////////////////////////////
72     // Part 1: standard FGSubsystem implementation.
73     //////////////////////////////////////////////////////////////////////
74
75     virtual void init ();
76     virtual void bind ();
77     virtual void unbind ();
78     void update (int dt);
79
80
81     //////////////////////////////////////////////////////////////////////
82     // Part 2: user settings.
83     //////////////////////////////////////////////////////////////////////
84
85     virtual fgViewType getType() const { return _type; }
86     virtual void setType( int type );
87
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);
98
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);
109
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);
118
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,
127                                      double y_offset_m,
128                                      double z_offset_m);
129
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);
148
149
150
151     //////////////////////////////////////////////////////////////////////
152     // Part 3: output vectors and matrices in FlightGear coordinates.
153     //////////////////////////////////////////////////////////////////////
154
155     // Vectors and positions...
156
157             // Get zero view_pos
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 ();
161             // Get zero elev
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; }
173
174             // Matrices...
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; }
178             // (future?)
179             // virtual double get_ground_elev() { if ( _dirty ) { recalc(); } return ground_elev; }
180
181
182 private:
183
184     // flag forcing a recalc of derived view parameters
185     bool _dirty;
186
187     void recalc ();
188     void recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const;
189
190     mutable sgdVec3 _absolute_view_pos;
191     mutable sgVec3 _relative_view_pos;
192     mutable sgVec3 _zero_elev_view_pos;
193
194     double _lon_deg;
195     double _lat_deg;
196     double _alt_ft;
197     double _target_lon_deg;
198     double _target_lat_deg;
199     double _target_alt_ft;
200
201     double _roll_deg;
202     double _pitch_deg;
203     double _heading_deg;
204
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
208     double _x_offset_m;
209     double _y_offset_m;
210     double _z_offset_m;
211
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;
219
220 protected:
221
222     fgViewType _type;
223     fgScalingType scalingType;
224
225     // the nominal field of view (angle, in degrees)
226     double fov; 
227
228     // ratio of window width and height; height = width * aspect_ratio
229     double aspect_ratio;
230
231     bool reverse_view_offset;
232
233     // view position in opengl world coordinates (this is the
234     // abs_view_pos translated to scenery.center)
235     sgVec3 view_pos;
236
237     // radius to sea level from center of the earth (m)
238     double sea_level_radius;
239
240     // cartesion coordinates of current lon/lat if at sea level
241     // translated to scenery.center
242     sgVec3 zero_elev;
243
244     // height ASL of the terrain for our current view position
245     // (future?) double ground_elev;
246
247     // surface vector heading south
248     sgVec3 surface_south;
249
250     // surface vector heading east (used to unambiguously align sky
251     // with sun)
252     sgVec3 surface_east;
253
254     // world up vector (normal to the plane tangent to the earth's
255     // surface at the spot we are directly above
256     sgVec3 world_up;
257
258     // sg versions of our friendly matrices
259     sgMat4 VIEW, VIEW_ROT, UP;
260
261     // up vector for the view (usually point straight up through the
262     // top of the aircraft
263     sgVec3 view_up;
264
265     // the vector pointing straight out the nose of the aircraft
266     sgVec3 view_forward;
267
268     // Transformation matrix for the view direction offset relative to
269     // the AIRCRAFT matrix
270     sgMat4 VIEW_OFFSET;
271
272     // sg versions of our friendly matrices (from lookat)
273     sgMat4 LOCAL, TRANS, LARC_TO_SSG;
274
275     inline void set_dirty() { _dirty = true; }
276     inline void set_clean() { _dirty = false; }
277
278     // from lookat
279     void fgMakeLookAtMat4 ( sgMat4 dst, const sgVec3 eye, const sgVec3 center,
280                         const sgVec3 up );
281
282     // from rph
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);
286
287
288 public:
289
290
291     //////////////////////////////////////////////////////////////////////
292     // setter functions
293     //////////////////////////////////////////////////////////////////////
294
295     inline void set_fov( double fov_deg ) {
296         fov = fov_deg;
297     }
298
299     inline void set_aspect_ratio( double r ) {
300         aspect_ratio = r;
301     }
302     inline void inc_view_offset( double amt ) {
303         set_dirty();
304         _heading_offset_deg += amt;
305     }
306     inline void set_reverse_view_offset( bool val ) {
307         reverse_view_offset = val;
308     }
309     inline void inc_view_tilt( double amt ) {
310         set_dirty();
311         _pitch_offset_deg += amt;
312     }
313     inline void set_sea_level_radius( double r ) {
314         // data should be in meters from the center of the earth
315         set_dirty();
316         sea_level_radius = r;
317     }
318
319     /* from lookat */
320     inline void set_view_forward( sgVec3 vf ) {
321         set_dirty();
322         sgCopyVec3( view_forward, vf );
323     }
324     inline void set_view_up( sgVec3 vf ) {
325         set_dirty();
326         sgCopyVec3( view_up, vf );
327     }
328     /* end from lookat */
329
330
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.
342     double get_h_fov();
343     // Get vertical field of view angle, in degrees.
344     double get_v_fov();
345
346     /* from lookat */
347     inline float *get_view_forward() { return view_forward; }
348     inline float *get_view_up() { return view_up; }
349     /* end from lookat */
350
351     //////////////////////////////////////////////////////////////////////
352     // derived values accessor functions
353     //////////////////////////////////////////////////////////////////////
354
355 };
356
357
358 #endif // _VIEWER_HXX
359
360
361