]> git.mxchange.org Git - flightgear.git/blob - src/Main/views.cxx
Return to glider model ...
[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     MAT3identity(WORLD);
72 }
73
74
75 // Initialize a view structure
76 void FGView::Init( void ) {
77     FG_LOG( FG_VIEW, FG_INFO, "Initializing View parameters" );
78
79     view_mode = FG_VIEW_FIRST_PERSON;
80     view_offset = 0.0;
81     goal_view_offset = 0.0;
82
83     winWidth = current_options.get_xsize();
84     winHeight = current_options.get_ysize();
85
86     if ( ! current_options.get_panel_status() ) {
87         current_view.set_win_ratio( (GLfloat) winWidth / (GLfloat) winHeight );
88     } else {
89         current_view.set_win_ratio( (GLfloat) winWidth / 
90                                     ((GLfloat) (winHeight)*0.4232) );
91     }
92
93     force_update_fov_math();
94 }
95
96
97 // Update the field of view coefficients
98 void FGView::UpdateFOV( const fgOPTIONS& o ) {
99     ssgSetFOV( o.get_fov(), 0.0 );
100
101     double fov, theta_x, theta_y;
102
103     fov = o.get_fov();
104         
105     // printf("win_ratio = %.2f\n", win_ratio);
106     // calculate sin() and cos() of fov / 2 in X direction;
107     theta_x = (fov * win_ratio * DEG_TO_RAD) / 2.0;
108     // printf("theta_x = %.2f\n", theta_x);
109     sin_fov_x = sin(theta_x);
110     cos_fov_x = cos(theta_x);
111     slope_x =  -cos_fov_x / sin_fov_x;
112     // printf("slope_x = %.2f\n", slope_x);
113
114     // fov_x_clip and fov_y_clip convoluted algebraic simplification
115     // see code executed in tilemgr.cxx when USE_FAST_FOV_CLIP not
116     // defined Norman Vine -- nhv@yahoo.com
117 #if defined( USE_FAST_FOV_CLIP )
118     fov_x_clip = slope_x*cos_fov_x - sin_fov_x;
119 #endif // defined( USE_FAST_FOV_CLIP )
120
121     // calculate sin() and cos() of fov / 2 in Y direction;
122     theta_y = (fov * DEG_TO_RAD) / 2.0;
123     // printf("theta_y = %.2f\n", theta_y);
124     sin_fov_y = sin(theta_y);
125     cos_fov_y = cos(theta_y);
126     slope_y = cos_fov_y / sin_fov_y;
127     // printf("slope_y = %.2f\n", slope_y);
128
129 #if defined( USE_FAST_FOV_CLIP )
130     fov_y_clip = -(slope_y*cos_fov_y + sin_fov_y);      
131 #endif // defined( USE_FAST_FOV_CLIP )
132 }
133
134
135 // Cycle view mode
136 void FGView::cycle_view_mode() {
137     if ( view_mode == FG_VIEW_FIRST_PERSON ) {
138         view_mode = FG_VIEW_FOLLOW;
139     } else if ( view_mode == FG_VIEW_FOLLOW ) {
140         view_mode = FG_VIEW_FIRST_PERSON;
141     }
142 }
143
144
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
229
230 // Update the view volume, position, and orientation
231 void FGView::UpdateViewParams( void ) {
232     FGInterface *f = current_aircraft.fdm_state;
233
234     UpdateViewMath(f);
235     UpdateWorldToEye(f);
236     
237     if ((current_options.get_panel_status() != panel_hist) &&                          (current_options.get_panel_status()))
238     {
239         FGPanel::OurPanel->ReInit( 0, 0, 1024, 768);
240     }
241
242     if ( ! current_options.get_panel_status() ) {
243         xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
244     } else {
245         xglViewport(0, (GLint)((winHeight)*0.5768), (GLint)(winWidth), 
246                     (GLint)((winHeight)*0.4232) );
247     }
248
249     // Tell GL we are about to modify the projection parameters
250     xglMatrixMode(GL_PROJECTION);
251     xglLoadIdentity();
252     if ( f->get_Altitude() * FEET_TO_METER - scenery.cur_elev > 10.0 ) {
253         // ssgSetNearFar( 10.0, 100000.0 );
254         gluPerspective(current_options.get_fov(), win_ratio, 10.0, 100000.0);
255     } else {
256         // ssgSetNearFar( 0.5, 100000.0 );
257         gluPerspective(current_options.get_fov(), win_ratio, 0.5, 100000.0);
258         // printf("Near ground, minimizing near clip plane\n");
259     }
260     // }
261
262     xglMatrixMode(GL_MODELVIEW);
263     xglLoadIdentity();
264
265     // set up our view volume (default)
266 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
267     LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
268            view_pos.x() + view_forward[0], 
269            view_pos.y() + view_forward[1], 
270            view_pos.z() + view_forward[2],
271            view_up[0], view_up[1], view_up[2]);
272
273     // look almost straight up (testing and eclipse watching)
274     /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
275        view_pos.x() + view_up[0] + .001, 
276        view_pos.y() + view_up[1] + .001, 
277        view_pos.z() + view_up[2] + .001,
278        view_up[0], view_up[1], view_up[2]); */
279
280     // lock view horizontally towards sun (testing)
281     /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
282        view_pos.x() + surface_to_sun[0], 
283        view_pos.y() + surface_to_sun[1], 
284        view_pos.z() + surface_to_sun[2],
285        view_up[0], view_up[1], view_up[2]); */
286
287     // lock view horizontally towards south (testing)
288     /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
289        view_pos.x() + surface_south[0], 
290        view_pos.y() + surface_south[1], 
291        view_pos.z() + surface_south[2],
292        view_up[0], view_up[1], view_up[2]); */
293
294 #else // defined(FG_VIEW_INLINE_OPTIMIZATIONS)
295     //void FGView::LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
296     //               GLdouble centerx, GLdouble centery, GLdouble centerz,
297     //               GLdouble upx, GLdouble upy, GLdouble upz )
298     {
299         GLfloat *m;
300         GLdouble x[3], y[3], z[3];
301         //    GLdouble mag;
302
303         m = current_view.MODEL_VIEW;
304
305         /* Make rotation matrix */
306
307         /* Z vector */
308         z[0] = -view_forward[0]; //eyex - centerx;
309         z[1] = -view_forward[1]; //eyey - centery;
310         z[2] = -view_forward[2]; //eyez - centerz;
311         
312         // In our case this is a unit vector  NHV
313         
314         //    mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
315         //    if (mag) {  /* mpichler, 19950515 */
316         //              mag = 1.0/mag;
317         //              printf("mag(%f)  ", mag);
318         //      z[0] *= mag;
319         //      z[1] *= mag;
320         //      z[2] *= mag;
321         //    }
322
323         /* Y vector */
324         y[0] = view_up[0]; //upx;
325         y[1] = view_up[1]; //upy;
326         y[2] = view_up[2]; //upz;
327
328         /* X vector = Y cross Z */
329         x[0] =  y[1]*z[2] - y[2]*z[1];
330         x[1] = -y[0]*z[2] + y[2]*z[0];
331         x[2] =  y[0]*z[1] - y[1]*z[0];
332
333         //      printf(" %f %f %f  ", y[0], y[1], y[2]);
334     
335         /* Recompute Y = Z cross X */
336         //    y[0] =  z[1]*x[2] - z[2]*x[1];
337         //    y[1] = -z[0]*x[2] + z[2]*x[0];
338         //    y[2] =  z[0]*x[1] - z[1]*x[0];
339
340         //      printf(" %f %f %f\n", y[0], y[1], y[2]);
341         
342         // In our case these are unit vectors  NHV
343
344         /* mpichler, 19950515 */
345         /* cross product gives area of parallelogram, which is < 1.0 for
346          * non-perpendicular unit-length vectors; so normalize x, y here
347          */
348
349         //    mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
350         //    if (mag) {
351         //              mag = 1.0/mag;
352         //              printf("mag2(%f) ", mag);
353         //      x[0] *= mag;
354         //      x[1] *= mag;
355         //      x[2] *= mag;
356         //    }
357
358         //    mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
359         //    if (mag) {
360         //              mag = 1.0/mag;
361         //              printf("mag3(%f)\n", mag);
362         //      y[0] *= mag;
363         //      y[1] *= mag;
364         //      y[2] *= mag;
365         //    }
366
367 #define M(row,col)  m[col*4+row]
368         M(0,0) = x[0];  M(0,1) = x[1];  M(0,2) = x[2];  M(0,3) = 0.0;
369         M(1,0) = y[0];  M(1,1) = y[1];  M(1,2) = y[2];  M(1,3) = 0.0;
370         M(2,0) = z[0];  M(2,1) = z[1];  M(2,2) = z[2];  M(2,3) = 0.0;
371         // the following is part of the original gluLookAt(), but we are
372         // commenting it out because we know we are going to be doing a
373         // translation below which will set these values anyways
374         // M(3,0) = 0.0;   M(3,1) = 0.0;   M(3,2) = 0.0;   M(3,3) = 1.0;
375 #undef M
376
377         // Translate Eye to Origin
378         // replaces: glTranslated( -eyex, -eyey, -eyez );
379
380         // this has been slightly modified from the original glTranslate()
381         // code because we know that coming into this m[12] = m[13] =
382         // m[14] = 0.0, and m[15] = 1.0;
383         m[12] = m[0] * -view_pos.x() + m[4] * -view_pos.y() + m[8]  * -view_pos.z() /* + m[12] */;
384         m[13] = m[1] * -view_pos.x() + m[5] * -view_pos.y() + m[9]  * -view_pos.z() /* + m[13] */;
385         m[14] = m[2] * -view_pos.x() + m[6] * -view_pos.y() + m[10] * -view_pos.z() /* + m[14] */;
386         m[15] = 1.0 /* m[3] * -view_pos.x() + m[7] * -view_pos.y() + m[11] * -view_pos.z() + m[15] */;
387
388         // xglMultMatrixd( m );
389         xglLoadMatrixf( m );
390     }
391 #endif // FG_VIEW_INLINE_OPTIMIZATIONS
392
393     panel_hist = current_options.get_panel_status();
394 }
395
396
397 void getRotMatrix(double* out, MAT3vec vec, double radians)
398 {
399     /* This function contributed by Erich Boleyn (erich@uruk.org) */
400     /* This function used from the Mesa OpenGL code (matrix.c)  */
401     double s, c; // mag,
402     double vx, vy, vz, xy, yz, zx, xs, ys, zs, one_c; //, xx, yy, zz
403   
404     MAT3identity(out);
405     s = sin(radians);
406     c = cos(radians);
407   
408     //  mag = getMagnitude();
409   
410     vx = vec[0];
411     vy = vec[1];
412     vz = vec[2];
413   
414 #define M(row,col)  out[row*4 + col]
415   
416     /*
417      *     Arbitrary axis rotation matrix.
418      *
419      *  This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
420      *  like so:  Rz * Ry * T * Ry' * Rz'.  T is the final rotation
421      *  (which is about the X-axis), and the two composite transforms
422      *  Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
423      *  from the arbitrary axis to the X-axis then back.  They are
424      *  all elementary rotations.
425      *
426      *  Rz' is a rotation about the Z-axis, to bring the axis vector
427      *  into the x-z plane.  Then Ry' is applied, rotating about the
428      *  Y-axis to bring the axis vector parallel with the X-axis.  The
429      *  rotation about the X-axis is then performed.  Ry and Rz are
430      *  simply the respective inverse transforms to bring the arbitrary
431      *  axis back to it's original orientation.  The first transforms
432      *  Rz' and Ry' are considered inverses, since the data from the
433      *  arbitrary axis gives you info on how to get to it, not how
434      *  to get away from it, and an inverse must be applied.
435      *
436      *  The basic calculation used is to recognize that the arbitrary
437      *  axis vector (x, y, z), since it is of unit length, actually
438      *  represents the sines and cosines of the angles to rotate the
439      *  X-axis to the same orientation, with theta being the angle about
440      *  Z and phi the angle about Y (in the order described above)
441      *  as follows:
442      *
443      *  cos ( theta ) = x / sqrt ( 1 - z^2 )
444      *  sin ( theta ) = y / sqrt ( 1 - z^2 )
445      *
446      *  cos ( phi ) = sqrt ( 1 - z^2 )
447      *  sin ( phi ) = z
448      *
449      *  Note that cos ( phi ) can further be inserted to the above
450      *  formulas:
451      *
452      *  cos ( theta ) = x / cos ( phi )
453      *  sin ( theta ) = y / cos ( phi )
454      *
455      *  ...etc.  Because of those relations and the standard trigonometric
456      *  relations, it is pssible to reduce the transforms down to what
457      *  is used below.  It may be that any primary axis chosen will give the
458      *  same results (modulo a sign convention) using thie method.
459      *
460      *  Particularly nice is to notice that all divisions that might
461      *  have caused trouble when parallel to certain planes or
462      *  axis go away with care paid to reducing the expressions.
463      *  After checking, it does perform correctly under all cases, since
464      *  in all the cases of division where the denominator would have
465      *  been zero, the numerator would have been zero as well, giving
466      *  the expected result.
467      */
468     
469     one_c = 1.0F - c;
470     
471     //  xx = vx * vx;
472     //  yy = vy * vy;
473     //  zz = vz * vz;
474   
475     //  xy = vx * vy;
476     //  yz = vy * vz;
477     //  zx = vz * vx;
478   
479   
480     M(0,0) = (one_c * vx * vx) + c;  
481     xs = vx * s;
482     yz = vy * vz * one_c;
483     M(1,2) = yz + xs;
484     M(2,1) = yz - xs;
485
486     M(1,1) = (one_c * vy * vy) + c;
487     ys = vy * s;
488     zx = vz * vx * one_c;
489     M(0,2) = zx - ys;
490     M(2,0) = zx + ys;
491   
492     M(2,2) = (one_c * vz *vz) + c;
493     zs = vz * s;
494     xy = vx * vy * one_c;
495     M(0,1) = xy + zs;
496     M(1,0) = xy - zs;
497   
498     //  M(0,0) = (one_c * xx) + c;
499     //  M(1,0) = (one_c * xy) - zs;
500     //  M(2,0) = (one_c * zx) + ys;
501   
502     //  M(0,1) = (one_c * xy) + zs;
503     //  M(1,1) = (one_c * yy) + c;
504     //  M(2,1) = (one_c * yz) - xs;
505   
506     //  M(0,2) = (one_c * zx) - ys;
507     //  M(1,2) = (one_c * yz) + xs;
508     //  M(2,2) = (one_c * zz) + c;
509   
510 #undef M
511 }
512
513
514 // Update the view parameters
515 void FGView::UpdateViewMath( FGInterface *f ) {
516     Point3D p;
517     MAT3vec vec, forward, v0, minus_z;
518     MAT3mat R, TMP, UP, LOCAL, VIEW;
519     double ntmp;
520
521     if ( update_fov ) {
522         // printf("Updating fov\n");
523         UpdateFOV( current_options );
524         update_fov = false;
525     }
526                 
527     scenery.center = scenery.next_center;
528
529 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
530     // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x,
531     //        scenery.center.y, scenery.center.z);
532
533     // calculate the cartesion coords of the current lat/lon/0 elev
534     p = Point3D( f->get_Longitude(), 
535                  f->get_Lat_geocentric(), 
536                  f->get_Sea_level_radius() * FEET_TO_METER );
537
538     cur_zero_elev = fgPolarToCart3d(p) - scenery.center;
539
540     // calculate view position in current FG view coordinate system
541     // p.lon & p.lat are already defined earlier, p.radius was set to
542     // the sea level radius, so now we add in our altitude.
543     if ( f->get_Altitude() * FEET_TO_METER > 
544          (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
545         p.setz( p.radius() + f->get_Altitude() * FEET_TO_METER );
546     } else {
547         p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
548     }
549
550     abs_view_pos = fgPolarToCart3d(p);
551         
552 #else // FG_VIEW_INLINE_OPTIMIZATIONS
553         
554     double tmp_radius = f->get_Sea_level_radius() * FEET_TO_METER;
555     double tmp = f->get_cos_lat_geocentric() * tmp_radius;
556         
557     cur_zero_elev.setx(f->get_cos_longitude()*tmp - scenery.center.x());
558     cur_zero_elev.sety(f->get_sin_longitude()*tmp - scenery.center.y());
559     cur_zero_elev.setz(f->get_sin_lat_geocentric()*tmp_radius - scenery.center.z());
560
561     // calculate view position in current FG view coordinate system
562     // p.lon & p.lat are already defined earlier, p.radius was set to
563     // the sea level radius, so now we add in our altitude.
564     if ( f->get_Altitude() * FEET_TO_METER > 
565          (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
566         tmp_radius += f->get_Altitude() * FEET_TO_METER;
567     } else {
568         tmp_radius += scenery.cur_elev + 0.5 * METER_TO_FEET ;
569     }
570     tmp = f->get_cos_lat_geocentric() * tmp_radius;
571     abs_view_pos.setx(f->get_cos_longitude()*tmp);
572     abs_view_pos.sety(f->get_sin_longitude()*tmp);
573     abs_view_pos.setz(f->get_sin_lat_geocentric()*tmp_radius);
574         
575 #endif // FG_VIEW_INLINE_OPTIMIZATIONS
576         
577     view_pos = abs_view_pos - scenery.center;
578
579     FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
580     FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
581     FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = " << view_pos );
582
583     // Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw)
584     // from FG_T_local_to_body[3][3]
585
586     if ( use_larcsim_local_to_body ) {
587
588         // Question: Why is the LaRCsim matrix arranged so differently
589         // than the one we need???
590
591         // Answer (I think): The LaRCsim matrix is generated in a
592         // different reference frame than we've set up for our world
593
594         LOCAL[0][0] = f->get_T_local_to_body_33();
595         LOCAL[0][1] = -f->get_T_local_to_body_32();
596         LOCAL[0][2] = -f->get_T_local_to_body_31();
597         LOCAL[0][3] = 0.0;
598         LOCAL[1][0] = -f->get_T_local_to_body_23();
599         LOCAL[1][1] = f->get_T_local_to_body_22();
600         LOCAL[1][2] = f->get_T_local_to_body_21();
601         LOCAL[1][3] = 0.0;
602         LOCAL[2][0] = -f->get_T_local_to_body_13();
603         LOCAL[2][1] = f->get_T_local_to_body_12();
604         LOCAL[2][2] = f->get_T_local_to_body_11();
605         LOCAL[2][3] = 0.0;
606         LOCAL[3][0] = LOCAL[3][1] = LOCAL[3][2] = LOCAL[3][3] = 0.0;
607         LOCAL[3][3] = 1.0;
608
609         // printf("LaRCsim LOCAL matrix\n");
610         // MAT3print(LOCAL, stdout);
611
612     } else {
613
614         // calculate the transformation matrix to go from LaRCsim to ssg
615         sgVec3 vec1;
616         sgSetVec3( vec1, 0.0, 1.0, 0.0 );
617         sgMat4 mat1;
618         sgMakeRotMat4( mat1, 90, vec1 );
619
620         sgVec3 vec2;
621         sgSetVec3( vec2, 1.0, 0.0, 0.0 );
622         sgMat4 mat2;
623         sgMakeRotMat4( mat2, 90, vec2 );
624
625         sgMultMat4( sgLARC_TO_SSG, mat1, mat2 );
626
627         /*
628         cout << "LaRCsim to SSG:" << endl;
629         MAT3mat print;
630         int i;
631         int j;
632         for ( i = 0; i < 4; i++ ) {
633             for ( j = 0; j < 4; j++ ) {
634                 print[i][j] = sgLARC_TO_SSG[i][j];
635             }
636         }
637         MAT3print( print, stdout);
638         */
639
640         // code to calculate LOCAL matrix calculated from Phi, Theta, and
641         // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
642         // flight model
643
644         MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
645         MAT3rotate(R, vec, f->get_Phi());
646         // cout << "Roll matrix" << endl;
647         // MAT3print(R, stdout);
648
649         sgVec3 sgrollvec;
650         sgSetVec3( sgrollvec, 0.0, 0.0, 1.0 );
651         sgMat4 sgPHI;           // roll
652         sgMakeRotMat4( sgPHI, f->get_Phi() * RAD_TO_DEG, sgrollvec );
653
654
655         MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
656         MAT3rotate(TMP, vec, f->get_Theta());
657         // cout << "Pitch matrix" << endl;;
658         // MAT3print(TMP, stdout);
659         MAT3mult(R, R, TMP);
660         // cout << "tmp rotation matrix, R:" << endl;;
661         // MAT3print(R, stdout);
662
663         sgVec3 sgpitchvec;
664         sgSetVec3( sgpitchvec, 0.0, 1.0, 0.0 );
665         sgMat4 sgTHETA;         // pitch
666         sgMakeRotMat4( sgTHETA, f->get_Theta() * RAD_TO_DEG,
667                        sgpitchvec );
668
669         sgMat4 sgROT;
670         sgMultMat4( sgROT, sgPHI, sgTHETA );
671
672
673         MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
674         MAT3rotate(TMP, vec, -f->get_Psi());
675         // cout << "Yaw matrix" << endl;
676         // MAT3print(TMP, stdout);
677         MAT3mult(LOCAL, R, TMP);
678         // cout << "LOCAL matrix:" << endl;
679         // MAT3print(LOCAL, stdout);
680
681         sgVec3 sgyawvec;
682         sgSetVec3( sgyawvec, 1.0, 0.0, 0.0 );
683         sgMat4 sgPSI;           // pitch
684         sgMakeRotMat4( sgPSI, -f->get_Psi() * RAD_TO_DEG, sgyawvec );
685
686         sgMultMat4( sgLOCAL, sgROT, sgPSI );
687
688         /*
689         MAT3mat print;
690         int i;
691         int j;
692         for ( i = 0; i < 4; i++ ) {
693             for ( j = 0; j < 4; j++ ) {
694                 print[i][j] = sgLOCAL[i][j];
695             }
696         }
697         MAT3print( print, stdout);
698         */
699     } // if ( use_larcsim_local_to_body ) 
700
701 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
702         
703     // Derive the local UP transformation matrix based on *geodetic*
704     // coordinates
705     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
706     MAT3rotate(R, vec, f->get_Longitude());     // R = rotate about Z axis
707     // printf("Longitude matrix\n");
708     // MAT3print(R, stdout);
709
710     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
711     MAT3mult_vec(vec, vec, R);
712     MAT3rotate(TMP, vec, -f->get_Latitude());  // TMP = rotate about X axis
713     // printf("Latitude matrix\n");
714     // MAT3print(TMP, stdout);
715
716     MAT3mult(UP, R, TMP);
717     // cout << "Local up matrix" << endl;;
718     // MAT3print(UP, stdout);
719
720     sgMakeRotMat4( sgUP, 
721                    f->get_Longitude() * RAD_TO_DEG,
722                    0.0,
723                    -f->get_Latitude() * RAD_TO_DEG );
724     /*
725     cout << "FG derived UP matrix using sg routines" << endl;
726     MAT3mat print;
727     int i;
728     int j;
729     for ( i = 0; i < 4; i++ ) {
730         for ( j = 0; j < 4; j++ ) {
731             print[i][j] = sgUP[i][j];
732         }
733     }
734     MAT3print( print, stdout);
735     */
736
737     MAT3_SET_VEC(local_up, 1.0, 0.0, 0.0);
738     MAT3mult_vec(local_up, local_up, UP);
739
740     // printf( "Local Up = (%.4f, %.4f, %.4f)\n",
741     //         local_up[0], local_up[1], local_up[2]);
742     
743     // Alternative method to Derive local up vector based on
744     // *geodetic* coordinates
745     // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
746     // printf( "    Alt Up = (%.4f, %.4f, %.4f)\n", 
747     //         alt_up.x, alt_up.y, alt_up.z);
748
749     // Calculate the VIEW matrix
750     MAT3mult(VIEW, LOCAL, UP);
751     // cout << "VIEW matrix" << endl;;
752     // MAT3print(VIEW, stdout);
753
754     sgMat4 sgTMP;
755     sgMultMat4( sgTMP, sgLOCAL, sgUP );
756     sgMultMat4( sgVIEW_ROT, sgLARC_TO_SSG, sgTMP );
757
758     sgMakeTransMat4( sgTRANS, view_pos.x(), view_pos.y(), view_pos.z() );
759
760     sgMultMat4( sgVIEW, sgVIEW_ROT, sgTRANS );
761
762     FGMat4Wrapper tmp;
763     sgCopyMat4( tmp.m, sgVIEW );
764     follow.push_back( tmp );
765
766     /*
767     cout << "FG derived VIEW matrix using sg routines" << endl;
768     MAT3mat print;
769     int i;
770     int j;
771     for ( i = 0; i < 4; i++ ) {
772         for ( j = 0; j < 4; j++ ) {
773             print[i][j] = sgVIEW[i][j];
774         }
775     }
776     MAT3print( print, stdout);
777     */
778
779
780     // generate the current up, forward, and fwrd-view vectors
781     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
782     MAT3mult_vec(view_up, vec, VIEW);
783
784     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
785     MAT3mult_vec(forward, vec, VIEW);
786     // printf( "Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1], 
787     //         forward[2]);
788
789     MAT3rotate(TMP, view_up, view_offset);
790     MAT3mult_vec(view_forward, forward, TMP);
791
792     // make a vector to the current view position
793     MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
794
795     // Given a vector pointing straight down (-Z), map into onto the
796     // local plane representing "horizontal".  This should give us the
797     // local direction for moving "south".
798     MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
799     map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
800     MAT3_NORMALIZE_VEC(surface_south, ntmp);
801     // printf( "Surface direction directly south %.2f %.2f %.2f\n",
802     //         surface_south[0], surface_south[1], surface_south[2]);
803
804     // now calculate the surface east vector
805     MAT3rotate(TMP, view_up, FG_PI_2);
806     MAT3mult_vec(surface_east, surface_south, TMP);
807     // printf( "Surface direction directly east %.2f %.2f %.2f\n",
808     //         surface_east[0], surface_east[1], surface_east[2]);
809     // printf( "Should be close to zero = %.2f\n", 
810     //         MAT3_DOT_PRODUCT(surface_south, surface_east));
811         
812 #else // FG_VIEW_INLINE_OPTIMIZATIONS
813          
814     //  // Build spherical to cartesian transform matrix directly
815     double cos_lat = f->get_cos_latitude(); // cos(-f->get_Latitude());
816     double sin_lat = -f->get_sin_latitude(); // sin(-f->get_Latitude());
817     double cos_lon = f->get_cos_longitude(); //cos(f->get_Longitude());
818     double sin_lon = f->get_sin_longitude(); //sin(f->get_Longitude());
819
820     double *mat = (double *)UP;
821         
822     mat[0] =  cos_lat*cos_lon;
823     mat[1] =  cos_lat*sin_lon;
824     mat[2] = -sin_lat;
825     mat[3] =  0.0;
826     mat[4] =  -sin_lon;
827     mat[5] =  cos_lon;
828     mat[6] =  0.0;
829     mat[7] =  0.0;
830     mat[8]  =  sin_lat*cos_lon;
831     mat[9]  =  sin_lat*sin_lon;
832     mat[10] =  cos_lat;
833     mat[11] =  mat[12] = mat[13] = mat[14] = 0.0;
834     mat[15] =  1.0;
835
836     MAT3mult(VIEW, LOCAL, UP);
837         
838     // THESE COULD JUST BE POINTERS !!!
839     MAT3_SET_VEC(local_up, mat[0],     mat[1],     mat[2]);
840     MAT3_SET_VEC(view_up,  VIEW[0][0], VIEW[0][1], VIEW[0][2]);
841     MAT3_SET_VEC(forward,  VIEW[2][0], VIEW[2][1], VIEW[2][2]);
842
843     getRotMatrix((double *)TMP, view_up, view_offset);
844     MAT3mult_vec(view_forward, forward, TMP);
845
846     // make a vector to the current view position
847     MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
848
849     // Given a vector pointing straight down (-Z), map into onto the
850     // local plane representing "horizontal".  This should give us the
851     // local direction for moving "south".
852     MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
853     map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
854
855     MAT3_NORMALIZE_VEC(surface_south, ntmp);
856     // printf( "Surface direction directly south %.6f %.6f %.6f\n",
857     //         surface_south[0], surface_south[1], surface_south[2]);
858
859     // now calculate the surface east vector
860     getRotMatrix((double *)TMP, view_up, FG_PI_2);
861     MAT3mult_vec(surface_east, surface_south, TMP);
862     // printf( "Surface direction directly east %.6f %.6f %.6f\n",
863     //         surface_east[0], surface_east[1], surface_east[2]);
864     // printf( "Should be close to zero = %.6f\n", 
865     //         MAT3_DOT_PRODUCT(surface_south, surface_east));
866 #endif // !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
867 }
868
869
870 // Update the "World to Eye" transformation matrix
871 // This is most useful for view frustum culling
872 void FGView::UpdateWorldToEye( FGInterface *f ) {
873     MAT3mat R_Phi, R_Theta, R_Psi, R_Lat, R_Lon, T_view;
874     MAT3mat TMP;
875     MAT3hvec vec;
876
877     if ( use_larcsim_local_to_body ) {
878
879         // Question: hey this is even different then LOCAL[][] above??
880         // Answer: yet another coordinate system, this time the
881         // coordinate system in which we do our view frustum culling.
882
883         AIRCRAFT[0][0] = -f->get_T_local_to_body_22();
884         AIRCRAFT[0][1] = -f->get_T_local_to_body_23();
885         AIRCRAFT[0][2] = f->get_T_local_to_body_21();
886         AIRCRAFT[0][3] = 0.0;
887         AIRCRAFT[1][0] = f->get_T_local_to_body_32();
888         AIRCRAFT[1][1] = f->get_T_local_to_body_33();
889         AIRCRAFT[1][2] = -f->get_T_local_to_body_31();
890         AIRCRAFT[1][3] = 0.0;
891         AIRCRAFT[2][0] = f->get_T_local_to_body_12();
892         AIRCRAFT[2][1] = f->get_T_local_to_body_13();
893         AIRCRAFT[2][2] = -f->get_T_local_to_body_11();
894         AIRCRAFT[2][3] = 0.0;
895         AIRCRAFT[3][0] = AIRCRAFT[3][1] = AIRCRAFT[3][2] = AIRCRAFT[3][3] = 0.0;
896         AIRCRAFT[3][3] = 1.0;
897
898     } else {
899
900         // Roll Matrix
901         MAT3_SET_HVEC(vec, 0.0, 0.0, -1.0, 1.0);
902         MAT3rotate(R_Phi, vec, f->get_Phi());
903         // printf("Roll matrix (Phi)\n");
904         // MAT3print(R_Phi, stdout);
905
906         // Pitch Matrix
907         MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
908         MAT3rotate(R_Theta, vec, f->get_Theta());
909         // printf("\nPitch matrix (Theta)\n");
910         // MAT3print(R_Theta, stdout);
911
912         // Yaw Matrix
913         MAT3_SET_HVEC(vec, 0.0, -1.0, 0.0, 1.0);
914         MAT3rotate(R_Psi, vec, f->get_Psi() + FG_PI /* - view_offset */ );
915         // MAT3rotate(R_Psi, vec, f->get_Psi() + FG_PI - view_offset );
916         // printf("\nYaw matrix (Psi)\n");
917         // MAT3print(R_Psi, stdout);
918
919         // aircraft roll/pitch/yaw
920         MAT3mult(TMP, R_Phi, R_Theta);
921         MAT3mult(AIRCRAFT, TMP, R_Psi);
922
923     } // if ( use_larcsim_local_to_body )
924
925 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
926         
927     // printf("AIRCRAFT matrix\n");
928     // MAT3print(AIRCRAFT, stdout);
929
930     // View rotation matrix relative to current aircraft orientation
931     MAT3_SET_HVEC(vec, 0.0, -1.0, 0.0, 1.0);
932     MAT3mult_vec(vec, vec, AIRCRAFT);
933     // printf("aircraft up vector = %.2f %.2f %.2f\n", 
934     //        vec[0], vec[1], vec[2]);
935     MAT3rotate(TMP, vec, -view_offset );
936     MAT3mult(VIEW_OFFSET, AIRCRAFT, TMP);
937     // printf("VIEW_OFFSET matrix\n");
938     // MAT3print(VIEW_OFFSET, stdout);
939
940     // View position in scenery centered coordinates
941     MAT3_SET_HVEC(vec, view_pos.x(), view_pos.y(), view_pos.z(), 1.0);
942     MAT3translate(T_view, vec);
943     // printf("\nTranslation matrix\n");
944     // MAT3print(T_view, stdout);
945
946     // Latitude
947     MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
948     // R_Lat = rotate about X axis
949     MAT3rotate(R_Lat, vec, f->get_Latitude());
950     // printf("\nLatitude matrix\n");
951     // MAT3print(R_Lat, stdout);
952
953     // Longitude
954     MAT3_SET_HVEC(vec, 0.0, 0.0, 1.0, 1.0);
955     // R_Lon = rotate about Z axis
956     MAT3rotate(R_Lon, vec, f->get_Longitude() - FG_PI_2 );
957     // printf("\nLongitude matrix\n");
958     // MAT3print(R_Lon, stdout);
959
960     // lon/lat
961     MAT3mult(WORLD, R_Lat, R_Lon);
962     // printf("\nworld\n");
963     // MAT3print(WORLD, stdout);
964
965     MAT3mult(EYE_TO_WORLD, VIEW_OFFSET, WORLD);
966     MAT3mult(EYE_TO_WORLD, EYE_TO_WORLD, T_view);
967     // printf("\nEye to world\n");
968     // MAT3print(EYE_TO_WORLD, stdout);
969
970     MAT3invert(WORLD_TO_EYE, EYE_TO_WORLD);
971     // printf("\nWorld to eye\n");
972     // MAT3print(WORLD_TO_EYE, stdout);
973
974     // printf( "\nview_pos = %.2f %.2f %.2f\n", 
975     //         view_pos.x, view_pos.y, view_pos.z );
976
977     // MAT3_SET_HVEC(eye, 0.0, 0.0, 0.0, 1.0);
978     // MAT3mult_vec(vec, eye, EYE_TO_WORLD);
979     // printf("\neye -> world = %.2f %.2f %.2f\n", vec[0], vec[1], vec[2]);
980
981     // MAT3_SET_HVEC(vec1, view_pos.x, view_pos.y, view_pos.z, 1.0);
982     // MAT3mult_vec(vec, vec1, WORLD_TO_EYE);
983     // printf( "\nabs_view_pos -> eye = %.2f %.2f %.2f\n", 
984     //         vec[0], vec[1], vec[2]);
985 #else  // FG_VIEW_INLINE_OPTIMIZATIONS
986         
987     MAT3_SET_HVEC(vec, -AIRCRAFT[1][0], -AIRCRAFT[1][1], -AIRCRAFT[1][2], -AIRCRAFT[1][3]);
988     getRotMatrix((double *)TMP, vec, -view_offset );
989     MAT3mult(VIEW_OFFSET, AIRCRAFT, TMP);
990     // MAT3print_formatted(VIEW_OFFSET, stdout, "VIEW_OFFSET matrix:\n",
991     //                                   NULL, "%#8.6f  ", "\n");
992
993     // Build spherical to cartesian transform matrix directly
994     double *mat = (double *)WORLD; //T_view; //WORLD;
995     double cos_lat = f->get_cos_latitude(); //cos(f->get_Latitude());
996     double sin_lat = f->get_sin_latitude(); //sin(f->get_Latitude());
997     // using trig identities  this:
998     //  mat[0]  =  cos(f->get_Longitude() - FG_PI_2);//cos_lon;
999     //  mat[1]  =  sin(f->get_Longitude() - FG_PI_2);//sin_lon;
1000     // becomes this: :-)
1001     mat[0]  =  f->get_sin_longitude(); //cos_lon;
1002     mat[1]  = -f->get_cos_longitude(); //sin_lon;
1003     mat[4]  = -cos_lat*mat[1]; //mat[1]=sin_lon;
1004     mat[5]  =  cos_lat*mat[0]; //mat[0]=cos_lon;
1005     mat[6]  =  sin_lat;
1006     mat[8]  =  sin_lat*mat[1]; //mat[1]=sin_lon;
1007     mat[9]  = -sin_lat*mat[0]; //mat[0]=cos_lon;
1008     mat[10] =  cos_lat;
1009
1010     // BUILD EYE_TO_WORLD = AIRCRAFT * WORLD
1011     // and WORLD_TO_EYE = Inverse( EYE_TO_WORLD) concurrently
1012     // by Transposing the 3x3 rotation sub-matrix
1013     WORLD_TO_EYE[0][0] = EYE_TO_WORLD[0][0] =
1014         VIEW_OFFSET[0][0]*mat[0] + VIEW_OFFSET[0][1]*mat[4] + VIEW_OFFSET[0][2]*mat[8];
1015         
1016     WORLD_TO_EYE[1][0] = EYE_TO_WORLD[0][1] =
1017         VIEW_OFFSET[0][0]*mat[1] + VIEW_OFFSET[0][1]*mat[5] + VIEW_OFFSET[0][2]*mat[9];
1018         
1019     WORLD_TO_EYE[2][0] = EYE_TO_WORLD[0][2] =
1020         VIEW_OFFSET[0][1]*mat[6] + VIEW_OFFSET[0][2]*mat[10];
1021         
1022     WORLD_TO_EYE[0][1] = EYE_TO_WORLD[1][0] =
1023         VIEW_OFFSET[1][0]*mat[0] + VIEW_OFFSET[1][1]*mat[4] + VIEW_OFFSET[1][2]*mat[8];
1024         
1025     WORLD_TO_EYE[1][1] = EYE_TO_WORLD[1][1] =
1026         VIEW_OFFSET[1][0]*mat[1] + VIEW_OFFSET[1][1]*mat[5] + VIEW_OFFSET[1][2]*mat[9];
1027         
1028     WORLD_TO_EYE[2][1] = EYE_TO_WORLD[1][2] =
1029         VIEW_OFFSET[1][1]*mat[6] + VIEW_OFFSET[1][2]*mat[10];
1030         
1031     WORLD_TO_EYE[0][2] = EYE_TO_WORLD[2][0] =
1032         VIEW_OFFSET[2][0]*mat[0] + VIEW_OFFSET[2][1]*mat[4] + VIEW_OFFSET[2][2]*mat[8];
1033         
1034     WORLD_TO_EYE[1][2] = EYE_TO_WORLD[2][1] =
1035         VIEW_OFFSET[2][0]*mat[1] + VIEW_OFFSET[2][1]*mat[5] + VIEW_OFFSET[2][2]*mat[9];
1036         
1037     WORLD_TO_EYE[2][2] = EYE_TO_WORLD[2][2] =
1038         VIEW_OFFSET[2][1]*mat[6] + VIEW_OFFSET[2][2]*mat[10];
1039         
1040     // TRANSLATE TO VIEW POSITION
1041     EYE_TO_WORLD[3][0] = view_pos.x();
1042     EYE_TO_WORLD[3][1] = view_pos.y();
1043     EYE_TO_WORLD[3][2] = view_pos.z();
1044         
1045     // FILL 0 ENTRIES
1046     WORLD_TO_EYE[0][3] = WORLD_TO_EYE[1][3] = WORLD_TO_EYE[2][3] = 
1047         EYE_TO_WORLD[0][3] = EYE_TO_WORLD[1][3] = EYE_TO_WORLD[2][3] = 0.0;
1048
1049     // FILL UNITY ENTRIES
1050     WORLD_TO_EYE[3][3] = EYE_TO_WORLD[3][3] = 1.0;
1051         
1052     /* MAKE THE INVERTED TRANSLATIONS */
1053     mat = (double *)EYE_TO_WORLD;
1054     WORLD_TO_EYE[3][0] = -mat[12]*mat[0]
1055         -mat[13]*mat[1]
1056         -mat[14]*mat[2];
1057         
1058     WORLD_TO_EYE[3][1] = -mat[12]*mat[4]
1059         -mat[13]*mat[5]
1060         -mat[14]*mat[6];
1061         
1062     WORLD_TO_EYE[3][2] = -mat[12]*mat[8]
1063         -mat[13]*mat[9]
1064         -mat[14]*mat[10];
1065         
1066     // MAT3print_formatted(EYE_TO_WORLD, stdout, "EYE_TO_WORLD matrix:\n",
1067     //                                   NULL, "%#8.6f  ", "\n");
1068
1069     // MAT3print_formatted(WORLD_TO_EYE, stdout, "WORLD_TO_EYE matrix:\n",
1070     //                                   NULL, "%#8.6f  ", "\n");
1071
1072 #endif // defined(FG_VIEW_INLINE_OPTIMIZATIONS)
1073 }
1074
1075
1076 #if 0
1077 // Reject non viewable spheres from current View Frustrum by Curt
1078 // Olson curt@me.umn.edu and Norman Vine nhv@yahoo.com with 'gentle
1079 // guidance' from Steve Baker sbaker@link.com
1080 int
1081 FGView::SphereClip( const Point3D& cp, const double radius )
1082 {
1083     double x1, y1;
1084
1085     MAT3vec eye;        
1086     double *mat;
1087     double x, y, z;
1088
1089     x = cp->x;
1090     y = cp->y;
1091     z = cp->z;
1092         
1093     mat = (double *)(WORLD_TO_EYE);
1094         
1095     eye[2] =  x*mat[2] + y*mat[6] + z*mat[10] + mat[14];
1096         
1097     // Check near and far clip plane
1098     if( ( eye[2] > radius ) ||
1099         ( eye[2] + radius + current_weather.visibility < 0) )
1100         // ( eye[2] + radius + far_plane < 0) )
1101     {
1102         return 1;
1103     }
1104         
1105     // check right and left clip plane (from eye perspective)
1106     x1 = radius * fov_x_clip;
1107     eye[0] = (x*mat[0] + y*mat[4] + z*mat[8] + mat[12]) * slope_x;
1108     if( (eye[2] > -(eye[0]+x1)) || (eye[2] > (eye[0]-x1)) ) {
1109         return(1);
1110     }
1111         
1112     // check bottom and top clip plane (from eye perspective)
1113     y1 = radius * fov_y_clip;
1114     eye[1] = (x*mat[1] + y*mat[5] + z*mat[9] + mat[13]) * slope_y; 
1115     if( (eye[2] > -(eye[1]+y1)) || (eye[2] > (eye[1]-y1)) ) {
1116         return 1;
1117     }
1118
1119     return 0;
1120 }
1121 #endif
1122
1123
1124 // Destructor
1125 FGView::~FGView( void ) {
1126 }