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