]> git.mxchange.org Git - flightgear.git/blob - src/Main/views.cxx
Panel updates from David Megginson. Radials can now be adjusted on the fly
[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         current_view.set_win_ratio( (GLfloat) winWidth / (GLfloat) winHeight );
77     } else {
78         current_view.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 // Update the view volume, position, and orientation
107 void FGView::UpdateViewParams( const FGInterface& f ) {
108     UpdateViewMath(f);
109     
110     if ( ! current_options.get_panel_status() ) {
111         xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
112     } else {
113         xglViewport(0, (GLint)((winHeight)*0.5768), (GLint)(winWidth), 
114                     (GLint)((winHeight)*0.4232) );
115     }
116
117     panel_hist = current_options.get_panel_status();
118 }
119
120
121 // convert sgMat4 to MAT3 and print
122 static void print_sgMat4( sgMat4 &in) {
123     int i, j;
124     for ( i = 0; i < 4; i++ ) {
125         for ( j = 0; j < 4; j++ ) {
126             printf("%10.4f ", in[i][j]);
127         }
128         cout << endl;
129     }
130 }
131
132
133 // Update the view parameters
134 void FGView::UpdateViewMath( const FGInterface& f ) {
135
136     Point3D p;
137     sgVec3 v0, minus_z, sgvec, forward;
138     sgMat4 VIEWo, TMP;
139
140     if ( update_fov ) {
141         ssgSetFOV( current_options.get_fov(), 
142                    current_options.get_fov() * win_ratio );
143         update_fov = false;
144     }
145                 
146     scenery.center = scenery.next_center;
147
148     // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x,
149     //        scenery.center.y, scenery.center.z);
150
151     // calculate the cartesion coords of the current lat/lon/0 elev
152     p = Point3D( f.get_Longitude(), 
153                  f.get_Lat_geocentric(), 
154                  f.get_Sea_level_radius() * FEET_TO_METER );
155
156     cur_zero_elev = fgPolarToCart3d(p) - scenery.center;
157
158     // calculate view position in current FG view coordinate system
159     // p.lon & p.lat are already defined earlier, p.radius was set to
160     // the sea level radius, so now we add in our altitude.
161     if ( f.get_Altitude() * FEET_TO_METER > 
162          (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
163         p.setz( p.radius() + f.get_Altitude() * FEET_TO_METER );
164     } else {
165         p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
166     }
167
168     abs_view_pos = fgPolarToCart3d(p);
169         
170     view_pos = abs_view_pos - scenery.center;
171
172     FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
173     FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
174     FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = " << view_pos );
175
176     // code to calculate LOCAL matrix calculated from Phi, Theta, and
177     // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
178     // flight model
179
180     sgVec3 rollvec;
181     sgSetVec3( rollvec, 0.0, 0.0, 1.0 );
182     sgMat4 PHI;         // roll
183     sgMakeRotMat4( PHI, f.get_Phi() * RAD_TO_DEG, rollvec );
184
185     sgVec3 pitchvec;
186     sgSetVec3( pitchvec, 0.0, 1.0, 0.0 );
187     sgMat4 THETA;               // pitch
188     sgMakeRotMat4( THETA, f.get_Theta() * RAD_TO_DEG, pitchvec );
189
190     // ROT = PHI * THETA
191     sgMat4 ROT;
192     // sgMultMat4( ROT, PHI, THETA );
193     sgCopyMat4( ROT, PHI );
194     sgPostMultMat4( ROT, THETA );
195
196     sgVec3 yawvec;
197     sgSetVec3( yawvec, 1.0, 0.0, 0.0 );
198     sgMat4 PSI;         // pitch
199     sgMakeRotMat4( PSI, -f.get_Psi() * RAD_TO_DEG, yawvec );
200
201     // LOCAL = ROT * PSI
202     // sgMultMat4( LOCAL, ROT, PSI );
203     sgCopyMat4( LOCAL, ROT );
204     sgPostMultMat4( LOCAL, PSI );
205     // cout << "LOCAL matrix" << endl;
206     // print_sgMat4( LOCAL );
207         
208     sgMakeRotMat4( UP, 
209                    f.get_Longitude() * RAD_TO_DEG,
210                    0.0,
211                    -f.get_Latitude() * RAD_TO_DEG );
212
213     sgSetVec3( local_up, 1.0, 0.0, 0.0 );
214     sgXformVec3( local_up, UP );
215     // cout << "Local Up = " << local_up[0] << "," << local_up[1] << ","
216     //      << local_up[2] << endl;
217     
218     // Alternative method to Derive local up vector based on
219     // *geodetic* coordinates
220     // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
221     // printf( "    Alt Up = (%.4f, %.4f, %.4f)\n", 
222     //         alt_up.x, alt_up.y, alt_up.z);
223
224     // VIEWo = LOCAL * UP
225     // sgMultMat4( VIEWo, LOCAL, UP );
226     sgCopyMat4( VIEWo, LOCAL );
227     sgPostMultMat4( VIEWo, UP );
228     // cout << "VIEWo matrix" << endl;
229     // print_sgMat4( VIEWo );
230
231     // generate the sg view up vector
232     sgVec3 vec1;
233     sgSetVec3( vec1, 1.0, 0.0, 0.0 );
234     sgXformVec3( view_up, vec1, VIEWo );
235
236     // generate the pilot offset vector in world coordinates
237     sgVec3 pilot_offset_world;
238     sgSetVec3( vec1, 
239                pilot_offset[2], pilot_offset[1], -pilot_offset[0] );
240     sgXformVec3( pilot_offset_world, vec1, VIEWo );
241
242     // generate the view offset matrix
243     sgMakeRotMat4( VIEW_OFFSET, view_offset * RAD_TO_DEG, view_up );
244     // cout << "VIEW_OFFSET matrix" << endl;
245     // print_sgMat4( VIEW_OFFSET );
246         
247     // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
248     sgMat4 TMP2;
249     // sgMultMat4( TMP2, VIEWo, VIEW_OFFSET );
250     // sgMultMat4( VIEW_ROT, LARC_TO_SSG, TMP2 );
251     sgCopyMat4( VIEW_ROT, VIEWo );
252     sgPostMultMat4( VIEW_ROT, VIEW_OFFSET );
253     sgPreMultMat4( VIEW_ROT, LARC_TO_SSG );
254     // cout << "VIEW_ROT matrix" << endl;
255     // print_sgMat4( VIEW_ROT );
256
257     sgMakeTransMat4( TRANS, 
258                      view_pos.x() + pilot_offset_world[0],
259                      view_pos.y() + pilot_offset_world[1],
260                      view_pos.z() + pilot_offset_world[2] );
261
262     // VIEW = VIEW_ROT * TRANS
263     // sgMultMat4( VIEW, VIEW_ROT, TRANS );
264     sgCopyMat4( VIEW, VIEW_ROT );
265     sgPostMultMat4( VIEW, TRANS );
266
267 //!!!!!!!!!!!!!!!!!!!   
268     // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
269     // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
270     // this in gui.cxx for now just testing
271         extern float quat_mat[4][4];
272         sgPreMultMat4( VIEW, quat_mat);
273 // !!!!!!!!!! testing   
274
275     sgSetVec3( sgvec, 0.0, 0.0, 1.0 );
276     sgXformVec3( forward, sgvec, VIEWo );
277     // cout << "forward = " << forward[0] << ","
278     //      << forward[1] << "," << forward[2] << endl;
279
280     sgMakeRotMat4( TMP, view_offset * RAD_TO_DEG, view_up );
281     sgXformVec3( view_forward, forward, TMP );
282     // cout << "view_forward = " << view_forward[0] << ","
283     //      << view_forward[1] << "," << view_forward[2] << endl;
284     
285     // make a vector to the current view position
286     sgSetVec3( v0, view_pos.x(), view_pos.y(), view_pos.z() );
287
288     // Given a vector pointing straight down (-Z), map into onto the
289     // local plane representing "horizontal".  This should give us the
290     // local direction for moving "south".
291     sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
292
293     sgmap_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
294     sgNormalizeVec3(surface_south);
295     // cout << "Surface direction directly south " << surface_south[0] << ","
296     //      << surface_south[1] << "," << surface_south[2] << endl;
297
298     // now calculate the surface east vector
299     sgMakeRotMat4( TMP, FG_PI_2 * RAD_TO_DEG, view_up );
300     // cout << "sgMat4 TMP" << endl;
301     // print_sgMat4( TMP );
302     sgXformVec3(surface_east, surface_south, TMP);
303     // cout << "Surface direction directly east" << surface_east[0] << ","
304     //      << surface_east[1] << "," << surface_east[2] << endl;
305     // cout << "Should be close to zero = "
306     //      << sgScalarProductVec3(surface_south, surface_east) << endl;
307 }
308
309
310 // Destructor
311 FGView::~FGView( void ) {
312 }