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