1 // viewer.cxx -- class for managing a viewer in the flightgear world.
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
8 // Copyright (C) 1997 - 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
30 #include <simgear/compiler.h>
32 #include "fg_props.hxx"
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>
47 #include <Main/globals.hxx>
48 #include <Scenery/scenery.hxx>
49 #include <Model/acmodel.hxx>
54 ////////////////////////////////////////////////////////////////////////
55 // Implementation of FGViewer.
56 ////////////////////////////////////////////////////////////////////////
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 ):
82 _scaling_type(FG_SCALING_MAX),
86 _absolute_view_pos = SGVec3d(0, 0, 0);
88 _from_model = from_model;
89 _from_model_index = from_model_index;
91 _at_model_index = at_model_index;
96 _damp_roll = 1.0 / pow(10.0, fabs(damp_roll));
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));
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;
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
126 FGViewer::~FGViewer( void ) {
133 _location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
135 _location = new SGLocation;
137 if ( _type == FG_LOOKAT ) {
139 _target_location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
141 _target_location = (SGLocation *) new SGLocation;
156 FGViewer::setType ( int type )
165 FGViewer::setInternal ( bool internal )
167 _internal = internal;
171 FGViewer::setLongitude_deg (double lon_deg)
178 FGViewer::setLatitude_deg (double lat_deg)
185 FGViewer::setAltitude_ft (double alt_ft)
192 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
201 FGViewer::setTargetLongitude_deg (double lon_deg)
204 _target_lon_deg = lon_deg;
208 FGViewer::setTargetLatitude_deg (double lat_deg)
211 _target_lat_deg = lat_deg;
215 FGViewer::setTargetAltitude_ft (double alt_ft)
218 _target_alt_ft = alt_ft;
222 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
225 _target_lon_deg = lon_deg;
226 _target_lat_deg = lat_deg;
227 _target_alt_ft = alt_ft;
231 FGViewer::setRoll_deg (double roll_deg)
234 _roll_deg = roll_deg;
238 FGViewer::setPitch_deg (double pitch_deg)
241 _pitch_deg = pitch_deg;
245 FGViewer::setHeading_deg (double heading_deg)
248 _heading_deg = heading_deg;
252 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
255 _roll_deg = roll_deg;
256 _pitch_deg = pitch_deg;
257 _heading_deg = heading_deg;
261 FGViewer::setTargetRoll_deg (double target_roll_deg)
264 _target_roll_deg = target_roll_deg;
268 FGViewer::setTargetPitch_deg (double target_pitch_deg)
271 _target_pitch_deg = target_pitch_deg;
275 FGViewer::setTargetHeading_deg (double target_heading_deg)
278 _target_heading_deg = target_heading_deg;
282 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
285 _target_roll_deg = target_roll_deg;
286 _target_pitch_deg = target_pitch_deg;
287 _target_heading_deg = target_heading_deg;
291 FGViewer::setXOffset_m (double x_offset_m)
294 _offset_m.x() = x_offset_m;
298 FGViewer::setYOffset_m (double y_offset_m)
301 _offset_m.y() = y_offset_m;
305 FGViewer::setZOffset_m (double z_offset_m)
308 _offset_m.z() = z_offset_m;
312 FGViewer::setTargetXOffset_m (double target_x_offset_m)
315 _target_offset_m.x() = target_x_offset_m;
319 FGViewer::setTargetYOffset_m (double target_y_offset_m)
322 _target_offset_m.y() = target_y_offset_m;
326 FGViewer::setTargetZOffset_m (double target_z_offset_m)
329 _target_offset_m.z() = target_z_offset_m;
333 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
336 _offset_m.x() = x_offset_m;
337 _offset_m.y() = y_offset_m;
338 _offset_m.z() = z_offset_m;
342 FGViewer::setRollOffset_deg (double roll_offset_deg)
345 _roll_offset_deg = roll_offset_deg;
349 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
352 _pitch_offset_deg = pitch_offset_deg;
356 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
359 _heading_offset_deg = heading_offset_deg;
363 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
366 _goal_roll_offset_deg = goal_roll_offset_deg;
370 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
373 _goal_pitch_offset_deg = goal_pitch_offset_deg;
374 if ( _goal_pitch_offset_deg < -90 ) {
375 _goal_pitch_offset_deg = -90.0;
377 if ( _goal_pitch_offset_deg > 90.0 ) {
378 _goal_pitch_offset_deg = 90.0;
384 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
387 _goal_heading_offset_deg = goal_heading_offset_deg;
388 while ( _goal_heading_offset_deg < 0.0 ) {
389 _goal_heading_offset_deg += 360;
391 while ( _goal_heading_offset_deg > 360 ) {
392 _goal_heading_offset_deg -= 360;
397 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
400 _roll_offset_deg = roll_offset_deg;
401 _pitch_offset_deg = pitch_offset_deg;
402 _heading_offset_deg = heading_offset_deg;
406 FGViewer::get_absolute_view_pos ()
410 return _absolute_view_pos.data();
413 // recalc() is done every time one of the setters is called (making the
414 // cached data "dirty") on the next "get". It calculates all the outputs
419 if (_type == FG_LOOKFROM) {
425 SGVec3d center = globals->get_scenery()->get_center();
426 _view_pos = toVec3f(_absolute_view_pos - center);
428 SGGeod geodEyePoint = SGGeod::fromCart(_absolute_view_pos);
429 geodEyePoint.setElevationM(0);
430 _zero_elev = toVec3f(SGVec3d::fromGeod(geodEyePoint) - center);
432 SGQuatd hlOr = SGQuatd::fromLonLat(geodEyePoint);
433 _surface_south = toVec3f(hlOr.backTransform(-SGVec3d::e1()));
434 _surface_east = toVec3f(hlOr.backTransform(SGVec3d::e2()));
435 _world_up = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
437 // Update viewer's postion data for the eye location...
438 _lon_deg = _location->getLongitude_deg();
439 _lat_deg = _location->getLatitude_deg();
440 _alt_ft = _location->getAltitudeASL_ft();
441 _roll_deg = _location->getRoll_deg();
442 _pitch_deg = _location->getPitch_deg();
443 _heading_deg = _location->getHeading_deg();
445 // Update viewer's postion data for the target (at object) location
446 if (_type == FG_LOOKAT) {
447 _target_lon_deg = _target_location->getLongitude_deg();
448 _target_lat_deg = _target_location->getLatitude_deg();
449 _target_alt_ft = _target_location->getAltitudeASL_ft();
450 _target_roll_deg = _target_location->getRoll_deg();
451 _target_pitch_deg = _target_location->getPitch_deg();
452 _target_heading_deg = _target_location->getHeading_deg();
458 // recalculate for LookFrom view type...
460 FGViewer::recalcLookFrom ()
462 // Update location data ...
463 if ( !_from_model ) {
464 _location->setPosition( _lon_deg, _lat_deg, _alt_ft );
465 _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
466 _location->getTransformMatrix();
468 double lat = _location->getLatitude_deg();
469 double lon = _location->getLongitude_deg();
470 double alt = _location->getAltitudeASL_ft();
471 double head = _location->getHeading_deg();
472 double pitch = _location->getPitch_deg();
473 double roll = _location->getRoll_deg();
474 if ( !_from_model ) {
475 // update from our own data...
476 dampEyeData(roll, pitch, head);
479 // The geodetic position of our base view position
480 SGGeod geodPos = SGGeod::fromDegFt(lon, lat, alt);
481 // The rotation rotating from the earth centerd frame to
482 // the horizontal local frame
483 SGQuatd hlOr = SGQuatd::fromLonLat(geodPos);
484 // the rotation from the horizontal local frame to the basic view orientation
485 SGQuatd hlToBody = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
486 // The cartesian position of the basic view coordinate
487 SGVec3d position = SGVec3d::fromGeod(geodPos);
488 // the rotation offset, don't know why heading is negative here ...
489 SGQuatd viewOffsetOr =
490 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg, _pitch_offset_deg,
493 // The offset vector is meant: x +right/-left, y +up/-down, z, +back/-fwd
494 // We work in the body coordinate system which is slightly different
495 SGVec3d off(-_offset_m.z(), _offset_m.x(), -_offset_m.y());
497 // Compute the eyepoints orientation and position
498 // wrt the earth centered frame - that is global coorinates
499 SGQuatd ec2body = hlOr*hlToBody;
500 _absolute_view_pos = position + ec2body.backTransform(off);
501 mViewOrientation = ec2body*viewOffsetOr;
505 FGViewer::recalcLookAt ()
507 // The geodetic position of our target to look at
508 SGGeod geodTargetPos;
509 SGQuatd geodTargetOr;
511 geodTargetPos = SGGeod::fromDegFt(_target_location->getLongitude_deg(),
512 _target_location->getLatitude_deg(),
513 _target_location->getAltitudeASL_ft());
514 double head = _target_location->getHeading_deg();
515 double pitch = _target_location->getPitch_deg();
516 double roll = _target_location->getRoll_deg();
517 geodTargetOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
519 dampEyeData(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
520 _target_location->setPosition( _target_lon_deg, _target_lat_deg, _target_alt_ft );
521 _target_location->setOrientation( _target_roll_deg, _target_pitch_deg, _target_heading_deg );
522 _target_location->getTransformMatrix();
524 // if not model then calculate our own target position...
525 geodTargetPos = SGGeod::fromDegFt(_target_lon_deg,
528 geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
532 SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(geodTargetPos);
538 geodEyePos = SGGeod::fromDegFt(_location->getLongitude_deg(),
539 _location->getLatitude_deg(),
540 _location->getAltitudeASL_ft());
541 double head = _location->getHeading_deg();
542 double pitch = _location->getPitch_deg();
543 double roll = _location->getRoll_deg();
544 geodEyeOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
546 dampEyeData(_roll_deg, _pitch_deg, _heading_deg);
547 _location->setPosition( _lon_deg, _lat_deg, _alt_ft );
548 _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
549 _location->getTransformMatrix();
551 // update from our own data, just the rotation here...
552 geodEyePos = SGGeod::fromDegFt(_lon_deg, _lat_deg, _alt_ft);
553 geodEyeOr = SGQuatd::fromYawPitchRollDeg(_heading_deg,
557 SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(geodEyePos);
559 // the rotation offset, don't know why heading is negative here ...
560 SGQuatd eyeOffsetOr =
561 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg + 180, _pitch_offset_deg,
564 // Offsets to the eye position
565 SGVec3d eyeOff(-_offset_m.z(), _offset_m.y(), -_offset_m.x());
566 SGQuatd ec2eye = geodEyeHlOr*geodEyeOr;
567 SGVec3d eyeCart = SGVec3d::fromGeod(geodEyePos);
568 eyeCart += (ec2eye*eyeOffsetOr).backTransform(eyeOff);
570 SGVec3d atCart = SGVec3d::fromGeod(geodTargetPos);
572 // add target offsets to at_position...
573 SGVec3d target_pos_off(-_target_offset_m.z(), _target_offset_m.x(),
574 -_target_offset_m.y());
575 target_pos_off = (geodTargetHlOr*geodTargetOr).backTransform(target_pos_off);
576 atCart += target_pos_off;
577 eyeCart += target_pos_off;
579 // Compute the eyepoints orientation and position
580 // wrt the earth centered frame - that is global coorinates
581 _absolute_view_pos = eyeCart;
583 // the view direction
584 SGVec3d dir = normalize(atCart - eyeCart);
586 SGVec3d up = ec2eye.backTransform(SGVec3d(0, 0, 1));
587 // rotate dir to the 0-th unit vector
588 // rotate up to 2-th unit vector
589 mViewOrientation = SGQuatd::fromRotateTo(dir, 0, up, 2);
593 FGViewer::dampEyeData (double &roll_deg, double &pitch_deg, double &heading_deg)
595 const double interval = 0.01;
597 static FGViewer *last_view = 0;
598 if (last_view != this) {
600 _damped_roll_deg = roll_deg;
601 _damped_pitch_deg = pitch_deg;
602 _damped_heading_deg = heading_deg;
607 if (_damp_sync < interval) {
608 if (_damp_roll > 0.0)
609 roll_deg = _damped_roll_deg;
610 if (_damp_pitch > 0.0)
611 pitch_deg = _damped_pitch_deg;
612 if (_damp_heading > 0.0)
613 heading_deg = _damped_heading_deg;
617 while (_damp_sync >= interval) {
618 _damp_sync -= interval;
621 if (_damp_roll > 0.0) {
622 d = _damped_roll_deg - roll_deg;
624 _damped_roll_deg -= 360.0;
626 _damped_roll_deg += 360.0;
627 roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
630 if (_damp_pitch > 0.0) {
631 d = _damped_pitch_deg - pitch_deg;
633 _damped_pitch_deg -= 360.0;
635 _damped_pitch_deg += 360.0;
636 pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
639 if (_damp_heading > 0.0) {
640 d = _damped_heading_deg - heading_deg;
642 _damped_heading_deg -= 360.0;
644 _damped_heading_deg += 360.0;
645 heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
651 FGViewer::get_h_fov()
653 switch (_scaling_type) {
654 case FG_SCALING_WIDTH: // h_fov == fov
657 if (_aspect_ratio < 1.0) {
663 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
664 / (_aspect_ratio*_aspect_ratio_multiplier))
665 * SG_RADIANS_TO_DEGREES * 2;
676 FGViewer::get_v_fov()
678 switch (_scaling_type) {
679 case FG_SCALING_WIDTH: // h_fov == fov
681 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
682 * (_aspect_ratio*_aspect_ratio_multiplier))
683 * SG_RADIANS_TO_DEGREES * 2;
685 if (_aspect_ratio < 1.0) {
688 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
689 * (_aspect_ratio*_aspect_ratio_multiplier))
690 * SG_RADIANS_TO_DEGREES * 2;
702 FGViewer::update (double dt)
707 int dt_ms = int(dt * 1000);
708 for ( i = 0; i < dt_ms; i++ ) {
709 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
710 setHeadingOffset_deg( _goal_heading_offset_deg );
713 // move current_view.headingoffset towards
714 // current_view.goal_view_offset
715 if ( _goal_heading_offset_deg > _heading_offset_deg )
717 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
718 incHeadingOffset_deg( 0.5 );
720 incHeadingOffset_deg( -0.5 );
723 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
724 incHeadingOffset_deg( -0.5 );
726 incHeadingOffset_deg( 0.5 );
729 if ( _heading_offset_deg > 360 ) {
730 incHeadingOffset_deg( -360 );
731 } else if ( _heading_offset_deg < 0 ) {
732 incHeadingOffset_deg( 360 );
737 for ( i = 0; i < dt_ms; i++ ) {
738 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
739 setPitchOffset_deg( _goal_pitch_offset_deg );
742 // move current_view.pitch_offset_deg towards
743 // current_view.goal_pitch_offset
744 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
746 incPitchOffset_deg( 1.0 );
748 incPitchOffset_deg( -1.0 );
750 if ( _pitch_offset_deg > 90 ) {
751 setPitchOffset_deg(90);
752 } else if ( _pitch_offset_deg < -90 ) {
753 setPitchOffset_deg( -90 );
759 for ( i = 0; i < dt_ms; i++ ) {
760 if ( fabs( _goal_roll_offset_deg - _roll_offset_deg ) < 1 ) {
761 setRollOffset_deg( _goal_roll_offset_deg );
764 // move current_view.roll_offset_deg towards
765 // current_view.goal_roll_offset
766 if ( _goal_roll_offset_deg > _roll_offset_deg )
768 incRollOffset_deg( 1.0 );
770 incRollOffset_deg( -1.0 );
772 if ( _roll_offset_deg > 90 ) {
773 setRollOffset_deg(90);
774 } else if ( _roll_offset_deg < -90 ) {
775 setRollOffset_deg( -90 );