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