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/placement.hxx>
43 #include <simgear/math/vector.hxx>
45 #include <Main/globals.hxx>
46 #include <Scenery/scenery.hxx>
47 #include <Model/acmodel.hxx>
51 #include "CameraGroup.hxx"
53 using namespace flightgear;
55 ////////////////////////////////////////////////////////////////////////
56 // Implementation of FGViewer.
57 ////////////////////////////////////////////////////////////////////////
60 FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index,
61 bool at_model, int at_model_index,
62 double damp_roll, double damp_pitch, double damp_heading,
63 double x_offset_m, double y_offset_m, double z_offset_m,
64 double heading_offset_deg, double pitch_offset_deg,
65 double roll_offset_deg,
66 double fov_deg, double aspect_ratio_multiplier,
67 double target_x_offset_m, double target_y_offset_m,
68 double target_z_offset_m, double near_m, bool internal ):
83 _scaling_type(FG_SCALING_MAX),
84 _cameraGroup(CameraGroup::getDefault())
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;
117 _aspect_ratio_multiplier = aspect_ratio_multiplier;
118 _target_offset_m.x() = target_x_offset_m;
119 _target_offset_m.y() = target_y_offset_m;
120 _target_offset_m.z() = target_z_offset_m;
121 _ground_level_nearplane_m = near_m;
122 // a reasonable guess for init, so that the math doesn't blow up
127 FGViewer::~FGViewer( void ) {
146 FGViewer::setType ( int type )
155 FGViewer::setInternal ( bool internal )
157 _internal = internal;
161 FGViewer::setLongitude_deg (double lon_deg)
168 FGViewer::setLatitude_deg (double lat_deg)
175 FGViewer::setAltitude_ft (double alt_ft)
182 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
191 FGViewer::setTargetLongitude_deg (double lon_deg)
194 _target_lon_deg = lon_deg;
198 FGViewer::setTargetLatitude_deg (double lat_deg)
201 _target_lat_deg = lat_deg;
205 FGViewer::setTargetAltitude_ft (double alt_ft)
208 _target_alt_ft = alt_ft;
212 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
215 _target_lon_deg = lon_deg;
216 _target_lat_deg = lat_deg;
217 _target_alt_ft = alt_ft;
221 FGViewer::setRoll_deg (double roll_deg)
224 _roll_deg = roll_deg;
228 FGViewer::setPitch_deg (double pitch_deg)
231 _pitch_deg = pitch_deg;
235 FGViewer::setHeading_deg (double heading_deg)
238 _heading_deg = heading_deg;
242 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
245 _roll_deg = roll_deg;
246 _pitch_deg = pitch_deg;
247 _heading_deg = heading_deg;
251 FGViewer::setTargetRoll_deg (double target_roll_deg)
254 _target_roll_deg = target_roll_deg;
258 FGViewer::setTargetPitch_deg (double target_pitch_deg)
261 _target_pitch_deg = target_pitch_deg;
265 FGViewer::setTargetHeading_deg (double target_heading_deg)
268 _target_heading_deg = target_heading_deg;
272 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
275 _target_roll_deg = target_roll_deg;
276 _target_pitch_deg = target_pitch_deg;
277 _target_heading_deg = target_heading_deg;
281 FGViewer::setXOffset_m (double x_offset_m)
284 _offset_m.x() = x_offset_m;
288 FGViewer::setYOffset_m (double y_offset_m)
291 _offset_m.y() = y_offset_m;
295 FGViewer::setZOffset_m (double z_offset_m)
298 _offset_m.z() = z_offset_m;
302 FGViewer::setTargetXOffset_m (double target_x_offset_m)
305 _target_offset_m.x() = target_x_offset_m;
309 FGViewer::setTargetYOffset_m (double target_y_offset_m)
312 _target_offset_m.y() = target_y_offset_m;
316 FGViewer::setTargetZOffset_m (double target_z_offset_m)
319 _target_offset_m.z() = target_z_offset_m;
323 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
326 _offset_m.x() = x_offset_m;
327 _offset_m.y() = y_offset_m;
328 _offset_m.z() = z_offset_m;
332 FGViewer::setRollOffset_deg (double roll_offset_deg)
335 _roll_offset_deg = roll_offset_deg;
339 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
342 _pitch_offset_deg = pitch_offset_deg;
346 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
349 _heading_offset_deg = heading_offset_deg;
353 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
356 _goal_roll_offset_deg = goal_roll_offset_deg;
360 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
363 _goal_pitch_offset_deg = goal_pitch_offset_deg;
364 if ( _goal_pitch_offset_deg < -90 ) {
365 _goal_pitch_offset_deg = -90.0;
367 if ( _goal_pitch_offset_deg > 90.0 ) {
368 _goal_pitch_offset_deg = 90.0;
374 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
377 _goal_heading_offset_deg = goal_heading_offset_deg;
378 while ( _goal_heading_offset_deg < 0.0 ) {
379 _goal_heading_offset_deg += 360;
381 while ( _goal_heading_offset_deg > 360 ) {
382 _goal_heading_offset_deg -= 360;
387 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
390 _roll_offset_deg = roll_offset_deg;
391 _pitch_offset_deg = pitch_offset_deg;
392 _heading_offset_deg = heading_offset_deg;
395 // recalc() is done every time one of the setters is called (making the
396 // cached data "dirty") on the next "get". It calculates all the outputs
401 if (_type == FG_LOOKFROM) {
410 // recalculate for LookFrom view type...
412 FGViewer::recalcLookFrom ()
414 // Update location data ...
415 double lat, lon, alt, head, pitch, roll;
417 SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
418 lat = placement->getLatitudeDeg();
419 lon = placement->getLongitudeDeg();
420 alt = placement->getElevationFt();
422 head = placement->getHeadingDeg();
423 pitch = placement->getPitchDeg();
424 roll = placement->getRollDeg();
433 if ( !_from_model ) {
434 // update from our own data...
435 dampEyeData(roll, pitch, head);
438 // The geodetic position of our base view position
439 SGGeod geodPos = SGGeod::fromDegFt(lon, lat, alt);
440 // The rotation rotating from the earth centerd frame to
441 // the horizontal local OpenGL frame
442 SGQuatd hlOr = SGQuatd::viewHL(geodPos);
444 // the rotation from the horizontal local frame to the basic view orientation
445 SGQuatd hlToBody = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
446 hlToBody = SGQuatd::simToView(hlToBody);
448 // The cartesian position of the basic view coordinate
449 SGVec3d position = SGVec3d::fromGeod(geodPos);
450 // the rotation offset, don't know why heading is negative here ...
451 SGQuatd viewOffsetOr = SGQuatd::simToView(
452 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg, _pitch_offset_deg,
455 // Compute the eyepoints orientation and position
456 // wrt the earth centered frame - that is global coorinates
457 SGQuatd ec2body = hlOr*hlToBody;
458 _absolute_view_pos = position + ec2body.backTransform(_offset_m);
459 mViewOrientation = ec2body*viewOffsetOr;
463 FGViewer::recalcLookAt ()
465 // The geodetic position of our target to look at
466 SGGeod geodTargetPos;
467 SGQuatd geodTargetOr;
469 SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
470 double lat = placement->getLatitudeDeg();
471 double lon = placement->getLongitudeDeg();
472 double alt = placement->getElevationFt();
473 geodTargetPos = SGGeod::fromDegFt(lon, lat, alt);
475 double head = placement->getHeadingDeg();
476 double pitch = placement->getPitchDeg();
477 double roll = placement->getRollDeg();
478 geodTargetOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
480 dampEyeData(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
482 // if not model then calculate our own target position...
483 geodTargetPos = SGGeod::fromDegFt(_target_lon_deg,
486 geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
490 SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(geodTargetPos);
496 SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
497 double lat = placement->getLatitudeDeg();
498 double lon = placement->getLongitudeDeg();
499 double alt = placement->getElevationFt();
500 geodEyePos = SGGeod::fromDegFt(lon, lat, alt);
502 double head = placement->getHeadingDeg();
503 double pitch = placement->getPitchDeg();
504 double roll = placement->getRollDeg();
505 geodEyeOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
507 dampEyeData(_roll_deg, _pitch_deg, _heading_deg);
509 // update from our own data, just the rotation here...
510 geodEyePos = SGGeod::fromDegFt(_lon_deg, _lat_deg, _alt_ft);
511 geodEyeOr = SGQuatd::fromYawPitchRollDeg(_heading_deg,
515 SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(geodEyePos);
517 // the rotation offset, don't know why heading is negative here ...
518 SGQuatd eyeOffsetOr =
519 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg + 180, _pitch_offset_deg,
522 // Offsets to the eye position
523 SGVec3d eyeOff(-_offset_m.z(), _offset_m.x(), -_offset_m.y());
524 SGQuatd ec2eye = geodEyeHlOr*geodEyeOr;
525 SGVec3d eyeCart = SGVec3d::fromGeod(geodEyePos);
526 eyeCart += (ec2eye*eyeOffsetOr).backTransform(eyeOff);
528 SGVec3d atCart = SGVec3d::fromGeod(geodTargetPos);
530 // add target offsets to at_position...
531 SGVec3d target_pos_off(-_target_offset_m.z(), _target_offset_m.x(),
532 -_target_offset_m.y());
533 target_pos_off = (geodTargetHlOr*geodTargetOr).backTransform(target_pos_off);
534 atCart += target_pos_off;
535 eyeCart += target_pos_off;
537 // Compute the eyepoints orientation and position
538 // wrt the earth centered frame - that is global coorinates
539 _absolute_view_pos = eyeCart;
541 // the view direction
542 SGVec3d dir = normalize(atCart - eyeCart);
544 SGVec3d up = ec2eye.backTransform(SGVec3d(0, 0, -1));
545 // rotate dir to the 0-th unit vector
546 // rotate up to 2-th unit vector
547 mViewOrientation = SGQuatd::fromRotateTo(-dir, 2, up, 1);
551 FGViewer::dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg)
553 const double interval = 0.01;
555 static FGViewer *last_view = 0;
556 if (last_view != this) {
558 _damped_roll_deg = roll_deg;
559 _damped_pitch_deg = pitch_deg;
560 _damped_heading_deg = heading_deg;
565 if (_damp_sync < interval) {
566 if (_damp_roll > 0.0)
567 roll_deg = _damped_roll_deg;
568 if (_damp_pitch > 0.0)
569 pitch_deg = _damped_pitch_deg;
570 if (_damp_heading > 0.0)
571 heading_deg = _damped_heading_deg;
575 while (_damp_sync >= interval) {
576 _damp_sync -= interval;
579 if (_damp_roll > 0.0) {
580 d = _damped_roll_deg - roll_deg;
582 _damped_roll_deg -= 360.0;
584 _damped_roll_deg += 360.0;
585 roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
588 if (_damp_pitch > 0.0) {
589 d = _damped_pitch_deg - pitch_deg;
591 _damped_pitch_deg -= 360.0;
593 _damped_pitch_deg += 360.0;
594 pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
597 if (_damp_heading > 0.0) {
598 d = _damped_heading_deg - heading_deg;
600 _damped_heading_deg -= 360.0;
602 _damped_heading_deg += 360.0;
603 heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
609 FGViewer::get_h_fov()
611 switch (_scaling_type) {
612 case FG_SCALING_WIDTH: // h_fov == fov
615 if (_aspect_ratio < 1.0) {
621 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
622 / (_aspect_ratio*_aspect_ratio_multiplier))
623 * SG_RADIANS_TO_DEGREES * 2;
634 FGViewer::get_v_fov()
636 switch (_scaling_type) {
637 case FG_SCALING_WIDTH: // h_fov == fov
639 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
640 * (_aspect_ratio*_aspect_ratio_multiplier))
641 * SG_RADIANS_TO_DEGREES * 2;
643 if (_aspect_ratio < 1.0) {
646 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
647 * (_aspect_ratio*_aspect_ratio_multiplier))
648 * SG_RADIANS_TO_DEGREES * 2;
660 FGViewer::update (double dt)
665 int dt_ms = int(dt * 1000);
666 for ( i = 0; i < dt_ms; i++ ) {
667 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
668 setHeadingOffset_deg( _goal_heading_offset_deg );
671 // move current_view.headingoffset towards
672 // current_view.goal_view_offset
673 if ( _goal_heading_offset_deg > _heading_offset_deg )
675 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
676 incHeadingOffset_deg( 0.5 );
678 incHeadingOffset_deg( -0.5 );
681 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
682 incHeadingOffset_deg( -0.5 );
684 incHeadingOffset_deg( 0.5 );
687 if ( _heading_offset_deg > 360 ) {
688 incHeadingOffset_deg( -360 );
689 } else if ( _heading_offset_deg < 0 ) {
690 incHeadingOffset_deg( 360 );
695 for ( i = 0; i < dt_ms; i++ ) {
696 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
697 setPitchOffset_deg( _goal_pitch_offset_deg );
700 // move current_view.pitch_offset_deg towards
701 // current_view.goal_pitch_offset
702 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
704 incPitchOffset_deg( 1.0 );
706 incPitchOffset_deg( -1.0 );
708 if ( _pitch_offset_deg > 90 ) {
709 setPitchOffset_deg(90);
710 } else if ( _pitch_offset_deg < -90 ) {
711 setPitchOffset_deg( -90 );
717 for ( i = 0; i < dt_ms; i++ ) {
718 if ( fabs( _goal_roll_offset_deg - _roll_offset_deg ) < 1 ) {
719 setRollOffset_deg( _goal_roll_offset_deg );
722 // move current_view.roll_offset_deg towards
723 // current_view.goal_roll_offset
724 if ( _goal_roll_offset_deg > _roll_offset_deg )
726 incRollOffset_deg( 1.0 );
728 incRollOffset_deg( -1.0 );
730 if ( _roll_offset_deg > 90 ) {
731 setRollOffset_deg(90);
732 } else if ( _roll_offset_deg < -90 ) {
733 setRollOffset_deg( -90 );
738 _cameraGroup->update(_absolute_view_pos.osg(), mViewOrientation.osg());
739 _cameraGroup->setCameraParameters(get_v_fov(), get_aspect_ratio());