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