]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/location.hxx
Mathias:
[simgear.git] / simgear / scene / model / 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 _SG_LOCATION_HXX
25 #define _SG_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/math/point3d.hxx>
35
36 #include <plib/sg.h>            // plib include
37
38
39 // Define a structure containing view information
40 class SGLocation
41 {
42
43 public:
44
45     // Constructor
46     SGLocation( void );
47
48     // Destructor
49     virtual ~SGLocation( void );
50
51     //////////////////////////////////////////////////////////////////////
52     // Part 1: standard FGSubsystem implementation.
53     //////////////////////////////////////////////////////////////////////
54
55     virtual void init ();
56     virtual void bind ();
57     virtual void unbind ();
58     void update (int dt);
59
60
61     //////////////////////////////////////////////////////////////////////
62     // Part 2: user settings.
63     //////////////////////////////////////////////////////////////////////
64
65     // Geodetic position of model...
66     virtual double getLongitude_deg () const { return _lon_deg; }
67     virtual double getLatitude_deg () const { return _lat_deg; }
68     virtual double getAltitudeASL_ft () const { return _alt_ft; }
69     virtual void setPosition (double lon_deg, double lat_deg, double alt_ft);
70
71
72     // Reference orientation rotations...
73     //   These are rotations that represent the plane attitude effect on
74     //   the view (in Pilot view).  IE The view frustrum rotates as the plane
75     //   turns, pitches, and rolls.
76     //   In model view (lookat/chaseview) these end up changing the angle that
77     //   the eye is looking at the ojbect (ie the model).
78     //   FIXME: the FGModel class should have its own version of these so that
79     //   it can generate it's own model rotations.
80     virtual double getRoll_deg () const { return _roll_deg; }
81     virtual double getPitch_deg () const {return _pitch_deg; }
82     virtual double getHeading_deg () const {return _heading_deg; }
83     virtual void setOrientation (double roll_deg, double pitch_deg, double heading_deg);
84
85
86     //////////////////////////////////////////////////////////////////////
87     // Part 3: output vectors and matrices in FlightGear coordinates.
88     //////////////////////////////////////////////////////////////////////
89
90     // Vectors and positions...
91
92     // Get zero view_pos
93     virtual float * get_view_pos() { return _relative_view_pos; }
94     // Get the absolute view position in fgfs coordinates.
95     virtual double * get_absolute_view_pos( const Point3D scenery_center );
96     // Get zero elev
97     virtual float * get_zero_elev() { return _zero_elev_view_pos; }
98     // Get world up vector
99     virtual float *get_world_up() { return _world_up; }
100     // Get the relative (to scenery center) view position in fgfs coordinates.
101     virtual float * getRelativeViewPos( const Point3D scenery_center );
102     // Get the absolute zero-elevation view position in fgfs coordinates.
103     virtual float * getZeroElevViewPos( const Point3D scenery_center );
104     // Get surface east vector
105     virtual float *get_surface_east() { return _surface_east; }
106     // Get surface south vector
107     virtual float *get_surface_south() { return _surface_south; }
108     // Elevation of ground under location (based on scenery output)...
109     void set_cur_elev_m ( double elev ) { _cur_elev_m = elev; }
110     inline double get_cur_elev_m () { return _cur_elev_m; }
111     // Interface to current buckets for use with tilemgr...
112     void set_tile_center ( Point3D tile_center ) { set_dirty(); _tile_center = tile_center; }
113     inline Point3D get_tile_center () { return _tile_center; }
114
115     // Matrices...
116     virtual const sgVec4 *getTransformMatrix( const Point3D scenery_center ) {
117         if ( _dirty || scenery_center != _scenery_center ) {
118             _scenery_center = scenery_center;
119             recalc( scenery_center );
120         }
121         return TRANS;
122     }
123     virtual const sgVec4 *getCachedTransformMatrix() { return TRANS; }
124     virtual const sgVec4 *getUpMatrix( const Point3D scenery_center )  {
125         if ( _dirty || scenery_center != _scenery_center ) {
126             _scenery_center = scenery_center;
127             recalc( scenery_center );
128         }
129         return UP;
130     }
131     virtual const sgVec4 *getCachedUpMatrix() { return UP; }
132
133
134 private:
135
136     //////////////////////////////////////////////////////////////////
137     // private data                                                 //
138     //////////////////////////////////////////////////////////////////
139
140     // flag forcing a recalc of derived view parameters
141     bool _dirty;
142
143     mutable sgdVec3 _absolute_view_pos;
144     mutable sgVec3 _relative_view_pos;
145     mutable sgVec3 _zero_elev_view_pos;
146
147     double _lon_deg;
148     double _lat_deg;
149     double _alt_ft;
150
151     double _roll_deg;
152     double _pitch_deg;
153     double _heading_deg;
154
155     // elevation of ground under this location...
156     double _cur_elev_m;
157     Point3D _tile_center;
158     Point3D _scenery_center;
159
160     // surface vector heading south
161     sgVec3 _surface_south;
162
163     // surface vector heading east (used to unambiguously align sky
164     // with sun)
165     sgVec3 _surface_east;
166
167     // world up vector (normal to the plane tangent to the earth's
168     // surface at the spot we are directly above
169     sgVec3 _world_up;
170
171     // sg versions of our friendly matrices
172     sgMat4 TRANS, UP;
173
174     //////////////////////////////////////////////////////////////////
175     // private functions                                            //
176     //////////////////////////////////////////////////////////////////
177
178     void recalc( const Point3D scenery_center );
179     void recalcPosition( double lon_deg, double lat_deg, double alt_ft,
180                          const Point3D scenery_center ) const;
181
182     inline void set_dirty() { _dirty = true; }
183     inline void set_clean() { _dirty = false; }
184
185 };
186
187
188 #endif // _SG_LOCATION_HXX