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 OpenGL frame
483 SGQuatd hlOr = SGQuatd::viewHL(geodPos);
485 // the rotation from the horizontal local frame to the basic view orientation
486 SGQuatd hlToBody = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
487 hlToBody = SGQuatd::simToView(hlToBody);
489 // The cartesian position of the basic view coordinate
490 SGVec3d position = SGVec3d::fromGeod(geodPos);
491 // the rotation offset, don't know why heading is negative here ...
492 SGQuatd viewOffsetOr = SGQuatd::simToView(
493 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg, _pitch_offset_deg,
496 // Compute the eyepoints orientation and position
497 // wrt the earth centered frame - that is global coorinates
498 SGQuatd ec2body = hlOr*hlToBody;
499 _absolute_view_pos = position + ec2body.backTransform(_offset_m);
500 mViewOrientation = ec2body*viewOffsetOr;
504 FGViewer::recalcLookAt ()
506 // The geodetic position of our target to look at
507 SGGeod geodTargetPos;
508 SGQuatd geodTargetOr;
510 geodTargetPos = SGGeod::fromDegFt(_target_location->getLongitude_deg(),
511 _target_location->getLatitude_deg(),
512 _target_location->getAltitudeASL_ft());
513 double head = _target_location->getHeading_deg();
514 double pitch = _target_location->getPitch_deg();
515 double roll = _target_location->getRoll_deg();
516 geodTargetOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
518 dampEyeData(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
519 _target_location->setPosition( _target_lon_deg, _target_lat_deg, _target_alt_ft );
520 _target_location->setOrientation( _target_roll_deg, _target_pitch_deg, _target_heading_deg );
521 _target_location->getTransformMatrix();
523 // if not model then calculate our own target position...
524 geodTargetPos = SGGeod::fromDegFt(_target_lon_deg,
527 geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
531 SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(geodTargetPos);
537 geodEyePos = SGGeod::fromDegFt(_location->getLongitude_deg(),
538 _location->getLatitude_deg(),
539 _location->getAltitudeASL_ft());
540 double head = _location->getHeading_deg();
541 double pitch = _location->getPitch_deg();
542 double roll = _location->getRoll_deg();
543 geodEyeOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
545 dampEyeData(_roll_deg, _pitch_deg, _heading_deg);
546 _location->setPosition( _lon_deg, _lat_deg, _alt_ft );
547 _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
548 _location->getTransformMatrix();
550 // update from our own data, just the rotation here...
551 geodEyePos = SGGeod::fromDegFt(_lon_deg, _lat_deg, _alt_ft);
552 geodEyeOr = SGQuatd::fromYawPitchRollDeg(_heading_deg,
556 SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(geodEyePos);
558 // the rotation offset, don't know why heading is negative here ...
559 SGQuatd eyeOffsetOr =
560 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg + 180, _pitch_offset_deg,
563 // Offsets to the eye position
564 SGVec3d eyeOff(-_offset_m.z(), _offset_m.y(), -_offset_m.x());
565 SGQuatd ec2eye = geodEyeHlOr*geodEyeOr;
566 SGVec3d eyeCart = SGVec3d::fromGeod(geodEyePos);
567 eyeCart += (ec2eye*eyeOffsetOr).backTransform(eyeOff);
569 SGVec3d atCart = SGVec3d::fromGeod(geodTargetPos);
571 // add target offsets to at_position...
572 SGVec3d target_pos_off(-_target_offset_m.z(), _target_offset_m.x(),
573 -_target_offset_m.y());
574 target_pos_off = (geodTargetHlOr*geodTargetOr).backTransform(target_pos_off);
575 atCart += target_pos_off;
576 eyeCart += target_pos_off;
578 // Compute the eyepoints orientation and position
579 // wrt the earth centered frame - that is global coorinates
580 _absolute_view_pos = eyeCart;
582 // the view direction
583 SGVec3d dir = normalize(atCart - eyeCart);
585 SGVec3d up = ec2eye.backTransform(SGVec3d(0, 0, -1));
586 // rotate dir to the 0-th unit vector
587 // rotate up to 2-th unit vector
588 mViewOrientation = SGQuatd::fromRotateTo(-dir, 2, up, 1);
592 FGViewer::dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg)
594 const double interval = 0.01;
596 static FGViewer *last_view = 0;
597 if (last_view != this) {
599 _damped_roll_deg = roll_deg;
600 _damped_pitch_deg = pitch_deg;
601 _damped_heading_deg = heading_deg;
606 if (_damp_sync < interval) {
607 if (_damp_roll > 0.0)
608 roll_deg = _damped_roll_deg;
609 if (_damp_pitch > 0.0)
610 pitch_deg = _damped_pitch_deg;
611 if (_damp_heading > 0.0)
612 heading_deg = _damped_heading_deg;
616 while (_damp_sync >= interval) {
617 _damp_sync -= interval;
620 if (_damp_roll > 0.0) {
621 d = _damped_roll_deg - roll_deg;
623 _damped_roll_deg -= 360.0;
625 _damped_roll_deg += 360.0;
626 roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
629 if (_damp_pitch > 0.0) {
630 d = _damped_pitch_deg - pitch_deg;
632 _damped_pitch_deg -= 360.0;
634 _damped_pitch_deg += 360.0;
635 pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
638 if (_damp_heading > 0.0) {
639 d = _damped_heading_deg - heading_deg;
641 _damped_heading_deg -= 360.0;
643 _damped_heading_deg += 360.0;
644 heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
650 FGViewer::get_h_fov()
652 switch (_scaling_type) {
653 case FG_SCALING_WIDTH: // h_fov == fov
656 if (_aspect_ratio < 1.0) {
662 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
663 / (_aspect_ratio*_aspect_ratio_multiplier))
664 * SG_RADIANS_TO_DEGREES * 2;
675 FGViewer::get_v_fov()
677 switch (_scaling_type) {
678 case FG_SCALING_WIDTH: // h_fov == fov
680 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
681 * (_aspect_ratio*_aspect_ratio_multiplier))
682 * SG_RADIANS_TO_DEGREES * 2;
684 if (_aspect_ratio < 1.0) {
687 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
688 * (_aspect_ratio*_aspect_ratio_multiplier))
689 * SG_RADIANS_TO_DEGREES * 2;
701 FGViewer::update (double dt)
706 int dt_ms = int(dt * 1000);
707 for ( i = 0; i < dt_ms; i++ ) {
708 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
709 setHeadingOffset_deg( _goal_heading_offset_deg );
712 // move current_view.headingoffset towards
713 // current_view.goal_view_offset
714 if ( _goal_heading_offset_deg > _heading_offset_deg )
716 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
717 incHeadingOffset_deg( 0.5 );
719 incHeadingOffset_deg( -0.5 );
722 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
723 incHeadingOffset_deg( -0.5 );
725 incHeadingOffset_deg( 0.5 );
728 if ( _heading_offset_deg > 360 ) {
729 incHeadingOffset_deg( -360 );
730 } else if ( _heading_offset_deg < 0 ) {
731 incHeadingOffset_deg( 360 );
736 for ( i = 0; i < dt_ms; i++ ) {
737 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
738 setPitchOffset_deg( _goal_pitch_offset_deg );
741 // move current_view.pitch_offset_deg towards
742 // current_view.goal_pitch_offset
743 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
745 incPitchOffset_deg( 1.0 );
747 incPitchOffset_deg( -1.0 );
749 if ( _pitch_offset_deg > 90 ) {
750 setPitchOffset_deg(90);
751 } else if ( _pitch_offset_deg < -90 ) {
752 setPitchOffset_deg( -90 );
758 for ( i = 0; i < dt_ms; i++ ) {
759 if ( fabs( _goal_roll_offset_deg - _roll_offset_deg ) < 1 ) {
760 setRollOffset_deg( _goal_roll_offset_deg );
763 // move current_view.roll_offset_deg towards
764 // current_view.goal_roll_offset
765 if ( _goal_roll_offset_deg > _roll_offset_deg )
767 incRollOffset_deg( 1.0 );
769 incRollOffset_deg( -1.0 );
771 if ( _roll_offset_deg > 90 ) {
772 setRollOffset_deg(90);
773 } else if ( _roll_offset_deg < -90 ) {
774 setRollOffset_deg( -90 );