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