]> git.mxchange.org Git - flightgear.git/blob - src/Viewer/viewer.cxx
Kill off some globals.
[flightgear.git] / src / Viewer / 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 #include <cassert>
32
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/constants.h>
35 #include <simgear/scene/model/placement.hxx>
36 #include <simgear/scene/util/OsgMath.hxx>
37
38 #include <Main/fg_props.hxx>
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     _scaling_type(FG_SCALING_MAX),
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
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   if (_at_model && (_offset_m.x() == 0.0)&&(_offset_m.z() == 0.0))
292   {
293       /* avoid optical effects (e.g. rotating sky) when "looking at" with
294        * heading offsets x==z==0 (view heading cannot change). */
295       _heading_offset_deg = 0.0;
296   }
297   else
298       _heading_offset_deg = heading_offset_deg;
299 }
300
301 void
302 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
303 {
304   _dirty = true;
305   _goal_roll_offset_deg = goal_roll_offset_deg;
306 }
307
308 void
309 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
310 {
311   _dirty = true;
312   _goal_pitch_offset_deg = goal_pitch_offset_deg;
313   if ( _goal_pitch_offset_deg < -90 ) {
314     _goal_pitch_offset_deg = -90.0;
315   }
316   if ( _goal_pitch_offset_deg > 90.0 ) {
317     _goal_pitch_offset_deg = 90.0;
318   }
319
320 }
321
322 void
323 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
324 {
325   _dirty = true;
326   if (_at_model && (_offset_m.x() == 0.0)&&(_offset_m.z() == 0.0))
327   {
328       /* avoid optical effects (e.g. rotating sky) when "looking at" with
329        * heading offsets x==z==0 (view heading cannot change). */
330       _goal_heading_offset_deg = 0.0;
331       return;
332   }
333   
334   _goal_heading_offset_deg = goal_heading_offset_deg;
335   while ( _goal_heading_offset_deg < 0.0 ) {
336     _goal_heading_offset_deg += 360;
337   }
338   while ( _goal_heading_offset_deg > 360 ) {
339     _goal_heading_offset_deg -= 360;
340   }
341 }
342
343 void
344 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
345 {
346   _dirty = true;
347   _roll_offset_deg = roll_offset_deg;
348   _pitch_offset_deg = pitch_offset_deg;
349   _heading_offset_deg = heading_offset_deg;
350 }
351
352 // recalc() is done every time one of the setters is called (making the 
353 // cached data "dirty") on the next "get".  It calculates all the outputs 
354 // for viewer.
355 void
356 FGViewer::recalc ()
357 {
358   if (_type == FG_LOOKFROM) {
359     recalcLookFrom();
360   } else {
361     recalcLookAt();
362   }
363
364   set_clean();
365 }
366
367 // recalculate for LookFrom view type...
368 void
369 FGViewer::recalcLookFrom ()
370 {
371   // Update location data ...
372   if ( _from_model ) {
373     _position = globals->get_aircraft_position();
374     globals->get_aircraft_orientation(_heading_deg, _pitch_deg, _roll_deg);
375   }
376
377   double head = _heading_deg;
378   double pitch = _pitch_deg;
379   double roll = _roll_deg;
380   if ( !_from_model ) {
381     // update from our own data...
382     setDampTarget(roll, pitch, head);
383     getDampOutput(roll, pitch, head);
384   }
385
386   // The rotation rotating from the earth centerd frame to
387   // the horizontal local frame
388   SGQuatd hlOr = SGQuatd::fromLonLat(_position);
389
390   // The rotation from the horizontal local frame to the basic view orientation
391   SGQuatd hlToBody = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
392
393   // The rotation offset, don't know why heading is negative here ...
394   mViewOffsetOr
395       = SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg, _pitch_offset_deg,
396                                      _roll_offset_deg);
397
398   // Compute the eyepoints orientation and position
399   // wrt the earth centered frame - that is global coorinates
400   SGQuatd ec2body = hlOr*hlToBody;
401
402   // The cartesian position of the basic view coordinate
403   SGVec3d position = SGVec3d::fromGeod(_position);
404
405   // This is rotates the x-forward, y-right, z-down coordinate system the where
406   // simulation runs into the OpenGL camera system with x-right, y-up, z-back.
407   SGQuatd q(-0.5, -0.5, 0.5, 0.5);
408
409   _absolute_view_pos = position + (ec2body*q).backTransform(_offset_m);
410   mViewOrientation = ec2body*mViewOffsetOr*q;
411 }
412
413 void
414 FGViewer::recalcLookAt ()
415 {
416   // The geodetic position of our target to look at
417   if ( _at_model ) {
418     _target = globals->get_aircraft_position();
419     globals->get_aircraft_orientation(_target_heading_deg,
420                                       _target_pitch_deg,
421                                       _target_roll_deg);
422   } else {
423     // if not model then calculate our own target position...
424     setDampTarget(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
425     getDampOutput(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
426   }
427
428   SGQuatd geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
429                                                    _target_pitch_deg,
430                                                    _target_roll_deg);
431   SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(_target);
432
433
434   if ( _from_model ) {
435     _position = globals->get_aircraft_position();
436     globals->get_aircraft_orientation(_heading_deg, _pitch_deg, _roll_deg);
437   } else {
438     // update from our own data, just the rotation here...
439     setDampTarget(_roll_deg, _pitch_deg, _heading_deg);
440     getDampOutput(_roll_deg, _pitch_deg, _heading_deg);
441   }
442   SGQuatd geodEyeOr = SGQuatd::fromYawPitchRollDeg(_heading_deg, _pitch_deg, _roll_deg);
443   SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(_position);
444
445   // the rotation offset, don't know why heading is negative here ...
446   mViewOffsetOr =
447     SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg + 180, _pitch_offset_deg,
448                                  _roll_offset_deg);
449
450   // Offsets to the eye position
451   SGVec3d eyeOff(-_offset_m.z(), _offset_m.x(), -_offset_m.y());
452   SGQuatd ec2eye = geodEyeHlOr*geodEyeOr;
453   SGVec3d eyeCart = SGVec3d::fromGeod(_position);
454   eyeCart += (ec2eye*mViewOffsetOr).backTransform(eyeOff);
455
456   SGVec3d atCart = SGVec3d::fromGeod(_target);
457
458   // add target offsets to at_position...
459   SGVec3d target_pos_off(-_target_offset_m.z(), _target_offset_m.x(),
460                          -_target_offset_m.y());
461   target_pos_off = (geodTargetHlOr*geodTargetOr).backTransform(target_pos_off);
462   atCart += target_pos_off;
463   eyeCart += target_pos_off;
464
465   // Compute the eyepoints orientation and position
466   // wrt the earth centered frame - that is global coorinates
467   _absolute_view_pos = eyeCart;
468
469   // the view direction
470   SGVec3d dir = normalize(atCart - eyeCart);
471   // the up directon
472   SGVec3d up = ec2eye.backTransform(SGVec3d(0, 0, -1));
473   // rotate -dir to the 2-th unit vector
474   // rotate up to 1-th unit vector
475   // Note that this matches the OpenGL camera coordinate system
476   // with x-right, y-up, z-back.
477   mViewOrientation = SGQuatd::fromRotateTo(-dir, 2, up, 1);
478 }
479
480 void
481 FGViewer::setDampTarget(double roll, double pitch, double heading)
482 {
483   _dampTarget = SGVec3d(roll, pitch, heading);
484 }
485
486 void
487 FGViewer::getDampOutput(double& roll, double& pitch, double& heading)
488 {
489   roll = _dampOutput[0];
490   pitch = _dampOutput[1];
491   heading = _dampOutput[2];
492 }
493
494
495 void
496 FGViewer::updateDampOutput(double dt)
497 {
498   static FGViewer *last_view = 0;
499   if ((last_view != this) || (dt > 1.0)) {
500     _dampOutput = _dampTarget;
501     last_view = this;
502     return;
503   }
504   
505   const double interval = 0.01;
506   while (dt > interval) {
507     
508     for (unsigned int i=0; i<3; ++i) {
509       if (_dampFactor[i] <= 0.0) {
510         // axis is un-damped, set output to target directly
511         _dampOutput[i] = _dampTarget[i];
512         continue;
513       }
514       
515       double d = _dampOutput[i] - _dampTarget[i];
516       if (d > 180.0) {
517         _dampOutput[i] -= 360.0;
518       } else if (d < -180.0) {
519         _dampOutput[i] += 360.0;
520       }
521       
522       _dampOutput[i] = (_dampTarget[i] * _dampFactor[i]) + 
523         (_dampOutput[i] * (1.0 - _dampFactor[i]));
524     } // of axis iteration
525     
526     dt -= interval;
527   } // of dt subdivision by interval
528 }
529
530 double
531 FGViewer::get_h_fov()
532 {
533     double aspectRatio = _cameraGroup->getMasterAspectRatio();
534     switch (_scaling_type) {
535     case FG_SCALING_WIDTH:  // h_fov == fov
536         return _fov_deg;
537     case FG_SCALING_MAX:
538         if (aspectRatio < 1.0) {
539             // h_fov == fov
540             return _fov_deg;
541         } else {
542             // v_fov == fov
543             return
544                 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
545                      / (aspectRatio*_aspect_ratio_multiplier))
546                 * SG_RADIANS_TO_DEGREES * 2;
547         }
548     default:
549         assert(false);
550     }
551     return 0.0;
552 }
553
554
555
556 double
557 FGViewer::get_v_fov()
558 {
559     double aspectRatio = _cameraGroup->getMasterAspectRatio();
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                  * (aspectRatio*_aspect_ratio_multiplier))
565             * SG_RADIANS_TO_DEGREES * 2;
566     case FG_SCALING_MAX:
567         if (aspectRatio < 1.0) {
568             // h_fov == fov
569             return
570                 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
571                      * (aspectRatio*_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   updateDampOutput(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   if( fgGetBool( "/sim/rendering/draw-otw", true ) ) {
663     _cameraGroup->update(toOsg(_absolute_view_pos), toOsg(mViewOrientation));
664     _cameraGroup->setCameraParameters(get_v_fov(), get_aspect_ratio());
665   }
666 }
667
668 double FGViewer::get_aspect_ratio() const
669 {
670     return _cameraGroup->getMasterAspectRatio();
671 }