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