]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.cxx
bed93454bb58e0a447b74c7b874a8cdc754743db
[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 #ifdef HAVE_CONFIG_H
30 #  include <config.h>
31 #endif
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/constants.h>
35 #include <simgear/math/point3d.hxx>
36 #include <simgear/math/polar3d.hxx>
37 #include <simgear/math/sg_geodesy.hxx>
38
39 #include <Scenery/scenery.hxx>
40
41 /* from lookat */
42 #include <simgear/math/vector.hxx>
43 #include "globals.hxx"
44 /* end from lookat */
45
46 #include "viewer.hxx"
47
48 \f
49 //////////////////////////////////////////////////////////////////
50 // Norman's Optimized matrix rotators!                          //
51 //////////////////////////////////////////////////////////////////
52
53 static void fgMakeLOCAL( sgMat4 dst, const double Theta,
54                                 const double Phi, const double Psi)
55 {
56     SGfloat cosTheta = (SGfloat) cos(Theta);
57     SGfloat sinTheta = (SGfloat) sin(Theta);
58     SGfloat cosPhi   = (SGfloat) cos(Phi);
59     SGfloat sinPhi   = (SGfloat) sin(Phi);
60     SGfloat sinPsi   = (SGfloat) sin(Psi) ;
61     SGfloat cosPsi   = (SGfloat) cos(Psi) ;
62         
63     dst[0][0] = cosPhi * cosTheta;
64     dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
65     dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
66     dst[0][3] = SG_ZERO;
67
68     dst[1][0] = -sinPhi * cosTheta;
69     dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
70     dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
71     dst[1][3] = SG_ZERO ;
72         
73     dst[2][0] = sinTheta;
74     dst[2][1] = cosTheta * -sinPsi;
75     dst[2][2] = cosTheta * cosPsi;
76     dst[2][3] = SG_ZERO;
77         
78     dst[3][0] = SG_ZERO;
79     dst[3][1] = SG_ZERO;
80     dst[3][2] = SG_ZERO;
81     dst[3][3] = SG_ONE ;
82 }
83
84
85 // Since these are pure rotation matrices we can save some bookwork
86 // by considering them to be 3x3 until the very end -- NHV
87 static void MakeVIEW_OFFSET( sgMat4 dst,
88                       const float angle1, const sgVec3 axis1,
89                       const float angle2, const sgVec3 axis2 )
90 {
91     // make rotmatrix1 from angle and axis
92     float s = (float) sin ( angle1 ) ;
93     float c = (float) cos ( angle1 ) ;
94     float t = SG_ONE - c ;
95
96     sgMat3 mat1;
97     float tmp = t * axis1[0];
98     mat1[0][0] = tmp * axis1[0] + c ;
99     mat1[0][1] = tmp * axis1[1] + s * axis1[2] ;
100     mat1[0][2] = tmp * axis1[2] - s * axis1[1] ;
101
102     tmp = t * axis1[1];
103     mat1[1][0] = tmp * axis1[0] - s * axis1[2] ;
104     mat1[1][1] = tmp * axis1[1] + c ;
105     mat1[1][2] = tmp * axis1[2] + s * axis1[0] ;
106
107     tmp = t * axis1[2];
108     mat1[2][0] = tmp * axis1[0] + s * axis1[1] ;
109     mat1[2][1] = tmp * axis1[1] - s * axis1[0] ;
110     mat1[2][2] = tmp * axis1[2] + c ;
111
112     // make rotmatrix2 from angle and axis
113     s = (float) sin ( angle2 ) ;
114     c = (float) cos ( angle2 ) ;
115     t = SG_ONE - c ;
116
117     sgMat3 mat2;
118     tmp = t * axis2[0];
119     mat2[0][0] = tmp * axis2[0] + c ;
120     mat2[0][1] = tmp * axis2[1] + s * axis2[2] ;
121     mat2[0][2] = tmp * axis2[2] - s * axis2[1] ;
122
123     tmp = t * axis2[1];
124     mat2[1][0] = tmp * axis2[0] - s * axis2[2] ;
125     mat2[1][1] = tmp * axis2[1] + c ;
126     mat2[1][2] = tmp * axis2[2] + s * axis2[0] ;
127
128     tmp = t * axis2[2];
129     mat2[2][0] = tmp * axis2[0] + s * axis2[1] ;
130     mat2[2][1] = tmp * axis2[1] - s * axis2[0] ;
131     mat2[2][2] = tmp * axis2[2] + c ;
132
133     // multiply matrices
134     for ( int j = 0 ; j < 3 ; j++ ) {
135         dst[0][j] = mat2[0][0] * mat1[0][j] +
136                     mat2[0][1] * mat1[1][j] +
137                     mat2[0][2] * mat1[2][j];
138
139         dst[1][j] = mat2[1][0] * mat1[0][j] +
140                     mat2[1][1] * mat1[1][j] +
141                     mat2[1][2] * mat1[2][j];
142
143         dst[2][j] = mat2[2][0] * mat1[0][j] +
144                     mat2[2][1] * mat1[1][j] +
145                     mat2[2][2] * mat1[2][j];
146     }
147     // fill in 4x4 matrix elements
148     dst[0][3] = SG_ZERO; 
149     dst[1][3] = SG_ZERO; 
150     dst[2][3] = SG_ZERO;
151     dst[3][0] = SG_ZERO;
152     dst[3][1] = SG_ZERO;
153     dst[3][2] = SG_ZERO;
154     dst[3][3] = SG_ONE;
155 }
156
157 // Taking advantage of the 3x3 nature of this -- NHV
158 inline static void MakeWithWorldUp( sgMat4 dst, const sgMat4 UP, const sgMat4 LOCAL )
159 {
160     sgMat4 tmp;
161
162     float a = UP[0][0];
163     float b = UP[1][0];
164     float c = UP[2][0];
165     tmp[0][0] = a*LOCAL[0][0] + b*LOCAL[0][1] + c*LOCAL[0][2] ;
166     tmp[1][0] = a*LOCAL[1][0] + b*LOCAL[1][1] + c*LOCAL[1][2] ;
167     tmp[2][0] = a*LOCAL[2][0] + b*LOCAL[2][1] + c*LOCAL[2][2] ;
168     tmp[3][0] = SG_ZERO ;
169
170     a = UP[0][1];
171     b = UP[1][1];
172     c = UP[2][1];
173     tmp[0][1] = a*LOCAL[0][0] + b*LOCAL[0][1] + c*LOCAL[0][2] ;
174     tmp[1][1] = a*LOCAL[1][0] + b*LOCAL[1][1] + c*LOCAL[1][2] ;
175     tmp[2][1] = a*LOCAL[2][0] + b*LOCAL[2][1] + c*LOCAL[2][2] ;
176     tmp[3][1] = SG_ZERO ;
177
178     a = UP[0][2];
179     c = UP[2][2];
180     tmp[0][2] = a*LOCAL[0][0] + c*LOCAL[0][2] ;
181     tmp[1][2] = a*LOCAL[1][0] + c*LOCAL[1][2] ;
182     tmp[2][2] = a*LOCAL[2][0] + c*LOCAL[2][2] ;
183     tmp[3][2] = SG_ZERO ;
184
185     tmp[0][3] = SG_ZERO ;
186     tmp[1][3] = SG_ZERO ;
187     tmp[2][3] = SG_ZERO ;
188     tmp[3][3] = SG_ONE ;
189     sgCopyMat4(dst, tmp);
190 }
191
192
193 ////////////////////////////////////////////////////////////////////////
194 // Implementation of FGViewer.
195 ////////////////////////////////////////////////////////////////////////
196
197 // Constructor
198 FGViewer::FGViewer( void ):
199     _scaling_type(FG_SCALING_MAX),
200     _fov_deg(55.0),
201     _dirty(true),
202     _lon_deg(0),
203     _lat_deg(0),
204     _alt_ft(0),
205     _target_lon_deg(0),
206     _target_lat_deg(0),
207     _target_alt_ft(0),
208     _roll_deg(0),
209     _pitch_deg(0),
210     _heading_deg(0),
211     _x_offset_m(0),
212     _y_offset_m(0),
213     _z_offset_m(0),
214     _heading_offset_deg(0),
215     _pitch_offset_deg(0),
216     _roll_offset_deg(0),
217     _goal_heading_offset_deg(0.0),
218     _goal_pitch_offset_deg(0.0)
219 {
220     sgdZeroVec3(_absolute_view_pos);
221     //a reasonable guess for init, so that the math doesn't blow up
222 }
223
224
225 // Destructor
226 FGViewer::~FGViewer( void ) {
227 }
228
229 void
230 FGViewer::init ()
231 {
232 }
233
234 void
235 FGViewer::bind ()
236 {
237 }
238
239 void
240 FGViewer::unbind ()
241 {
242 }
243
244 void
245 FGViewer::setType ( int type )
246 {
247   if (type == 0)
248     _type = FG_RPH;
249   if (type == 1)
250     _type = FG_LOOKAT;
251 }
252
253 void
254 FGViewer::setLongitude_deg (double lon_deg)
255 {
256   _dirty = true;
257   _lon_deg = lon_deg;
258 }
259
260 void
261 FGViewer::setLatitude_deg (double lat_deg)
262 {
263   _dirty = true;
264   _lat_deg = lat_deg;
265 }
266
267 void
268 FGViewer::setAltitude_ft (double alt_ft)
269 {
270   _dirty = true;
271   _alt_ft = alt_ft;
272 }
273
274 void
275 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
276 {
277   _dirty = true;
278   _lon_deg = lon_deg;
279   _lat_deg = lat_deg;
280   _alt_ft = alt_ft;
281 }
282
283 void
284 FGViewer::setTargetLongitude_deg (double lon_deg)
285 {
286   _dirty = true;
287   _target_lon_deg = lon_deg;
288 }
289
290 void
291 FGViewer::setTargetLatitude_deg (double lat_deg)
292 {
293   _dirty = true;
294   _target_lat_deg = lat_deg;
295 }
296
297 void
298 FGViewer::setTargetAltitude_ft (double alt_ft)
299 {
300   _dirty = true;
301   _target_alt_ft = alt_ft;
302 }
303
304 void
305 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
306 {
307   _dirty = true;
308   _target_lon_deg = lon_deg;
309   _target_lat_deg = lat_deg;
310   _target_alt_ft = alt_ft;
311 }
312
313 void
314 FGViewer::setRoll_deg (double roll_deg)
315 {
316   _dirty = true;
317   _roll_deg = roll_deg;
318 }
319
320 void
321 FGViewer::setPitch_deg (double pitch_deg)
322 {
323   _dirty = true;
324   _pitch_deg = pitch_deg;
325 }
326
327 void
328 FGViewer::setHeading_deg (double heading_deg)
329 {
330   _dirty = true;
331   _heading_deg = heading_deg;
332 }
333
334 void
335 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
336 {
337   _dirty = true;
338   _roll_deg = roll_deg;
339   _pitch_deg = pitch_deg;
340   _heading_deg = heading_deg;
341 }
342
343 void
344 FGViewer::setTargetRoll_deg (double target_roll_deg)
345 {
346   _dirty = true;
347   _target_roll_deg = target_roll_deg;
348 }
349
350 void
351 FGViewer::setTargetPitch_deg (double target_pitch_deg)
352 {
353   _dirty = true;
354   _target_pitch_deg = target_pitch_deg;
355 }
356
357 void
358 FGViewer::setTargetHeading_deg (double target_heading_deg)
359 {
360   _dirty = true;
361   _target_heading_deg = target_heading_deg;
362 }
363
364 void
365 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
366 {
367   _dirty = true;
368   _target_roll_deg = target_roll_deg;
369   _target_pitch_deg = target_pitch_deg;
370   _target_heading_deg = target_heading_deg;
371 }
372
373 void
374 FGViewer::setXOffset_m (double x_offset_m)
375 {
376   _dirty = true;
377   _x_offset_m = x_offset_m;
378 }
379
380 void
381 FGViewer::setYOffset_m (double y_offset_m)
382 {
383   _dirty = true;
384   _y_offset_m = y_offset_m;
385 }
386
387 void
388 FGViewer::setZOffset_m (double z_offset_m)
389 {
390   _dirty = true;
391   _z_offset_m = z_offset_m;
392 }
393
394 void
395 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
396 {
397   _dirty = true;
398   _x_offset_m = x_offset_m;
399   _y_offset_m = y_offset_m;
400   _z_offset_m = z_offset_m;
401 }
402
403 void
404 FGViewer::setRollOffset_deg (double roll_offset_deg)
405 {
406   _dirty = true;
407   _roll_offset_deg = roll_offset_deg;
408 }
409
410 void
411 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
412 {
413   _dirty = true;
414   _pitch_offset_deg = pitch_offset_deg;
415 }
416
417 void
418 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
419 {
420   _dirty = true;
421   _heading_offset_deg = heading_offset_deg;
422 }
423
424 void
425 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
426 {
427   _dirty = true;
428   _goal_roll_offset_deg = goal_roll_offset_deg;
429 }
430
431 void
432 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
433 {
434   _dirty = true;
435   _goal_pitch_offset_deg = goal_pitch_offset_deg;
436   if ( _goal_pitch_offset_deg < -90 ) {
437     _goal_pitch_offset_deg = -90.0;
438   }
439   if ( _goal_pitch_offset_deg > 90.0 ) {
440     _goal_pitch_offset_deg = 90.0;
441   }
442
443 }
444
445 void
446 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
447 {
448   _dirty = true;
449   _goal_heading_offset_deg = goal_heading_offset_deg;
450   while ( _goal_heading_offset_deg < 0.0 ) {
451     _goal_heading_offset_deg += 360;
452   }
453   while ( _goal_heading_offset_deg > 360 ) {
454     _goal_heading_offset_deg -= 360;
455   }
456 }
457
458 void
459 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
460 {
461   _dirty = true;
462   _roll_offset_deg = roll_offset_deg;
463   _pitch_offset_deg = pitch_offset_deg;
464   _heading_offset_deg = heading_offset_deg;
465 }
466
467 double *
468 FGViewer::get_absolute_view_pos () 
469 {
470   if (_dirty)
471     recalc();
472   return _absolute_view_pos;
473 }
474
475 float *
476 FGViewer::getRelativeViewPos () 
477 {
478   if (_dirty)
479     recalc();
480   return _relative_view_pos;
481 }
482
483 float *
484 FGViewer::getZeroElevViewPos () 
485 {
486   if (_dirty)
487     recalc();
488   return _zero_elev_view_pos;
489 }
490
491
492 // recalc() is done every time one of the setters is called (making the 
493 // cached data "dirty") on the next "get".  It calculates all the outputs 
494 // for viewer.
495 void
496 FGViewer::recalc ()
497 {
498   sgVec3 minus_z, right, forward, tilt;
499   sgMat4 tmpROT;  // temp rotation work matrices
500   sgMat4 VIEW_HEADINGOFFSET, VIEW_PITCHOFFSET;
501   sgVec3 tmpVec3;  // temp work vector (3)
502   sgVec3 eye_pos, object_pos;
503
504   // The position vectors originate from the view point or target location
505   // depending on the type of view.
506   // FIXME: Later note: actually the object (target) info needs to be held
507   //        by the model class.
508
509   if (_type == FG_RPH) {
510     // eye position is the location of the pilot
511     recalcPositionVectors( _lon_deg, _lat_deg, _alt_ft );
512   } else {
513     // eye position is now calculated based on lon/lat;
514     recalcPositionVectors( _lon_deg, _lat_deg, _alt_ft );
515     sgCopyVec3(eye_pos, _relative_view_pos);
516
517     // object position is the location of the object being looked at
518     recalcPositionVectors( _target_lon_deg, _target_lat_deg, _target_alt_ft );
519   }
520   // the coordinates generated by the above "recalcPositionVectors"
521   sgCopyVec3(_zero_elev, _zero_elev_view_pos);
522   sgCopyVec3(_view_pos, _relative_view_pos);
523
524   // Make the world up rotation matrix for eye positioin...
525   sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
526
527
528   // get the world up radial vector from planet center
529   // (ie. effect of aircraft location on earth "sphere" approximation)
530   sgSetVec3( _world_up, UP[0][0], UP[0][1], UP[0][2] );
531
532
533
534   // Creat local matrix with current geodetic position.  Converting
535   // the orientation (pitch/roll/heading) to vectors.
536   fgMakeLOCAL( LOCAL, _pitch_deg * SG_DEGREES_TO_RADIANS,
537                       _roll_deg * SG_DEGREES_TO_RADIANS,
538                       -_heading_deg * SG_DEGREES_TO_RADIANS);
539   // Adjust LOCAL to current world_up vector (adjustment for planet location)
540   MakeWithWorldUp( LOCAL, UP, LOCAL );
541   // copy the LOCAL matrix to COCKPIT_ROT for publication...
542   sgCopyMat4( LOCAL_ROT, LOCAL );
543
544   // make sg vectors view up, right and forward vectors from LOCAL
545   sgSetVec3( _view_up, LOCAL[0][0], LOCAL[0][1], LOCAL[0][2] );
546   sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
547   sgSetVec3( forward, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
548
549
550
551   // create xyz offsets Vector
552   sgVec3 position_offset;
553   sgSetVec3( position_offset, _y_offset_m, _x_offset_m, _z_offset_m );
554
555
556   // Eye rotations.  
557   // Looking up/down left/right in pilot view (lookfrom mode)
558   // or Floating Rotatation around the object in chase view (lookat mode).
559   // Generate the offset matrix to be applied using offset angles:
560   if (_type == FG_LOOKAT) {
561     // Note that when in "lookat" view the "world up" vector is always applied
562     // to the viewer.  World up is based on verticle at a given lon/lat (see
563     // matrix "UP" above).
564     MakeVIEW_OFFSET( VIEW_OFFSET,
565       _heading_offset_deg * SG_DEGREES_TO_RADIANS, _world_up,
566       _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
567   }
568   if (_type == FG_RPH) {
569     // Note that when in "lookfrom" view the "view up" vector is always applied
570     // to the viewer.  View up is based on verticle of the aircraft itself. (see
571     // "LOCAL" matrix above)
572     MakeVIEW_OFFSET( VIEW_OFFSET,
573       _heading_offset_deg  * SG_DEGREES_TO_RADIANS, _view_up,
574       _pitch_offset_deg  * SG_DEGREES_TO_RADIANS, right );
575   }
576
577
578
579   if (_type == FG_LOOKAT) {
580    
581     // transfrom "offset" and "orientation offset" to vector
582     sgXformVec3( position_offset, position_offset, UP );
583
584     // add heading to offset so that the eye does heading as such...
585     sgMakeRotMat4(tmpROT, -_heading_deg, _world_up);
586     sgPostMultMat4(VIEW_OFFSET, tmpROT);
587     sgXformVec3( position_offset, position_offset, VIEW_OFFSET );
588
589     // add the offsets from object to the eye position
590     sgAddVec3( eye_pos, eye_pos, position_offset );
591     // copy object
592     sgCopyVec3( object_pos, _view_pos );
593
594     // Make the VIEW matrix for "lookat".
595     sgMakeLookAtMat4( VIEW, eye_pos, object_pos, _view_up );
596   }
597
598   if (_type == FG_RPH) {
599
600     sgXformVec3( position_offset, position_offset, LOCAL);
601     // add the offsets including rotations to the coordinates
602     sgAddVec3( _view_pos, position_offset );
603
604     // Make the VIEW matrix.
605     VIEW[0][0] = right[0];
606     VIEW[0][1] = right[1];
607     VIEW[0][2] = right[2];
608     VIEW[0][3] = 0.0;
609     VIEW[1][0] = forward[0];
610     VIEW[1][1] = forward[1];
611     VIEW[1][2] = forward[2];
612     VIEW[1][3] = 0.0;
613     VIEW[2][0] = _view_up[0];
614     VIEW[2][1] = _view_up[1];
615     VIEW[2][2] = _view_up[2];
616     VIEW[2][3] = 0.0;
617     VIEW[3][0] = 0.0;
618     VIEW[3][1] = 0.0;
619     VIEW[3][2] = 0.0;
620     VIEW[3][3] = 0.0;
621     // multiply the OFFSETS (for heading and pitch) into the VIEW
622     sgPostMultMat4(VIEW, VIEW_OFFSET);
623
624     // add the position data to the matrix
625     VIEW[3][0] = _view_pos[0];
626     VIEW[3][1] = _view_pos[1];
627     VIEW[3][2] = _view_pos[2];
628     VIEW[3][3] = 1.0f;
629
630   }
631
632   // the VIEW matrix includes both rotation and translation.  Let's
633   // knock out the translation part to make the VIEW_ROT matrix
634   sgCopyMat4( VIEW_ROT, VIEW );
635   VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
636
637   // Given a vector pointing straight down (-Z), map into onto the
638   // local plane representing "horizontal".  This should give us the
639   // local direction for moving "south".
640   sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
641
642   sgmap_vec_onto_cur_surface_plane(_world_up, _view_pos, minus_z,
643                                      _surface_south);
644   sgNormalizeVec3(_surface_south);
645
646   // now calculate the surface east vector
647   sgVec3 world_down;
648   sgNegateVec3(world_down, _world_up);
649   sgVectorProductVec3(_surface_east, _surface_south, world_down);
650
651   set_clean();
652 }
653
654 void
655 FGViewer::recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const
656 {
657   double sea_level_radius_m;
658   double lat_geoc_rad;
659
660
661                                 // Convert from geodetic to geocentric
662                                 // coordinates.
663   sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
664                alt_ft * SG_FEET_TO_METER,
665                &sea_level_radius_m,
666                &lat_geoc_rad);
667
668                                 // Calculate the cartesian coordinates
669                                 // of point directly below at sea level.
670                                 // aka Zero Elevation Position
671   Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
672                       lat_geoc_rad,
673                       sea_level_radius_m);
674   Point3D tmp = sgPolarToCart3d(p) - scenery.get_next_center();
675   sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
676
677                                 // Calculate the absolute view position
678                                 // in fgfs coordinates.
679                                 // aka Absolute View Position
680   p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
681   tmp = sgPolarToCart3d(p);
682   sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
683
684                                 // Calculate the relative view position
685                                 // from the scenery center.
686                                 // aka Relative View Position
687   sgdVec3 scenery_center;
688   sgdSetVec3(scenery_center,
689              scenery.get_next_center().x(),
690              scenery.get_next_center().y(),
691              scenery.get_next_center().z());
692   sgdVec3 view_pos;
693   sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
694   sgSetVec3(_relative_view_pos, view_pos);
695
696 }
697
698 double
699 FGViewer::get_h_fov()
700 {
701     switch (_scaling_type) {
702     case FG_SCALING_WIDTH:  // h_fov == fov
703         return _fov_deg;
704     case FG_SCALING_MAX:
705         if (_aspect_ratio < 1.0) {
706             // h_fov == fov
707             return _fov_deg;
708         } else {
709             // v_fov == fov
710             return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) / _aspect_ratio) *
711                 SG_RADIANS_TO_DEGREES * 2;
712         }
713     default:
714         assert(false);
715     }
716 }
717
718 double
719 FGViewer::get_v_fov()
720 {
721     switch (_scaling_type) {
722     case FG_SCALING_WIDTH:  // h_fov == fov
723         return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
724             SG_RADIANS_TO_DEGREES * 2;
725     case FG_SCALING_MAX:
726         if (_aspect_ratio < 1.0) {
727             // h_fov == fov
728             return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
729                 SG_RADIANS_TO_DEGREES * 2;
730         } else {
731             // v_fov == fov
732             return _fov_deg;
733         }
734     default:
735         assert(false);
736     }
737 }
738
739 void
740 FGViewer::update (int dt)
741 {
742   int i;
743   for ( i = 0; i < dt; i++ ) {
744     if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
745       setHeadingOffset_deg( _goal_heading_offset_deg );
746       break;
747     } else {
748       // move current_view.headingoffset towards
749       // current_view.goal_view_offset
750       if ( _goal_heading_offset_deg > _heading_offset_deg )
751         {
752           if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
753             incHeadingOffset_deg( 0.5 );
754           } else {
755             incHeadingOffset_deg( -0.5 );
756           }
757         } else {
758           if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
759             incHeadingOffset_deg( -0.5 );
760           } else {
761             incHeadingOffset_deg( 0.5 );
762           }
763         }
764       if ( _heading_offset_deg > 360 ) {
765         incHeadingOffset_deg( -360 );
766       } else if ( _heading_offset_deg < 0 ) {
767         incHeadingOffset_deg( 360 );
768       }
769     }
770   }
771
772   for ( i = 0; i < dt; i++ ) {
773     if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
774       setPitchOffset_deg( _goal_pitch_offset_deg );
775       break;
776     } else {
777       // move current_view.pitch_offset_deg towards
778       // current_view.goal_pitch_offset
779       if ( _goal_pitch_offset_deg > _pitch_offset_deg )
780         {
781           incPitchOffset_deg( 1.0 );
782         } else {
783             incPitchOffset_deg( -1.0 );
784         }
785       if ( _pitch_offset_deg > 90 ) {
786         setPitchOffset_deg(90);
787       } else if ( _pitch_offset_deg < -90 ) {
788         setPitchOffset_deg( -90 );
789       }
790     }
791   }
792 }
793
794
795
796