]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.cxx
Added options to set up avionics from the command line:
[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 #include "fg_props.hxx"
30
31 #ifdef HAVE_CONFIG_H
32 #  include <config.h>
33 #endif
34
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/constants.h>
37 #include <simgear/math/point3d.hxx>
38 #include <simgear/math/polar3d.hxx>
39 #include <simgear/math/sg_geodesy.hxx>
40 #include <simgear/scene/model/location.hxx>
41 #include <simgear/scene/model/placement.hxx>
42 #include <simgear/math/vector.hxx>
43
44 #include <Main/globals.hxx>
45 #include <Scenery/scenery.hxx>
46 #include <Model/acmodel.hxx>
47
48 #include "viewer.hxx"
49
50 \f
51 //////////////////////////////////////////////////////////////////
52 // Norman's Optimized matrix rotators!                          //
53 //////////////////////////////////////////////////////////////////
54
55
56 // Since these are pure rotation matrices we can save some bookwork
57 // by considering them to be 3x3 until the very end -- NHV
58 static void MakeVIEW_OFFSET( sgMat4 dst,
59                       const float angle1, const sgVec3 axis1,
60                       const float angle2, const sgVec3 axis2 )
61 {
62     // make rotmatrix1 from angle and axis
63     float s = (float) sin ( angle1 ) ;
64     float c = (float) cos ( angle1 ) ;
65     float t = SG_ONE - c ;
66
67     sgMat3 mat1;
68     float tmp = t * axis1[0];
69     mat1[0][0] = tmp * axis1[0] + c ;
70     mat1[0][1] = tmp * axis1[1] + s * axis1[2] ;
71     mat1[0][2] = tmp * axis1[2] - s * axis1[1] ;
72
73     tmp = t * axis1[1];
74     mat1[1][0] = tmp * axis1[0] - s * axis1[2] ;
75     mat1[1][1] = tmp * axis1[1] + c ;
76     mat1[1][2] = tmp * axis1[2] + s * axis1[0] ;
77
78     tmp = t * axis1[2];
79     mat1[2][0] = tmp * axis1[0] + s * axis1[1] ;
80     mat1[2][1] = tmp * axis1[1] - s * axis1[0] ;
81     mat1[2][2] = tmp * axis1[2] + c ;
82
83     // make rotmatrix2 from angle and axis
84     s = (float) sin ( angle2 ) ;
85     c = (float) cos ( angle2 ) ;
86     t = SG_ONE - c ;
87
88     sgMat3 mat2;
89     tmp = t * axis2[0];
90     mat2[0][0] = tmp * axis2[0] + c ;
91     mat2[0][1] = tmp * axis2[1] + s * axis2[2] ;
92     mat2[0][2] = tmp * axis2[2] - s * axis2[1] ;
93
94     tmp = t * axis2[1];
95     mat2[1][0] = tmp * axis2[0] - s * axis2[2] ;
96     mat2[1][1] = tmp * axis2[1] + c ;
97     mat2[1][2] = tmp * axis2[2] + s * axis2[0] ;
98
99     tmp = t * axis2[2];
100     mat2[2][0] = tmp * axis2[0] + s * axis2[1] ;
101     mat2[2][1] = tmp * axis2[1] - s * axis2[0] ;
102     mat2[2][2] = tmp * axis2[2] + c ;
103
104     // multiply matrices
105     for ( int j = 0 ; j < 3 ; j++ ) {
106         dst[0][j] = mat2[0][0] * mat1[0][j] +
107                     mat2[0][1] * mat1[1][j] +
108                     mat2[0][2] * mat1[2][j];
109
110         dst[1][j] = mat2[1][0] * mat1[0][j] +
111                     mat2[1][1] * mat1[1][j] +
112                     mat2[1][2] * mat1[2][j];
113
114         dst[2][j] = mat2[2][0] * mat1[0][j] +
115                     mat2[2][1] * mat1[1][j] +
116                     mat2[2][2] * mat1[2][j];
117     }
118     // fill in 4x4 matrix elements
119     dst[0][3] = SG_ZERO; 
120     dst[1][3] = SG_ZERO; 
121     dst[2][3] = SG_ZERO;
122     dst[3][0] = SG_ZERO;
123     dst[3][1] = SG_ZERO;
124     dst[3][2] = SG_ZERO;
125     dst[3][3] = SG_ONE;
126 }
127
128
129 ////////////////////////////////////////////////////////////////////////
130 // Implementation of FGViewer.
131 ////////////////////////////////////////////////////////////////////////
132
133 // Constructor...
134 FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index,
135                     bool at_model, int at_model_index,
136                     double damp_alt, double damp_roll, double damp_pitch, double damp_heading,
137                     double x_offset_m, double y_offset_m, double z_offset_m,
138                     double heading_offset_deg, double pitch_offset_deg,
139                     double roll_offset_deg, double fov_deg,
140                     double target_x_offset_m, double target_y_offset_m,
141                     double target_z_offset_m, double near_m ):
142     _dirty(true),
143     _lon_deg(0),
144     _lat_deg(0),
145     _alt_ft(0),
146     _target_lon_deg(0),
147     _target_lat_deg(0),
148     _target_alt_ft(0),
149     _roll_deg(0),
150     _pitch_deg(0),
151     _heading_deg(0),
152     _damp_alt(0),
153     _damp_roll(0),
154     _damp_pitch(0),
155     _damp_heading(0),
156     _scaling_type(FG_SCALING_MAX)
157 {
158     sgdZeroVec3(_absolute_view_pos);
159     _type = Type;
160     _from_model = from_model;
161     _from_model_index = from_model_index;
162     _at_model = at_model;
163     _at_model_index = at_model_index;
164     if (damp_alt > 0.0)
165       _damp_alt = 1 - 1.0 / pow(10, fabs(damp_alt));
166     if (damp_roll > 0.0)
167       _damp_roll = 1 - 1.0 / pow(10, fabs(damp_roll));
168     if (damp_pitch > 0.0)
169       _damp_pitch = 1 - 1.0 / pow(10, fabs(damp_pitch));
170     if (damp_heading > 0.0)
171       _damp_heading = 1 - 1.0 / pow(10, fabs(damp_heading));
172
173     _x_offset_m = x_offset_m;
174     _y_offset_m = y_offset_m;
175     _z_offset_m = z_offset_m;
176     _heading_offset_deg = heading_offset_deg;
177     _pitch_offset_deg = pitch_offset_deg;
178     _roll_offset_deg = roll_offset_deg;
179     _goal_heading_offset_deg = heading_offset_deg;
180     _goal_pitch_offset_deg = pitch_offset_deg;
181     _goal_roll_offset_deg = roll_offset_deg;
182     if (fov_deg > 0) {
183       _fov_deg = fov_deg;
184     } else {
185       _fov_deg = 55;
186     }
187     _target_x_offset_m = target_x_offset_m;
188     _target_y_offset_m = target_y_offset_m;
189     _target_z_offset_m = target_z_offset_m;
190     _ground_level_nearplane_m = near_m;
191     // a reasonable guess for init, so that the math doesn't blow up
192 }
193
194
195 // Destructor
196 FGViewer::~FGViewer( void ) {
197 }
198
199 void
200 FGViewer::init ()
201 {
202   if ( _from_model )
203     _location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
204   else
205     _location = (SGLocation *) new SGLocation;
206
207   if ( _type == FG_LOOKAT ) {
208     if ( _at_model )
209       _target_location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
210     else
211       _target_location = (SGLocation *) new SGLocation;
212   }
213 }
214
215 void
216 FGViewer::bind ()
217 {
218 }
219
220 void
221 FGViewer::unbind ()
222 {
223 }
224
225 void
226 FGViewer::setType ( int type )
227 {
228   if (type == 0)
229     _type = FG_LOOKFROM;
230   if (type == 1)
231     _type = FG_LOOKAT;
232 }
233
234 void
235 FGViewer::setLongitude_deg (double lon_deg)
236 {
237   _dirty = true;
238   _lon_deg = lon_deg;
239 }
240
241 void
242 FGViewer::setLatitude_deg (double lat_deg)
243 {
244   _dirty = true;
245   _lat_deg = lat_deg;
246 }
247
248 void
249 FGViewer::setAltitude_ft (double alt_ft)
250 {
251   _dirty = true;
252   _alt_ft = alt_ft;
253 }
254
255 void
256 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
257 {
258   _dirty = true;
259   _lon_deg = lon_deg;
260   _lat_deg = lat_deg;
261   _alt_ft = alt_ft;
262 }
263
264 void
265 FGViewer::setTargetLongitude_deg (double lon_deg)
266 {
267   _dirty = true;
268   _target_lon_deg = lon_deg;
269 }
270
271 void
272 FGViewer::setTargetLatitude_deg (double lat_deg)
273 {
274   _dirty = true;
275   _target_lat_deg = lat_deg;
276 }
277
278 void
279 FGViewer::setTargetAltitude_ft (double alt_ft)
280 {
281   _dirty = true;
282   _target_alt_ft = alt_ft;
283 }
284
285 void
286 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
287 {
288   _dirty = true;
289   _target_lon_deg = lon_deg;
290   _target_lat_deg = lat_deg;
291   _target_alt_ft = alt_ft;
292 }
293
294 void
295 FGViewer::setRoll_deg (double roll_deg)
296 {
297   _dirty = true;
298   _roll_deg = roll_deg;
299 }
300
301 void
302 FGViewer::setPitch_deg (double pitch_deg)
303 {
304   _dirty = true;
305   _pitch_deg = pitch_deg;
306 }
307
308 void
309 FGViewer::setHeading_deg (double heading_deg)
310 {
311   _dirty = true;
312   _heading_deg = heading_deg;
313 }
314
315 void
316 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
317 {
318   _dirty = true;
319   _roll_deg = roll_deg;
320   _pitch_deg = pitch_deg;
321   _heading_deg = heading_deg;
322 }
323
324 void
325 FGViewer::setTargetRoll_deg (double target_roll_deg)
326 {
327   _dirty = true;
328   _target_roll_deg = target_roll_deg;
329 }
330
331 void
332 FGViewer::setTargetPitch_deg (double target_pitch_deg)
333 {
334   _dirty = true;
335   _target_pitch_deg = target_pitch_deg;
336 }
337
338 void
339 FGViewer::setTargetHeading_deg (double target_heading_deg)
340 {
341   _dirty = true;
342   _target_heading_deg = target_heading_deg;
343 }
344
345 void
346 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
347 {
348   _dirty = true;
349   _target_roll_deg = target_roll_deg;
350   _target_pitch_deg = target_pitch_deg;
351   _target_heading_deg = target_heading_deg;
352 }
353
354 void
355 FGViewer::setXOffset_m (double x_offset_m)
356 {
357   _dirty = true;
358   _x_offset_m = x_offset_m;
359 }
360
361 void
362 FGViewer::setYOffset_m (double y_offset_m)
363 {
364   _dirty = true;
365   _y_offset_m = y_offset_m;
366 }
367
368 void
369 FGViewer::setZOffset_m (double z_offset_m)
370 {
371   _dirty = true;
372   _z_offset_m = z_offset_m;
373 }
374
375 void
376 FGViewer::setTargetXOffset_m (double target_x_offset_m)
377 {
378   _dirty = true;
379   _target_x_offset_m = target_x_offset_m;
380 }
381
382 void
383 FGViewer::setTargetYOffset_m (double target_y_offset_m)
384 {
385   _dirty = true;
386   _target_y_offset_m = target_y_offset_m;
387 }
388
389 void
390 FGViewer::setTargetZOffset_m (double target_z_offset_m)
391 {
392   _dirty = true;
393   _target_z_offset_m = target_z_offset_m;
394 }
395
396 void
397 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
398 {
399   _dirty = true;
400   _x_offset_m = x_offset_m;
401   _y_offset_m = y_offset_m;
402   _z_offset_m = z_offset_m;
403 }
404
405 void
406 FGViewer::setRollOffset_deg (double roll_offset_deg)
407 {
408   _dirty = true;
409   _roll_offset_deg = roll_offset_deg;
410 }
411
412 void
413 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
414 {
415   _dirty = true;
416   _pitch_offset_deg = pitch_offset_deg;
417 }
418
419 void
420 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
421 {
422   _dirty = true;
423   _heading_offset_deg = heading_offset_deg;
424 }
425
426 void
427 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
428 {
429   _dirty = true;
430   _goal_roll_offset_deg = goal_roll_offset_deg;
431 }
432
433 void
434 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
435 {
436   _dirty = true;
437   _goal_pitch_offset_deg = goal_pitch_offset_deg;
438   if ( _goal_pitch_offset_deg < -90 ) {
439     _goal_pitch_offset_deg = -90.0;
440   }
441   if ( _goal_pitch_offset_deg > 90.0 ) {
442     _goal_pitch_offset_deg = 90.0;
443   }
444
445 }
446
447 void
448 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
449 {
450   _dirty = true;
451   _goal_heading_offset_deg = goal_heading_offset_deg;
452   while ( _goal_heading_offset_deg < 0.0 ) {
453     _goal_heading_offset_deg += 360;
454   }
455   while ( _goal_heading_offset_deg > 360 ) {
456     _goal_heading_offset_deg -= 360;
457   }
458 }
459
460 void
461 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
462 {
463   _dirty = true;
464   _roll_offset_deg = roll_offset_deg;
465   _pitch_offset_deg = pitch_offset_deg;
466   _heading_offset_deg = heading_offset_deg;
467 }
468
469 double *
470 FGViewer::get_absolute_view_pos () 
471 {
472   if (_dirty)
473     recalc();
474   return _absolute_view_pos;
475 }
476
477 float *
478 FGViewer::getRelativeViewPos () 
479 {
480   if (_dirty)
481     recalc();
482   return _relative_view_pos;
483 }
484
485 float *
486 FGViewer::getZeroElevViewPos () 
487 {
488   if (_dirty)
489     recalc();
490   return _zero_elev_view_pos;
491 }
492
493 void
494 FGViewer::updateFromModelLocation (SGLocation * location)
495 {
496   sgCopyMat4(LOCAL, location->getCachedTransformMatrix());
497 }
498
499 void
500 FGViewer::updateAtModelLocation (SGLocation * location)
501 {
502   sgCopyMat4(ATLOCAL, 
503              location->getCachedTransformMatrix());
504 }
505
506 void
507 FGViewer::recalcOurOwnLocation (SGLocation * location, double lon_deg, double lat_deg, double alt_ft, 
508                         double roll_deg, double pitch_deg, double heading_deg)
509 {
510   // update from our own data...
511   dampEyeData(alt_ft, roll_deg, pitch_deg, heading_deg);
512   location->setPosition( lon_deg, lat_deg, alt_ft );
513   location->setOrientation( roll_deg, pitch_deg, heading_deg );
514   sgCopyMat4(LOCAL,
515              location->getTransformMatrix(globals->get_scenery()->get_center()));
516 }
517
518 // recalc() is done every time one of the setters is called (making the 
519 // cached data "dirty") on the next "get".  It calculates all the outputs 
520 // for viewer.
521 void
522 FGViewer::recalc ()
523 {
524   if (_type == FG_LOOKFROM) {
525     recalcLookFrom();
526   } else {
527     recalcLookAt();
528   }
529
530   set_clean();
531 }
532
533 // recalculate for LookFrom view type...
534 void
535 FGViewer::recalcLookFrom ()
536 {
537
538   sgVec3 right, forward;
539   // sgVec3 eye_pos;
540   sgVec3 position_offset; // eye position offsets (xyz)
541
542   // LOOKFROM mode...
543
544   // Update location data...
545   if ( _from_model ) {
546     // update or data from model location
547     updateFromModelLocation(_location);
548   } else {
549     // update from our own data...
550     recalcOurOwnLocation( _location, _lon_deg, _lat_deg, _alt_ft, 
551           _roll_deg, _pitch_deg, _heading_deg );
552   }
553
554   // copy data from location class to local items...
555   copyLocationData();
556
557   // make sg vectors view up, right and forward vectors from LOCAL
558   sgSetVec3( _view_up, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
559   sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
560   sgSetVec3( forward, -LOCAL[0][0], -LOCAL[0][1], -LOCAL[0][2] );
561
562
563   // Note that when in "lookfrom" view the "view up" vector is always applied
564   // to the viewer.  View up is based on verticle of the aircraft itself. (see
565   // "LOCAL" matrix above)
566
567   // Orientation Offsets matrix
568   MakeVIEW_OFFSET( VIEW_OFFSET,
569     _heading_offset_deg  * SG_DEGREES_TO_RADIANS, _view_up,
570     _pitch_offset_deg  * SG_DEGREES_TO_RADIANS, right );
571
572   // Make the VIEW matrix.
573   sgSetVec4(VIEW[0], right[0], right[1], right[2],SG_ZERO);
574   sgSetVec4(VIEW[1], forward[0], forward[1], forward[2],SG_ZERO);
575   sgSetVec4(VIEW[2], _view_up[0], _view_up[1], _view_up[2],SG_ZERO);
576   sgSetVec4(VIEW[3], SG_ZERO, SG_ZERO, SG_ZERO,SG_ONE);
577
578   // rotate model or local matrix to get a matrix to apply Eye Position Offsets
579   sgMat4 VIEW_UP; // L0 forward L1 right L2 up
580   sgCopyVec4(VIEW_UP[0], LOCAL[1]); 
581   sgCopyVec4(VIEW_UP[1], LOCAL[2]);
582   sgCopyVec4(VIEW_UP[2], LOCAL[0]);
583   sgZeroVec4(VIEW_UP[3]);
584
585   // Eye Position Offsets to vector
586   sgSetVec3( position_offset, _x_offset_m, _y_offset_m, _z_offset_m );
587   sgXformVec3( position_offset, position_offset, VIEW_UP);
588
589   // add the offsets including rotations to the translation vector
590   sgAddVec3( _view_pos, position_offset );
591
592   // multiply the OFFSETS (for heading and pitch) into the VIEW
593   sgPostMultMat4(VIEW, VIEW_OFFSET);
594
595   // add the position data to the matrix
596   sgSetVec4(VIEW[3], _view_pos[0], _view_pos[1], _view_pos[2],SG_ONE);
597
598 }
599
600 void
601 FGViewer::recalcLookAt ()
602 {
603
604   sgVec3 right;
605   sgVec3 eye_pos, at_pos;
606   sgVec3 position_offset; // eye position offsets (xyz)
607   sgVec3 target_position_offset; // target position offsets (xyz)
608
609   // The position vectors originate from the view point or target location
610   // depending on the type of view.
611
612   // LOOKAT mode...
613
614   // Update location data for target...
615   if ( _at_model ) {
616     // update or data from model location
617     updateAtModelLocation(_target_location);
618   } else {
619     // if not model then calculate our own target position...
620     recalcOurOwnLocation( _target_location, _target_lon_deg, _target_lat_deg, _target_alt_ft, 
621           _target_roll_deg, _target_pitch_deg, _target_heading_deg );
622   }
623   // calculate the "at" target object positon relative to eye or view's tile center...
624   sgdVec3 dVec3;
625   sgdSetVec3(dVec3,  _location->get_tile_center()[0], _location->get_tile_center()[1], _location->get_tile_center()[2]);
626   sgdSubVec3(dVec3,
627              _target_location->get_absolute_view_pos(globals->get_scenery()->get_center()),
628              dVec3 );
629   sgSetVec3(at_pos, dVec3[0], dVec3[1], dVec3[2]);
630
631   // Update location data for eye...
632   if ( _from_model ) {
633     // update or data from model location
634     updateFromModelLocation(_location);
635   } else {
636     // update from our own data, just the rotation here...
637
638     recalcOurOwnLocation( _location, _lon_deg, _lat_deg, _alt_ft, 
639           _roll_deg, _pitch_deg, _heading_deg );
640   }
641   // save the eye positon...
642   sgCopyVec3(eye_pos,  _location->get_view_pos());
643
644   // copy data from location class to local items...
645   copyLocationData();
646
647   // make sg vectors view up, right and forward vectors from LOCAL
648   sgSetVec3( _view_up, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
649   sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
650
651   // rotate model or local matrix to get a matrix to apply Eye Position Offsets
652   sgMat4 VIEW_UP; // L0 forward L1 right L2 up
653   sgCopyVec4(VIEW_UP[0], LOCAL[1]); 
654   sgCopyVec4(VIEW_UP[1], LOCAL[2]);
655   sgCopyVec4(VIEW_UP[2], LOCAL[0]);
656   sgZeroVec4(VIEW_UP[3]);
657
658   // get Orientation Offsets matrix
659   MakeVIEW_OFFSET( VIEW_OFFSET,
660     (_heading_offset_deg - 180) * SG_DEGREES_TO_RADIANS, _view_up,
661     _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
662
663   // add in the position offsets
664   sgSetVec3( position_offset, _y_offset_m, _x_offset_m, _z_offset_m );
665   sgXformVec3( position_offset, position_offset, VIEW_UP);
666
667   // apply the Orientation offsets
668   sgXformVec3( position_offset, position_offset, VIEW_OFFSET );
669
670   // add the Position offsets from object to the eye position
671   sgAddVec3( eye_pos, eye_pos, position_offset );
672
673   // add target offsets to at_position...
674   sgSetVec3(target_position_offset, _target_z_offset_m,  _target_x_offset_m,
675                                     _target_y_offset_m );
676   sgXformVec3(target_position_offset, target_position_offset, ATLOCAL);
677   sgAddVec3( at_pos, at_pos, target_position_offset);
678
679   sgAddVec3( eye_pos, eye_pos, target_position_offset);
680
681   // Make the VIEW matrix for a "LOOKAT".
682   sgMakeLookAtMat4( VIEW, eye_pos, at_pos, _view_up );
683
684 }
685
686 // copy results from location class to viewer...
687 // FIXME: some of these should be changed to reference directly to SGLocation...
688 void
689 FGViewer::copyLocationData()
690 {
691   // Get our friendly vectors from the eye location...
692   sgCopyVec3(_zero_elev_view_pos,  _location->get_zero_elev());
693   sgCopyVec3(_relative_view_pos, _location->get_view_pos());
694   sgdCopyVec3(_absolute_view_pos,
695               _location->get_absolute_view_pos(globals->get_scenery()->get_center()));
696   sgCopyMat4(UP, _location->getCachedUpMatrix());
697   sgCopyVec3(_world_up, _location->get_world_up());
698   // these are the vectors that the sun and moon code like to get...
699   sgCopyVec3(_surface_east, _location->get_surface_east());
700   sgCopyVec3(_surface_south, _location->get_surface_south());
701
702   // Update viewer's postion data for the eye location...
703   _lon_deg = _location->getLongitude_deg();
704   _lat_deg = _location->getLatitude_deg();
705   _alt_ft = _location->getAltitudeASL_ft();
706   _roll_deg = _location->getRoll_deg();
707   _pitch_deg = _location->getPitch_deg();
708   _heading_deg = _location->getHeading_deg();
709
710   // Update viewer's postion data for the target (at object) location
711   if (_type == FG_LOOKAT) {
712     _target_lon_deg = _target_location->getLongitude_deg();
713     _target_lat_deg = _target_location->getLatitude_deg();
714     _target_alt_ft = _target_location->getAltitudeASL_ft();
715     _target_roll_deg = _target_location->getRoll_deg();
716     _target_pitch_deg = _target_location->getPitch_deg();
717     _target_heading_deg = _target_location->getHeading_deg();
718   }
719
720   // copy coordinates to outputs for viewer...
721   sgCopyVec3(_zero_elev, _zero_elev_view_pos);
722   sgCopyVec3(_view_pos, _relative_view_pos);
723 }
724
725 void
726 FGViewer::dampEyeData (double &alt_ft, double &roll_deg, double &pitch_deg, double &heading_deg)
727 {
728   static FGViewer *last = 0;
729   if (last != this) {
730     _damp_sync = 0.0;
731     _damped_alt_ft = alt_ft;
732     _damped_roll_deg = roll_deg;
733     _damped_pitch_deg = pitch_deg;
734     _damped_heading_deg = heading_deg;
735     last = this;
736     return;
737   }
738
739   if (_damp_sync < 0.01) {
740     alt_ft = _damped_alt_ft;
741     roll_deg = _damped_roll_deg;
742     pitch_deg = _damped_pitch_deg;
743     heading_deg = _damped_heading_deg;
744     return;
745   }
746
747   while (_damp_sync >= 0.01) {
748     _damp_sync -= 0.01;
749
750     double d;
751     if (_damp_alt > 0.0)
752       alt_ft = _damped_alt_ft = alt_ft * (1 - _damp_alt) + _damped_alt_ft * _damp_alt;
753
754     if (_damp_roll > 0.0) {
755       d = _damped_roll_deg - roll_deg;
756       if (d >= 180.0)
757         _damped_roll_deg -= 360.0;
758       else if (d < -180.0)
759         _damped_roll_deg += 360.0;
760       roll_deg = _damped_roll_deg = roll_deg * (1 - _damp_roll) + _damped_roll_deg * _damp_roll;
761     }
762
763     if (_damp_pitch > 0.0) {
764       d = _damped_pitch_deg - pitch_deg;
765       if (d >= 180.0)
766         _damped_pitch_deg -= 360.0;
767       else if (d < -180.0)
768         _damped_pitch_deg += 360.0;
769       pitch_deg = _damped_pitch_deg = pitch_deg * (1 - _damp_pitch) + _damped_pitch_deg * _damp_pitch;
770     }
771
772     if (_damp_heading > 0.0) {
773       d = _damped_heading_deg - heading_deg;
774       if (d >= 180.0)
775         _damped_heading_deg -= 360.0;
776       else if (d < -180.0)
777         _damped_heading_deg += 360.0;
778       heading_deg = _damped_heading_deg = heading_deg * (1 - _damp_heading) + _damped_heading_deg * _damp_heading;
779     }
780   }
781 }
782
783 double
784 FGViewer::get_h_fov()
785 {
786     switch (_scaling_type) {
787     case FG_SCALING_WIDTH:  // h_fov == fov
788         return _fov_deg;
789     case FG_SCALING_MAX:
790         if (_aspect_ratio < 1.0) {
791             // h_fov == fov
792             return _fov_deg;
793         } else {
794             // v_fov == fov
795             return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) / _aspect_ratio) *
796                 SG_RADIANS_TO_DEGREES * 2;
797         }
798     default:
799         assert(false);
800     }
801     return 0.0;
802 }
803
804
805
806 double
807 FGViewer::get_v_fov()
808 {
809     switch (_scaling_type) {
810     case FG_SCALING_WIDTH:  // h_fov == fov
811         return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
812             SG_RADIANS_TO_DEGREES * 2;
813     case FG_SCALING_MAX:
814         if (_aspect_ratio < 1.0) {
815             // h_fov == fov
816             return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
817                 SG_RADIANS_TO_DEGREES * 2;
818         } else {
819             // v_fov == fov
820             return _fov_deg;
821         }
822     default:
823         assert(false);
824     }
825     return 0.0;
826 }
827
828 void
829 FGViewer::update (double dt)
830 {
831   _damp_sync += dt;
832
833   int i;
834   int dt_ms = int(dt * 1000);
835   for ( i = 0; i < dt_ms; i++ ) {
836     if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
837       setHeadingOffset_deg( _goal_heading_offset_deg );
838       break;
839     } else {
840       // move current_view.headingoffset towards
841       // current_view.goal_view_offset
842       if ( _goal_heading_offset_deg > _heading_offset_deg )
843         {
844           if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
845             incHeadingOffset_deg( 0.5 );
846           } else {
847             incHeadingOffset_deg( -0.5 );
848           }
849         } else {
850           if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
851             incHeadingOffset_deg( -0.5 );
852           } else {
853             incHeadingOffset_deg( 0.5 );
854           }
855         }
856       if ( _heading_offset_deg > 360 ) {
857         incHeadingOffset_deg( -360 );
858       } else if ( _heading_offset_deg < 0 ) {
859         incHeadingOffset_deg( 360 );
860       }
861     }
862   }
863
864   for ( i = 0; i < dt_ms; i++ ) {
865     if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
866       setPitchOffset_deg( _goal_pitch_offset_deg );
867       break;
868     } else {
869       // move current_view.pitch_offset_deg towards
870       // current_view.goal_pitch_offset
871       if ( _goal_pitch_offset_deg > _pitch_offset_deg )
872         {
873           incPitchOffset_deg( 1.0 );
874         } else {
875             incPitchOffset_deg( -1.0 );
876         }
877       if ( _pitch_offset_deg > 90 ) {
878         setPitchOffset_deg(90);
879       } else if ( _pitch_offset_deg < -90 ) {
880         setPitchOffset_deg( -90 );
881       }
882     }
883   }
884 }