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