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