]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.hxx
b87f97caab86df7bd5349fda919525b778ad7325
[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 // Define a structure containing view information
46 class FGViewer {
47
48 public:
49
50     enum fgViewType {
51         FG_RPH = 0,
52         FG_LOOKAT = 1,
53         FG_HPR = 2
54     };
55
56     enum fgScalingType {  // nominal Field Of View actually applies to ...
57         FG_SCALING_WIDTH,       // window width
58         FG_SCALING_MAX,         // max(width, height)
59         // FG_SCALING_G_MEAN,      // geometric_mean(width, height)
60         // FG_SCALING_INDEPENDENT  // whole screen
61     };
62
63 private:
64
65     // flag forcing a recalc of derived view parameters
66     bool dirty;
67
68 protected:
69
70     fgViewType _type;
71     fgScalingType scalingType;
72
73     // the nominal field of view (angle, in degrees)
74     double fov; 
75
76     // ratio of window width and height; height = width * aspect_ratio
77     double aspect_ratio;
78
79     // the current view offset angle from forward (rotated about the
80     // view_up vector)
81     double view_offset;
82     bool reverse_view_offset;
83
84     // the goal view offset angle  (used for smooth view changes)
85     double goal_view_offset;
86
87     // the view tilt angles
88     double view_tilt;
89     double goal_view_tilt;
90
91     // geodetic view position
92     sgdVec3 geod_view_pos;
93
94     // absolute view position in earth coordinates
95     sgdVec3 abs_view_pos;
96
97     // view position in opengl world coordinates (this is the
98     // abs_view_pos translated to scenery.center)
99     sgVec3 view_pos;
100
101     // radius to sea level from center of the earth (m)
102     double sea_level_radius;
103
104     // cartesion coordinates of current lon/lat if at sea level
105     // translated to scenery.center
106     sgVec3 zero_elev;
107
108     // height ASL of the terrain for our current view position
109     // (future?) double ground_elev;
110
111     // pilot offset from center of gravity.  The X axis is positive
112     // out the tail, Y is out the right wing, and Z is positive up.
113     // Distances in meters of course.
114     sgVec3 pilot_offset;
115
116     // surface vector heading south
117     sgVec3 surface_south;
118
119     // surface vector heading east (used to unambiguously align sky
120     // with sun)
121     sgVec3 surface_east;
122
123     // world up vector (normal to the plane tangent to the earth's
124     // surface at the spot we are directly above
125     sgVec3 world_up;
126
127     // sg versions of our friendly matrices
128     sgMat4 VIEW, VIEW_ROT, UP;
129
130     inline void set_dirty() { dirty = true; }
131     inline void set_clean() { dirty = false; }
132
133     // Update the view volume, position, and orientation
134     virtual void update() = 0;
135
136 public:
137
138     // Constructor
139     FGViewer( void );
140
141     // Destructor
142     virtual ~FGViewer( void );
143
144     virtual void init ();
145     virtual void bind ();
146     virtual void unbind ();
147     virtual void update (int dt);
148
149     //////////////////////////////////////////////////////////////////////
150     // setter functions
151     //////////////////////////////////////////////////////////////////////
152
153     inline void set_fov( double fov_deg ) {
154         fov = fov_deg;
155     }
156
157     inline void set_aspect_ratio( double r ) {
158         aspect_ratio = r;
159     }
160     inline void set_view_offset( double a ) {
161         set_dirty();
162         view_offset = a;
163     }
164     inline void inc_view_offset( double amt ) {
165         set_dirty();
166         view_offset += amt;
167     }
168     inline void set_goal_view_offset( double a) {
169         set_dirty();
170         goal_view_offset = a;
171         while ( goal_view_offset < 0.0 ) {
172             goal_view_offset += SGD_2PI;
173         }
174         while ( goal_view_offset > SGD_2PI ) {
175             goal_view_offset -= SGD_2PI;
176         }
177     }
178     inline void set_reverse_view_offset( bool val ) {
179         reverse_view_offset = val;
180     }
181     inline void set_view_tilt( double a ) {
182         set_dirty();
183         view_tilt = a;
184     }
185     inline void inc_view_tilt( double amt ) {
186         set_dirty();
187         view_tilt += amt;
188     }
189     inline void set_goal_view_tilt( double a) {
190         set_dirty();
191         goal_view_tilt = a;
192         while ( goal_view_tilt < 0 ) {
193             goal_view_tilt += 360.0;
194         }
195         while ( goal_view_tilt > 360.0 ) {
196             goal_view_tilt -= 360.0;
197         }
198     }
199     inline void set_geod_view_pos( double lon, double lat, double alt ) {
200         // data should be in radians and meters asl
201         set_dirty();
202         // cout << "set_geod_view_pos = " << lon << ", " << lat << ", " << alt
203         //      << endl;
204         sgdSetVec3( geod_view_pos, lon, lat, alt );
205     }
206     inline void set_pilot_offset( float x, float y, float z ) {
207         set_dirty();
208         sgSetVec3( pilot_offset, x, y, z );
209     }
210     inline void set_sea_level_radius( double r ) {
211         // data should be in meters from the center of the earth
212         set_dirty();
213         sea_level_radius = r;
214     }
215
216     //////////////////////////////////////////////////////////////////////
217     // accessor functions
218     //////////////////////////////////////////////////////////////////////
219     inline int get_type() const { return _type ; }
220     inline int is_a( int t ) const { return get_type() == t ; }
221     inline bool is_dirty() const { return dirty; }
222     inline double get_fov() const { return fov; }
223     inline double get_aspect_ratio() const { return aspect_ratio; }
224     inline double get_view_offset() const { return view_offset; }
225     inline bool get_reverse_view_offset() const { return reverse_view_offset; }
226     inline double get_goal_view_offset() const { return goal_view_offset; }
227     inline double get_view_tilt() const { return view_tilt; }
228     inline double get_goal_view_tilt() const { return goal_view_tilt; }
229     inline double *get_geod_view_pos() { return geod_view_pos; }
230     inline float *get_pilot_offset() { return pilot_offset; }
231     inline double get_sea_level_radius() const { return sea_level_radius; }
232     // Get horizontal field of view angle, in degrees.
233     double get_h_fov();
234     // Get vertical field of view angle, in degrees.
235     double get_v_fov();
236
237     //////////////////////////////////////////////////////////////////////
238     // derived values accessor functions
239     //////////////////////////////////////////////////////////////////////
240     inline double *get_abs_view_pos() {
241         if ( dirty ) { update(); }
242         return abs_view_pos;
243     }
244     inline float *get_view_pos() {
245         if ( dirty ) { update(); }
246         return view_pos;
247     }
248     inline float *get_zero_elev() {
249         if ( dirty ) { update(); }
250         return zero_elev;
251     }
252     // (future?)
253     // inline double get_ground_elev() {
254     //  if ( dirty ) { update(); }
255     //  return ground_elev;
256     // }
257     inline float *get_surface_south() {
258         if ( dirty ) { update(); }
259         return surface_south;
260     }
261     inline float *get_surface_east() {
262         if ( dirty ) { update(); }
263         return surface_east;
264     }
265     inline float *get_world_up() {
266         if ( dirty ) { update(); }
267         return world_up;
268     }
269     inline const sgVec4 *get_VIEW() {
270         if ( dirty ) { update(); }
271         return VIEW;
272     }
273     inline const sgVec4 *get_VIEW_ROT() {
274         if ( dirty ) { update(); }
275         return VIEW_ROT;
276     }
277     inline const sgVec4 *get_UP() {
278         if ( dirty ) { update(); }
279         return UP;
280     }
281 };
282
283
284 #endif // _VIEWER_HXX
285
286