]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.hxx
Erik Hofman:
[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 //   partially rewritten by Jim Wilson jim@kelcomaine.com using interface
6 //                          by David Megginson March 2002
7 //
8 // Copyright (C) 1997 - 2000  Curtis L. Olson  - curt@flightgear.org
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #ifndef _VIEWER_HXX
28 #define _VIEWER_HXX
29
30
31 #ifndef __cplusplus                                                          
32 # error This library requires C++
33 #endif                                   
34
35 #include <simgear/compiler.h>
36 #include <simgear/constants.h>
37
38 #include <plib/sg.h>            // plib include
39
40 #include "fgfs.hxx"
41
42 #include <Main/location.hxx>
43
44 #define FG_FOV_MIN 0.1
45 #define FG_FOV_MAX 179.9
46
47 enum fgViewType {
48  FG_LOOKFROM = 0,
49  FG_LOOKAT = 1,
50 };
51
52 // Define a structure containing view information
53 class FGViewer : public FGSubsystem {
54
55 public:
56
57     enum fgScalingType {  // nominal Field Of View actually applies to ...
58         FG_SCALING_WIDTH,       // window width
59         FG_SCALING_MAX,         // max(width, height)
60         // FG_SCALING_G_MEAN,      // geometric_mean(width, height)
61         // FG_SCALING_INDEPENDENT  // whole screen
62     };
63
64     // Constructor
65     FGViewer( fgViewType Type, bool from_model, int from_model_index,
66               bool at_model, int at_model_index, double x_offset_m,
67               double y_offset_m, double z_offset_m, double near_m );
68
69     // Destructor
70     virtual ~FGViewer( void );
71
72     //////////////////////////////////////////////////////////////////////
73     // Part 1: standard FGSubsystem implementation.
74     //////////////////////////////////////////////////////////////////////
75
76     virtual void init ();
77     virtual void bind ();
78     virtual void unbind ();
79     void update (double dt);
80
81
82     //////////////////////////////////////////////////////////////////////
83     // Part 2: user settings.
84     //////////////////////////////////////////////////////////////////////
85
86     virtual fgViewType getType() const { return _type; }
87     virtual void setType( int type );
88
89     // Reference geodetic position of view from position...
90     //   These are the actual aircraft position (pilot in
91     //   pilot view, model in model view).
92     //   FIXME: the model view position (ie target positions) 
93     //   should be in the model class.
94     virtual double getLongitude_deg () const { return _lon_deg; }
95     virtual double getLatitude_deg () const { return _lat_deg; }
96     virtual double getAltitudeASL_ft () const { return _alt_ft; }
97     virtual void setLongitude_deg (double lon_deg);
98     virtual void setLatitude_deg (double lat_deg);
99     virtual void setAltitude_ft (double alt_ft);
100     virtual void setPosition (double lon_deg, double lat_deg, double alt_ft);
101
102     // Reference geodetic target position...
103     virtual double getTargetLongitude_deg () const { return _target_lon_deg; }
104     virtual double getTargetLatitude_deg () const { return _target_lat_deg; }
105     virtual double getTargetAltitudeASL_ft () const { return _target_alt_ft; }
106     virtual void setTargetLongitude_deg (double lon_deg);
107     virtual void setTargetLatitude_deg (double lat_deg);
108     virtual void setTargetAltitude_ft (double alt_ft);
109     virtual void setTargetPosition (double lon_deg, double lat_deg, double alt_ft);
110
111
112
113
114     // Position offsets from reference
115     //   These offsets position they "eye" in the scene according to a given
116     //   location.  For example in pilot view they are used to position the 
117     //   head inside the aircraft.
118     //   Note that in pilot view these are applied "before" the orientation 
119     //   rotations (see below) so that the orientation rotations have the 
120     //   effect of the pilot staying in his seat and "looking out" in 
121     //   different directions.
122     //   In chase view these are applied "after" the application of the 
123     //   orientation rotations listed below.  This has the effect of the 
124     //   eye moving around and "looking at" the object (model) from 
125     //   different angles.
126     virtual double getXOffset_m () const { return _x_offset_m; }
127     virtual double getYOffset_m () const { return _y_offset_m; }
128     virtual double getZOffset_m () const { return _z_offset_m; }
129     virtual void setXOffset_m (double x_offset_m);
130     virtual void setYOffset_m (double y_offset_m);
131     virtual void setZOffset_m (double z_offset_m);
132     virtual void setPositionOffsets (double x_offset_m,
133                                      double y_offset_m,
134                                      double z_offset_m);
135
136
137
138
139     // Reference orientation rotations...
140     //   These are rotations that represent the plane attitude effect on
141     //   the view (in Pilot view).  IE The view frustrum rotates as the plane
142     //   turns, pitches, and rolls.
143     //   In model view (lookat/chaseview) these end up changing the angle that
144     //   the eye is looking at the ojbect (ie the model).
145     //   FIXME: the FGModel class should have its own version of these so that
146     //   it can generate it's own model rotations.
147     virtual double getRoll_deg () const { return _roll_deg; }
148     virtual double getPitch_deg () const {return _pitch_deg; }
149     virtual double getHeading_deg () const {return _heading_deg; }
150     virtual void setRoll_deg (double roll_deg);
151     virtual void setPitch_deg (double pitch_deg);
152     virtual void setHeading_deg (double heading_deg);
153     virtual void setOrientation (double roll_deg, double pitch_deg, double heading_deg);
154     virtual double getTargetRoll_deg () const { return _target_roll_deg; }
155     virtual double getTargetPitch_deg () const {return _target_pitch_deg; }
156     virtual double getTargetHeading_deg () const {return _target_heading_deg; }
157     virtual void setTargetRoll_deg (double roll_deg);
158     virtual void setTargetPitch_deg (double pitch_deg);
159     virtual void setTargetHeading_deg (double heading_deg);
160     virtual void setTargetOrientation (double roll_deg, double pitch_deg, double heading_deg);
161
162
163
164
165     // Orientation offsets rotations from reference orientation.
166     // Goal settings are for smooth transition from prior 
167     // offset when changing view direction.
168     //   These offsets are in ADDITION to the orientation rotations listed 
169     //   above.
170     //   In pilot view they are applied after the position offsets in order to
171     //   give the effect of the pilot looking around.
172     //   In lookat view they are applied before the position offsets so that
173     //   the effect is the eye moving around looking at the object (ie the model)
174     //   from different angles.
175     virtual double getRollOffset_deg () const { return _roll_offset_deg; }
176     virtual double getPitchOffset_deg () const { return _pitch_offset_deg; }
177     virtual double getHeadingOffset_deg () const { return _heading_offset_deg; }
178     virtual double getGoalRollOffset_deg () const { return _goal_roll_offset_deg; }
179     virtual double getGoalPitchOffset_deg () const { return _goal_pitch_offset_deg; }
180     virtual double getGoalHeadingOffset_deg () const {return _goal_heading_offset_deg; }
181     virtual void setRollOffset_deg (double roll_offset_deg);
182     virtual void setPitchOffset_deg (double pitch_offset_deg);
183     virtual void setHeadingOffset_deg (double heading_offset_deg);
184     virtual void setGoalRollOffset_deg (double goal_roll_offset_deg);
185     virtual void setGoalPitchOffset_deg (double goal_pitch_offset_deg);
186     virtual void setGoalHeadingOffset_deg (double goal_heading_offset_deg);
187     virtual void setOrientationOffsets (double roll_offset_deg,
188                                      double heading_offset_deg,
189                                      double pitch_offset_deg);
190
191
192
193     //////////////////////////////////////////////////////////////////////
194     // Part 3: output vectors and matrices in FlightGear coordinates.
195     //////////////////////////////////////////////////////////////////////
196
197     // Vectors and positions...
198
199     // Get zero view_pos
200     virtual float * get_view_pos() {if ( _dirty ) { recalc(); } return _view_pos; }
201     // Get the absolute view position in fgfs coordinates.
202     virtual double * get_absolute_view_pos ();
203     // Get zero elev
204     virtual float * get_zero_elev() {if ( _dirty ) { recalc(); } return _zero_elev; }
205     // Get world up vector
206     virtual float *get_world_up() {if ( _dirty ) { recalc(); } return _world_up; }
207     // Get the relative (to scenery center) view position in fgfs coordinates.
208     virtual float * getRelativeViewPos ();
209     // Get the absolute zero-elevation view position in fgfs coordinates.
210     virtual float * getZeroElevViewPos ();
211     // Get surface east vector
212     virtual float *get_surface_east() { if ( _dirty ) { recalc(); } return _surface_east; }
213     // Get surface south vector
214     virtual float *get_surface_south() {if ( _dirty ) { recalc(); } return _surface_south; }
215
216     // Matrices...
217     virtual const sgVec4 *get_VIEW() { if ( _dirty ) { recalc(); } return VIEW; }
218     virtual const sgVec4 *get_UP() { if ( _dirty ) { recalc(); } return UP; }
219
220     //////////////////////////////////////////////////////////////////////
221     // Part 4: View and frustrum data setters and getters
222     //////////////////////////////////////////////////////////////////////
223
224     virtual void set_fov( double fov_deg ) {
225         _fov_deg = fov_deg;
226     }
227     virtual double get_fov() const { return _fov_deg; }
228     virtual double get_h_fov();    // Get horizontal fov, in degrees.
229     virtual double get_v_fov();    // Get vertical fov, in degrees.
230
231     virtual void set_aspect_ratio( double r ) {
232         _aspect_ratio = r;
233     }
234     virtual double get_aspect_ratio() const { return _aspect_ratio; }
235
236     virtual double getNear_m () const { return _ground_level_nearplane_m; }
237     inline void setNear_m (double near_m) {
238         _ground_level_nearplane_m = near_m;
239     }
240
241     //////////////////////////////////////////////////////////////////////
242     // Part 5: misc setters and getters
243     //////////////////////////////////////////////////////////////////////
244
245     inline void set_dirty() { _dirty = true; }
246     inline void set_clean() { _dirty = false; }
247     
248     // return eye location...
249     virtual FGLocation * getFGLocation () const { return _location; }
250
251
252 private:
253
254     //////////////////////////////////////////////////////////////////
255     // private data                                                 //
256     //////////////////////////////////////////////////////////////////
257
258     // flag forcing a recalc of derived view parameters
259     bool _dirty;
260
261     mutable sgdVec3 _absolute_view_pos;
262     mutable sgVec3 _relative_view_pos;
263     mutable sgVec3 _zero_elev_view_pos;
264
265     double _lon_deg;
266     double _lat_deg;
267     double _alt_ft;
268     double _target_lon_deg;
269     double _target_lat_deg;
270     double _target_alt_ft;
271
272     double _roll_deg;
273     double _pitch_deg;
274     double _heading_deg;
275     double _target_roll_deg;
276     double _target_pitch_deg;
277     double _target_heading_deg;
278
279     // Position offsets from center of gravity.  The X axis is positive
280     // out the tail, Y is out the right wing, and Z is positive up.
281     // distance in meters
282     double _x_offset_m;
283     double _y_offset_m;
284     double _z_offset_m;
285
286     // orientation offsets from reference (_goal* are for smoothed transitions)
287     double _roll_offset_deg;
288     double _pitch_offset_deg;
289     double _heading_offset_deg;
290     double _goal_roll_offset_deg;
291     double _goal_pitch_offset_deg;
292     double _goal_heading_offset_deg;
293
294     // used to set nearplane when at ground level for this view
295     double _ground_level_nearplane_m;
296
297     fgViewType _type;
298     fgScalingType _scaling_type;
299
300     // view is looking from a model
301     bool _from_model;
302     int _from_model_index;  // number of model (for multi model)
303
304     // view is looking at a model
305     bool _at_model;
306     int _at_model_index;  // number of model (for multi model)
307
308     FGLocation * _location;
309     FGLocation * _target_location;
310
311     // the nominal field of view (angle, in degrees)
312     double _fov_deg; 
313
314     // ratio of window width and height; height = width * aspect_ratio
315     double _aspect_ratio;
316
317     bool _reverse_view_offset;
318
319     // view position in opengl world coordinates (this is the
320     // abs_view_pos translated to scenery.center)
321     sgVec3 _view_pos;
322
323     // cartesion coordinates of current lon/lat if at sea level
324     // translated to scenery.center
325     sgVec3 _zero_elev;
326
327     // surface vector heading south
328     sgVec3 _surface_south;
329
330     // surface vector heading east (used to unambiguously align sky
331     // with sun)
332     sgVec3 _surface_east;
333
334     // world up vector (normal to the plane tangent to the earth's
335     // surface at the spot we are directly above
336     sgVec3 _world_up;
337
338     // up vector for the view (usually point straight up through the
339     // top of the aircraft
340     sgVec3 _view_up;
341
342     // sg versions of our friendly matrices
343     sgMat4 VIEW, UP;
344     sgMat4 LOCAL, TRANS, LARC_TO_SSG;
345
346     // Transformation matrix for the view direction offset relative to
347     // the AIRCRAFT matrix
348     sgMat4 VIEW_OFFSET;
349
350     //////////////////////////////////////////////////////////////////
351     // private functions                                            //
352     //////////////////////////////////////////////////////////////////
353
354     void recalc ();
355     void recalcLookFrom();
356     void recalcLookAt();
357     void copyLocationData();
358     void updateFromModelLocation (FGLocation * location);
359     void recalcOurOwnLocation (FGLocation * location, double lon_deg, double lat_deg, double alt_ft,
360                  double roll_deg, double pitch_deg, double heading_deg);
361
362     // add to _heading_offset_deg
363     inline void incHeadingOffset_deg( double amt ) {
364         set_dirty();
365         _heading_offset_deg += amt;
366     }
367
368     // add to _pitch_offset_deg
369     inline void incPitchOffset_deg( double amt ) {
370         set_dirty();
371         _pitch_offset_deg += amt;
372     }
373
374     inline void set_reverse_view_offset( bool val ) {
375         _reverse_view_offset = val;
376     }
377
378 };
379
380
381 #endif // _VIEWER_HXX