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/polar3d.hxx>
41 #include <simgear/math/sg_geodesy.hxx>
42 #include <simgear/scene/model/location.hxx>
43 #include <simgear/scene/model/placement.hxx>
44 #include <simgear/math/vector.hxx>
46 #include <Main/globals.hxx>
47 #include <Scenery/scenery.hxx>
48 #include <Model/acmodel.hxx>
52 #include "CameraGroup.hxx"
54 using namespace flightgear;
56 ////////////////////////////////////////////////////////////////////////
57 // Implementation of FGViewer.
58 ////////////////////////////////////////////////////////////////////////
61 FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index,
62 bool at_model, int at_model_index,
63 double damp_roll, double damp_pitch, double damp_heading,
64 double x_offset_m, double y_offset_m, double z_offset_m,
65 double heading_offset_deg, double pitch_offset_deg,
66 double roll_offset_deg,
67 double fov_deg, double aspect_ratio_multiplier,
68 double target_x_offset_m, double target_y_offset_m,
69 double target_z_offset_m, double near_m, bool internal ):
84 _scaling_type(FG_SCALING_MAX),
87 _cameraGroup(CameraGroup::getDefault())
89 _absolute_view_pos = SGVec3d(0, 0, 0);
91 _from_model = from_model;
92 _from_model_index = from_model_index;
94 _at_model_index = at_model_index;
99 _damp_roll = 1.0 / pow(10.0, fabs(damp_roll));
100 if (damp_pitch > 0.0)
101 _damp_pitch = 1.0 / pow(10.0, fabs(damp_pitch));
102 if (damp_heading > 0.0)
103 _damp_heading = 1.0 / pow(10.0, fabs(damp_heading));
105 _offset_m.x() = x_offset_m;
106 _offset_m.y() = y_offset_m;
107 _offset_m.z() = z_offset_m;
108 _heading_offset_deg = heading_offset_deg;
109 _pitch_offset_deg = pitch_offset_deg;
110 _roll_offset_deg = roll_offset_deg;
111 _goal_heading_offset_deg = heading_offset_deg;
112 _goal_pitch_offset_deg = pitch_offset_deg;
113 _goal_roll_offset_deg = roll_offset_deg;
119 _aspect_ratio_multiplier = aspect_ratio_multiplier;
120 _target_offset_m.x() = target_x_offset_m;
121 _target_offset_m.y() = target_y_offset_m;
122 _target_offset_m.z() = target_z_offset_m;
123 _ground_level_nearplane_m = near_m;
124 // a reasonable guess for init, so that the math doesn't blow up
129 FGViewer::~FGViewer( void ) {
136 _location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
138 _location = new SGLocation;
140 if ( _type == FG_LOOKAT ) {
142 _target_location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
144 _target_location = (SGLocation *) new SGLocation;
159 FGViewer::setType ( int type )
168 FGViewer::setInternal ( bool internal )
170 _internal = internal;
174 FGViewer::setLongitude_deg (double lon_deg)
181 FGViewer::setLatitude_deg (double lat_deg)
188 FGViewer::setAltitude_ft (double alt_ft)
195 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
204 FGViewer::setTargetLongitude_deg (double lon_deg)
207 _target_lon_deg = lon_deg;
211 FGViewer::setTargetLatitude_deg (double lat_deg)
214 _target_lat_deg = lat_deg;
218 FGViewer::setTargetAltitude_ft (double alt_ft)
221 _target_alt_ft = alt_ft;
225 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
228 _target_lon_deg = lon_deg;
229 _target_lat_deg = lat_deg;
230 _target_alt_ft = alt_ft;
234 FGViewer::setRoll_deg (double roll_deg)
237 _roll_deg = roll_deg;
241 FGViewer::setPitch_deg (double pitch_deg)
244 _pitch_deg = pitch_deg;
248 FGViewer::setHeading_deg (double heading_deg)
251 _heading_deg = heading_deg;
255 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
258 _roll_deg = roll_deg;
259 _pitch_deg = pitch_deg;
260 _heading_deg = heading_deg;
264 FGViewer::setTargetRoll_deg (double target_roll_deg)
267 _target_roll_deg = target_roll_deg;
271 FGViewer::setTargetPitch_deg (double target_pitch_deg)
274 _target_pitch_deg = target_pitch_deg;
278 FGViewer::setTargetHeading_deg (double target_heading_deg)
281 _target_heading_deg = target_heading_deg;
285 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
288 _target_roll_deg = target_roll_deg;
289 _target_pitch_deg = target_pitch_deg;
290 _target_heading_deg = target_heading_deg;
294 FGViewer::setXOffset_m (double x_offset_m)
297 _offset_m.x() = x_offset_m;
301 FGViewer::setYOffset_m (double y_offset_m)
304 _offset_m.y() = y_offset_m;
308 FGViewer::setZOffset_m (double z_offset_m)
311 _offset_m.z() = z_offset_m;
315 FGViewer::setTargetXOffset_m (double target_x_offset_m)
318 _target_offset_m.x() = target_x_offset_m;
322 FGViewer::setTargetYOffset_m (double target_y_offset_m)
325 _target_offset_m.y() = target_y_offset_m;
329 FGViewer::setTargetZOffset_m (double target_z_offset_m)
332 _target_offset_m.z() = target_z_offset_m;
336 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
339 _offset_m.x() = x_offset_m;
340 _offset_m.y() = y_offset_m;
341 _offset_m.z() = z_offset_m;
345 FGViewer::setRollOffset_deg (double roll_offset_deg)
348 _roll_offset_deg = roll_offset_deg;
352 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
355 _pitch_offset_deg = pitch_offset_deg;
359 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
362 _heading_offset_deg = heading_offset_deg;
366 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
369 _goal_roll_offset_deg = goal_roll_offset_deg;
373 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
376 _goal_pitch_offset_deg = goal_pitch_offset_deg;
377 if ( _goal_pitch_offset_deg < -90 ) {
378 _goal_pitch_offset_deg = -90.0;
380 if ( _goal_pitch_offset_deg > 90.0 ) {
381 _goal_pitch_offset_deg = 90.0;
387 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
390 _goal_heading_offset_deg = goal_heading_offset_deg;
391 while ( _goal_heading_offset_deg < 0.0 ) {
392 _goal_heading_offset_deg += 360;
394 while ( _goal_heading_offset_deg > 360 ) {
395 _goal_heading_offset_deg -= 360;
400 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
403 _roll_offset_deg = roll_offset_deg;
404 _pitch_offset_deg = pitch_offset_deg;
405 _heading_offset_deg = heading_offset_deg;
408 // recalc() is done every time one of the setters is called (making the
409 // cached data "dirty") on the next "get". It calculates all the outputs
414 if (_type == FG_LOOKFROM) {
420 // Update viewer's postion data for the eye location...
421 _lon_deg = _location->getLongitude_deg();
422 _lat_deg = _location->getLatitude_deg();
423 _alt_ft = _location->getAltitudeASL_ft();
424 _roll_deg = _location->getRoll_deg();
425 _pitch_deg = _location->getPitch_deg();
426 _heading_deg = _location->getHeading_deg();
428 // Update viewer's postion data for the target (at object) location
429 if (_type == FG_LOOKAT) {
430 _target_lon_deg = _target_location->getLongitude_deg();
431 _target_lat_deg = _target_location->getLatitude_deg();
432 _target_alt_ft = _target_location->getAltitudeASL_ft();
433 _target_roll_deg = _target_location->getRoll_deg();
434 _target_pitch_deg = _target_location->getPitch_deg();
435 _target_heading_deg = _target_location->getHeading_deg();
441 // recalculate for LookFrom view type...
443 FGViewer::recalcLookFrom ()
445 // Update location data ...
446 if ( !_from_model ) {
447 _location->setPosition( _lon_deg, _lat_deg, _alt_ft );
448 _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
449 _location->getTransformMatrix();
451 double lat = _location->getLatitude_deg();
452 double lon = _location->getLongitude_deg();
453 double alt = _location->getAltitudeASL_ft();
454 double head = _location->getHeading_deg();
455 double pitch = _location->getPitch_deg();
456 double roll = _location->getRoll_deg();
457 if ( !_from_model ) {
458 // update from our own data...
459 dampEyeData(roll, pitch, head);
462 // The geodetic position of our base view position
463 SGGeod geodPos = SGGeod::fromDegFt(lon, lat, alt);
464 // The rotation rotating from the earth centerd frame to
465 // the horizontal local OpenGL frame
466 SGQuatd hlOr = SGQuatd::viewHL(geodPos);
468 // the rotation from the horizontal local frame to the basic view orientation
469 SGQuatd hlToBody = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
470 hlToBody = SGQuatd::simToView(hlToBody);
472 // The cartesian position of the basic view coordinate
473 SGVec3d position = SGVec3d::fromGeod(geodPos);
474 // the rotation offset, don't know why heading is negative here ...
475 SGQuatd viewOffsetOr = SGQuatd::simToView(
476 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg, _pitch_offset_deg,
479 // Compute the eyepoints orientation and position
480 // wrt the earth centered frame - that is global coorinates
481 SGQuatd ec2body = hlOr*hlToBody;
482 _absolute_view_pos = position + ec2body.backTransform(_offset_m);
483 mViewOrientation = ec2body*viewOffsetOr;
487 FGViewer::recalcLookAt ()
489 // The geodetic position of our target to look at
490 SGGeod geodTargetPos;
491 SGQuatd geodTargetOr;
493 geodTargetPos = SGGeod::fromDegFt(_target_location->getLongitude_deg(),
494 _target_location->getLatitude_deg(),
495 _target_location->getAltitudeASL_ft());
496 double head = _target_location->getHeading_deg();
497 double pitch = _target_location->getPitch_deg();
498 double roll = _target_location->getRoll_deg();
499 geodTargetOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
501 dampEyeData(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
502 _target_location->setPosition( _target_lon_deg, _target_lat_deg, _target_alt_ft );
503 _target_location->setOrientation( _target_roll_deg, _target_pitch_deg, _target_heading_deg );
504 _target_location->getTransformMatrix();
506 // if not model then calculate our own target position...
507 geodTargetPos = SGGeod::fromDegFt(_target_lon_deg,
510 geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
514 SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(geodTargetPos);
520 geodEyePos = SGGeod::fromDegFt(_location->getLongitude_deg(),
521 _location->getLatitude_deg(),
522 _location->getAltitudeASL_ft());
523 double head = _location->getHeading_deg();
524 double pitch = _location->getPitch_deg();
525 double roll = _location->getRoll_deg();
526 geodEyeOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
528 dampEyeData(_roll_deg, _pitch_deg, _heading_deg);
529 _location->setPosition( _lon_deg, _lat_deg, _alt_ft );
530 _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
531 _location->getTransformMatrix();
533 // update from our own data, just the rotation here...
534 geodEyePos = SGGeod::fromDegFt(_lon_deg, _lat_deg, _alt_ft);
535 geodEyeOr = SGQuatd::fromYawPitchRollDeg(_heading_deg,
539 SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(geodEyePos);
541 // the rotation offset, don't know why heading is negative here ...
542 SGQuatd eyeOffsetOr =
543 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg + 180, _pitch_offset_deg,
546 // Offsets to the eye position
547 SGVec3d eyeOff(-_offset_m.z(), _offset_m.x(), -_offset_m.y());
548 SGQuatd ec2eye = geodEyeHlOr*geodEyeOr;
549 SGVec3d eyeCart = SGVec3d::fromGeod(geodEyePos);
550 eyeCart += (ec2eye*eyeOffsetOr).backTransform(eyeOff);
552 SGVec3d atCart = SGVec3d::fromGeod(geodTargetPos);
554 // add target offsets to at_position...
555 SGVec3d target_pos_off(-_target_offset_m.z(), _target_offset_m.x(),
556 -_target_offset_m.y());
557 target_pos_off = (geodTargetHlOr*geodTargetOr).backTransform(target_pos_off);
558 atCart += target_pos_off;
559 eyeCart += target_pos_off;
561 // Compute the eyepoints orientation and position
562 // wrt the earth centered frame - that is global coorinates
563 _absolute_view_pos = eyeCart;
565 // the view direction
566 SGVec3d dir = normalize(atCart - eyeCart);
568 SGVec3d up = ec2eye.backTransform(SGVec3d(0, 0, -1));
569 // rotate dir to the 0-th unit vector
570 // rotate up to 2-th unit vector
571 mViewOrientation = SGQuatd::fromRotateTo(-dir, 2, up, 1);
575 FGViewer::dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg)
577 const double interval = 0.01;
579 static FGViewer *last_view = 0;
580 if (last_view != this) {
582 _damped_roll_deg = roll_deg;
583 _damped_pitch_deg = pitch_deg;
584 _damped_heading_deg = heading_deg;
589 if (_damp_sync < interval) {
590 if (_damp_roll > 0.0)
591 roll_deg = _damped_roll_deg;
592 if (_damp_pitch > 0.0)
593 pitch_deg = _damped_pitch_deg;
594 if (_damp_heading > 0.0)
595 heading_deg = _damped_heading_deg;
599 while (_damp_sync >= interval) {
600 _damp_sync -= interval;
603 if (_damp_roll > 0.0) {
604 d = _damped_roll_deg - roll_deg;
606 _damped_roll_deg -= 360.0;
608 _damped_roll_deg += 360.0;
609 roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
612 if (_damp_pitch > 0.0) {
613 d = _damped_pitch_deg - pitch_deg;
615 _damped_pitch_deg -= 360.0;
617 _damped_pitch_deg += 360.0;
618 pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
621 if (_damp_heading > 0.0) {
622 d = _damped_heading_deg - heading_deg;
624 _damped_heading_deg -= 360.0;
626 _damped_heading_deg += 360.0;
627 heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
633 FGViewer::get_h_fov()
635 switch (_scaling_type) {
636 case FG_SCALING_WIDTH: // h_fov == fov
639 if (_aspect_ratio < 1.0) {
645 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
646 / (_aspect_ratio*_aspect_ratio_multiplier))
647 * SG_RADIANS_TO_DEGREES * 2;
658 FGViewer::get_v_fov()
660 switch (_scaling_type) {
661 case FG_SCALING_WIDTH: // h_fov == fov
663 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
664 * (_aspect_ratio*_aspect_ratio_multiplier))
665 * SG_RADIANS_TO_DEGREES * 2;
667 if (_aspect_ratio < 1.0) {
670 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
671 * (_aspect_ratio*_aspect_ratio_multiplier))
672 * SG_RADIANS_TO_DEGREES * 2;
684 FGViewer::update (double dt)
689 int dt_ms = int(dt * 1000);
690 for ( i = 0; i < dt_ms; i++ ) {
691 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
692 setHeadingOffset_deg( _goal_heading_offset_deg );
695 // move current_view.headingoffset towards
696 // current_view.goal_view_offset
697 if ( _goal_heading_offset_deg > _heading_offset_deg )
699 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
700 incHeadingOffset_deg( 0.5 );
702 incHeadingOffset_deg( -0.5 );
705 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
706 incHeadingOffset_deg( -0.5 );
708 incHeadingOffset_deg( 0.5 );
711 if ( _heading_offset_deg > 360 ) {
712 incHeadingOffset_deg( -360 );
713 } else if ( _heading_offset_deg < 0 ) {
714 incHeadingOffset_deg( 360 );
719 for ( i = 0; i < dt_ms; i++ ) {
720 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
721 setPitchOffset_deg( _goal_pitch_offset_deg );
724 // move current_view.pitch_offset_deg towards
725 // current_view.goal_pitch_offset
726 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
728 incPitchOffset_deg( 1.0 );
730 incPitchOffset_deg( -1.0 );
732 if ( _pitch_offset_deg > 90 ) {
733 setPitchOffset_deg(90);
734 } else if ( _pitch_offset_deg < -90 ) {
735 setPitchOffset_deg( -90 );
741 for ( i = 0; i < dt_ms; i++ ) {
742 if ( fabs( _goal_roll_offset_deg - _roll_offset_deg ) < 1 ) {
743 setRollOffset_deg( _goal_roll_offset_deg );
746 // move current_view.roll_offset_deg towards
747 // current_view.goal_roll_offset
748 if ( _goal_roll_offset_deg > _roll_offset_deg )
750 incRollOffset_deg( 1.0 );
752 incRollOffset_deg( -1.0 );
754 if ( _roll_offset_deg > 90 ) {
755 setRollOffset_deg(90);
756 } else if ( _roll_offset_deg < -90 ) {
757 setRollOffset_deg( -90 );
762 _cameraGroup->update(_absolute_view_pos.osg(), mViewOrientation.osg());
763 _cameraGroup->setCameraParameters(get_v_fov(), get_aspect_ratio());