]> git.mxchange.org Git - flightgear.git/blob - src/Main/views.cxx
Cleaning cruft that can be removed now that ssg is taking over.
[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@infoplane.com
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 <ssg.h>                // plib include
30
31 #include <Aircraft/aircraft.hxx>
32 #include <Cockpit/panel.hxx>
33 #include <Debug/logstream.hxx>
34 #include <Include/fg_constants.h>
35 #include <Math/mat3.h>
36 #include <Math/point3d.hxx>
37 #include <Math/polar3d.hxx>
38 #include <Math/vector.hxx>
39 #include <Scenery/scenery.hxx>
40 #include <Time/fg_time.hxx>
41
42 #include "options.hxx"
43 #include "views.hxx"
44
45
46 // Define following to extract various vectors directly
47 // from matrices we have allready computed
48 // rather then performing 'textbook algebra' to rederive them
49 // Norman Vine -- nhv@yahoo.com
50 // #define FG_VIEW_INLINE_OPTIMIZATIONS
51
52 // temporary (hopefully) hack
53 static int panel_hist = 0;
54
55
56 // specify code paths ... these are done as variable rather than
57 // #define's because down the road we may want to choose between them
58 // on the fly for different flight models ... this way magic carpet
59 // and external modes wouldn't need to recreate the LaRCsim matrices
60 // themselves.
61
62 static const bool use_larcsim_local_to_body = false;
63
64
65 // This is a record containing current view parameters
66 FGView current_view;
67
68
69 // Constructor
70 FGView::FGView( void ) {
71 }
72
73
74 // Initialize a view structure
75 void FGView::Init( void ) {
76     FG_LOG( FG_VIEW, FG_INFO, "Initializing View parameters" );
77
78     view_mode = FG_VIEW_FIRST_PERSON;
79     view_offset = 0.0;
80     goal_view_offset = 0.0;
81
82     winWidth = current_options.get_xsize();
83     winHeight = current_options.get_ysize();
84
85     if ( ! current_options.get_panel_status() ) {
86         current_view.set_win_ratio( (GLfloat) winWidth / (GLfloat) winHeight );
87     } else {
88         current_view.set_win_ratio( (GLfloat) winWidth / 
89                                     ((GLfloat) (winHeight)*0.4232) );
90     }
91
92     force_update_fov_math();
93 }
94
95
96 // Update the field of view coefficients
97 void FGView::UpdateFOV( const fgOPTIONS& o ) {
98     ssgSetFOV( o.get_fov(), 0.0 );
99
100     double fov, theta_x, theta_y;
101
102     fov = o.get_fov();
103         
104     // printf("win_ratio = %.2f\n", win_ratio);
105     // calculate sin() and cos() of fov / 2 in X direction;
106     theta_x = (fov * win_ratio * DEG_TO_RAD) / 2.0;
107     // printf("theta_x = %.2f\n", theta_x);
108     sin_fov_x = sin(theta_x);
109     cos_fov_x = cos(theta_x);
110     slope_x =  -cos_fov_x / sin_fov_x;
111     // printf("slope_x = %.2f\n", slope_x);
112
113     // fov_x_clip and fov_y_clip convoluted algebraic simplification
114     // see code executed in tilemgr.cxx when USE_FAST_FOV_CLIP not
115     // defined Norman Vine -- nhv@yahoo.com
116 #if defined( USE_FAST_FOV_CLIP )
117     fov_x_clip = slope_x*cos_fov_x - sin_fov_x;
118 #endif // defined( USE_FAST_FOV_CLIP )
119
120     // calculate sin() and cos() of fov / 2 in Y direction;
121     theta_y = (fov * DEG_TO_RAD) / 2.0;
122     // printf("theta_y = %.2f\n", theta_y);
123     sin_fov_y = sin(theta_y);
124     cos_fov_y = cos(theta_y);
125     slope_y = cos_fov_y / sin_fov_y;
126     // printf("slope_y = %.2f\n", slope_y);
127
128 #if defined( USE_FAST_FOV_CLIP )
129     fov_y_clip = -(slope_y*cos_fov_y + sin_fov_y);      
130 #endif // defined( USE_FAST_FOV_CLIP )
131 }
132
133
134 // Cycle view mode
135 void FGView::cycle_view_mode() {
136     if ( view_mode == FG_VIEW_FIRST_PERSON ) {
137         view_mode = FG_VIEW_FOLLOW;
138     } else if ( view_mode == FG_VIEW_FOLLOW ) {
139         view_mode = FG_VIEW_FIRST_PERSON;
140     }
141 }
142
143
144 #ifdef 0
145 // Basically, this is a modified version of the Mesa gluLookAt()
146 // function that's been modified slightly so we can capture the
147 // result before sending it off to OpenGL land.
148 void FGView::LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
149                      GLdouble centerx, GLdouble centery, GLdouble centerz,
150                      GLdouble upx, GLdouble upy, GLdouble upz ) {
151     GLfloat *m;
152     GLdouble x[3], y[3], z[3];
153     GLdouble mag;
154
155     m = current_view.MODEL_VIEW;
156
157     /* Make rotation matrix */
158
159     /* Z vector */
160     z[0] = eyex - centerx;
161     z[1] = eyey - centery;
162     z[2] = eyez - centerz;
163     mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
164     if (mag) {  /* mpichler, 19950515 */
165         z[0] /= mag;
166         z[1] /= mag;
167         z[2] /= mag;
168     }
169
170     /* Y vector */
171     y[0] = upx;
172     y[1] = upy;
173     y[2] = upz;
174
175     /* X vector = Y cross Z */
176     x[0] =  y[1]*z[2] - y[2]*z[1];
177     x[1] = -y[0]*z[2] + y[2]*z[0];
178     x[2] =  y[0]*z[1] - y[1]*z[0];
179     
180     /* Recompute Y = Z cross X */
181     y[0] =  z[1]*x[2] - z[2]*x[1];
182     y[1] = -z[0]*x[2] + z[2]*x[0];
183     y[2] =  z[0]*x[1] - z[1]*x[0];
184
185     /* mpichler, 19950515 */
186     /* cross product gives area of parallelogram, which is < 1.0 for
187      * non-perpendicular unit-length vectors; so normalize x, y here
188      */
189
190     mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
191     if (mag) {
192         x[0] /= mag;
193         x[1] /= mag;
194         x[2] /= mag;
195     }
196
197     mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
198     if (mag) {
199         y[0] /= mag;
200         y[1] /= mag;
201         y[2] /= mag;
202     }
203
204 #define M(row,col)  m[col*4+row]
205     M(0,0) = x[0];  M(0,1) = x[1];  M(0,2) = x[2];  M(0,3) = 0.0;
206     M(1,0) = y[0];  M(1,1) = y[1];  M(1,2) = y[2];  M(1,3) = 0.0;
207     M(2,0) = z[0];  M(2,1) = z[1];  M(2,2) = z[2];  M(2,3) = 0.0;
208     // the following is part of the original gluLookAt(), but we are
209     // commenting it out because we know we are going to be doing a
210     // translation below which will set these values anyways
211     // M(3,0) = 0.0;   M(3,1) = 0.0;   M(3,2) = 0.0;   M(3,3) = 1.0;
212 #undef M
213
214     // Translate Eye to Origin
215     // replaces: glTranslated( -eyex, -eyey, -eyez );
216
217     // this has been slightly modified from the original glTranslate()
218     // code because we know that coming into this m[12] = m[13] =
219     // m[14] = 0.0, and m[15] = 1.0;
220     m[12] = m[0] * -eyex + m[4] * -eyey + m[8]  * -eyez /* + m[12] */;
221     m[13] = m[1] * -eyex + m[5] * -eyey + m[9]  * -eyez /* + m[13] */;
222     m[14] = m[2] * -eyex + m[6] * -eyey + m[10] * -eyez /* + m[14] */;
223     m[15] = 1.0 /* m[3] * -eyex + m[7] * -eyey + m[11] * -eyez + m[15] */;
224
225     // xglMultMatrixd( m );
226     xglLoadMatrixf( m );
227 }
228 #endif
229
230
231 // Update the view volume, position, and orientation
232 void FGView::UpdateViewParams( void ) {
233     FGInterface *f = current_aircraft.fdm_state;
234
235     UpdateViewMath(f);
236     // UpdateWorldToEye(f);
237     
238     if ((current_options.get_panel_status() != panel_hist) &&                          (current_options.get_panel_status()))
239     {
240         FGPanel::OurPanel->ReInit( 0, 0, 1024, 768);
241     }
242
243     if ( ! current_options.get_panel_status() ) {
244         xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
245     } else {
246         xglViewport(0, (GLint)((winHeight)*0.5768), (GLint)(winWidth), 
247                     (GLint)((winHeight)*0.4232) );
248     }
249
250     // Tell GL we are about to modify the projection parameters
251     xglMatrixMode(GL_PROJECTION);
252     xglLoadIdentity();
253     if ( f->get_Altitude() * FEET_TO_METER - scenery.cur_elev > 10.0 ) {
254         // ssgSetNearFar( 10.0, 100000.0 );
255         gluPerspective(current_options.get_fov(), win_ratio, 10.0, 100000.0);
256     } else {
257         // ssgSetNearFar( 0.5, 100000.0 );
258         gluPerspective(current_options.get_fov(), win_ratio, 0.5, 100000.0);
259         // printf("Near ground, minimizing near clip plane\n");
260     }
261     // }
262
263     xglMatrixMode(GL_MODELVIEW);
264     xglLoadIdentity();
265
266     // set up our view volume (default)
267 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
268     LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
269            view_pos.x() + view_forward[0], 
270            view_pos.y() + view_forward[1], 
271            view_pos.z() + view_forward[2],
272            view_up[0], view_up[1], view_up[2]);
273
274     // look almost straight up (testing and eclipse watching)
275     /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
276        view_pos.x() + view_up[0] + .001, 
277        view_pos.y() + view_up[1] + .001, 
278        view_pos.z() + view_up[2] + .001,
279        view_up[0], view_up[1], view_up[2]); */
280
281     // lock view horizontally towards sun (testing)
282     /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
283        view_pos.x() + surface_to_sun[0], 
284        view_pos.y() + surface_to_sun[1], 
285        view_pos.z() + surface_to_sun[2],
286        view_up[0], view_up[1], view_up[2]); */
287
288     // lock view horizontally towards south (testing)
289     /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
290        view_pos.x() + surface_south[0], 
291        view_pos.y() + surface_south[1], 
292        view_pos.z() + surface_south[2],
293        view_up[0], view_up[1], view_up[2]); */
294
295 #else // defined(FG_VIEW_INLINE_OPTIMIZATIONS)
296     //void FGView::LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
297     //               GLdouble centerx, GLdouble centery, GLdouble centerz,
298     //               GLdouble upx, GLdouble upy, GLdouble upz )
299     {
300         GLfloat *m;
301         GLdouble x[3], y[3], z[3];
302         //    GLdouble mag;
303
304         m = current_view.MODEL_VIEW;
305
306         /* Make rotation matrix */
307
308         /* Z vector */
309         z[0] = -view_forward[0]; //eyex - centerx;
310         z[1] = -view_forward[1]; //eyey - centery;
311         z[2] = -view_forward[2]; //eyez - centerz;
312         
313         // In our case this is a unit vector  NHV
314         
315         //    mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
316         //    if (mag) {  /* mpichler, 19950515 */
317         //              mag = 1.0/mag;
318         //              printf("mag(%f)  ", mag);
319         //      z[0] *= mag;
320         //      z[1] *= mag;
321         //      z[2] *= mag;
322         //    }
323
324         /* Y vector */
325         y[0] = view_up[0]; //upx;
326         y[1] = view_up[1]; //upy;
327         y[2] = view_up[2]; //upz;
328
329         /* X vector = Y cross Z */
330         x[0] =  y[1]*z[2] - y[2]*z[1];
331         x[1] = -y[0]*z[2] + y[2]*z[0];
332         x[2] =  y[0]*z[1] - y[1]*z[0];
333
334         //      printf(" %f %f %f  ", y[0], y[1], y[2]);
335     
336         /* Recompute Y = Z cross X */
337         //    y[0] =  z[1]*x[2] - z[2]*x[1];
338         //    y[1] = -z[0]*x[2] + z[2]*x[0];
339         //    y[2] =  z[0]*x[1] - z[1]*x[0];
340
341         //      printf(" %f %f %f\n", y[0], y[1], y[2]);
342         
343         // In our case these are unit vectors  NHV
344
345         /* mpichler, 19950515 */
346         /* cross product gives area of parallelogram, which is < 1.0 for
347          * non-perpendicular unit-length vectors; so normalize x, y here
348          */
349
350         //    mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
351         //    if (mag) {
352         //              mag = 1.0/mag;
353         //              printf("mag2(%f) ", mag);
354         //      x[0] *= mag;
355         //      x[1] *= mag;
356         //      x[2] *= mag;
357         //    }
358
359         //    mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
360         //    if (mag) {
361         //              mag = 1.0/mag;
362         //              printf("mag3(%f)\n", mag);
363         //      y[0] *= mag;
364         //      y[1] *= mag;
365         //      y[2] *= mag;
366         //    }
367
368 #define M(row,col)  m[col*4+row]
369         M(0,0) = x[0];  M(0,1) = x[1];  M(0,2) = x[2];  M(0,3) = 0.0;
370         M(1,0) = y[0];  M(1,1) = y[1];  M(1,2) = y[2];  M(1,3) = 0.0;
371         M(2,0) = z[0];  M(2,1) = z[1];  M(2,2) = z[2];  M(2,3) = 0.0;
372         // the following is part of the original gluLookAt(), but we are
373         // commenting it out because we know we are going to be doing a
374         // translation below which will set these values anyways
375         // M(3,0) = 0.0;   M(3,1) = 0.0;   M(3,2) = 0.0;   M(3,3) = 1.0;
376 #undef M
377
378         // Translate Eye to Origin
379         // replaces: glTranslated( -eyex, -eyey, -eyez );
380
381         // this has been slightly modified from the original glTranslate()
382         // code because we know that coming into this m[12] = m[13] =
383         // m[14] = 0.0, and m[15] = 1.0;
384         m[12] = m[0] * -view_pos.x() + m[4] * -view_pos.y() + m[8]  * -view_pos.z() /* + m[12] */;
385         m[13] = m[1] * -view_pos.x() + m[5] * -view_pos.y() + m[9]  * -view_pos.z() /* + m[13] */;
386         m[14] = m[2] * -view_pos.x() + m[6] * -view_pos.y() + m[10] * -view_pos.z() /* + m[14] */;
387         m[15] = 1.0 /* m[3] * -view_pos.x() + m[7] * -view_pos.y() + m[11] * -view_pos.z() + m[15] */;
388
389         // xglMultMatrixd( m );
390         xglLoadMatrixf( m );
391     }
392 #endif // FG_VIEW_INLINE_OPTIMIZATIONS
393
394     panel_hist = current_options.get_panel_status();
395 }
396
397
398 void getRotMatrix(double* out, MAT3vec vec, double radians)
399 {
400     /* This function contributed by Erich Boleyn (erich@uruk.org) */
401     /* This function used from the Mesa OpenGL code (matrix.c)  */
402     double s, c; // mag,
403     double vx, vy, vz, xy, yz, zx, xs, ys, zs, one_c; //, xx, yy, zz
404   
405     MAT3identity(out);
406     s = sin(radians);
407     c = cos(radians);
408   
409     //  mag = getMagnitude();
410   
411     vx = vec[0];
412     vy = vec[1];
413     vz = vec[2];
414   
415 #define M(row,col)  out[row*4 + col]
416   
417     /*
418      *     Arbitrary axis rotation matrix.
419      *
420      *  This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
421      *  like so:  Rz * Ry * T * Ry' * Rz'.  T is the final rotation
422      *  (which is about the X-axis), and the two composite transforms
423      *  Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
424      *  from the arbitrary axis to the X-axis then back.  They are
425      *  all elementary rotations.
426      *
427      *  Rz' is a rotation about the Z-axis, to bring the axis vector
428      *  into the x-z plane.  Then Ry' is applied, rotating about the
429      *  Y-axis to bring the axis vector parallel with the X-axis.  The
430      *  rotation about the X-axis is then performed.  Ry and Rz are
431      *  simply the respective inverse transforms to bring the arbitrary
432      *  axis back to it's original orientation.  The first transforms
433      *  Rz' and Ry' are considered inverses, since the data from the
434      *  arbitrary axis gives you info on how to get to it, not how
435      *  to get away from it, and an inverse must be applied.
436      *
437      *  The basic calculation used is to recognize that the arbitrary
438      *  axis vector (x, y, z), since it is of unit length, actually
439      *  represents the sines and cosines of the angles to rotate the
440      *  X-axis to the same orientation, with theta being the angle about
441      *  Z and phi the angle about Y (in the order described above)
442      *  as follows:
443      *
444      *  cos ( theta ) = x / sqrt ( 1 - z^2 )
445      *  sin ( theta ) = y / sqrt ( 1 - z^2 )
446      *
447      *  cos ( phi ) = sqrt ( 1 - z^2 )
448      *  sin ( phi ) = z
449      *
450      *  Note that cos ( phi ) can further be inserted to the above
451      *  formulas:
452      *
453      *  cos ( theta ) = x / cos ( phi )
454      *  sin ( theta ) = y / cos ( phi )
455      *
456      *  ...etc.  Because of those relations and the standard trigonometric
457      *  relations, it is pssible to reduce the transforms down to what
458      *  is used below.  It may be that any primary axis chosen will give the
459      *  same results (modulo a sign convention) using thie method.
460      *
461      *  Particularly nice is to notice that all divisions that might
462      *  have caused trouble when parallel to certain planes or
463      *  axis go away with care paid to reducing the expressions.
464      *  After checking, it does perform correctly under all cases, since
465      *  in all the cases of division where the denominator would have
466      *  been zero, the numerator would have been zero as well, giving
467      *  the expected result.
468      */
469     
470     one_c = 1.0F - c;
471     
472     //  xx = vx * vx;
473     //  yy = vy * vy;
474     //  zz = vz * vz;
475   
476     //  xy = vx * vy;
477     //  yz = vy * vz;
478     //  zx = vz * vx;
479   
480   
481     M(0,0) = (one_c * vx * vx) + c;  
482     xs = vx * s;
483     yz = vy * vz * one_c;
484     M(1,2) = yz + xs;
485     M(2,1) = yz - xs;
486
487     M(1,1) = (one_c * vy * vy) + c;
488     ys = vy * s;
489     zx = vz * vx * one_c;
490     M(0,2) = zx - ys;
491     M(2,0) = zx + ys;
492   
493     M(2,2) = (one_c * vz *vz) + c;
494     zs = vz * s;
495     xy = vx * vy * one_c;
496     M(0,1) = xy + zs;
497     M(1,0) = xy - zs;
498   
499     //  M(0,0) = (one_c * xx) + c;
500     //  M(1,0) = (one_c * xy) - zs;
501     //  M(2,0) = (one_c * zx) + ys;
502   
503     //  M(0,1) = (one_c * xy) + zs;
504     //  M(1,1) = (one_c * yy) + c;
505     //  M(2,1) = (one_c * yz) - xs;
506   
507     //  M(0,2) = (one_c * zx) - ys;
508     //  M(1,2) = (one_c * yz) + xs;
509     //  M(2,2) = (one_c * zz) + c;
510   
511 #undef M
512 }
513
514
515 // Update the view parameters
516 void FGView::UpdateViewMath( FGInterface *f ) {
517     Point3D p;
518     MAT3vec vec, forward, v0, minus_z;
519     MAT3mat R, TMP, UP, LOCAL, VIEW;
520     double ntmp;
521
522     if ( update_fov ) {
523         // printf("Updating fov\n");
524         UpdateFOV( current_options );
525         update_fov = false;
526     }
527                 
528     scenery.center = scenery.next_center;
529
530 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
531     // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x,
532     //        scenery.center.y, scenery.center.z);
533
534     // calculate the cartesion coords of the current lat/lon/0 elev
535     p = Point3D( f->get_Longitude(), 
536                  f->get_Lat_geocentric(), 
537                  f->get_Sea_level_radius() * FEET_TO_METER );
538
539     cur_zero_elev = fgPolarToCart3d(p) - scenery.center;
540
541     // calculate view position in current FG view coordinate system
542     // p.lon & p.lat are already defined earlier, p.radius was set to
543     // the sea level radius, so now we add in our altitude.
544     if ( f->get_Altitude() * FEET_TO_METER > 
545          (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
546         p.setz( p.radius() + f->get_Altitude() * FEET_TO_METER );
547     } else {
548         p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
549     }
550
551     abs_view_pos = fgPolarToCart3d(p);
552         
553 #else // FG_VIEW_INLINE_OPTIMIZATIONS
554         
555     double tmp_radius = f->get_Sea_level_radius() * FEET_TO_METER;
556     double tmp = f->get_cos_lat_geocentric() * tmp_radius;
557         
558     cur_zero_elev.setx(f->get_cos_longitude()*tmp - scenery.center.x());
559     cur_zero_elev.sety(f->get_sin_longitude()*tmp - scenery.center.y());
560     cur_zero_elev.setz(f->get_sin_lat_geocentric()*tmp_radius - scenery.center.z());
561
562     // calculate view position in current FG view coordinate system
563     // p.lon & p.lat are already defined earlier, p.radius was set to
564     // the sea level radius, so now we add in our altitude.
565     if ( f->get_Altitude() * FEET_TO_METER > 
566          (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
567         tmp_radius += f->get_Altitude() * FEET_TO_METER;
568     } else {
569         tmp_radius += scenery.cur_elev + 0.5 * METER_TO_FEET ;
570     }
571     tmp = f->get_cos_lat_geocentric() * tmp_radius;
572     abs_view_pos.setx(f->get_cos_longitude()*tmp);
573     abs_view_pos.sety(f->get_sin_longitude()*tmp);
574     abs_view_pos.setz(f->get_sin_lat_geocentric()*tmp_radius);
575         
576 #endif // FG_VIEW_INLINE_OPTIMIZATIONS
577         
578     view_pos = abs_view_pos - scenery.center;
579
580     FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
581     FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
582     FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = " << view_pos );
583
584     // Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw)
585     // from FG_T_local_to_body[3][3]
586
587     if ( use_larcsim_local_to_body ) {
588
589         // Question: Why is the LaRCsim matrix arranged so differently
590         // than the one we need???
591
592         // Answer (I think): The LaRCsim matrix is generated in a
593         // different reference frame than we've set up for our world
594
595         LOCAL[0][0] = f->get_T_local_to_body_33();
596         LOCAL[0][1] = -f->get_T_local_to_body_32();
597         LOCAL[0][2] = -f->get_T_local_to_body_31();
598         LOCAL[0][3] = 0.0;
599         LOCAL[1][0] = -f->get_T_local_to_body_23();
600         LOCAL[1][1] = f->get_T_local_to_body_22();
601         LOCAL[1][2] = f->get_T_local_to_body_21();
602         LOCAL[1][3] = 0.0;
603         LOCAL[2][0] = -f->get_T_local_to_body_13();
604         LOCAL[2][1] = f->get_T_local_to_body_12();
605         LOCAL[2][2] = f->get_T_local_to_body_11();
606         LOCAL[2][3] = 0.0;
607         LOCAL[3][0] = LOCAL[3][1] = LOCAL[3][2] = LOCAL[3][3] = 0.0;
608         LOCAL[3][3] = 1.0;
609
610         // printf("LaRCsim LOCAL matrix\n");
611         // MAT3print(LOCAL, stdout);
612
613     } else {
614
615         // calculate the transformation matrix to go from LaRCsim to ssg
616         sgVec3 vec1;
617         sgSetVec3( vec1, 0.0, 1.0, 0.0 );
618         sgMat4 mat1;
619         sgMakeRotMat4( mat1, 90, vec1 );
620
621         sgVec3 vec2;
622         sgSetVec3( vec2, 1.0, 0.0, 0.0 );
623         sgMat4 mat2;
624         sgMakeRotMat4( mat2, 90, vec2 );
625
626         sgMultMat4( sgLARC_TO_SSG, mat1, mat2 );
627
628         /*
629         cout << "LaRCsim to SSG:" << endl;
630         MAT3mat print;
631         int i;
632         int j;
633         for ( i = 0; i < 4; i++ ) {
634             for ( j = 0; j < 4; j++ ) {
635                 print[i][j] = sgLARC_TO_SSG[i][j];
636             }
637         }
638         MAT3print( print, stdout);
639         */
640
641         // code to calculate LOCAL matrix calculated from Phi, Theta, and
642         // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
643         // flight model
644
645         MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
646         MAT3rotate(R, vec, f->get_Phi());
647         // cout << "Roll matrix" << endl;
648         // MAT3print(R, stdout);
649
650         sgVec3 sgrollvec;
651         sgSetVec3( sgrollvec, 0.0, 0.0, 1.0 );
652         sgMat4 sgPHI;           // roll
653         sgMakeRotMat4( sgPHI, f->get_Phi() * RAD_TO_DEG, sgrollvec );
654
655
656         MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
657         MAT3rotate(TMP, vec, f->get_Theta());
658         // cout << "Pitch matrix" << endl;;
659         // MAT3print(TMP, stdout);
660         MAT3mult(R, R, TMP);
661         // cout << "tmp rotation matrix, R:" << endl;;
662         // MAT3print(R, stdout);
663
664         sgVec3 sgpitchvec;
665         sgSetVec3( sgpitchvec, 0.0, 1.0, 0.0 );
666         sgMat4 sgTHETA;         // pitch
667         sgMakeRotMat4( sgTHETA, f->get_Theta() * RAD_TO_DEG,
668                        sgpitchvec );
669
670         sgMat4 sgROT;
671         sgMultMat4( sgROT, sgPHI, sgTHETA );
672
673
674         MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
675         MAT3rotate(TMP, vec, -f->get_Psi());
676         // cout << "Yaw matrix" << endl;
677         // MAT3print(TMP, stdout);
678         MAT3mult(LOCAL, R, TMP);
679         // cout << "LOCAL matrix:" << endl;
680         // MAT3print(LOCAL, stdout);
681
682         sgVec3 sgyawvec;
683         sgSetVec3( sgyawvec, 1.0, 0.0, 0.0 );
684         sgMat4 sgPSI;           // pitch
685         sgMakeRotMat4( sgPSI, -f->get_Psi() * RAD_TO_DEG, sgyawvec );
686
687         sgMultMat4( sgLOCAL, sgROT, sgPSI );
688
689         /*
690         MAT3mat print;
691         int i;
692         int j;
693         for ( i = 0; i < 4; i++ ) {
694             for ( j = 0; j < 4; j++ ) {
695                 print[i][j] = sgLOCAL[i][j];
696             }
697         }
698         MAT3print( print, stdout);
699         */
700     } // if ( use_larcsim_local_to_body ) 
701
702 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
703         
704     // Derive the local UP transformation matrix based on *geodetic*
705     // coordinates
706     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
707     MAT3rotate(R, vec, f->get_Longitude());     // R = rotate about Z axis
708     // printf("Longitude matrix\n");
709     // MAT3print(R, stdout);
710
711     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
712     MAT3mult_vec(vec, vec, R);
713     MAT3rotate(TMP, vec, -f->get_Latitude());  // TMP = rotate about X axis
714     // printf("Latitude matrix\n");
715     // MAT3print(TMP, stdout);
716
717     MAT3mult(UP, R, TMP);
718     // cout << "Local up matrix" << endl;;
719     // MAT3print(UP, stdout);
720
721     sgMakeRotMat4( sgUP, 
722                    f->get_Longitude() * RAD_TO_DEG,
723                    0.0,
724                    -f->get_Latitude() * RAD_TO_DEG );
725     /*
726     cout << "FG derived UP matrix using sg routines" << endl;
727     MAT3mat print;
728     int i;
729     int j;
730     for ( i = 0; i < 4; i++ ) {
731         for ( j = 0; j < 4; j++ ) {
732             print[i][j] = sgUP[i][j];
733         }
734     }
735     MAT3print( print, stdout);
736     */
737
738     MAT3_SET_VEC(local_up, 1.0, 0.0, 0.0);
739     MAT3mult_vec(local_up, local_up, UP);
740
741     // printf( "Local Up = (%.4f, %.4f, %.4f)\n",
742     //         local_up[0], local_up[1], local_up[2]);
743     
744     // Alternative method to Derive local up vector based on
745     // *geodetic* coordinates
746     // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
747     // printf( "    Alt Up = (%.4f, %.4f, %.4f)\n", 
748     //         alt_up.x, alt_up.y, alt_up.z);
749
750     // Calculate the VIEW matrix
751     MAT3mult(VIEW, LOCAL, UP);
752     // cout << "VIEW matrix" << endl;;
753     // MAT3print(VIEW, stdout);
754
755     sgMat4 sgTMP;
756     sgMultMat4( sgTMP, sgLOCAL, sgUP );
757     sgMultMat4( sgVIEW_ROT, sgLARC_TO_SSG, sgTMP );
758
759     sgMakeTransMat4( sgTRANS, view_pos.x(), view_pos.y(), view_pos.z() );
760
761     sgMultMat4( sgVIEW, sgVIEW_ROT, sgTRANS );
762
763     FGMat4Wrapper tmp;
764     sgCopyMat4( tmp.m, sgVIEW );
765     follow.push_back( tmp );
766
767     /*
768     cout << "FG derived VIEW matrix using sg routines" << endl;
769     MAT3mat print;
770     int i;
771     int j;
772     for ( i = 0; i < 4; i++ ) {
773         for ( j = 0; j < 4; j++ ) {
774             print[i][j] = sgVIEW[i][j];
775         }
776     }
777     MAT3print( print, stdout);
778     */
779
780
781     // generate the current up, forward, and fwrd-view vectors
782     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
783     MAT3mult_vec(view_up, vec, VIEW);
784
785     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
786     MAT3mult_vec(forward, vec, VIEW);
787     // printf( "Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1], 
788     //         forward[2]);
789
790     MAT3rotate(TMP, view_up, view_offset);
791     MAT3mult_vec(view_forward, forward, TMP);
792
793     // make a vector to the current view position
794     MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
795
796     // Given a vector pointing straight down (-Z), map into onto the
797     // local plane representing "horizontal".  This should give us the
798     // local direction for moving "south".
799     MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
800     map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
801     MAT3_NORMALIZE_VEC(surface_south, ntmp);
802     // printf( "Surface direction directly south %.2f %.2f %.2f\n",
803     //         surface_south[0], surface_south[1], surface_south[2]);
804
805     // now calculate the surface east vector
806     MAT3rotate(TMP, view_up, FG_PI_2);
807     MAT3mult_vec(surface_east, surface_south, TMP);
808     // printf( "Surface direction directly east %.2f %.2f %.2f\n",
809     //         surface_east[0], surface_east[1], surface_east[2]);
810     // printf( "Should be close to zero = %.2f\n", 
811     //         MAT3_DOT_PRODUCT(surface_south, surface_east));
812         
813 #else // FG_VIEW_INLINE_OPTIMIZATIONS
814          
815     //  // Build spherical to cartesian transform matrix directly
816     double cos_lat = f->get_cos_latitude(); // cos(-f->get_Latitude());
817     double sin_lat = -f->get_sin_latitude(); // sin(-f->get_Latitude());
818     double cos_lon = f->get_cos_longitude(); //cos(f->get_Longitude());
819     double sin_lon = f->get_sin_longitude(); //sin(f->get_Longitude());
820
821     double *mat = (double *)UP;
822         
823     mat[0] =  cos_lat*cos_lon;
824     mat[1] =  cos_lat*sin_lon;
825     mat[2] = -sin_lat;
826     mat[3] =  0.0;
827     mat[4] =  -sin_lon;
828     mat[5] =  cos_lon;
829     mat[6] =  0.0;
830     mat[7] =  0.0;
831     mat[8]  =  sin_lat*cos_lon;
832     mat[9]  =  sin_lat*sin_lon;
833     mat[10] =  cos_lat;
834     mat[11] =  mat[12] = mat[13] = mat[14] = 0.0;
835     mat[15] =  1.0;
836
837     MAT3mult(VIEW, LOCAL, UP);
838         
839     // THESE COULD JUST BE POINTERS !!!
840     MAT3_SET_VEC(local_up, mat[0],     mat[1],     mat[2]);
841     MAT3_SET_VEC(view_up,  VIEW[0][0], VIEW[0][1], VIEW[0][2]);
842     MAT3_SET_VEC(forward,  VIEW[2][0], VIEW[2][1], VIEW[2][2]);
843
844     getRotMatrix((double *)TMP, view_up, view_offset);
845     MAT3mult_vec(view_forward, forward, TMP);
846
847     // make a vector to the current view position
848     MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
849
850     // Given a vector pointing straight down (-Z), map into onto the
851     // local plane representing "horizontal".  This should give us the
852     // local direction for moving "south".
853     MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
854     map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
855
856     MAT3_NORMALIZE_VEC(surface_south, ntmp);
857     // printf( "Surface direction directly south %.6f %.6f %.6f\n",
858     //         surface_south[0], surface_south[1], surface_south[2]);
859
860     // now calculate the surface east vector
861     getRotMatrix((double *)TMP, view_up, FG_PI_2);
862     MAT3mult_vec(surface_east, surface_south, TMP);
863     // printf( "Surface direction directly east %.6f %.6f %.6f\n",
864     //         surface_east[0], surface_east[1], surface_east[2]);
865     // printf( "Should be close to zero = %.6f\n", 
866     //         MAT3_DOT_PRODUCT(surface_south, surface_east));
867 #endif // !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
868 }
869
870
871 // Destructor
872 FGView::~FGView( void ) {
873 }