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