]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.hxx
8475b24c37b6a17fbe9e3adf9a1932f4872d390d
[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
38 #define FG_FOV_MIN 0.1
39 #define FG_FOV_MAX 179.9
40
41
42 // Define a structure containing view information
43 class FGViewer {
44
45 public:
46
47     enum fgViewType {
48         FG_RPH = 0,
49         FG_LOOKAT = 1,
50         FG_HPR = 2
51     };
52
53 private:
54
55     // flag forcing a recalc of derived view parameters
56     bool dirty;
57
58 protected:
59
60     fgViewType _type;
61
62     // the field of view in the x (width) direction
63     double fov; 
64
65     // ratio of x and y fov's; fov(y) = fov(x) * win_ratio
66     double win_ratio;
67
68     // the current view offset angle from forward (rotated about the
69     // view_up vector)
70     double view_offset;
71
72     // the goal view offset angle  (used for smooth view changes)
73     double goal_view_offset;
74
75     // geodetic view position
76     sgdVec3 geod_view_pos;
77
78     // absolute view position in earth coordinates
79     sgdVec3 abs_view_pos;
80
81     // view position in opengl world coordinates (this is the
82     // abs_view_pos translated to scenery.center)
83     sgVec3 view_pos;
84
85     // radius to sea level from center of the earth (m)
86     double sea_level_radius;
87
88     // cartesion coordinates of current lon/lat if at sea level
89     // translated to scenery.center
90     sgVec3 zero_elev;
91
92     // pilot offset from center of gravity.  The X axis is positive
93     // out the tail, Y is out the right wing, and Z is positive up.
94     // Distances in meters of course.
95     sgVec3 pilot_offset;
96
97     // surface vector heading south
98     sgVec3 surface_south;
99
100     // surface vector heading east (used to unambiguously align sky
101     // with sun)
102     sgVec3 surface_east;
103
104     // world up vector (normal to the plane tangent to the earth's
105     // surface at the spot we are directly above
106     sgVec3 world_up;
107
108     // sg versions of our friendly matrices
109     sgMat4 VIEW, VIEW_ROT, UP;
110
111     inline void set_dirty() { dirty = true; }
112     inline void set_clean() { dirty = false; }
113
114     // Update the view volume, position, and orientation
115     virtual void update();
116
117 public:
118
119     // Constructor
120     FGViewer( void );
121
122     // Destructor
123     virtual ~FGViewer( void );
124
125     //////////////////////////////////////////////////////////////////////
126     // setter functions
127     //////////////////////////////////////////////////////////////////////
128     inline void set_fov( double amount ) { fov = amount; }
129     inline void set_win_ratio( double r ) { win_ratio = r; }
130     inline void set_view_offset( double a ) {
131         set_dirty();
132         view_offset = a;
133     }
134     inline void inc_view_offset( double amt ) {
135         set_dirty();
136         view_offset += amt;
137     }
138     inline void set_goal_view_offset( double a) {
139         set_dirty();
140         goal_view_offset = a;
141     }
142     inline void set_geod_view_pos( double lon, double lat, double alt ) {
143         // data should be in radians and meters asl
144         set_dirty();
145         // cout << "set_geod_view_pos = " << lon << ", " << lat << ", " << alt
146         //      << endl;
147         sgdSetVec3( geod_view_pos, lon, lat, alt );
148     }
149     inline void set_pilot_offset( float x, float y, float z ) {
150         set_dirty();
151         sgSetVec3( pilot_offset, x, y, z );
152     }
153     inline void set_sea_level_radius( double r ) {
154         // data should be in meters from the center of the earth
155         set_dirty();
156         sea_level_radius = r;
157     }
158
159     //////////////////////////////////////////////////////////////////////
160     // accessor functions
161     //////////////////////////////////////////////////////////////////////
162     inline int get_type() const { return _type ; }
163     inline int is_a( int t ) const { return get_type() == t ; }
164     inline bool is_dirty() const { return dirty; }
165     inline double get_fov() const { return fov; }
166     inline double get_win_ratio() const { return win_ratio; }
167     inline double get_view_offset() const { return view_offset; }
168     inline double get_goal_view_offset() const { return goal_view_offset; }
169     inline double *get_geod_view_pos() { return geod_view_pos; }
170     inline float *get_pilot_offset() { return pilot_offset; }
171     inline double get_sea_level_radius() const { return sea_level_radius; }
172
173     //////////////////////////////////////////////////////////////////////
174     // derived values accessor functions
175     //////////////////////////////////////////////////////////////////////
176     inline double *get_abs_view_pos() {
177         if ( dirty ) { update(); }
178         return abs_view_pos;
179     }
180     inline float *get_view_pos() {
181         if ( dirty ) { update(); }
182         return view_pos;
183     }
184     inline float *get_zero_elev() {
185         if ( dirty ) { update(); }
186         return zero_elev;
187     }
188     inline float *get_surface_south() {
189         if ( dirty ) { update(); }
190         return surface_south;
191     }
192     inline float *get_surface_east() {
193         if ( dirty ) { update(); }
194         return surface_east;
195     }
196     inline float *get_world_up() {
197         if ( dirty ) { update(); }
198         return world_up;
199     }
200     inline const sgVec4 *get_VIEW() {
201         if ( dirty ) { update(); }
202         return VIEW;
203     }
204     inline const sgVec4 *get_VIEW_ROT() {
205         if ( dirty ) { update(); }
206         return VIEW_ROT;
207     }
208     inline const sgVec4 *get_UP() {
209         if ( dirty ) { update(); }
210         return UP;
211     }
212 };
213
214
215 #endif // _VIEWER_HXX
216
217