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