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