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