]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.cxx
initlialize _playing for FGATC. Proper listerner orientation based on view offset...
[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 #include <simgear/debug/logstream.hxx>
35 #include <simgear/constants.h>
36 #include <simgear/scene/model/placement.hxx>
37 #include <simgear/math/vector.hxx>
38
39 #include <Main/globals.hxx>
40 #include <Scenery/scenery.hxx>
41 #include <Model/acmodel.hxx>
42
43 #include "viewer.hxx"
44
45 #include "CameraGroup.hxx"
46
47 using namespace flightgear;
48 \f
49 ////////////////////////////////////////////////////////////////////////
50 // Implementation of FGViewer.
51 ////////////////////////////////////////////////////////////////////////
52
53 // Constructor...
54 FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index,
55                     bool at_model, int at_model_index,
56                     double damp_roll, double damp_pitch, double damp_heading,
57                     double x_offset_m, double y_offset_m, double z_offset_m,
58                     double heading_offset_deg, double pitch_offset_deg,
59                     double roll_offset_deg,
60                     double fov_deg, double aspect_ratio_multiplier,
61                     double target_x_offset_m, double target_y_offset_m,
62                     double target_z_offset_m, double near_m, bool internal ):
63     _dirty(true),
64     _roll_deg(0),
65     _pitch_deg(0),
66     _heading_deg(0),
67     _damp_sync(0),
68     _damp_roll(0),
69     _damp_pitch(0),
70     _damp_heading(0),
71     _scaling_type(FG_SCALING_MAX),
72     _aspect_ratio(0),
73     _cameraGroup(CameraGroup::getDefault())
74 {
75     _absolute_view_pos = SGVec3d(0, 0, 0);
76     _type = Type;
77     _from_model = from_model;
78     _from_model_index = from_model_index;
79     _at_model = at_model;
80     _at_model_index = at_model_index;
81
82     _internal = internal;
83
84     if (damp_roll > 0.0)
85       _damp_roll = 1.0 / pow(10.0, fabs(damp_roll));
86     if (damp_pitch > 0.0)
87       _damp_pitch = 1.0 / pow(10.0, fabs(damp_pitch));
88     if (damp_heading > 0.0)
89       _damp_heading = 1.0 / pow(10.0, fabs(damp_heading));
90
91     _offset_m.x() = x_offset_m;
92     _offset_m.y() = y_offset_m;
93     _offset_m.z() = z_offset_m;
94     _heading_offset_deg = heading_offset_deg;
95     _pitch_offset_deg = pitch_offset_deg;
96     _roll_offset_deg = roll_offset_deg;
97     _goal_heading_offset_deg = heading_offset_deg;
98     _goal_pitch_offset_deg = pitch_offset_deg;
99     _goal_roll_offset_deg = roll_offset_deg;
100     if (fov_deg > 0) {
101       _fov_deg = fov_deg;
102     } else {
103       _fov_deg = 55;
104     }
105     _aspect_ratio = 1;
106     _aspect_ratio_multiplier = aspect_ratio_multiplier;
107     _target_offset_m.x() = target_x_offset_m;
108     _target_offset_m.y() = target_y_offset_m;
109     _target_offset_m.z() = target_z_offset_m;
110     _ground_level_nearplane_m = near_m;
111     // a reasonable guess for init, so that the math doesn't blow up
112 }
113
114
115 // Destructor
116 FGViewer::~FGViewer( void ) {
117 }
118
119 void
120 FGViewer::init ()
121 {
122 }
123
124 void
125 FGViewer::bind ()
126 {
127 }
128
129 void
130 FGViewer::unbind ()
131 {
132 }
133
134 void
135 FGViewer::setType ( int type )
136 {
137   if (type == 0)
138     _type = FG_LOOKFROM;
139   if (type == 1)
140     _type = FG_LOOKAT;
141 }
142
143 void
144 FGViewer::setInternal ( bool internal )
145 {
146   _internal = internal;
147 }
148
149 void
150 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
151 {
152   _dirty = true;
153   _position = SGGeod::fromDegFt(lon_deg, lat_deg, alt_ft);
154 }
155
156 void
157 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
158 {
159   _dirty = true;
160   _target = SGGeod::fromDegFt(lon_deg, lat_deg, alt_ft);
161 }
162
163 void
164 FGViewer::setRoll_deg (double roll_deg)
165 {
166   _dirty = true;
167   _roll_deg = roll_deg;
168 }
169
170 void
171 FGViewer::setPitch_deg (double pitch_deg)
172 {
173   _dirty = true;
174   _pitch_deg = pitch_deg;
175 }
176
177 void
178 FGViewer::setHeading_deg (double heading_deg)
179 {
180   _dirty = true;
181   _heading_deg = heading_deg;
182 }
183
184 void
185 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
186 {
187   _dirty = true;
188   _roll_deg = roll_deg;
189   _pitch_deg = pitch_deg;
190   _heading_deg = heading_deg;
191 }
192
193 void
194 FGViewer::setTargetRoll_deg (double target_roll_deg)
195 {
196   _dirty = true;
197   _target_roll_deg = target_roll_deg;
198 }
199
200 void
201 FGViewer::setTargetPitch_deg (double target_pitch_deg)
202 {
203   _dirty = true;
204   _target_pitch_deg = target_pitch_deg;
205 }
206
207 void
208 FGViewer::setTargetHeading_deg (double target_heading_deg)
209 {
210   _dirty = true;
211   _target_heading_deg = target_heading_deg;
212 }
213
214 void
215 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
216 {
217   _dirty = true;
218   _target_roll_deg = target_roll_deg;
219   _target_pitch_deg = target_pitch_deg;
220   _target_heading_deg = target_heading_deg;
221 }
222
223 void
224 FGViewer::setXOffset_m (double x_offset_m)
225 {
226   _dirty = true;
227   _offset_m.x() = x_offset_m;
228 }
229
230 void
231 FGViewer::setYOffset_m (double y_offset_m)
232 {
233   _dirty = true;
234   _offset_m.y() = y_offset_m;
235 }
236
237 void
238 FGViewer::setZOffset_m (double z_offset_m)
239 {
240   _dirty = true;
241   _offset_m.z() = z_offset_m;
242 }
243
244 void
245 FGViewer::setTargetXOffset_m (double target_x_offset_m)
246 {
247   _dirty = true;
248   _target_offset_m.x() = target_x_offset_m;
249 }
250
251 void
252 FGViewer::setTargetYOffset_m (double target_y_offset_m)
253 {
254   _dirty = true;
255   _target_offset_m.y() = target_y_offset_m;
256 }
257
258 void
259 FGViewer::setTargetZOffset_m (double target_z_offset_m)
260 {
261   _dirty = true;
262   _target_offset_m.z() = target_z_offset_m;
263 }
264
265 void
266 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
267 {
268   _dirty = true;
269   _offset_m.x() = x_offset_m;
270   _offset_m.y() = y_offset_m;
271   _offset_m.z() = z_offset_m;
272 }
273
274 void
275 FGViewer::setRollOffset_deg (double roll_offset_deg)
276 {
277   _dirty = true;
278   _roll_offset_deg = roll_offset_deg;
279 }
280
281 void
282 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
283 {
284   _dirty = true;
285   _pitch_offset_deg = pitch_offset_deg;
286 }
287
288 void
289 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
290 {
291   _dirty = true;
292   _heading_offset_deg = heading_offset_deg;
293 }
294
295 void
296 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
297 {
298   _dirty = true;
299   _goal_roll_offset_deg = goal_roll_offset_deg;
300 }
301
302 void
303 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
304 {
305   _dirty = true;
306   _goal_pitch_offset_deg = goal_pitch_offset_deg;
307   if ( _goal_pitch_offset_deg < -90 ) {
308     _goal_pitch_offset_deg = -90.0;
309   }
310   if ( _goal_pitch_offset_deg > 90.0 ) {
311     _goal_pitch_offset_deg = 90.0;
312   }
313
314 }
315
316 void
317 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
318 {
319   _dirty = true;
320   _goal_heading_offset_deg = goal_heading_offset_deg;
321   while ( _goal_heading_offset_deg < 0.0 ) {
322     _goal_heading_offset_deg += 360;
323   }
324   while ( _goal_heading_offset_deg > 360 ) {
325     _goal_heading_offset_deg -= 360;
326   }
327 }
328
329 void
330 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
331 {
332   _dirty = true;
333   _roll_offset_deg = roll_offset_deg;
334   _pitch_offset_deg = pitch_offset_deg;
335   _heading_offset_deg = heading_offset_deg;
336 }
337
338 // recalc() is done every time one of the setters is called (making the 
339 // cached data "dirty") on the next "get".  It calculates all the outputs 
340 // for viewer.
341 void
342 FGViewer::recalc ()
343 {
344   if (_type == FG_LOOKFROM) {
345     recalcLookFrom();
346   } else {
347     recalcLookAt();
348   }
349
350   set_clean();
351 }
352
353 // recalculate for LookFrom view type...
354 void
355 FGViewer::recalcLookFrom ()
356 {
357   // Update location data ...
358   if ( _from_model ) {
359     SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
360     _position = placement->getPosition();
361   
362     _heading_deg = placement->getHeadingDeg();
363     _pitch_deg = placement->getPitchDeg();
364     _roll_deg = placement->getRollDeg();
365   }
366
367   double head = _heading_deg;
368   double pitch = _pitch_deg;
369   double roll = _roll_deg;
370   if ( !_from_model ) {
371     // update from our own data...
372     dampEyeData(roll, pitch, head);
373   }
374
375   // The rotation rotating from the earth centerd frame to
376   // the horizontal local frame
377   SGQuatd hlOr = SGQuatd::fromLonLat(_position);
378
379   // The rotation from the horizontal local frame to the basic view orientation
380   SGQuatd hlToBody = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
381
382   // The rotation offset, don't know why heading is negative here ...
383   mViewOffsetOr
384       = SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg, _pitch_offset_deg,
385                                      _roll_offset_deg);
386
387   // Compute the eyepoints orientation and position
388   // wrt the earth centered frame - that is global coorinates
389   SGQuatd ec2body = hlOr*hlToBody;
390
391   // The cartesian position of the basic view coordinate
392   SGVec3d position = SGVec3d::fromGeod(_position);
393
394   // This is rotates the x-forward, y-right, z-down coordinate system the where
395   // simulation runs into the OpenGL camera system with x-right, y-up, z-back.
396   SGQuatd q(-0.5, -0.5, 0.5, 0.5);
397
398   _absolute_view_pos = position + (ec2body*q).backTransform(_offset_m);
399   mViewOrientation = ec2body*mViewOffsetOr*q;
400 }
401
402 void
403 FGViewer::recalcLookAt ()
404 {
405   // The geodetic position of our target to look at
406   if ( _at_model ) {
407     SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
408     _target = placement->getPosition();
409     _target_heading_deg = placement->getHeadingDeg();
410     _target_pitch_deg = placement->getPitchDeg();
411     _target_roll_deg = placement->getRollDeg();
412   } else {
413     // if not model then calculate our own target position...
414     dampEyeData(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
415
416   }
417
418   SGQuatd geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
419                                                       _target_pitch_deg,
420                                                       _target_roll_deg);
421   SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(_target);
422
423
424   if ( _from_model ) {
425     SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
426     _position = placement->getPosition();
427     _heading_deg = placement->getHeadingDeg();
428     _pitch_deg = placement->getPitchDeg();
429     _roll_deg = placement->getRollDeg();
430   } else {
431     // update from our own data, just the rotation here...
432     dampEyeData(_roll_deg, _pitch_deg, _heading_deg);
433   }
434   SGQuatd geodEyeOr = SGQuatd::fromYawPitchRollDeg(_heading_deg,
435                                                    _pitch_deg,
436                                                    _roll_deg);
437   SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(_position);
438
439   // the rotation offset, don't know why heading is negative here ...
440   mViewOffsetOr =
441     SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg + 180, _pitch_offset_deg,
442                                  _roll_offset_deg);
443
444   // Offsets to the eye position
445   SGVec3d eyeOff(-_offset_m.z(), _offset_m.x(), -_offset_m.y());
446   SGQuatd ec2eye = geodEyeHlOr*geodEyeOr;
447   SGVec3d eyeCart = SGVec3d::fromGeod(_position);
448   eyeCart += (ec2eye*mViewOffsetOr).backTransform(eyeOff);
449
450   SGVec3d atCart = SGVec3d::fromGeod(_target);
451
452   // add target offsets to at_position...
453   SGVec3d target_pos_off(-_target_offset_m.z(), _target_offset_m.x(),
454                          -_target_offset_m.y());
455   target_pos_off = (geodTargetHlOr*geodTargetOr).backTransform(target_pos_off);
456   atCart += target_pos_off;
457   eyeCart += target_pos_off;
458
459   // Compute the eyepoints orientation and position
460   // wrt the earth centered frame - that is global coorinates
461   _absolute_view_pos = eyeCart;
462
463   // the view direction
464   SGVec3d dir = normalize(atCart - eyeCart);
465   // the up directon
466   SGVec3d up = ec2eye.backTransform(SGVec3d(0, 0, -1));
467   // rotate -dir to the 2-th unit vector
468   // rotate up to 1-th unit vector
469   // Note that this matches the OpenGL camera coordinate system
470   // with x-right, y-up, z-back.
471   mViewOrientation = SGQuatd::fromRotateTo(-dir, 2, up, 1);
472 }
473
474 void
475 FGViewer::dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg)
476 {
477   const double interval = 0.01;
478
479   static FGViewer *last_view = 0;
480   if (last_view != this) {
481     _damp_sync = 0.0;
482     _damped_roll_deg = roll_deg;
483     _damped_pitch_deg = pitch_deg;
484     _damped_heading_deg = heading_deg;
485     last_view = this;
486     return;
487   }
488
489   if (_damp_sync < interval) {
490     if (_damp_roll > 0.0)
491       roll_deg = _damped_roll_deg;
492     if (_damp_pitch > 0.0)
493       pitch_deg = _damped_pitch_deg;
494     if (_damp_heading > 0.0)
495       heading_deg = _damped_heading_deg;
496     return;
497   }
498
499   while (_damp_sync >= interval) {
500     _damp_sync -= interval;
501
502     double d;
503     if (_damp_roll > 0.0) {
504       d = _damped_roll_deg - roll_deg;
505       if (d >= 180.0)
506         _damped_roll_deg -= 360.0;
507       else if (d < -180.0)
508         _damped_roll_deg += 360.0;
509       roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
510     }
511
512     if (_damp_pitch > 0.0) {
513       d = _damped_pitch_deg - pitch_deg;
514       if (d >= 180.0)
515         _damped_pitch_deg -= 360.0;
516       else if (d < -180.0)
517         _damped_pitch_deg += 360.0;
518       pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
519     }
520
521     if (_damp_heading > 0.0) {
522       d = _damped_heading_deg - heading_deg;
523       if (d >= 180.0)
524         _damped_heading_deg -= 360.0;
525       else if (d < -180.0)
526         _damped_heading_deg += 360.0;
527       heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
528     }
529   }
530 }
531
532 double
533 FGViewer::get_h_fov()
534 {
535     switch (_scaling_type) {
536     case FG_SCALING_WIDTH:  // h_fov == fov
537         return _fov_deg;
538     case FG_SCALING_MAX:
539         if (_aspect_ratio < 1.0) {
540             // h_fov == fov
541             return _fov_deg;
542         } else {
543             // v_fov == fov
544             return
545                 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
546                      / (_aspect_ratio*_aspect_ratio_multiplier))
547                 * SG_RADIANS_TO_DEGREES * 2;
548         }
549     default:
550         assert(false);
551     }
552     return 0.0;
553 }
554
555
556
557 double
558 FGViewer::get_v_fov()
559 {
560     switch (_scaling_type) {
561     case FG_SCALING_WIDTH:  // h_fov == fov
562         return 
563             atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
564                  * (_aspect_ratio*_aspect_ratio_multiplier))
565             * SG_RADIANS_TO_DEGREES * 2;
566     case FG_SCALING_MAX:
567         if (_aspect_ratio < 1.0) {
568             // h_fov == fov
569             return
570                 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
571                      * (_aspect_ratio*_aspect_ratio_multiplier))
572                 * SG_RADIANS_TO_DEGREES * 2;
573         } else {
574             // v_fov == fov
575             return _fov_deg;
576         }
577     default:
578         assert(false);
579     }
580     return 0.0;
581 }
582
583 void
584 FGViewer::update (double dt)
585 {
586   _damp_sync += dt;
587
588   int i;
589   int dt_ms = int(dt * 1000);
590   for ( i = 0; i < dt_ms; i++ ) {
591     if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
592       setHeadingOffset_deg( _goal_heading_offset_deg );
593       break;
594     } else {
595       // move current_view.headingoffset towards
596       // current_view.goal_view_offset
597       if ( _goal_heading_offset_deg > _heading_offset_deg )
598         {
599           if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
600             incHeadingOffset_deg( 0.5 );
601           } else {
602             incHeadingOffset_deg( -0.5 );
603           }
604         } else {
605           if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
606             incHeadingOffset_deg( -0.5 );
607           } else {
608             incHeadingOffset_deg( 0.5 );
609           }
610         }
611       if ( _heading_offset_deg > 360 ) {
612         incHeadingOffset_deg( -360 );
613       } else if ( _heading_offset_deg < 0 ) {
614         incHeadingOffset_deg( 360 );
615       }
616     }
617   }
618
619   for ( i = 0; i < dt_ms; i++ ) {
620     if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
621       setPitchOffset_deg( _goal_pitch_offset_deg );
622       break;
623     } else {
624       // move current_view.pitch_offset_deg towards
625       // current_view.goal_pitch_offset
626       if ( _goal_pitch_offset_deg > _pitch_offset_deg )
627         {
628           incPitchOffset_deg( 1.0 );
629         } else {
630             incPitchOffset_deg( -1.0 );
631         }
632       if ( _pitch_offset_deg > 90 ) {
633         setPitchOffset_deg(90);
634       } else if ( _pitch_offset_deg < -90 ) {
635         setPitchOffset_deg( -90 );
636       }
637     }
638   }
639
640
641   for ( i = 0; i < dt_ms; i++ ) {
642     if ( fabs( _goal_roll_offset_deg - _roll_offset_deg ) < 1 ) {
643       setRollOffset_deg( _goal_roll_offset_deg );
644       break;
645     } else {
646       // move current_view.roll_offset_deg towards
647       // current_view.goal_roll_offset
648       if ( _goal_roll_offset_deg > _roll_offset_deg )
649         {
650           incRollOffset_deg( 1.0 );
651         } else {
652             incRollOffset_deg( -1.0 );
653         }
654       if ( _roll_offset_deg > 90 ) {
655         setRollOffset_deg(90);
656       } else if ( _roll_offset_deg < -90 ) {
657         setRollOffset_deg( -90 );
658       }
659     }
660   }
661   recalc();
662   _cameraGroup->update(toOsg(_absolute_view_pos), toOsg(mViewOrientation));
663   _cameraGroup->setCameraParameters(get_v_fov(), get_aspect_ratio());
664 }