]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer_rph.cxx
Minor tweaks.
[flightgear.git] / src / Main / viewer_rph.cxx
1 // viewer_rph.cxx -- class for managing a Roll/Pitch/Heading viewer in
2 //                   the flightgear world.
3 //
4 // Written by Curtis Olson, started August 1997.
5 //                          overhaul started October 2000.
6 //
7 // Copyright (C) 1997 - 2000  Curtis L. Olson  - curt@flightgear.org
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 //
23 // $Id$
24
25
26 #include <simgear/compiler.h>
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #include <plib/ssg.h>           // plib include
33
34 #include <simgear/constants.h>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/math/point3d.hxx>
37 #include <simgear/math/polar3d.hxx>
38 #include <simgear/math/vector.hxx>
39
40 #include <Scenery/scenery.hxx>
41
42 #include "globals.hxx"
43 #include "viewer_rph.hxx"
44
45
46 // Constructor
47 FGViewerRPH::FGViewerRPH( void )
48 {
49 #ifndef USE_FAST_VIEWROT
50     // This never changes -- NHV
51     LARC_TO_SSG[0][0] = 0.0; 
52     LARC_TO_SSG[0][1] = 1.0; 
53     LARC_TO_SSG[0][2] = -0.0; 
54     LARC_TO_SSG[0][3] = 0.0; 
55
56     LARC_TO_SSG[1][0] = 0.0; 
57     LARC_TO_SSG[1][1] = 0.0; 
58     LARC_TO_SSG[1][2] = 1.0; 
59     LARC_TO_SSG[1][3] = 0.0;
60         
61     LARC_TO_SSG[2][0] = 1.0; 
62     LARC_TO_SSG[2][1] = -0.0; 
63     LARC_TO_SSG[2][2] = 0.0; 
64     LARC_TO_SSG[2][3] = 0.0;
65         
66     LARC_TO_SSG[3][0] = 0.0; 
67     LARC_TO_SSG[3][1] = 0.0; 
68     LARC_TO_SSG[3][2] = 0.0; 
69     LARC_TO_SSG[3][3] = 1.0; 
70 #endif // USE_FAST_VIEWROT
71 }
72
73
74 #define USE_FAST_VIEWROT
75 #ifdef USE_FAST_VIEWROT
76 // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
77 // This takes advantage of the fact that VIEWo and VIEW_OFFSET
78 // only have entries in the upper 3x3 block
79 // and that LARC_TO_SSG is just a shift of rows   NHV
80 inline static void fgMakeViewRot( sgMat4 dst, const sgMat4 m1, const sgMat4 m2 )
81 {
82     for ( int j = 0 ; j < 3 ; j++ ) {
83         dst[2][j] = m2[0][0] * m1[0][j] +
84             m2[0][1] * m1[1][j] +
85             m2[0][2] * m1[2][j];
86
87         dst[0][j] = m2[1][0] * m1[0][j] +
88             m2[1][1] * m1[1][j] +
89             m2[1][2] * m1[2][j];
90
91         dst[1][j] = m2[2][0] * m1[0][j] +
92             m2[2][1] * m1[1][j] +
93             m2[2][2] * m1[2][j];
94     }
95     dst[0][3] = 
96         dst[1][3] = 
97         dst[2][3] = 
98         dst[3][0] = 
99         dst[3][1] = 
100         dst[3][2] = SG_ZERO;
101     dst[3][3] = SG_ONE;
102 }
103 #endif
104
105
106 #define USE_FAST_LOCAL
107 #ifdef USE_FAST_LOCAL
108 inline static void fgMakeLOCAL( sgMat4 dst, const double Theta,
109                                 const double Phi, const double Psi)
110 {
111     SGfloat cosTheta = (SGfloat) cos(Theta);
112     SGfloat sinTheta = (SGfloat) sin(Theta);
113     SGfloat cosPhi   = (SGfloat) cos(Phi);
114     SGfloat sinPhi   = (SGfloat) sin(Phi);
115     SGfloat sinPsi   = (SGfloat) sin(Psi) ;
116     SGfloat cosPsi   = (SGfloat) cos(Psi) ;
117         
118     dst[0][0] = cosPhi * cosTheta;
119     dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
120     dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
121     dst[0][3] = SG_ZERO;
122
123     dst[1][0] = -sinPhi * cosTheta;
124     dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
125     dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
126     dst[1][3] = SG_ZERO ;
127         
128     dst[2][0] = sinTheta;
129     dst[2][1] = cosTheta * -sinPsi;
130     dst[2][2] = cosTheta * cosPsi;
131     dst[2][3] = SG_ZERO;
132         
133     dst[3][0] = SG_ZERO;
134     dst[3][1] = SG_ZERO;
135     dst[3][2] = SG_ZERO;
136     dst[3][3] = SG_ONE ;
137 }
138 #endif
139
140
141 // convert sgMat4 to MAT3 and print
142 static void print_sgMat4( sgMat4 &in) {
143     int i, j;
144     for ( i = 0; i < 4; i++ ) {
145         for ( j = 0; j < 4; j++ ) {
146             printf("%10.4f ", in[i][j]);
147         }
148         cout << endl;
149     }
150 }
151
152
153 // Update the view parameters
154 void FGViewerRPH::update() {
155     Point3D tmp;
156     sgVec3 minus_z, forward;
157     sgMat4 VIEWo;
158
159     // calculate the cartesion coords of the current lat/lon/0 elev
160     Point3D p = Point3D( geod_view_pos[0], 
161                          geod_view_pos[1], 
162                          sea_level_radius );
163
164     tmp = sgPolarToCart3d(p) - scenery.center;
165     sgSetVec3( zero_elev, tmp[0], tmp[1], tmp[2] );
166
167     // calculate view position in current FG view coordinate system
168     // p.lon & p.lat are already defined earlier, p.radius was set to
169     // the sea level radius, so now we add in our altitude.
170     if ( geod_view_pos[2] > (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
171         p.setz( p.radius() + geod_view_pos[2] );
172     } else {
173         p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
174     }
175
176     tmp = sgPolarToCart3d(p);
177     sgdSetVec3( abs_view_pos, tmp[0], tmp[1], tmp[2] );
178
179     // view_pos = abs_view_pos - scenery.center;
180     sgdVec3 sc;
181     sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
182     sgdVec3 vp;
183     sgdSubVec3( vp, abs_view_pos, sc );
184     sgSetVec3( view_pos, vp );
185
186     FG_LOG( FG_VIEW, FG_DEBUG, "sea level radius = " << sea_level_radius );
187     FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
188     FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = "
189             << abs_view_pos[0] << ","
190             << abs_view_pos[1] << ","
191             << abs_view_pos[2] );
192     FG_LOG( FG_VIEW, FG_DEBUG, "(RPH) Relative view pos = "
193             << view_pos[0] << "," << view_pos[1] << "," << view_pos[2] );
194
195     // code to calculate LOCAL matrix calculated from Phi, Theta, and
196     // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
197     // flight model
198         
199 #ifdef USE_FAST_LOCAL
200         
201     fgMakeLOCAL( LOCAL, rph[1], rph[0], -rph[2] );
202         
203 #else // USE_TEXT_BOOK_METHOD
204         
205     sgVec3 rollvec;
206     sgSetVec3( rollvec, 0.0, 0.0, 1.0 );
207     sgMat4 PHI;         // roll
208     sgMakeRotMat4( PHI, rph[0] * RAD_TO_DEG, rollvec );
209
210     sgVec3 pitchvec;
211     sgSetVec3( pitchvec, 0.0, 1.0, 0.0 );
212     sgMat4 THETA;               // pitch
213     sgMakeRotMat4( THETA, rph[1] * RAD_TO_DEG, pitchvec );
214
215     // ROT = PHI * THETA
216     sgMat4 ROT;
217     // sgMultMat4( ROT, PHI, THETA );
218     sgCopyMat4( ROT, PHI );
219     sgPostMultMat4( ROT, THETA );
220
221     sgVec3 yawvec;
222     sgSetVec3( yawvec, 1.0, 0.0, 0.0 );
223     sgMat4 PSI;         // heading
224     sgMakeRotMat4( PSI, -rph[2] * RAD_TO_DEG, yawvec );
225
226     // LOCAL = ROT * PSI
227     // sgMultMat4( LOCAL, ROT, PSI );
228     sgCopyMat4( LOCAL, ROT );
229     sgPostMultMat4( LOCAL, PSI );
230
231 #endif // USE_FAST_LOCAL
232         
233     // cout << "LOCAL matrix" << endl;
234     // print_sgMat4( LOCAL );
235         
236     sgMakeRotMat4( UP, 
237                    geod_view_pos[0] * RAD_TO_DEG,
238                    0.0,
239                    -geod_view_pos[1] * RAD_TO_DEG );
240
241     sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
242     // sgXformVec3( world_up, UP );
243     // cout << "World Up = " << world_up[0] << "," << world_up[1] << ","
244     //      << world_up[2] << endl;
245     
246     // Alternative method to Derive world up vector based on
247     // *geodetic* coordinates
248     // alt_up = sgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
249     // printf( "    Alt Up = (%.4f, %.4f, %.4f)\n", 
250     //         alt_up.x, alt_up.y, alt_up.z);
251
252     // VIEWo = LOCAL * UP
253     // sgMultMat4( VIEWo, LOCAL, UP );
254     sgCopyMat4( VIEWo, LOCAL );
255     sgPostMultMat4( VIEWo, UP );
256     // cout << "VIEWo matrix" << endl;
257     // print_sgMat4( VIEWo );
258
259     // generate the sg view up and forward vectors
260     sgSetVec3( view_up, VIEWo[0][0], VIEWo[0][1], VIEWo[0][2] );
261     // cout << "view = " << view[0] << ","
262     //      << view[1] << "," << view[2] << endl;
263     sgSetVec3( forward, VIEWo[2][0], VIEWo[2][1], VIEWo[2][2] );
264     // cout << "forward = " << forward[0] << ","
265     //      << forward[1] << "," << forward[2] << endl;
266
267     // generate the pilot offset vector in world coordinates
268     sgVec3 pilot_offset_world;
269     sgSetVec3( pilot_offset_world, 
270                pilot_offset[2], pilot_offset[1], -pilot_offset[0] );
271     sgXformVec3( pilot_offset_world, pilot_offset_world, VIEWo );
272
273     // generate the view offset matrix
274     sgMakeRotMat4( VIEW_OFFSET, view_offset * RAD_TO_DEG, view_up );
275     // cout << "VIEW_OFFSET matrix" << endl;
276     // print_sgMat4( VIEW_OFFSET );
277     sgXformVec3( view_forward, forward, VIEW_OFFSET );
278     FG_LOG( FG_VIEW, FG_DEBUG, "(RPH) view forward = "
279             << view_forward[0] << "," << view_forward[1] << ","
280             << view_forward[2] );
281         
282     // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
283 #ifdef USE_FAST_VIEWROT
284     fgMakeViewRot( VIEW_ROT, VIEW_OFFSET, VIEWo );
285 #else
286     // sgMultMat4( VIEW_ROT, VIEW_OFFSET, VIEWo );
287     // sgPreMultMat4( VIEW_ROT, LARC_TO_SSG );
288     sgCopyMat4( VIEW_ROT, VIEWo );
289     sgPostMultMat4( VIEW_ROT, VIEW_OFFSET );
290     sgPreMultMat4( VIEW_ROT, LARC_TO_SSG );
291 #endif
292     // cout << "VIEW_ROT matrix" << endl;
293     // print_sgMat4( VIEW_ROT );
294
295     sgVec3 trans_vec;
296     sgAddVec3( trans_vec, view_pos, pilot_offset_world );
297
298     // VIEW = VIEW_ROT * TRANS
299     sgCopyMat4( VIEW, VIEW_ROT );
300     sgPostMultMat4ByTransMat4( VIEW, trans_vec );
301
302     //!!!!!!!!!!!!!!!!!!!       
303     // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
304     // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
305     // this in gui.cxx for now just testing
306     extern float quat_mat[4][4];
307     sgPreMultMat4( VIEW, quat_mat);
308     // !!!!!!!!!! testing       
309
310     // Given a vector pointing straight down (-Z), map into onto the
311     // local plane representing "horizontal".  This should give us the
312     // local direction for moving "south".
313     sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
314
315     sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
316                                      surface_south);
317     sgNormalizeVec3(surface_south);
318     // cout << "Surface direction directly south " << surface_south[0] << ","
319     //      << surface_south[1] << "," << surface_south[2] << endl;
320
321     // now calculate the surface east vector
322 #define USE_FAST_SURFACE_EAST
323 #ifdef USE_FAST_SURFACE_EAST
324     sgVec3 world_down;
325     sgNegateVec3(world_down, world_up);
326     sgVectorProductVec3(surface_east, surface_south, world_down);
327 #else
328     sgMakeRotMat4( TMP, FG_PI_2 * RAD_TO_DEG, world_up );
329     // cout << "sgMat4 TMP" << endl;
330     // print_sgMat4( TMP );
331     sgXformVec3(surface_east, surface_south, TMP);
332 #endif //  USE_FAST_SURFACE_EAST
333     // cout << "Surface direction directly east " << surface_east[0] << ","
334     //      << surface_east[1] << "," << surface_east[2] << endl;
335     // cout << "Should be close to zero = "
336     //      << sgScalarProductVec3(surface_south, surface_east) << endl;
337
338     set_clean();
339 }
340
341
342 // Destructor
343 FGViewerRPH::~FGViewerRPH( void ) {
344 }