]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.hxx
Checkpoint commit for cleaning up the FGViewer class. External views are
[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/math/point3d.hxx>
35 #include <simgear/timing/sg_time.hxx>
36
37 #include <list>
38
39 #include <plib/sg.h>            // plib include
40
41 #include <FDM/flight.hxx>
42 #include <Time/light.hxx>
43
44
45 FG_USING_STD(list);
46
47
48 #define FG_FOV_MIN 0.1
49 #define FG_FOV_MAX 179.9
50
51
52 // Define a structure containing view information
53 class FGViewer {
54
55 private:
56
57     // flag forcing a recalc of derived view parameters
58     bool dirty;
59         
60     // the current view offset angle from forward (rotated about the
61     // view_up vector)
62     double view_offset;
63
64     // the goal view offset angle  (used for smooth view changes)
65     double goal_view_offset;
66
67     // geodetic view position
68     Point3D geod_view_pos;
69
70     // radius to sea level from center of the earth (m)
71     double sea_level_radius;
72
73     // absolute view position in earth coordinates
74     Point3D abs_view_pos;
75
76     // view position in opengl world coordinates (this is the
77     // abs_view_pos translated to scenery.center)
78     Point3D view_pos;
79
80     // pilot offset from center of gravity.  The X axis is positive
81     // out the tail, Y is out the right wing, and Z is positive up.
82     // Distances in meters of course.
83     sgVec3 pilot_offset;
84
85     // view orientation (heading, pitch, roll)
86     sgVec3 hpr;
87
88     // cartesion coordinates of current lon/lat if at sea level
89     // translated to scenery.center
90     Point3D cur_zero_elev;
91
92     // vector in cartesian coordinates from current position to the
93     // postion on the earth's surface the sun is directly over
94     sgVec3 to_sun;
95
96     // surface direction to go to head towards sun
97     sgVec3 surface_to_sun;
98
99     // vector in cartesian coordinates from current position to the
100     // postion on the earth's surface the moon is directly over
101     sgVec3 to_moon;
102   
103     // surface direction to go to head towards moon
104     sgVec3 surface_to_moon;
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     // local up vector (normal to the plane tangent to the earth's
114     // surface at the spot we are directly above
115     sgVec3 local_up;
116
117     // up vector for the view (usually point straight up through the
118     // top of the aircraft
119     sgVec3 view_up;
120
121     // the vector pointing straight out the nose of the aircraft
122     sgVec3 view_forward;
123
124     // Transformation matrix for the view direction offset relative to
125     // the AIRCRAFT matrix
126     sgMat4 VIEW_OFFSET;
127
128     // sg versions of our friendly matrices
129     sgMat4 LOCAL, UP, VIEW_ROT, TRANS, VIEW, LARC_TO_SSG;
130
131     // Update the view volume, position, and orientation
132     void update();
133
134 public:
135
136     // Constructor
137     FGViewer( void );
138
139     // Destructor
140     ~FGViewer( void );
141
142     // Initialize a view class
143     void init( void );
144
145     // Transform a vector from world coordinates to the local plane
146     void CurrentNormalInLocalPlane(sgVec3 dst, sgVec3 src);
147
148     //////////////////////////////////////////////////////////////////////
149     // setter functions
150     //////////////////////////////////////////////////////////////////////
151     inline void set_geod_view_pos( double lon, double lat, double alt ) {
152         // data should be in radians and meters asl
153         dirty = true;
154         // cout << "set_geod_view_pos = " << lon << ", " << lat << ", " << alt
155         //      << endl;
156         geod_view_pos = Point3D( lon, lat, alt );
157     }
158     inline void set_sea_level_radius( double r ) {
159         // data should be in meters from the center of the earth
160         dirty = true;
161         sea_level_radius = r;
162     }
163     inline void set_hpr( double h, double p, double r ) {
164         // data should be in radians
165         dirty = true;
166         sgSetVec3( hpr, h, p, r );
167     }
168     inline void set_pilot_offset( float x, float y, float z ) {
169         dirty = true;
170         sgSetVec3( pilot_offset, x, y, z );
171     }
172     inline void set_view_offset( double a ) {
173         dirty = true;
174         view_offset = a;
175     }
176     inline void inc_view_offset( double amt ) {
177         dirty = true;
178         view_offset += amt;
179     }
180     inline void set_goal_view_offset( double a) {
181         dirty = true;
182         goal_view_offset = a;
183     }
184
185     //////////////////////////////////////////////////////////////////////
186     // accessor functions
187     //////////////////////////////////////////////////////////////////////
188     inline double get_view_offset() const { return view_offset; }
189     inline double get_goal_view_offset() const { return goal_view_offset; }
190     inline float *get_pilot_offset() { return pilot_offset; }
191     inline double get_sea_level_radius() const { return sea_level_radius; }
192     inline float *get_hpr() { return hpr; }
193
194     //////////////////////////////////////////////////////////////////////
195     // derived values accessor functions
196     //////////////////////////////////////////////////////////////////////
197     inline Point3D get_abs_view_pos() {
198         if ( dirty ) { update(); }
199         return abs_view_pos;
200     }
201     inline Point3D get_view_pos() {
202         if ( dirty ) { update(); }
203         return view_pos;
204     }
205     inline Point3D get_cur_zero_elev() {
206         if ( dirty ) { update(); }
207         return cur_zero_elev;
208     }
209     inline float *get_surface_south() {
210         if ( dirty ) { update(); }
211         return surface_south;
212     }
213     inline float *get_surface_east() {
214         if ( dirty ) { update(); }
215         return surface_east;
216     }
217     inline float *get_local_up() {
218         if ( dirty ) { update(); }
219         return local_up;
220     }
221     inline float *get_view_forward() {
222         if ( dirty ) { update(); }
223         return view_forward;
224     }
225     inline const sgVec4 *get_VIEW() {
226         if ( dirty ) { update(); }
227         return VIEW;
228     }
229     inline const sgVec4 *get_VIEW_ROT() {
230         if ( dirty ) { update(); }
231         return VIEW_ROT;
232     }
233
234     //////////////////////////////////////////////////////////////////////
235     // need to fix these
236     //////////////////////////////////////////////////////////////////////
237     inline float *get_to_sun() { return to_sun; }
238     inline void set_to_sun( float x, float y, float z ) {
239         sgSetVec3( to_sun, x, y, z );
240     }
241     inline float *get_surface_to_sun() { return surface_to_sun; }
242     inline void set_surface_to_sun( float x, float y, float z) {
243         sgSetVec3( surface_to_sun, x, y, z );
244     }
245     inline float *get_to_moon() { return to_moon; }
246     inline void set_to_moon( float x, float y, float z) {
247         sgSetVec3( to_moon, x, y, z );
248     }
249     inline float *get_surface_to_moon() { return surface_to_moon; }
250     inline void set_surface_to_moon( float x, float y, float z) {
251         sgSetVec3( surface_to_moon, x, y, z );
252     }
253 };
254
255
256 #endif // _VIEWER_HXX
257
258