]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.hxx
7c1a5e58d27cb39f38f110bd23bbb64f7c015c09
[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 //
6 // Copyright (C) 1997 - 2000  Curtis L. Olson  - curt@flightgear.org
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24
25 #ifndef _VIEWER_HXX
26 #define _VIEWER_HXX
27
28
29 #ifndef __cplusplus                                                          
30 # error This library requires C++
31 #endif                                   
32
33 #include <simgear/compiler.h>
34 #include <simgear/constants.h>
35
36 #include <plib/sg.h>            // plib include
37
38 #include "fgfs.hxx"
39
40
41 #define FG_FOV_MIN 0.1
42 #define FG_FOV_MAX 179.9
43
44
45 /**
46  * Representation of a single viewpoint in the FlightGear world.
47  */
48 class FGViewPoint
49 {
50 public:
51   FGViewPoint ();
52   virtual ~FGViewPoint ();
53
54   /**
55    * Set the geodetic position for the view point.
56    */
57   virtual void setPosition (double lon_deg, double lat_deg, double alt_ft);
58
59
60   /**
61    * Get the longitude in degrees.
62    */
63   virtual double getLongitude_deg () const { return _lon_deg; }
64
65   /**
66    * Get the latitude in degrees.
67    */
68   virtual double getLatitude_deg () const { return _lat_deg; }
69
70   /**
71    * Get the altitude in feet ASL.
72    */
73   virtual double getAltitudeASL_ft () const { return _alt_ft; }
74
75   /**
76    * Get the absolute view position in fgfs coordinates.
77    */
78   virtual const double * getAbsoluteViewPos () const;
79
80
81   /**
82    * Get the relative view position in fgfs coordinates.
83    *
84    * The position is relative to the scenery centre.
85    */
86   virtual const float * getRelativeViewPos () const;
87
88
89   /**
90    * Get the absolute zero-elevation view position in fgfs coordinates.
91    */
92   virtual const float * getZeroElevViewPos () const;
93
94 private:
95
96   void recalc () const;
97
98   mutable sgdVec3 _absolute_view_pos;
99   mutable sgVec3 _relative_view_pos;
100   mutable sgVec3 _zero_elev_view_pos;
101
102   bool _dirty;
103   double _lon_deg;
104   double _lat_deg;
105   double _alt_ft;
106
107 };
108
109
110 // Define a structure containing view information
111 class FGViewer {
112
113 public:
114
115     enum fgViewType {
116         FG_RPH = 0,
117         FG_LOOKAT = 1,
118         FG_HPR = 2
119     };
120
121     enum fgScalingType {  // nominal Field Of View actually applies to ...
122         FG_SCALING_WIDTH,       // window width
123         FG_SCALING_MAX,         // max(width, height)
124         // FG_SCALING_G_MEAN,      // geometric_mean(width, height)
125         // FG_SCALING_INDEPENDENT  // whole screen
126     };
127
128 private:
129
130     // flag forcing a recalc of derived view parameters
131     bool dirty;
132
133 protected:
134
135     fgViewType _type;
136     fgScalingType scalingType;
137
138     FGViewPoint view_point;
139
140     // the nominal field of view (angle, in degrees)
141     double fov; 
142
143     // ratio of window width and height; height = width * aspect_ratio
144     double aspect_ratio;
145
146     // the current view offset angle from forward (rotated about the
147     // view_up vector)
148     double view_offset;
149     bool reverse_view_offset;
150
151     // the goal view offset angle  (used for smooth view changes)
152     double goal_view_offset;
153
154     // the view tilt angles
155     double view_tilt;
156     double goal_view_tilt;
157
158     // geodetic view position
159     sgdVec3 geod_view_pos;
160
161     // absolute view position in earth coordinates
162     sgdVec3 abs_view_pos;
163
164     // view position in opengl world coordinates (this is the
165     // abs_view_pos translated to scenery.center)
166     sgVec3 view_pos;
167
168     // radius to sea level from center of the earth (m)
169     double sea_level_radius;
170
171     // cartesion coordinates of current lon/lat if at sea level
172     // translated to scenery.center
173     sgVec3 zero_elev;
174
175     // height ASL of the terrain for our current view position
176     // (future?) double ground_elev;
177
178     // pilot offset from center of gravity.  The X axis is positive
179     // out the tail, Y is out the right wing, and Z is positive up.
180     // Distances in meters of course.
181     sgVec3 pilot_offset;
182
183     // surface vector heading south
184     sgVec3 surface_south;
185
186     // surface vector heading east (used to unambiguously align sky
187     // with sun)
188     sgVec3 surface_east;
189
190     // world up vector (normal to the plane tangent to the earth's
191     // surface at the spot we are directly above
192     sgVec3 world_up;
193
194     // sg versions of our friendly matrices
195     sgMat4 VIEW, VIEW_ROT, UP;
196
197     inline void set_dirty() { dirty = true; }
198     inline void set_clean() { dirty = false; }
199
200     // Update the view volume, position, and orientation
201     virtual void update() = 0;
202
203 public:
204
205     // Constructor
206     FGViewer( void );
207
208     // Destructor
209     virtual ~FGViewer( void );
210
211     virtual void init ();
212     virtual void bind ();
213     virtual void unbind ();
214     virtual void update (int dt);
215
216     //////////////////////////////////////////////////////////////////////
217     // setter functions
218     //////////////////////////////////////////////////////////////////////
219
220     inline void set_fov( double fov_deg ) {
221         fov = fov_deg;
222     }
223
224     inline void set_aspect_ratio( double r ) {
225         aspect_ratio = r;
226     }
227     inline void set_view_offset( double a ) {
228         set_dirty();
229         view_offset = a;
230     }
231     inline void inc_view_offset( double amt ) {
232         set_dirty();
233         view_offset += amt;
234     }
235     inline void set_goal_view_offset( double a) {
236         set_dirty();
237         goal_view_offset = a;
238         while ( goal_view_offset < 0.0 ) {
239             goal_view_offset += SGD_2PI;
240         }
241         while ( goal_view_offset > SGD_2PI ) {
242             goal_view_offset -= SGD_2PI;
243         }
244     }
245     inline void set_reverse_view_offset( bool val ) {
246         reverse_view_offset = val;
247     }
248     inline void set_view_tilt( double a ) {
249         set_dirty();
250         view_tilt = a;
251     }
252     inline void inc_view_tilt( double amt ) {
253         set_dirty();
254         view_tilt += amt;
255     }
256     inline void set_goal_view_tilt( double a) {
257         set_dirty();
258         goal_view_tilt = a;
259         while ( goal_view_tilt < 0 ) {
260             goal_view_tilt += 360.0;
261         }
262         while ( goal_view_tilt > 360.0 ) {
263             goal_view_tilt -= 360.0;
264         }
265     }
266     inline void set_geod_view_pos( double lon, double lat, double alt ) {
267         // data should be in radians and meters asl
268         set_dirty();
269         // cout << "set_geod_view_pos = " << lon << ", " << lat << ", " << alt
270         //      << endl;
271         sgdSetVec3( geod_view_pos, lon, lat, alt );
272     }
273     inline void set_pilot_offset( float x, float y, float z ) {
274         set_dirty();
275         sgSetVec3( pilot_offset, x, y, z );
276     }
277     inline void set_sea_level_radius( double r ) {
278         // data should be in meters from the center of the earth
279         set_dirty();
280         sea_level_radius = r;
281     }
282
283     //////////////////////////////////////////////////////////////////////
284     // accessor functions
285     //////////////////////////////////////////////////////////////////////
286     inline int get_type() const { return _type ; }
287     inline int is_a( int t ) const { return get_type() == t ; }
288     inline bool is_dirty() const { return dirty; }
289     inline double get_fov() const { return fov; }
290     inline double get_aspect_ratio() const { return aspect_ratio; }
291     inline double get_view_offset() const { return view_offset; }
292     inline bool get_reverse_view_offset() const { return reverse_view_offset; }
293     inline double get_goal_view_offset() const { return goal_view_offset; }
294     inline double get_view_tilt() const { return view_tilt; }
295     inline double get_goal_view_tilt() const { return goal_view_tilt; }
296     inline double *get_geod_view_pos() { return geod_view_pos; }
297     inline float *get_pilot_offset() { return pilot_offset; }
298     inline double get_sea_level_radius() const { return sea_level_radius; }
299     // Get horizontal field of view angle, in degrees.
300     double get_h_fov();
301     // Get vertical field of view angle, in degrees.
302     double get_v_fov();
303
304     //////////////////////////////////////////////////////////////////////
305     // derived values accessor functions
306     //////////////////////////////////////////////////////////////////////
307     inline double *get_abs_view_pos() {
308         if ( dirty ) { update(); }
309         return abs_view_pos;
310     }
311     inline float *get_view_pos() {
312         if ( dirty ) { update(); }
313         return view_pos;
314     }
315     inline float *get_zero_elev() {
316         if ( dirty ) { update(); }
317         return zero_elev;
318     }
319     // (future?)
320     // inline double get_ground_elev() {
321     //  if ( dirty ) { update(); }
322     //  return ground_elev;
323     // }
324     inline float *get_surface_south() {
325         if ( dirty ) { update(); }
326         return surface_south;
327     }
328     inline float *get_surface_east() {
329         if ( dirty ) { update(); }
330         return surface_east;
331     }
332     inline float *get_world_up() {
333         if ( dirty ) { update(); }
334         return world_up;
335     }
336     inline const sgVec4 *get_VIEW() {
337         if ( dirty ) { update(); }
338         return VIEW;
339     }
340     inline const sgVec4 *get_VIEW_ROT() {
341         if ( dirty ) { update(); }
342         return VIEW_ROT;
343     }
344     inline const sgVec4 *get_UP() {
345         if ( dirty ) { update(); }
346         return UP;
347     }
348 };
349
350
351 #endif // _VIEWER_HXX
352
353