]> git.mxchange.org Git - flightgear.git/blob - src/Main/location.hxx
Erik Hofman:
[flightgear.git] / src / Main / location.hxx
1 // location.hxx -- class for determining model location in the flightgear world.
2 //
3 // Written by Jim Wilson, David Megginson, started April 2002.
4 //
5 // Copyright (C) 2002  Jim Wilson, David Megginson
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef _LOCATION_HXX
25 #define _LOCATION_HXX
26
27
28 #ifndef __cplusplus                                                          
29 # error This library requires C++
30 #endif                                   
31
32 #include <simgear/compiler.h>
33 #include <simgear/constants.h>
34 #include <simgear/bucket/newbucket.hxx>
35 #include <simgear/math/point3d.hxx>
36
37 #include <plib/sg.h>            // plib include
38
39 #include "fgfs.hxx"
40
41
42 // Define a structure containing view information
43 class FGLocation
44 {
45
46 public:
47
48     // Constructor
49     FGLocation( void );
50
51     // Destructor
52     virtual ~FGLocation( void );
53
54     //////////////////////////////////////////////////////////////////////
55     // Part 1: standard FGSubsystem implementation.
56     //////////////////////////////////////////////////////////////////////
57
58     virtual void init ();
59     virtual void bind ();
60     virtual void unbind ();
61     void update (int dt);
62
63
64     //////////////////////////////////////////////////////////////////////
65     // Part 2: user settings.
66     //////////////////////////////////////////////////////////////////////
67
68     // Geodetic position of model...
69     virtual double getLongitude_deg () const { return _lon_deg; }
70     virtual double getLatitude_deg () const { return _lat_deg; }
71     virtual double getAltitudeASL_ft () const { return _alt_ft; }
72     virtual void setPosition (double lon_deg, double lat_deg, double alt_ft);
73
74
75     // Reference orientation rotations...
76     //   These are rotations that represent the plane attitude effect on
77     //   the view (in Pilot view).  IE The view frustrum rotates as the plane
78     //   turns, pitches, and rolls.
79     //   In model view (lookat/chaseview) these end up changing the angle that
80     //   the eye is looking at the ojbect (ie the model).
81     //   FIXME: the FGModel class should have its own version of these so that
82     //   it can generate it's own model rotations.
83     virtual double getRoll_deg () const { return _roll_deg; }
84     virtual double getPitch_deg () const {return _pitch_deg; }
85     virtual double getHeading_deg () const {return _heading_deg; }
86     virtual void setOrientation (double roll_deg, double pitch_deg, double heading_deg);
87
88
89     //////////////////////////////////////////////////////////////////////
90     // Part 3: output vectors and matrices in FlightGear coordinates.
91     //////////////////////////////////////////////////////////////////////
92
93     // Vectors and positions...
94
95     // Get zero view_pos
96     virtual float * get_view_pos() { return _relative_view_pos; }
97     // Get the absolute view position in fgfs coordinates.
98     virtual double * get_absolute_view_pos ();
99     // Get zero elev
100     virtual float * get_zero_elev() { return _zero_elev_view_pos; }
101     // Get world up vector
102     virtual float *get_world_up() { return _world_up; }
103     // Get the relative (to scenery center) view position in fgfs coordinates.
104     virtual float * getRelativeViewPos ();
105     // Get the absolute zero-elevation view position in fgfs coordinates.
106     virtual float * getZeroElevViewPos ();
107     // Get surface east vector
108     virtual float *get_surface_east() { return _surface_east; }
109     // Get surface south vector
110     virtual float *get_surface_south() { return _surface_south; }
111     // Elevation of ground under location (based on scenery output)...
112     void set_cur_elev_m ( double elev ) { _cur_elev_m = elev; }
113     inline double get_cur_elev_m () { return _cur_elev_m; }
114     // Interface to current buckets for use with tilemgr...
115     void set_current_bucket ( SGBucket current_bucket ) { _current_bucket = current_bucket; }
116     inline SGBucket get_current_bucket () { return _current_bucket; }
117     void set_previous_bucket ( SGBucket previous_bucket ) { _previous_bucket = previous_bucket; }
118     inline SGBucket get_previous_bucket () { return _previous_bucket; }
119     void set_tile_center ( Point3D tile_center ) { _tile_center = tile_center; }
120     inline Point3D get_tile_center () { return _tile_center; }
121
122     // Matrices...
123     virtual const sgVec4 * getTransformMatrix() { if ( _dirty ) { recalc(); }   return TRANS; }
124     virtual const sgVec4 * getCachedTransformMatrix() { return TRANS; }
125     virtual const sgVec4 * getUpMatrix()  { if ( _dirty ) { recalc(); } return UP; }
126     virtual const sgVec4 * getCachedUpMatrix()  { return UP; }
127
128
129 private:
130
131     //////////////////////////////////////////////////////////////////
132     // private data                                                 //
133     //////////////////////////////////////////////////////////////////
134
135     // flag forcing a recalc of derived view parameters
136     bool _dirty;
137
138     mutable sgdVec3 _absolute_view_pos;
139     mutable sgVec3 _relative_view_pos;
140     mutable sgVec3 _zero_elev_view_pos;
141
142     double _lon_deg;
143     double _lat_deg;
144     double _alt_ft;
145
146     double _roll_deg;
147     double _pitch_deg;
148     double _heading_deg;
149
150     // elevation of ground under this location...
151     double _cur_elev_m;
152     // current and previous scenery buckets to be saved for use in
153     // getting current elevation from tilemgr.
154     SGBucket _previous_bucket;
155     SGBucket _current_bucket;
156     Point3D _tile_center;
157
158     // surface vector heading south
159     sgVec3 _surface_south;
160
161     // surface vector heading east (used to unambiguously align sky
162     // with sun)
163     sgVec3 _surface_east;
164
165     // world up vector (normal to the plane tangent to the earth's
166     // surface at the spot we are directly above
167     sgVec3 _world_up;
168
169     // sg versions of our friendly matrices
170     sgMat4 TRANS, UP;
171
172     //////////////////////////////////////////////////////////////////
173     // private functions                                            //
174     //////////////////////////////////////////////////////////////////
175
176     void recalc ();
177     void recalcPosition (double lon_deg, double lat_deg, double alt_ft) const;
178
179     inline void set_dirty() { _dirty = true; }
180     inline void set_clean() { _dirty = false; }
181
182 };
183
184
185 #endif // _LOCATION_HXX