]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.cxx
Really implement fgWarpMouse for osgviewer
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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 // Implementation of FGViewer.
56 ////////////////////////////////////////////////////////////////////////
57
58 // Constructor...
59 FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index,
60                     bool at_model, int at_model_index,
61                     double damp_roll, double damp_pitch, double damp_heading,
62                     double x_offset_m, double y_offset_m, double z_offset_m,
63                     double heading_offset_deg, double pitch_offset_deg,
64                     double roll_offset_deg,
65                     double fov_deg, double aspect_ratio_multiplier,
66                     double target_x_offset_m, double target_y_offset_m,
67                     double target_z_offset_m, double near_m, bool internal ):
68     _dirty(true),
69     _lon_deg(0),
70     _lat_deg(0),
71     _alt_ft(0),
72     _target_lon_deg(0),
73     _target_lat_deg(0),
74     _target_alt_ft(0),
75     _roll_deg(0),
76     _pitch_deg(0),
77     _heading_deg(0),
78     _damp_sync(0),
79     _damp_roll(0),
80     _damp_pitch(0),
81     _damp_heading(0),
82     _scaling_type(FG_SCALING_MAX),
83     _location(0),
84     _target_location(0)
85 {
86     _absolute_view_pos = SGVec3d(0, 0, 0);
87     _type = Type;
88     _from_model = from_model;
89     _from_model_index = from_model_index;
90     _at_model = at_model;
91     _at_model_index = at_model_index;
92
93     _internal = internal;
94
95     if (damp_roll > 0.0)
96       _damp_roll = 1.0 / pow(10.0, fabs(damp_roll));
97     if (damp_pitch > 0.0)
98       _damp_pitch = 1.0 / pow(10.0, fabs(damp_pitch));
99     if (damp_heading > 0.0)
100       _damp_heading = 1.0 / pow(10.0, fabs(damp_heading));
101
102     _offset_m.x() = x_offset_m;
103     _offset_m.y() = y_offset_m;
104     _offset_m.z() = z_offset_m;
105     _heading_offset_deg = heading_offset_deg;
106     _pitch_offset_deg = pitch_offset_deg;
107     _roll_offset_deg = roll_offset_deg;
108     _goal_heading_offset_deg = heading_offset_deg;
109     _goal_pitch_offset_deg = pitch_offset_deg;
110     _goal_roll_offset_deg = roll_offset_deg;
111     if (fov_deg > 0) {
112       _fov_deg = fov_deg;
113     } else {
114       _fov_deg = 55;
115     }
116     _aspect_ratio_multiplier = aspect_ratio_multiplier;
117     _target_offset_m.x() = target_x_offset_m;
118     _target_offset_m.y() = target_y_offset_m;
119     _target_offset_m.z() = target_z_offset_m;
120     _ground_level_nearplane_m = near_m;
121     // a reasonable guess for init, so that the math doesn't blow up
122 }
123
124
125 // Destructor
126 FGViewer::~FGViewer( void ) {
127 }
128
129 void
130 FGViewer::init ()
131 {
132   if ( _from_model )
133     _location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
134   else
135     _location = new SGLocation;
136
137   if ( _type == FG_LOOKAT ) {
138     if ( _at_model )
139       _target_location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
140     else
141       _target_location = (SGLocation *) new SGLocation;
142   }
143 }
144
145 void
146 FGViewer::bind ()
147 {
148 }
149
150 void
151 FGViewer::unbind ()
152 {
153 }
154
155 void
156 FGViewer::setType ( int type )
157 {
158   if (type == 0)
159     _type = FG_LOOKFROM;
160   if (type == 1)
161     _type = FG_LOOKAT;
162 }
163
164 void
165 FGViewer::setInternal ( bool internal )
166 {
167   _internal = internal;
168 }
169
170 void
171 FGViewer::setLongitude_deg (double lon_deg)
172 {
173   _dirty = true;
174   _lon_deg = lon_deg;
175 }
176
177 void
178 FGViewer::setLatitude_deg (double lat_deg)
179 {
180   _dirty = true;
181   _lat_deg = lat_deg;
182 }
183
184 void
185 FGViewer::setAltitude_ft (double alt_ft)
186 {
187   _dirty = true;
188   _alt_ft = alt_ft;
189 }
190
191 void
192 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
193 {
194   _dirty = true;
195   _lon_deg = lon_deg;
196   _lat_deg = lat_deg;
197   _alt_ft = alt_ft;
198 }
199
200 void
201 FGViewer::setTargetLongitude_deg (double lon_deg)
202 {
203   _dirty = true;
204   _target_lon_deg = lon_deg;
205 }
206
207 void
208 FGViewer::setTargetLatitude_deg (double lat_deg)
209 {
210   _dirty = true;
211   _target_lat_deg = lat_deg;
212 }
213
214 void
215 FGViewer::setTargetAltitude_ft (double alt_ft)
216 {
217   _dirty = true;
218   _target_alt_ft = alt_ft;
219 }
220
221 void
222 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
223 {
224   _dirty = true;
225   _target_lon_deg = lon_deg;
226   _target_lat_deg = lat_deg;
227   _target_alt_ft = alt_ft;
228 }
229
230 void
231 FGViewer::setRoll_deg (double roll_deg)
232 {
233   _dirty = true;
234   _roll_deg = roll_deg;
235 }
236
237 void
238 FGViewer::setPitch_deg (double pitch_deg)
239 {
240   _dirty = true;
241   _pitch_deg = pitch_deg;
242 }
243
244 void
245 FGViewer::setHeading_deg (double heading_deg)
246 {
247   _dirty = true;
248   _heading_deg = heading_deg;
249 }
250
251 void
252 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
253 {
254   _dirty = true;
255   _roll_deg = roll_deg;
256   _pitch_deg = pitch_deg;
257   _heading_deg = heading_deg;
258 }
259
260 void
261 FGViewer::setTargetRoll_deg (double target_roll_deg)
262 {
263   _dirty = true;
264   _target_roll_deg = target_roll_deg;
265 }
266
267 void
268 FGViewer::setTargetPitch_deg (double target_pitch_deg)
269 {
270   _dirty = true;
271   _target_pitch_deg = target_pitch_deg;
272 }
273
274 void
275 FGViewer::setTargetHeading_deg (double target_heading_deg)
276 {
277   _dirty = true;
278   _target_heading_deg = target_heading_deg;
279 }
280
281 void
282 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
283 {
284   _dirty = true;
285   _target_roll_deg = target_roll_deg;
286   _target_pitch_deg = target_pitch_deg;
287   _target_heading_deg = target_heading_deg;
288 }
289
290 void
291 FGViewer::setXOffset_m (double x_offset_m)
292 {
293   _dirty = true;
294   _offset_m.x() = x_offset_m;
295 }
296
297 void
298 FGViewer::setYOffset_m (double y_offset_m)
299 {
300   _dirty = true;
301   _offset_m.y() = y_offset_m;
302 }
303
304 void
305 FGViewer::setZOffset_m (double z_offset_m)
306 {
307   _dirty = true;
308   _offset_m.z() = z_offset_m;
309 }
310
311 void
312 FGViewer::setTargetXOffset_m (double target_x_offset_m)
313 {
314   _dirty = true;
315   _target_offset_m.x() = target_x_offset_m;
316 }
317
318 void
319 FGViewer::setTargetYOffset_m (double target_y_offset_m)
320 {
321   _dirty = true;
322   _target_offset_m.y() = target_y_offset_m;
323 }
324
325 void
326 FGViewer::setTargetZOffset_m (double target_z_offset_m)
327 {
328   _dirty = true;
329   _target_offset_m.z() = target_z_offset_m;
330 }
331
332 void
333 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
334 {
335   _dirty = true;
336   _offset_m.x() = x_offset_m;
337   _offset_m.y() = y_offset_m;
338   _offset_m.z() = z_offset_m;
339 }
340
341 void
342 FGViewer::setRollOffset_deg (double roll_offset_deg)
343 {
344   _dirty = true;
345   _roll_offset_deg = roll_offset_deg;
346 }
347
348 void
349 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
350 {
351   _dirty = true;
352   _pitch_offset_deg = pitch_offset_deg;
353 }
354
355 void
356 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
357 {
358   _dirty = true;
359   _heading_offset_deg = heading_offset_deg;
360 }
361
362 void
363 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
364 {
365   _dirty = true;
366   _goal_roll_offset_deg = goal_roll_offset_deg;
367 }
368
369 void
370 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
371 {
372   _dirty = true;
373   _goal_pitch_offset_deg = goal_pitch_offset_deg;
374   if ( _goal_pitch_offset_deg < -90 ) {
375     _goal_pitch_offset_deg = -90.0;
376   }
377   if ( _goal_pitch_offset_deg > 90.0 ) {
378     _goal_pitch_offset_deg = 90.0;
379   }
380
381 }
382
383 void
384 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
385 {
386   _dirty = true;
387   _goal_heading_offset_deg = goal_heading_offset_deg;
388   while ( _goal_heading_offset_deg < 0.0 ) {
389     _goal_heading_offset_deg += 360;
390   }
391   while ( _goal_heading_offset_deg > 360 ) {
392     _goal_heading_offset_deg -= 360;
393   }
394 }
395
396 void
397 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
398 {
399   _dirty = true;
400   _roll_offset_deg = roll_offset_deg;
401   _pitch_offset_deg = pitch_offset_deg;
402   _heading_offset_deg = heading_offset_deg;
403 }
404
405 // recalc() is done every time one of the setters is called (making the 
406 // cached data "dirty") on the next "get".  It calculates all the outputs 
407 // for viewer.
408 void
409 FGViewer::recalc ()
410 {
411   if (_type == FG_LOOKFROM) {
412     recalcLookFrom();
413   } else {
414     recalcLookAt();
415   }
416
417   SGGeod geodEyePoint = SGGeod::fromCart(_absolute_view_pos);
418   geodEyePoint.setElevationM(0);
419   _zero_elev = SGVec3d::fromGeod(geodEyePoint);
420   
421   SGQuatd hlOr = SGQuatd::fromLonLat(geodEyePoint);
422   _surface_south = toVec3f(hlOr.backTransform(-SGVec3d::e1()));
423   _surface_east = toVec3f(hlOr.backTransform(SGVec3d::e2()));
424   _world_up = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
425
426   // Update viewer's postion data for the eye location...
427   _lon_deg = _location->getLongitude_deg();
428   _lat_deg = _location->getLatitude_deg();
429   _alt_ft = _location->getAltitudeASL_ft();
430   _roll_deg = _location->getRoll_deg();
431   _pitch_deg = _location->getPitch_deg();
432   _heading_deg = _location->getHeading_deg();
433
434   // Update viewer's postion data for the target (at object) location
435   if (_type == FG_LOOKAT) {
436     _target_lon_deg = _target_location->getLongitude_deg();
437     _target_lat_deg = _target_location->getLatitude_deg();
438     _target_alt_ft = _target_location->getAltitudeASL_ft();
439     _target_roll_deg = _target_location->getRoll_deg();
440     _target_pitch_deg = _target_location->getPitch_deg();
441     _target_heading_deg = _target_location->getHeading_deg();
442   }
443
444   set_clean();
445 }
446
447 // recalculate for LookFrom view type...
448 void
449 FGViewer::recalcLookFrom ()
450 {
451   // Update location data ...
452   if ( !_from_model ) {
453     _location->setPosition( _lon_deg, _lat_deg, _alt_ft );
454     _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
455     _location->getTransformMatrix();
456   }
457   double lat = _location->getLatitude_deg();
458   double lon = _location->getLongitude_deg();
459   double alt = _location->getAltitudeASL_ft();
460   double head = _location->getHeading_deg();
461   double pitch = _location->getPitch_deg();
462   double roll = _location->getRoll_deg();
463   if ( !_from_model ) {
464     // update from our own data...
465     dampEyeData(roll, pitch, head);
466   }
467
468   // The geodetic position of our base view position
469   SGGeod geodPos = SGGeod::fromDegFt(lon, lat, alt);
470   // The rotation rotating from the earth centerd frame to
471   // the horizontal local OpenGL frame
472   SGQuatd hlOr = SGQuatd::viewHL(geodPos);
473
474   // the rotation from the horizontal local frame to the basic view orientation
475   SGQuatd hlToBody = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
476   hlToBody = SGQuatd::simToView(hlToBody);
477
478   // The cartesian position of the basic view coordinate
479   SGVec3d position = SGVec3d::fromGeod(geodPos);
480   // the rotation offset, don't know why heading is negative here ...
481   SGQuatd viewOffsetOr = SGQuatd::simToView(
482     SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg, _pitch_offset_deg,
483                                  _roll_offset_deg));
484
485   // Compute the eyepoints orientation and position
486   // wrt the earth centered frame - that is global coorinates
487   SGQuatd ec2body = hlOr*hlToBody;
488   _absolute_view_pos = position + ec2body.backTransform(_offset_m);
489   mViewOrientation = ec2body*viewOffsetOr;
490 }
491
492 void
493 FGViewer::recalcLookAt ()
494 {
495   // The geodetic position of our target to look at
496   SGGeod geodTargetPos;
497   SGQuatd geodTargetOr;
498   if ( _at_model ) {
499     geodTargetPos = SGGeod::fromDegFt(_target_location->getLongitude_deg(),
500                                       _target_location->getLatitude_deg(),
501                                       _target_location->getAltitudeASL_ft());
502     double head = _target_location->getHeading_deg();
503     double pitch = _target_location->getPitch_deg();
504     double roll = _target_location->getRoll_deg();
505     geodTargetOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
506   } else {
507     dampEyeData(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
508     _target_location->setPosition( _target_lon_deg, _target_lat_deg, _target_alt_ft );
509     _target_location->setOrientation( _target_roll_deg, _target_pitch_deg, _target_heading_deg );
510     _target_location->getTransformMatrix();
511
512     // if not model then calculate our own target position...
513     geodTargetPos = SGGeod::fromDegFt(_target_lon_deg,
514                                       _target_lat_deg,
515                                       _target_alt_ft);
516     geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
517                                                 _target_pitch_deg,
518                                                 _target_roll_deg);
519   }
520   SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(geodTargetPos);
521
522
523   SGGeod geodEyePos;
524   SGQuatd geodEyeOr;
525   if ( _from_model ) {
526     geodEyePos = SGGeod::fromDegFt(_location->getLongitude_deg(),
527                                    _location->getLatitude_deg(),
528                                    _location->getAltitudeASL_ft());
529     double head = _location->getHeading_deg();
530     double pitch = _location->getPitch_deg();
531     double roll = _location->getRoll_deg();
532     geodEyeOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
533   } else {
534     dampEyeData(_roll_deg, _pitch_deg, _heading_deg);
535     _location->setPosition( _lon_deg, _lat_deg, _alt_ft );
536     _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
537     _location->getTransformMatrix();
538
539     // update from our own data, just the rotation here...
540     geodEyePos = SGGeod::fromDegFt(_lon_deg, _lat_deg, _alt_ft);
541     geodEyeOr = SGQuatd::fromYawPitchRollDeg(_heading_deg,
542                                              _pitch_deg,
543                                              _roll_deg);
544   }
545   SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(geodEyePos);
546
547   // the rotation offset, don't know why heading is negative here ...
548   SGQuatd eyeOffsetOr =
549     SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg + 180, _pitch_offset_deg,
550                                  _roll_offset_deg);
551
552   // Offsets to the eye position
553   SGVec3d eyeOff(-_offset_m.z(), _offset_m.y(), -_offset_m.x());
554   SGQuatd ec2eye = geodEyeHlOr*geodEyeOr;
555   SGVec3d eyeCart = SGVec3d::fromGeod(geodEyePos);
556   eyeCart += (ec2eye*eyeOffsetOr).backTransform(eyeOff);
557
558   SGVec3d atCart = SGVec3d::fromGeod(geodTargetPos);
559
560   // add target offsets to at_position...
561   SGVec3d target_pos_off(-_target_offset_m.z(), _target_offset_m.x(),
562                          -_target_offset_m.y());
563   target_pos_off = (geodTargetHlOr*geodTargetOr).backTransform(target_pos_off);
564   atCart += target_pos_off;
565   eyeCart += target_pos_off;
566
567   // Compute the eyepoints orientation and position
568   // wrt the earth centered frame - that is global coorinates
569   _absolute_view_pos = eyeCart;
570
571   // the view direction
572   SGVec3d dir = normalize(atCart - eyeCart);
573   // the up directon
574   SGVec3d up = ec2eye.backTransform(SGVec3d(0, 0, -1));
575   // rotate dir to the 0-th unit vector
576   // rotate up to 2-th unit vector
577   mViewOrientation = SGQuatd::fromRotateTo(-dir, 2, up, 1);
578 }
579
580 void
581 FGViewer::dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg)
582 {
583   const double interval = 0.01;
584
585   static FGViewer *last_view = 0;
586   if (last_view != this) {
587     _damp_sync = 0.0;
588     _damped_roll_deg = roll_deg;
589     _damped_pitch_deg = pitch_deg;
590     _damped_heading_deg = heading_deg;
591     last_view = this;
592     return;
593   }
594
595   if (_damp_sync < interval) {
596     if (_damp_roll > 0.0)
597       roll_deg = _damped_roll_deg;
598     if (_damp_pitch > 0.0)
599       pitch_deg = _damped_pitch_deg;
600     if (_damp_heading > 0.0)
601       heading_deg = _damped_heading_deg;
602     return;
603   }
604
605   while (_damp_sync >= interval) {
606     _damp_sync -= interval;
607
608     double d;
609     if (_damp_roll > 0.0) {
610       d = _damped_roll_deg - roll_deg;
611       if (d >= 180.0)
612         _damped_roll_deg -= 360.0;
613       else if (d < -180.0)
614         _damped_roll_deg += 360.0;
615       roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
616     }
617
618     if (_damp_pitch > 0.0) {
619       d = _damped_pitch_deg - pitch_deg;
620       if (d >= 180.0)
621         _damped_pitch_deg -= 360.0;
622       else if (d < -180.0)
623         _damped_pitch_deg += 360.0;
624       pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
625     }
626
627     if (_damp_heading > 0.0) {
628       d = _damped_heading_deg - heading_deg;
629       if (d >= 180.0)
630         _damped_heading_deg -= 360.0;
631       else if (d < -180.0)
632         _damped_heading_deg += 360.0;
633       heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
634     }
635   }
636 }
637
638 double
639 FGViewer::get_h_fov()
640 {
641     switch (_scaling_type) {
642     case FG_SCALING_WIDTH:  // h_fov == fov
643         return _fov_deg;
644     case FG_SCALING_MAX:
645         if (_aspect_ratio < 1.0) {
646             // h_fov == fov
647             return _fov_deg;
648         } else {
649             // v_fov == fov
650             return
651                 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
652                      / (_aspect_ratio*_aspect_ratio_multiplier))
653                 * SG_RADIANS_TO_DEGREES * 2;
654         }
655     default:
656         assert(false);
657     }
658     return 0.0;
659 }
660
661
662
663 double
664 FGViewer::get_v_fov()
665 {
666     switch (_scaling_type) {
667     case FG_SCALING_WIDTH:  // h_fov == fov
668         return 
669             atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
670                  * (_aspect_ratio*_aspect_ratio_multiplier))
671             * SG_RADIANS_TO_DEGREES * 2;
672     case FG_SCALING_MAX:
673         if (_aspect_ratio < 1.0) {
674             // h_fov == fov
675             return
676                 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
677                      * (_aspect_ratio*_aspect_ratio_multiplier))
678                 * SG_RADIANS_TO_DEGREES * 2;
679         } else {
680             // v_fov == fov
681             return _fov_deg;
682         }
683     default:
684         assert(false);
685     }
686     return 0.0;
687 }
688
689 void
690 FGViewer::update (double dt)
691 {
692   _damp_sync += dt;
693
694   int i;
695   int dt_ms = int(dt * 1000);
696   for ( i = 0; i < dt_ms; i++ ) {
697     if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
698       setHeadingOffset_deg( _goal_heading_offset_deg );
699       break;
700     } else {
701       // move current_view.headingoffset towards
702       // current_view.goal_view_offset
703       if ( _goal_heading_offset_deg > _heading_offset_deg )
704         {
705           if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
706             incHeadingOffset_deg( 0.5 );
707           } else {
708             incHeadingOffset_deg( -0.5 );
709           }
710         } else {
711           if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
712             incHeadingOffset_deg( -0.5 );
713           } else {
714             incHeadingOffset_deg( 0.5 );
715           }
716         }
717       if ( _heading_offset_deg > 360 ) {
718         incHeadingOffset_deg( -360 );
719       } else if ( _heading_offset_deg < 0 ) {
720         incHeadingOffset_deg( 360 );
721       }
722     }
723   }
724
725   for ( i = 0; i < dt_ms; i++ ) {
726     if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
727       setPitchOffset_deg( _goal_pitch_offset_deg );
728       break;
729     } else {
730       // move current_view.pitch_offset_deg towards
731       // current_view.goal_pitch_offset
732       if ( _goal_pitch_offset_deg > _pitch_offset_deg )
733         {
734           incPitchOffset_deg( 1.0 );
735         } else {
736             incPitchOffset_deg( -1.0 );
737         }
738       if ( _pitch_offset_deg > 90 ) {
739         setPitchOffset_deg(90);
740       } else if ( _pitch_offset_deg < -90 ) {
741         setPitchOffset_deg( -90 );
742       }
743     }
744   }
745
746
747   for ( i = 0; i < dt_ms; i++ ) {
748     if ( fabs( _goal_roll_offset_deg - _roll_offset_deg ) < 1 ) {
749       setRollOffset_deg( _goal_roll_offset_deg );
750       break;
751     } else {
752       // move current_view.roll_offset_deg towards
753       // current_view.goal_roll_offset
754       if ( _goal_roll_offset_deg > _roll_offset_deg )
755         {
756           incRollOffset_deg( 1.0 );
757         } else {
758             incRollOffset_deg( -1.0 );
759         }
760       if ( _roll_offset_deg > 90 ) {
761         setRollOffset_deg(90);
762       } else if ( _roll_offset_deg < -90 ) {
763         setRollOffset_deg( -90 );
764       }
765     }
766   }
767
768 }