]> git.mxchange.org Git - flightgear.git/blob - src/Main/location.cxx
Added static port system and a new altimeter model connected to it.
[flightgear.git] / src / Main / location.cxx
1 // location.cxx -- class for determining model location in the flightgear world.
2 //
3 // Written by Jim Wilson, David Megginson, started April 2002.
4 // Based largely on code by Curtis Olson and Norman Vine.
5 //
6 // Copyright (C) 2002  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 #include <simgear/compiler.h>
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/constants.h>
33 #include <simgear/math/point3d.hxx>
34 #include <simgear/math/polar3d.hxx>
35 #include <simgear/math/sg_geodesy.hxx>
36 #include <simgear/math/vector.hxx>
37
38 #include <Scenery/scenery.hxx>
39
40 #include "globals.hxx"
41
42 #include "location.hxx"
43
44
45 /**
46  * make model transformation Matrix - based on optimizations by NHV
47  */
48 static void MakeTRANS( sgMat4 dst, const double Theta,
49                         const double Phi, const double Psi, 
50                         const sgMat4 UP)
51 {
52     SGfloat cosTheta = (SGfloat) cos(Theta);
53     SGfloat sinTheta = (SGfloat) sin(Theta);
54     SGfloat cosPhi   = (SGfloat) cos(Phi);
55     SGfloat sinPhi   = (SGfloat) sin(Phi);
56     SGfloat sinPsi   = (SGfloat) sin(Psi) ;
57     SGfloat cosPsi   = (SGfloat) cos(Psi) ;
58
59     sgMat4 tmp;
60         
61     tmp[0][0] = cosPhi * cosTheta;
62     tmp[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
63     tmp[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
64
65     tmp[1][0] = -sinPhi * cosTheta;
66     tmp[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
67     tmp[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
68         
69     tmp[2][0] = sinTheta;
70     tmp[2][1] = cosTheta * -sinPsi;
71     tmp[2][2] = cosTheta * cosPsi;
72         
73     float a = UP[0][0];
74     float b = UP[1][0];
75     float c = UP[2][0];
76     dst[2][0] = a*tmp[0][0] + b*tmp[0][1] + c*tmp[0][2] ;
77     dst[1][0] = a*tmp[1][0] + b*tmp[1][1] + c*tmp[1][2] ;
78     dst[0][0] = -(a*tmp[2][0] + b*tmp[2][1] + c*tmp[2][2]) ;
79     dst[3][0] = SG_ZERO ;
80
81     a = UP[0][1];
82     b = UP[1][1];
83     c = UP[2][1];
84     dst[2][1] = a*tmp[0][0] + b*tmp[0][1] + c*tmp[0][2] ;
85     dst[1][1] = a*tmp[1][0] + b*tmp[1][1] + c*tmp[1][2] ;
86     dst[0][1] = -(a*tmp[2][0] + b*tmp[2][1] + c*tmp[2][2]) ;
87     dst[3][1] = SG_ZERO ;
88
89     a = UP[0][2];
90     c = UP[2][2];
91     dst[2][2] = a*tmp[0][0] + c*tmp[0][2] ;
92     dst[1][2] = a*tmp[1][0] + c*tmp[1][2] ;
93     dst[0][2] = -(a*tmp[2][0] + c*tmp[2][2]) ;
94     dst[3][2] = SG_ZERO ;
95
96     dst[2][3] = SG_ZERO ;
97     dst[1][3] = SG_ZERO ;
98     dst[0][3] = SG_ZERO ;
99     dst[3][3] = SG_ONE ;
100
101 }
102
103
104 ////////////////////////////////////////////////////////////////////////
105 // Implementation of FGLocation.
106 ////////////////////////////////////////////////////////////////////////
107
108 // Constructor
109 FGLocation::FGLocation( void ):
110     _dirty(true),
111     _lon_deg(0),
112     _lat_deg(0),
113     _alt_ft(0),
114     _roll_deg(0),
115     _pitch_deg(0),
116     _heading_deg(0),
117     _cur_elev_m(0),
118     _tile_center(0)
119 {
120     sgdZeroVec3(_absolute_view_pos);
121     sgZeroVec3(_relative_view_pos);
122     sgZeroVec3(_zero_elev_view_pos);
123     sgMakeRotMat4( UP, 0.0, 0.0, 0.0 );
124     sgMakeRotMat4( TRANS, 0.0, 0.0, 0.0 );
125 }
126
127
128 // Destructor
129 FGLocation::~FGLocation( void ) {
130 }
131
132 void
133 FGLocation::init ()
134 {
135 }
136
137 void
138 FGLocation::bind ()
139 {
140 }
141
142 void
143 FGLocation::unbind ()
144 {
145 }
146
147 void
148 FGLocation::setPosition (double lon_deg, double lat_deg, double alt_ft)
149 {
150   _dirty = true;
151   _lon_deg = lon_deg;
152   _lat_deg = lat_deg;
153   _alt_ft = alt_ft;
154 }
155
156 void
157 FGLocation::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
158 {
159   _dirty = true;
160   _roll_deg = roll_deg;
161   _pitch_deg = pitch_deg;
162   _heading_deg = heading_deg;
163 }
164
165 double *
166 FGLocation::get_absolute_view_pos () 
167 {
168   if (_dirty)
169     recalc();
170   return _absolute_view_pos;
171 }
172
173 float *
174 FGLocation::getRelativeViewPos () 
175 {
176   if (_dirty)
177     recalc();
178   return _relative_view_pos;
179 }
180
181 float *
182 FGLocation::getZeroElevViewPos () 
183 {
184   if (_dirty)
185     recalc();
186   return _zero_elev_view_pos;
187 }
188
189
190 // recalc() is done every time one of the setters is called (making the 
191 // cached data "dirty") on the next "get".  It calculates all the outputs 
192 // for viewer.
193 void
194 FGLocation::recalc ()
195 {
196
197   recalcPosition( _lon_deg, _lat_deg, _alt_ft );
198
199   // Make the world up rotation matrix for eye positioin...
200   sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
201
202
203   // get the world up radial vector from planet center for output
204   sgSetVec3( _world_up, UP[0][0], UP[0][1], UP[0][2] );
205
206   // Creat local matrix with current geodetic position.  Converting
207   // the orientation (pitch/roll/heading) to vectors.
208   MakeTRANS( TRANS, _pitch_deg * SG_DEGREES_TO_RADIANS,
209                       _roll_deg * SG_DEGREES_TO_RADIANS,
210                       -_heading_deg * SG_DEGREES_TO_RADIANS,
211                       UP);
212
213   // Given a vector pointing straight down (-Z), map into onto the
214   // local plane representing "horizontal".  This should give us the
215   // local direction for moving "south".
216   sgVec3 minus_z;
217   sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
218
219   sgmap_vec_onto_cur_surface_plane(_world_up, _relative_view_pos, minus_z,
220                                      _surface_south);
221   sgNormalizeVec3(_surface_south);
222
223   // now calculate the surface east vector
224   sgVec3 world_down;
225   sgNegateVec3(world_down, _world_up);
226   sgVectorProductVec3(_surface_east, _surface_south, world_down);
227
228   set_clean();
229 }
230
231 void
232 FGLocation::recalcPosition (double lon_deg, double lat_deg, double alt_ft) const
233 {
234   double sea_level_radius_m;
235   double lat_geoc_rad;
236
237
238                                 // Convert from geodetic to geocentric
239                                 // coordinates.
240   sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
241                alt_ft * SG_FEET_TO_METER,
242                &sea_level_radius_m,
243                &lat_geoc_rad);
244
245                                 // Calculate the cartesian coordinates
246                                 // of point directly below at sea level.
247                                 // aka Zero Elevation Position
248   Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
249                       lat_geoc_rad,
250                       sea_level_radius_m);
251   Point3D tmp = sgPolarToCart3d(p) - _tile_center;
252   sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
253
254                                 // Calculate the absolute view position
255                                 // in fgfs coordinates.
256                                 // aka Absolute View Position
257   p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
258   tmp = sgPolarToCart3d(p);
259   sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
260
261                                 // Calculate the relative view position
262                                 // from the scenery center.
263                                 // aka Relative View Position
264
265   // FIXME: view position should ONLY be calculated in the viewer...
266   // Anything else should calculate their own positions relative to the 
267   // viewer's tile_center.
268   sgdVec3 scenery_center;
269   sgdSetVec3(scenery_center,
270         globals->get_scenery()->get_center().x(),
271         globals->get_scenery()->get_center().y(),
272         globals->get_scenery()->get_center().z());
273   sgdVec3 view_pos;
274   sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
275   sgSetVec3(_relative_view_pos, view_pos);
276
277 }
278
279 void
280 FGLocation::update (int dt)
281 {
282 }