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;
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
411 if (_type == FG_LOOKFROM) {
417 SGGeod geodEyePoint = SGGeod::fromCart(_absolute_view_pos);
418 geodEyePoint.setElevationM(0);
419 _zero_elev = SGVec3d::fromGeod(geodEyePoint);
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()));
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();
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();
447 // recalculate for LookFrom view type...
449 FGViewer::recalcLookFrom ()
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();
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);
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);
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);
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,
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;
493 FGViewer::recalcLookAt ()
495 // The geodetic position of our target to look at
496 SGGeod geodTargetPos;
497 SGQuatd geodTargetOr;
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);
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();
512 // if not model then calculate our own target position...
513 geodTargetPos = SGGeod::fromDegFt(_target_lon_deg,
516 geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
520 SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(geodTargetPos);
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);
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();
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,
545 SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(geodEyePos);
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,
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);
558 SGVec3d atCart = SGVec3d::fromGeod(geodTargetPos);
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;
567 // Compute the eyepoints orientation and position
568 // wrt the earth centered frame - that is global coorinates
569 _absolute_view_pos = eyeCart;
571 // the view direction
572 SGVec3d dir = normalize(atCart - eyeCart);
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);
581 FGViewer::dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg)
583 const double interval = 0.01;
585 static FGViewer *last_view = 0;
586 if (last_view != this) {
588 _damped_roll_deg = roll_deg;
589 _damped_pitch_deg = pitch_deg;
590 _damped_heading_deg = heading_deg;
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;
605 while (_damp_sync >= interval) {
606 _damp_sync -= interval;
609 if (_damp_roll > 0.0) {
610 d = _damped_roll_deg - roll_deg;
612 _damped_roll_deg -= 360.0;
614 _damped_roll_deg += 360.0;
615 roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
618 if (_damp_pitch > 0.0) {
619 d = _damped_pitch_deg - pitch_deg;
621 _damped_pitch_deg -= 360.0;
623 _damped_pitch_deg += 360.0;
624 pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
627 if (_damp_heading > 0.0) {
628 d = _damped_heading_deg - heading_deg;
630 _damped_heading_deg -= 360.0;
632 _damped_heading_deg += 360.0;
633 heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
639 FGViewer::get_h_fov()
641 switch (_scaling_type) {
642 case FG_SCALING_WIDTH: // h_fov == fov
645 if (_aspect_ratio < 1.0) {
651 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
652 / (_aspect_ratio*_aspect_ratio_multiplier))
653 * SG_RADIANS_TO_DEGREES * 2;
664 FGViewer::get_v_fov()
666 switch (_scaling_type) {
667 case FG_SCALING_WIDTH: // h_fov == fov
669 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
670 * (_aspect_ratio*_aspect_ratio_multiplier))
671 * SG_RADIANS_TO_DEGREES * 2;
673 if (_aspect_ratio < 1.0) {
676 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
677 * (_aspect_ratio*_aspect_ratio_multiplier))
678 * SG_RADIANS_TO_DEGREES * 2;
690 FGViewer::update (double dt)
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 );
701 // move current_view.headingoffset towards
702 // current_view.goal_view_offset
703 if ( _goal_heading_offset_deg > _heading_offset_deg )
705 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
706 incHeadingOffset_deg( 0.5 );
708 incHeadingOffset_deg( -0.5 );
711 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
712 incHeadingOffset_deg( -0.5 );
714 incHeadingOffset_deg( 0.5 );
717 if ( _heading_offset_deg > 360 ) {
718 incHeadingOffset_deg( -360 );
719 } else if ( _heading_offset_deg < 0 ) {
720 incHeadingOffset_deg( 360 );
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 );
730 // move current_view.pitch_offset_deg towards
731 // current_view.goal_pitch_offset
732 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
734 incPitchOffset_deg( 1.0 );
736 incPitchOffset_deg( -1.0 );
738 if ( _pitch_offset_deg > 90 ) {
739 setPitchOffset_deg(90);
740 } else if ( _pitch_offset_deg < -90 ) {
741 setPitchOffset_deg( -90 );
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 );
752 // move current_view.roll_offset_deg towards
753 // current_view.goal_roll_offset
754 if ( _goal_roll_offset_deg > _roll_offset_deg )
756 incRollOffset_deg( 1.0 );
758 incRollOffset_deg( -1.0 );
760 if ( _roll_offset_deg > 90 ) {
761 setRollOffset_deg(90);
762 } else if ( _roll_offset_deg < -90 ) {
763 setRollOffset_deg( -90 );