]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.cxx
b5a90a470a51c85952ef2a688acf3d77fc70006f
[flightgear.git] / src / Main / viewer.cxx
1 // viewer.cxx -- class for managing a viewer in the flightgear world.
2 //
3 // Written by Curtis Olson, started August 1997.
4 //                          overhaul started October 2000.
5 //   partially rewritten by Jim Wilson jim@kelcomaine.com using interface
6 //                          by David Megginson March 2002
7 //
8 // Copyright (C) 1997 - 2000  Curtis L. Olson  - curt@flightgear.org
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #include <simgear/compiler.h>
28
29 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/constants.h>
35 #include <simgear/math/point3d.hxx>
36 #include <simgear/math/polar3d.hxx>
37 #include <simgear/math/sg_geodesy.hxx>
38
39 #include <Scenery/scenery.hxx>
40
41 /* from lookat */
42 #include <simgear/math/vector.hxx>
43 #include "globals.hxx"
44 /* end from lookat */
45
46 #include "viewer.hxx"
47
48 \f
49 //////////////////////////////////////////////////////////////////
50 // Norman's Optimized matrix rotators!                          //
51 //////////////////////////////////////////////////////////////////
52
53 static void fgMakeLOCAL( sgMat4 dst, const double Theta,
54                                 const double Phi, const double Psi)
55 {
56     SGfloat cosTheta = (SGfloat) cos(Theta);
57     SGfloat sinTheta = (SGfloat) sin(Theta);
58     SGfloat cosPhi   = (SGfloat) cos(Phi);
59     SGfloat sinPhi   = (SGfloat) sin(Phi);
60     SGfloat sinPsi   = (SGfloat) sin(Psi) ;
61     SGfloat cosPsi   = (SGfloat) cos(Psi) ;
62         
63     dst[0][0] = cosPhi * cosTheta;
64     dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
65     dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
66     dst[0][3] = SG_ZERO;
67
68     dst[1][0] = -sinPhi * cosTheta;
69     dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
70     dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
71     dst[1][3] = SG_ZERO ;
72         
73     dst[2][0] = sinTheta;
74     dst[2][1] = cosTheta * -sinPsi;
75     dst[2][2] = cosTheta * cosPsi;
76     dst[2][3] = SG_ZERO;
77         
78     dst[3][0] = SG_ZERO;
79     dst[3][1] = SG_ZERO;
80     dst[3][2] = SG_ZERO;
81     dst[3][3] = SG_ONE ;
82 }
83
84
85 // Since these are pure rotation matrices we can save some bookwork
86 // by considering them to be 3x3 until the very end -- NHV
87 static void MakeVIEW_OFFSET( sgMat4 dst,
88                       const float angle1, const sgVec3 axis1,
89                       const float angle2, const sgVec3 axis2 )
90 {
91     // make rotmatrix1 from angle and axis
92     float s = (float) sin ( angle1 ) ;
93     float c = (float) cos ( angle1 ) ;
94     float t = SG_ONE - c ;
95
96     sgMat3 mat1;
97     float tmp = t * axis1[0];
98     mat1[0][0] = tmp * axis1[0] + c ;
99     mat1[0][1] = tmp * axis1[1] + s * axis1[2] ;
100     mat1[0][2] = tmp * axis1[2] - s * axis1[1] ;
101
102     tmp = t * axis1[1];
103     mat1[1][0] = tmp * axis1[0] - s * axis1[2] ;
104     mat1[1][1] = tmp * axis1[1] + c ;
105     mat1[1][2] = tmp * axis1[2] + s * axis1[0] ;
106
107     tmp = t * axis1[2];
108     mat1[2][0] = tmp * axis1[0] + s * axis1[1] ;
109     mat1[2][1] = tmp * axis1[1] - s * axis1[0] ;
110     mat1[2][2] = tmp * axis1[2] + c ;
111
112     // make rotmatrix2 from angle and axis
113     s = (float) sin ( angle2 ) ;
114     c = (float) cos ( angle2 ) ;
115     t = SG_ONE - c ;
116
117     sgMat3 mat2;
118     tmp = t * axis2[0];
119     mat2[0][0] = tmp * axis2[0] + c ;
120     mat2[0][1] = tmp * axis2[1] + s * axis2[2] ;
121     mat2[0][2] = tmp * axis2[2] - s * axis2[1] ;
122
123     tmp = t * axis2[1];
124     mat2[1][0] = tmp * axis2[0] - s * axis2[2] ;
125     mat2[1][1] = tmp * axis2[1] + c ;
126     mat2[1][2] = tmp * axis2[2] + s * axis2[0] ;
127
128     tmp = t * axis2[2];
129     mat2[2][0] = tmp * axis2[0] + s * axis2[1] ;
130     mat2[2][1] = tmp * axis2[1] - s * axis2[0] ;
131     mat2[2][2] = tmp * axis2[2] + c ;
132
133     // multiply matrices
134     for ( int j = 0 ; j < 3 ; j++ ) {
135         dst[0][j] = mat2[0][0] * mat1[0][j] +
136                     mat2[0][1] * mat1[1][j] +
137                     mat2[0][2] * mat1[2][j];
138
139         dst[1][j] = mat2[1][0] * mat1[0][j] +
140                     mat2[1][1] * mat1[1][j] +
141                     mat2[1][2] * mat1[2][j];
142
143         dst[2][j] = mat2[2][0] * mat1[0][j] +
144                     mat2[2][1] * mat1[1][j] +
145                     mat2[2][2] * mat1[2][j];
146     }
147     // fill in 4x4 matrix elements
148     dst[0][3] = SG_ZERO; 
149     dst[1][3] = SG_ZERO; 
150     dst[2][3] = SG_ZERO;
151     dst[3][0] = SG_ZERO;
152     dst[3][1] = SG_ZERO;
153     dst[3][2] = SG_ZERO;
154     dst[3][3] = SG_ONE;
155 }
156
157 // Taking advantage of the 3x3 nature of this -- NHV
158 inline static void MakeWithWorldUp( sgMat4 dst, const sgMat4 UP, const sgMat4 LOCAL )
159 {
160     sgMat4 tmp;
161
162     float a = UP[0][0];
163     float b = UP[1][0];
164     float c = UP[2][0];
165     tmp[0][0] = a*LOCAL[0][0] + b*LOCAL[0][1] + c*LOCAL[0][2] ;
166     tmp[1][0] = a*LOCAL[1][0] + b*LOCAL[1][1] + c*LOCAL[1][2] ;
167     tmp[2][0] = a*LOCAL[2][0] + b*LOCAL[2][1] + c*LOCAL[2][2] ;
168     tmp[3][0] = SG_ZERO ;
169
170     a = UP[0][1];
171     b = UP[1][1];
172     c = UP[2][1];
173     tmp[0][1] = a*LOCAL[0][0] + b*LOCAL[0][1] + c*LOCAL[0][2] ;
174     tmp[1][1] = a*LOCAL[1][0] + b*LOCAL[1][1] + c*LOCAL[1][2] ;
175     tmp[2][1] = a*LOCAL[2][0] + b*LOCAL[2][1] + c*LOCAL[2][2] ;
176     tmp[3][1] = SG_ZERO ;
177
178     a = UP[0][2];
179     c = UP[2][2];
180     tmp[0][2] = a*LOCAL[0][0] + c*LOCAL[0][2] ;
181     tmp[1][2] = a*LOCAL[1][0] + c*LOCAL[1][2] ;
182     tmp[2][2] = a*LOCAL[2][0] + c*LOCAL[2][2] ;
183     tmp[3][2] = SG_ZERO ;
184
185     tmp[0][3] = SG_ZERO ;
186     tmp[1][3] = SG_ZERO ;
187     tmp[2][3] = SG_ZERO ;
188     tmp[3][3] = SG_ONE ;
189     sgCopyMat4(dst, tmp);
190 }
191
192
193 ////////////////////////////////////////////////////////////////////////
194 // Implementation of FGViewer.
195 ////////////////////////////////////////////////////////////////////////
196
197 // Constructor
198 FGViewer::FGViewer( void ):
199     _scaling_type(FG_SCALING_MAX),
200     _fov_deg(55.0),
201     _dirty(true),
202     _lon_deg(0),
203     _lat_deg(0),
204     _alt_ft(0),
205     _target_lon_deg(0),
206     _target_lat_deg(0),
207     _target_alt_ft(0),
208     _roll_deg(0),
209     _pitch_deg(0),
210     _heading_deg(0),
211     _x_offset_m(0),
212     _y_offset_m(0),
213     _z_offset_m(0),
214     _heading_offset_deg(0),
215     _pitch_offset_deg(0),
216     _roll_offset_deg(0),
217     _goal_heading_offset_deg(0.0),
218     _goal_pitch_offset_deg(0.0)
219 {
220     sgdZeroVec3(_absolute_view_pos);
221     //a reasonable guess for init, so that the math doesn't blow up
222 }
223
224
225 // Destructor
226 FGViewer::~FGViewer( void ) {
227 }
228
229 void
230 FGViewer::init ()
231 {
232 }
233
234 void
235 FGViewer::bind ()
236 {
237 }
238
239 void
240 FGViewer::unbind ()
241 {
242 }
243
244 void
245 FGViewer::setType ( int type )
246 {
247   if (type == 0)
248     _type = FG_RPH;
249   if (type == 1)
250     _type = FG_LOOKAT;
251 }
252
253 void
254 FGViewer::setLongitude_deg (double lon_deg)
255 {
256   _dirty = true;
257   _lon_deg = lon_deg;
258 }
259
260 void
261 FGViewer::setLatitude_deg (double lat_deg)
262 {
263   _dirty = true;
264   _lat_deg = lat_deg;
265 }
266
267 void
268 FGViewer::setAltitude_ft (double alt_ft)
269 {
270   _dirty = true;
271   _alt_ft = alt_ft;
272 }
273
274 void
275 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
276 {
277   _dirty = true;
278   _lon_deg = lon_deg;
279   _lat_deg = lat_deg;
280   _alt_ft = alt_ft;
281 }
282
283 void
284 FGViewer::setTargetLongitude_deg (double lon_deg)
285 {
286   _dirty = true;
287   _target_lon_deg = lon_deg;
288 }
289
290 void
291 FGViewer::setTargetLatitude_deg (double lat_deg)
292 {
293   _dirty = true;
294   _target_lat_deg = lat_deg;
295 }
296
297 void
298 FGViewer::setTargetAltitude_ft (double alt_ft)
299 {
300   _dirty = true;
301   _target_alt_ft = alt_ft;
302 }
303
304 void
305 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
306 {
307   _dirty = true;
308   _target_lon_deg = lon_deg;
309   _target_lat_deg = lat_deg;
310   _target_alt_ft = alt_ft;
311 }
312
313 void
314 FGViewer::setRoll_deg (double roll_deg)
315 {
316   _dirty = true;
317   _roll_deg = roll_deg;
318 }
319
320 void
321 FGViewer::setPitch_deg (double pitch_deg)
322 {
323   _dirty = true;
324   _pitch_deg = pitch_deg;
325 }
326
327 void
328 FGViewer::setHeading_deg (double heading_deg)
329 {
330   _dirty = true;
331   _heading_deg = heading_deg;
332 }
333
334 void
335 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
336 {
337   _dirty = true;
338   _roll_deg = roll_deg;
339   _pitch_deg = pitch_deg;
340   _heading_deg = heading_deg;
341 }
342
343 void
344 FGViewer::setXOffset_m (double x_offset_m)
345 {
346   _dirty = true;
347   _x_offset_m = x_offset_m;
348 }
349
350 void
351 FGViewer::setYOffset_m (double y_offset_m)
352 {
353   _dirty = true;
354   _y_offset_m = y_offset_m;
355 }
356
357 void
358 FGViewer::setZOffset_m (double z_offset_m)
359 {
360   _dirty = true;
361   _z_offset_m = z_offset_m;
362 }
363
364 void
365 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
366 {
367   _dirty = true;
368   _x_offset_m = x_offset_m;
369   _y_offset_m = y_offset_m;
370   _z_offset_m = z_offset_m;
371 }
372
373 void
374 FGViewer::setRollOffset_deg (double roll_offset_deg)
375 {
376   _dirty = true;
377   _roll_offset_deg = roll_offset_deg;
378 }
379
380 void
381 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
382 {
383   _dirty = true;
384   _pitch_offset_deg = pitch_offset_deg;
385 }
386
387 void
388 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
389 {
390   _dirty = true;
391   _heading_offset_deg = heading_offset_deg;
392 }
393
394 void
395 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
396 {
397   _dirty = true;
398   _goal_roll_offset_deg = goal_roll_offset_deg;
399 }
400
401 void
402 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
403 {
404   _dirty = true;
405   _goal_pitch_offset_deg = goal_pitch_offset_deg;
406   if ( _goal_pitch_offset_deg < -90 ) {
407     _goal_pitch_offset_deg = -90.0;
408   }
409   if ( _goal_pitch_offset_deg > 90.0 ) {
410     _goal_pitch_offset_deg = 90.0;
411   }
412
413 }
414
415 void
416 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
417 {
418   _dirty = true;
419   _goal_heading_offset_deg = goal_heading_offset_deg;
420   while ( _goal_heading_offset_deg < 0.0 ) {
421     _goal_heading_offset_deg += 360;
422   }
423   while ( _goal_heading_offset_deg > 360 ) {
424     _goal_heading_offset_deg -= 360;
425   }
426 }
427
428 void
429 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
430 {
431   _dirty = true;
432   _roll_offset_deg = roll_offset_deg;
433   _pitch_offset_deg = pitch_offset_deg;
434   _heading_offset_deg = heading_offset_deg;
435 }
436
437 double *
438 FGViewer::get_absolute_view_pos () 
439 {
440   if (_dirty)
441     recalc();
442   return _absolute_view_pos;
443 }
444
445 float *
446 FGViewer::getRelativeViewPos () 
447 {
448   if (_dirty)
449     recalc();
450   return _relative_view_pos;
451 }
452
453 float *
454 FGViewer::getZeroElevViewPos () 
455 {
456   if (_dirty)
457     recalc();
458   return _zero_elev_view_pos;
459 }
460
461
462 // recalc() is done every time one of the setters is called (making the 
463 // cached data "dirty") on the next "get".  It calculates all the outputs 
464 // for viewer.
465 void
466 FGViewer::recalc ()
467 {
468   sgVec3 minus_z, right, forward, tilt;
469   sgMat4 tmpROT;  // temp rotation work matrices
470   sgMat4 VIEW_HEADINGOFFSET, VIEW_PITCHOFFSET;
471   sgVec3 tmpVec3;  // temp work vector (3)
472
473
474   // The position vectors originate from the view point or target location
475   // depending on the type of view.
476   // FIXME: In particular this routine will need to support both locations
477   //        and chase view (aka lookat) is only unique in that the 
478   //        eye position is calculated in relation to the object's position.
479   // FIXME: Later note: actually the object (target) info needs to be held
480   //        by the model class.
481   if (_type == FG_RPH) {
482     // position is the location of the pilot
483     recalcPositionVectors( _lon_deg, _lat_deg, _alt_ft );
484     // Make the world up rotation matrix for rph
485     sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
486   } else {
487     // position is the location of the object being looked at
488     recalcPositionVectors( _target_lon_deg, _target_lat_deg, _target_alt_ft );
489     // Make the world up rotation matrix for lookat
490     sgMakeRotMat4( UP, _target_lon_deg, 0.0, -_target_lat_deg );
491   }
492   // the coordinates generated by the above "recalcPositionVectors"
493   sgCopyVec3(_zero_elev, _zero_elev_view_pos);
494   sgCopyVec3(_view_pos, _relative_view_pos);
495
496
497
498   // get the world up radial vector from planet center
499   // (ie. effect of aircraft location on earth "sphere" approximation)
500   sgSetVec3( _world_up, UP[0][0], UP[0][1], UP[0][2] );
501
502
503
504   // Creat local matrix with current geodetic position.  Converting
505   // the orientation (pitch/roll/heading) to vectors.
506   fgMakeLOCAL( LOCAL, _pitch_deg * SG_DEGREES_TO_RADIANS,
507                       _roll_deg * SG_DEGREES_TO_RADIANS,
508                       -_heading_deg * SG_DEGREES_TO_RADIANS);
509   // Adjust LOCAL to current world_up vector (adjustment for planet location)
510   MakeWithWorldUp( LOCAL, UP, LOCAL );
511   // copy the LOCAL matrix to COCKPIT_ROT for publication...
512   sgCopyMat4( LOCAL_ROT, LOCAL );
513
514   // make sg vectors view up, right and forward vectors from LOCAL
515   sgSetVec3( _view_up, LOCAL[0][0], LOCAL[0][1], LOCAL[0][2] );
516   sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
517   sgSetVec3( forward, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
518
519
520
521   // create xyz offsets Vector
522   sgVec3 position_offset;
523   sgSetVec3( position_offset, _y_offset_m, _x_offset_m, _z_offset_m );
524
525
526
527   // Eye rotations.  
528   // Looking up/down left/right in pilot view (lookfrom mode)
529   // or Floating Rotatation around the object in chase view (lookat mode).
530   // Generate the offset matrix to be applied using offset angles:
531   if (_type == FG_LOOKAT) {
532     // Note that when in "chase view" the offset is in relation to the 
533     // orientation heading (_heading_deg) of the model being looked at as 
534     // it is used to rotate around the model.
535     MakeVIEW_OFFSET( VIEW_OFFSET,
536       (_heading_offset_deg - _heading_deg) * SG_DEGREES_TO_RADIANS, _world_up,
537       _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
538   }
539   if (_type == FG_RPH) {
540     // generate the view offset matrix using orientation offsets
541     MakeVIEW_OFFSET( VIEW_OFFSET,
542       _heading_offset_deg  * SG_DEGREES_TO_RADIANS, _view_up,
543       _pitch_offset_deg  * SG_DEGREES_TO_RADIANS, right );
544   }
545
546
547
548   if (_type == FG_LOOKAT) {
549    
550     // transfrom "offset" and "orientation offset" to vector
551     sgXformVec3( position_offset, position_offset, UP );
552     sgXformVec3( position_offset, position_offset, VIEW_OFFSET );
553
554     sgVec3 object_pos, eye_pos;
555     // copy to coordinates to object...
556     sgCopyVec3( object_pos, _view_pos );
557
558     // add the offsets from object to the coordinates to get "eye"
559     sgAddVec3( eye_pos, _view_pos, position_offset );
560
561     // Make the VIEW matrix for "lookat".
562     sgMakeLookAtMat4( VIEW, eye_pos, object_pos, _view_up );
563   }
564
565   if (_type == FG_RPH) {
566
567     sgXformVec3( position_offset, position_offset, LOCAL);
568     // add the offsets including rotations to the coordinates
569     sgAddVec3( _view_pos, position_offset );
570
571     // Make the VIEW matrix.
572     VIEW[0][0] = right[0];
573     VIEW[0][1] = right[1];
574     VIEW[0][2] = right[2];
575     VIEW[1][0] = forward[0];
576     VIEW[1][1] = forward[1];
577     VIEW[1][2] = forward[2];
578     VIEW[2][0] = _view_up[0];
579     VIEW[2][1] = _view_up[1];
580     VIEW[2][2] = _view_up[2];
581     // multiply the OFFSETS (for heading and pitch) into the VIEW
582     sgPostMultMat4(VIEW, VIEW_OFFSET);
583
584     // add the position data to the matrix
585     VIEW[3][0] = _view_pos[0];
586     VIEW[3][1] = _view_pos[1];
587     VIEW[3][2] = _view_pos[2];
588     VIEW[3][3] = 1.0f;
589
590   }
591
592   // the VIEW matrix includes both rotation and translation.  Let's
593   // knock out the translation part to make the VIEW_ROT matrix
594   sgCopyMat4( VIEW_ROT, VIEW );
595   VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
596
597   // Given a vector pointing straight down (-Z), map into onto the
598   // local plane representing "horizontal".  This should give us the
599   // local direction for moving "south".
600   sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
601
602   sgmap_vec_onto_cur_surface_plane(_world_up, _view_pos, minus_z,
603                                      _surface_south);
604   sgNormalizeVec3(_surface_south);
605
606   // now calculate the surface east vector
607   sgVec3 world_down;
608   sgNegateVec3(world_down, _world_up);
609   sgVectorProductVec3(_surface_east, _surface_south, world_down);
610
611   set_clean();
612 }
613
614 void
615 FGViewer::recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const
616 {
617   double sea_level_radius_m;
618   double lat_geoc_rad;
619
620
621                                 // Convert from geodetic to geocentric
622                                 // coordinates.
623   sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
624                alt_ft * SG_FEET_TO_METER,
625                &sea_level_radius_m,
626                &lat_geoc_rad);
627
628                                 // Calculate the cartesian coordinates
629                                 // of point directly below at sea level.
630                                 // aka Zero Elevation Position
631   Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
632                       lat_geoc_rad,
633                       sea_level_radius_m);
634   Point3D tmp = sgPolarToCart3d(p) - scenery.get_next_center();
635   sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
636
637                                 // Calculate the absolute view position
638                                 // in fgfs coordinates.
639                                 // aka Absolute View Position
640   p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
641   tmp = sgPolarToCart3d(p);
642   sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
643
644                                 // Calculate the relative view position
645                                 // from the scenery center.
646                                 // aka Relative View Position
647   sgdVec3 scenery_center;
648   sgdSetVec3(scenery_center,
649              scenery.get_next_center().x(),
650              scenery.get_next_center().y(),
651              scenery.get_next_center().z());
652   sgdVec3 view_pos;
653   sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
654   sgSetVec3(_relative_view_pos, view_pos);
655
656 }
657
658 double
659 FGViewer::get_h_fov()
660 {
661     switch (_scaling_type) {
662     case FG_SCALING_WIDTH:  // h_fov == fov
663         return _fov_deg;
664     case FG_SCALING_MAX:
665         if (_aspect_ratio < 1.0) {
666             // h_fov == fov
667             return _fov_deg;
668         } else {
669             // v_fov == fov
670             return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) / _aspect_ratio) *
671                 SG_RADIANS_TO_DEGREES * 2;
672         }
673     default:
674         assert(false);
675     }
676 }
677
678 double
679 FGViewer::get_v_fov()
680 {
681     switch (_scaling_type) {
682     case FG_SCALING_WIDTH:  // h_fov == fov
683         return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
684             SG_RADIANS_TO_DEGREES * 2;
685     case FG_SCALING_MAX:
686         if (_aspect_ratio < 1.0) {
687             // h_fov == fov
688             return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
689                 SG_RADIANS_TO_DEGREES * 2;
690         } else {
691             // v_fov == fov
692             return _fov_deg;
693         }
694     default:
695         assert(false);
696     }
697 }
698
699 void
700 FGViewer::update (int dt)
701 {
702   int i;
703   for ( i = 0; i < dt; i++ ) {
704     if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
705       setHeadingOffset_deg( _goal_heading_offset_deg );
706       break;
707     } else {
708       // move current_view.headingoffset towards
709       // current_view.goal_view_offset
710       if ( _goal_heading_offset_deg > _heading_offset_deg )
711         {
712           if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
713             incHeadingOffset_deg( 0.5 );
714           } else {
715             incHeadingOffset_deg( -0.5 );
716           }
717         } else {
718           if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
719             incHeadingOffset_deg( -0.5 );
720           } else {
721             incHeadingOffset_deg( 0.5 );
722           }
723         }
724       if ( _heading_offset_deg > 360 ) {
725         incHeadingOffset_deg( -360 );
726       } else if ( _heading_offset_deg < 0 ) {
727         incHeadingOffset_deg( 360 );
728       }
729     }
730   }
731
732   for ( i = 0; i < dt; i++ ) {
733     if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
734       setPitchOffset_deg( _goal_pitch_offset_deg );
735       break;
736     } else {
737       // move current_view.pitch_offset_deg towards
738       // current_view.goal_pitch_offset
739       if ( _goal_pitch_offset_deg > _pitch_offset_deg )
740         {
741           incPitchOffset_deg( 1.0 );
742         } else {
743             incPitchOffset_deg( -1.0 );
744         }
745       if ( _pitch_offset_deg > 90 ) {
746         setPitchOffset_deg(90);
747       } else if ( _pitch_offset_deg < -90 ) {
748         setPitchOffset_deg( -90 );
749       }
750     }
751   }
752 }
753
754