]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer_rph.cxx
- adjusted for no-value constructor for FGPanel
[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     set_reverse_view_offset(false);
50 #ifndef USE_FAST_VIEWROT
51     // This never changes -- NHV
52     LARC_TO_SSG[0][0] = 0.0; 
53     LARC_TO_SSG[0][1] = 1.0; 
54     LARC_TO_SSG[0][2] = -0.0; 
55     LARC_TO_SSG[0][3] = 0.0; 
56
57     LARC_TO_SSG[1][0] = 0.0; 
58     LARC_TO_SSG[1][1] = 0.0; 
59     LARC_TO_SSG[1][2] = 1.0; 
60     LARC_TO_SSG[1][3] = 0.0;
61         
62     LARC_TO_SSG[2][0] = 1.0; 
63     LARC_TO_SSG[2][1] = -0.0; 
64     LARC_TO_SSG[2][2] = 0.0; 
65     LARC_TO_SSG[2][3] = 0.0;
66         
67     LARC_TO_SSG[3][0] = 0.0; 
68     LARC_TO_SSG[3][1] = 0.0; 
69     LARC_TO_SSG[3][2] = 0.0; 
70     LARC_TO_SSG[3][3] = 1.0; 
71 #endif // USE_FAST_VIEWROT
72 }
73
74
75 #define USE_FAST_VIEWROT
76 #ifdef USE_FAST_VIEWROT
77 // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
78 // This takes advantage of the fact that VIEWo and VIEW_OFFSET
79 // only have entries in the upper 3x3 block
80 // and that LARC_TO_SSG is just a shift of rows   NHV
81 inline static void fgMakeViewRot( sgMat4 dst, const sgMat4 m1, const sgMat4 m2 )
82 {
83     for ( int j = 0 ; j < 3 ; j++ ) {
84         dst[2][j] = m2[0][0] * m1[0][j] +
85             m2[0][1] * m1[1][j] +
86             m2[0][2] * m1[2][j];
87
88         dst[0][j] = m2[1][0] * m1[0][j] +
89             m2[1][1] * m1[1][j] +
90             m2[1][2] * m1[2][j];
91
92         dst[1][j] = m2[2][0] * m1[0][j] +
93             m2[2][1] * m1[1][j] +
94             m2[2][2] * m1[2][j];
95     }
96     dst[0][3] = 
97         dst[1][3] = 
98         dst[2][3] = 
99         dst[3][0] = 
100         dst[3][1] = 
101         dst[3][2] = SG_ZERO;
102     dst[3][3] = SG_ONE;
103 }
104 #endif
105
106
107 #define USE_FAST_LOCAL
108 #ifdef USE_FAST_LOCAL
109 inline static void fgMakeLOCAL( sgMat4 dst, const double Theta,
110                                 const double Phi, const double Psi)
111 {
112     SGfloat cosTheta = (SGfloat) cos(Theta);
113     SGfloat sinTheta = (SGfloat) sin(Theta);
114     SGfloat cosPhi   = (SGfloat) cos(Phi);
115     SGfloat sinPhi   = (SGfloat) sin(Phi);
116     SGfloat sinPsi   = (SGfloat) sin(Psi) ;
117     SGfloat cosPsi   = (SGfloat) cos(Psi) ;
118         
119     dst[0][0] = cosPhi * cosTheta;
120     dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
121     dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
122     dst[0][3] = SG_ZERO;
123
124     dst[1][0] = -sinPhi * cosTheta;
125     dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
126     dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
127     dst[1][3] = SG_ZERO ;
128         
129     dst[2][0] = sinTheta;
130     dst[2][1] = cosTheta * -sinPsi;
131     dst[2][2] = cosTheta * cosPsi;
132     dst[2][3] = SG_ZERO;
133         
134     dst[3][0] = SG_ZERO;
135     dst[3][1] = SG_ZERO;
136     dst[3][2] = SG_ZERO;
137     dst[3][3] = SG_ONE ;
138 }
139 #endif
140
141
142 #if 0
143 // convert sgMat4 to MAT3 and print
144 static void print_sgMat4( sgMat4 &in) {
145     int i, j;
146     for ( i = 0; i < 4; i++ ) {
147         for ( j = 0; j < 4; j++ ) {
148             printf("%10.4f ", in[i][j]);
149         }
150         cout << endl;
151     }
152 }
153 #endif
154
155
156 // Update the view parameters
157 void FGViewerRPH::update() {
158     Point3D tmp;
159     sgVec3 minus_z, forward;
160     sgMat4 VIEWo;
161
162     // calculate the cartesion coords of the current lat/lon/0 elev
163     Point3D p = Point3D( geod_view_pos[0], 
164                          geod_view_pos[1], 
165                          sea_level_radius );
166
167     tmp = sgPolarToCart3d(p) - scenery.center;
168     sgSetVec3( zero_elev, tmp[0], tmp[1], tmp[2] );
169
170     // calculate view position in current FG view coordinate system
171     // p.lon & p.lat are already defined earlier, p.radius was set to
172     // the sea level radius, so now we add in our altitude.
173     if ( geod_view_pos[2] > (scenery.cur_elev + 0.5 * SG_METER_TO_FEET) ) {
174         p.setz( p.radius() + geod_view_pos[2] );
175     } else {
176         p.setz( p.radius() + scenery.cur_elev + 0.5 * SG_METER_TO_FEET );
177     }
178
179     tmp = sgPolarToCart3d(p);
180     sgdSetVec3( abs_view_pos, tmp[0], tmp[1], tmp[2] );
181
182     // view_pos = abs_view_pos - scenery.center;
183     sgdVec3 sc;
184     sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
185     sgdVec3 vp;
186     sgdSubVec3( vp, abs_view_pos, sc );
187     sgSetVec3( view_pos, vp );
188
189     SG_LOG( SG_VIEW, SG_DEBUG, "sea level radius = " << sea_level_radius );
190     SG_LOG( SG_VIEW, SG_DEBUG, "Polar view pos = " << p );
191     SG_LOG( SG_VIEW, SG_DEBUG, "Absolute view pos = "
192             << abs_view_pos[0] << ","
193             << abs_view_pos[1] << ","
194             << abs_view_pos[2] );
195     SG_LOG( SG_VIEW, SG_DEBUG, "(RPH) Relative view pos = "
196             << view_pos[0] << "," << view_pos[1] << "," << view_pos[2] );
197
198     // code to calculate LOCAL matrix calculated from Phi, Theta, and
199     // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
200     // flight model
201         
202 #ifdef USE_FAST_LOCAL
203         
204     fgMakeLOCAL( LOCAL, rph[1], rph[0], -rph[2] );
205         
206 #else // USE_TEXT_BOOK_METHOD
207         
208     sgVec3 rollvec;
209     sgSetVec3( rollvec, 0.0, 0.0, 1.0 );
210     sgMat4 PHI;         // roll
211     sgMakeRotMat4( PHI, rph[0] * SGD_RADIANS_TO_DEGREES, rollvec );
212
213     sgVec3 pitchvec;
214     sgSetVec3( pitchvec, 0.0, 1.0, 0.0 );
215     sgMat4 THETA;               // pitch
216     sgMakeRotMat4( THETA, rph[1] * SGD_RADIANS_TO_DEGREES, pitchvec );
217
218     // ROT = PHI * THETA
219     sgMat4 ROT;
220     // sgMultMat4( ROT, PHI, THETA );
221     sgCopyMat4( ROT, PHI );
222     sgPostMultMat4( ROT, THETA );
223
224     sgVec3 yawvec;
225     sgSetVec3( yawvec, 1.0, 0.0, 0.0 );
226     sgMat4 PSI;         // heading
227     sgMakeRotMat4( PSI, -rph[2] * SGD_RADIANS_TO_DEGREES, yawvec );
228
229     // LOCAL = ROT * PSI
230     // sgMultMat4( LOCAL, ROT, PSI );
231     sgCopyMat4( LOCAL, ROT );
232     sgPostMultMat4( LOCAL, PSI );
233
234 #endif // USE_FAST_LOCAL
235         
236     // cout << "LOCAL matrix" << endl;
237     // print_sgMat4( LOCAL );
238         
239     sgMakeRotMat4( UP, 
240                    geod_view_pos[0] * SGD_RADIANS_TO_DEGREES,
241                    0.0,
242                    -geod_view_pos[1] * SGD_RADIANS_TO_DEGREES );
243
244     sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
245     // sgXformVec3( world_up, UP );
246     // cout << "World Up = " << world_up[0] << "," << world_up[1] << ","
247     //      << world_up[2] << endl;
248     
249     // Alternative method to Derive world up vector based on
250     // *geodetic* coordinates
251     // alt_up = sgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
252     // printf( "    Alt Up = (%.4f, %.4f, %.4f)\n", 
253     //         alt_up.x, alt_up.y, alt_up.z);
254
255     // VIEWo = LOCAL * UP
256     // sgMultMat4( VIEWo, LOCAL, UP );
257     sgCopyMat4( VIEWo, LOCAL );
258     sgPostMultMat4( VIEWo, UP );
259     // cout << "VIEWo matrix" << endl;
260     // print_sgMat4( VIEWo );
261
262     // generate the sg view up and forward vectors
263     sgSetVec3( view_up, VIEWo[0][0], VIEWo[0][1], VIEWo[0][2] );
264     // cout << "view = " << view[0] << ","
265     //      << view[1] << "," << view[2] << endl;
266     sgSetVec3( forward, VIEWo[2][0], VIEWo[2][1], VIEWo[2][2] );
267     // cout << "forward = " << forward[0] << ","
268     //      << forward[1] << "," << forward[2] << endl;
269
270     // generate the pilot offset vector in world coordinates
271     sgVec3 pilot_offset_world;
272     sgSetVec3( pilot_offset_world, 
273                pilot_offset[2], pilot_offset[1], -pilot_offset[0] );
274     sgXformVec3( pilot_offset_world, pilot_offset_world, VIEWo );
275
276     // generate the view offset matrix
277     sgMakeRotMat4( VIEW_OFFSET, view_offset * SGD_RADIANS_TO_DEGREES, view_up );
278     // cout << "VIEW_OFFSET matrix" << endl;
279     // print_sgMat4( VIEW_OFFSET );
280     sgXformVec3( view_forward, forward, VIEW_OFFSET );
281     SG_LOG( SG_VIEW, SG_DEBUG, "(RPH) view forward = "
282             << view_forward[0] << "," << view_forward[1] << ","
283             << view_forward[2] );
284         
285     // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
286 #ifdef USE_FAST_VIEWROT
287     fgMakeViewRot( VIEW_ROT, VIEW_OFFSET, VIEWo );
288 #else
289     // sgMultMat4( VIEW_ROT, VIEW_OFFSET, VIEWo );
290     // sgPreMultMat4( VIEW_ROT, LARC_TO_SSG );
291     sgCopyMat4( VIEW_ROT, VIEWo );
292     sgPostMultMat4( VIEW_ROT, VIEW_OFFSET );
293     sgPreMultMat4( VIEW_ROT, LARC_TO_SSG );
294 #endif
295     // cout << "VIEW_ROT matrix" << endl;
296     // print_sgMat4( VIEW_ROT );
297
298     sgVec3 trans_vec;
299     sgAddVec3( trans_vec, view_pos, pilot_offset_world );
300
301     // VIEW = VIEW_ROT * TRANS
302     sgCopyMat4( VIEW, VIEW_ROT );
303     sgPostMultMat4ByTransMat4( VIEW, trans_vec );
304
305     //!!!!!!!!!!!!!!!!!!!       
306     // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
307     // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
308     // this in gui.cxx for now just testing
309     extern float GuiQuat_mat[4][4];
310     sgPreMultMat4( VIEW, GuiQuat_mat);
311     // !!!!!!!!!! testing       
312
313     // Given a vector pointing straight down (-Z), map into onto the
314     // local plane representing "horizontal".  This should give us the
315     // local direction for moving "south".
316     sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
317
318     sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
319                                      surface_south);
320     sgNormalizeVec3(surface_south);
321     // cout << "Surface direction directly south " << surface_south[0] << ","
322     //      << surface_south[1] << "," << surface_south[2] << endl;
323
324     // now calculate the surface east vector
325 #define USE_FAST_SURFACE_EAST
326 #ifdef USE_FAST_SURFACE_EAST
327     sgVec3 world_down;
328     sgNegateVec3(world_down, world_up);
329     sgVectorProductVec3(surface_east, surface_south, world_down);
330 #else
331     sgMakeRotMat4( TMP, SGD_PI_2 * SGD_RADIANS_TO_DEGREES, world_up );
332     // cout << "sgMat4 TMP" << endl;
333     // print_sgMat4( TMP );
334     sgXformVec3(surface_east, surface_south, TMP);
335 #endif //  USE_FAST_SURFACE_EAST
336     // cout << "Surface direction directly east " << surface_east[0] << ","
337     //      << surface_east[1] << "," << surface_east[2] << endl;
338     // cout << "Should be close to zero = "
339     //      << sgScalarProductVec3(surface_south, surface_east) << endl;
340
341     set_clean();
342 }
343
344
345 // Destructor
346 FGViewerRPH::~FGViewerRPH( void ) {
347 }