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