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