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