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