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