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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <simgear/compiler.h>
29 #include "fg_props.hxx"
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/constants.h>
37 #include <simgear/math/point3d.hxx>
38 #include <simgear/math/polar3d.hxx>
39 #include <simgear/math/sg_geodesy.hxx>
40 #include <simgear/scene/model/location.hxx>
41 #include <simgear/scene/model/placement.hxx>
42 #include <simgear/math/vector.hxx>
44 #include <Main/globals.hxx>
45 #include <Scenery/scenery.hxx>
46 #include <Model/acmodel.hxx>
51 //////////////////////////////////////////////////////////////////
52 // Norman's Optimized matrix rotators! //
53 //////////////////////////////////////////////////////////////////
56 // Since these are pure rotation matrices we can save some bookwork
57 // by considering them to be 3x3 until the very end -- NHV
58 static void MakeVIEW_OFFSET( sgMat4 dst,
59 const float angle1, const sgVec3 axis1,
60 const float angle2, const sgVec3 axis2 )
62 // make rotmatrix1 from angle and axis
63 float s = (float) sin ( angle1 ) ;
64 float c = (float) cos ( angle1 ) ;
65 float t = SG_ONE - c ;
68 float tmp = t * axis1[0];
69 mat1[0][0] = tmp * axis1[0] + c ;
70 mat1[0][1] = tmp * axis1[1] + s * axis1[2] ;
71 mat1[0][2] = tmp * axis1[2] - s * axis1[1] ;
74 mat1[1][0] = tmp * axis1[0] - s * axis1[2] ;
75 mat1[1][1] = tmp * axis1[1] + c ;
76 mat1[1][2] = tmp * axis1[2] + s * axis1[0] ;
79 mat1[2][0] = tmp * axis1[0] + s * axis1[1] ;
80 mat1[2][1] = tmp * axis1[1] - s * axis1[0] ;
81 mat1[2][2] = tmp * axis1[2] + c ;
83 // make rotmatrix2 from angle and axis
84 s = (float) sin ( angle2 ) ;
85 c = (float) cos ( angle2 ) ;
90 mat2[0][0] = tmp * axis2[0] + c ;
91 mat2[0][1] = tmp * axis2[1] + s * axis2[2] ;
92 mat2[0][2] = tmp * axis2[2] - s * axis2[1] ;
95 mat2[1][0] = tmp * axis2[0] - s * axis2[2] ;
96 mat2[1][1] = tmp * axis2[1] + c ;
97 mat2[1][2] = tmp * axis2[2] + s * axis2[0] ;
100 mat2[2][0] = tmp * axis2[0] + s * axis2[1] ;
101 mat2[2][1] = tmp * axis2[1] - s * axis2[0] ;
102 mat2[2][2] = tmp * axis2[2] + c ;
105 for ( int j = 0 ; j < 3 ; j++ ) {
106 dst[0][j] = mat2[0][0] * mat1[0][j] +
107 mat2[0][1] * mat1[1][j] +
108 mat2[0][2] * mat1[2][j];
110 dst[1][j] = mat2[1][0] * mat1[0][j] +
111 mat2[1][1] * mat1[1][j] +
112 mat2[1][2] * mat1[2][j];
114 dst[2][j] = mat2[2][0] * mat1[0][j] +
115 mat2[2][1] * mat1[1][j] +
116 mat2[2][2] * mat1[2][j];
118 // fill in 4x4 matrix elements
129 ////////////////////////////////////////////////////////////////////////
130 // Implementation of FGViewer.
131 ////////////////////////////////////////////////////////////////////////
134 FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index,
135 bool at_model, int at_model_index,
136 double damp_roll, double damp_pitch, double damp_heading,
137 double x_offset_m, double y_offset_m, double z_offset_m,
138 double heading_offset_deg, double pitch_offset_deg,
139 double roll_offset_deg, double fov_deg,
140 double target_x_offset_m, double target_y_offset_m,
141 double target_z_offset_m, double near_m ):
156 _scaling_type(FG_SCALING_MAX)
158 sgdZeroVec3(_absolute_view_pos);
160 _from_model = from_model;
161 _from_model_index = from_model_index;
162 _at_model = at_model;
163 _at_model_index = at_model_index;
166 _damp_roll = 1.0 / pow(10, fabs(damp_roll));
167 if (damp_pitch > 0.0)
168 _damp_pitch = 1.0 / pow(10, fabs(damp_pitch));
169 if (damp_heading > 0.0)
170 _damp_heading = 1.0 / pow(10, fabs(damp_heading));
172 _x_offset_m = x_offset_m;
173 _y_offset_m = y_offset_m;
174 _z_offset_m = z_offset_m;
175 _heading_offset_deg = heading_offset_deg;
176 _pitch_offset_deg = pitch_offset_deg;
177 _roll_offset_deg = roll_offset_deg;
178 _goal_heading_offset_deg = heading_offset_deg;
179 _goal_pitch_offset_deg = pitch_offset_deg;
180 _goal_roll_offset_deg = roll_offset_deg;
186 _target_x_offset_m = target_x_offset_m;
187 _target_y_offset_m = target_y_offset_m;
188 _target_z_offset_m = target_z_offset_m;
189 _ground_level_nearplane_m = near_m;
190 // a reasonable guess for init, so that the math doesn't blow up
195 FGViewer::~FGViewer( void ) {
202 _location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
204 _location = (SGLocation *) new SGLocation;
206 if ( _type == FG_LOOKAT ) {
208 _target_location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
210 _target_location = (SGLocation *) new SGLocation;
225 FGViewer::setType ( int type )
234 FGViewer::setLongitude_deg (double lon_deg)
241 FGViewer::setLatitude_deg (double lat_deg)
248 FGViewer::setAltitude_ft (double alt_ft)
255 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
264 FGViewer::setTargetLongitude_deg (double lon_deg)
267 _target_lon_deg = lon_deg;
271 FGViewer::setTargetLatitude_deg (double lat_deg)
274 _target_lat_deg = lat_deg;
278 FGViewer::setTargetAltitude_ft (double alt_ft)
281 _target_alt_ft = alt_ft;
285 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
288 _target_lon_deg = lon_deg;
289 _target_lat_deg = lat_deg;
290 _target_alt_ft = alt_ft;
294 FGViewer::setRoll_deg (double roll_deg)
297 _roll_deg = roll_deg;
301 FGViewer::setPitch_deg (double pitch_deg)
304 _pitch_deg = pitch_deg;
308 FGViewer::setHeading_deg (double heading_deg)
311 _heading_deg = heading_deg;
315 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
318 _roll_deg = roll_deg;
319 _pitch_deg = pitch_deg;
320 _heading_deg = heading_deg;
324 FGViewer::setTargetRoll_deg (double target_roll_deg)
327 _target_roll_deg = target_roll_deg;
331 FGViewer::setTargetPitch_deg (double target_pitch_deg)
334 _target_pitch_deg = target_pitch_deg;
338 FGViewer::setTargetHeading_deg (double target_heading_deg)
341 _target_heading_deg = target_heading_deg;
345 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
348 _target_roll_deg = target_roll_deg;
349 _target_pitch_deg = target_pitch_deg;
350 _target_heading_deg = target_heading_deg;
354 FGViewer::setXOffset_m (double x_offset_m)
357 _x_offset_m = x_offset_m;
361 FGViewer::setYOffset_m (double y_offset_m)
364 _y_offset_m = y_offset_m;
368 FGViewer::setZOffset_m (double z_offset_m)
371 _z_offset_m = z_offset_m;
375 FGViewer::setTargetXOffset_m (double target_x_offset_m)
378 _target_x_offset_m = target_x_offset_m;
382 FGViewer::setTargetYOffset_m (double target_y_offset_m)
385 _target_y_offset_m = target_y_offset_m;
389 FGViewer::setTargetZOffset_m (double target_z_offset_m)
392 _target_z_offset_m = target_z_offset_m;
396 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
399 _x_offset_m = x_offset_m;
400 _y_offset_m = y_offset_m;
401 _z_offset_m = z_offset_m;
405 FGViewer::setRollOffset_deg (double roll_offset_deg)
408 _roll_offset_deg = roll_offset_deg;
412 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
415 _pitch_offset_deg = pitch_offset_deg;
419 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
422 _heading_offset_deg = heading_offset_deg;
426 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
429 _goal_roll_offset_deg = goal_roll_offset_deg;
433 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
436 _goal_pitch_offset_deg = goal_pitch_offset_deg;
437 if ( _goal_pitch_offset_deg < -90 ) {
438 _goal_pitch_offset_deg = -90.0;
440 if ( _goal_pitch_offset_deg > 90.0 ) {
441 _goal_pitch_offset_deg = 90.0;
447 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
450 _goal_heading_offset_deg = goal_heading_offset_deg;
451 while ( _goal_heading_offset_deg < 0.0 ) {
452 _goal_heading_offset_deg += 360;
454 while ( _goal_heading_offset_deg > 360 ) {
455 _goal_heading_offset_deg -= 360;
460 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
463 _roll_offset_deg = roll_offset_deg;
464 _pitch_offset_deg = pitch_offset_deg;
465 _heading_offset_deg = heading_offset_deg;
469 FGViewer::get_absolute_view_pos ()
473 return _absolute_view_pos;
477 FGViewer::getRelativeViewPos ()
481 return _relative_view_pos;
485 FGViewer::getZeroElevViewPos ()
489 return _zero_elev_view_pos;
493 FGViewer::updateFromModelLocation (SGLocation * location)
495 sgCopyMat4(LOCAL, location->getCachedTransformMatrix());
499 FGViewer::updateAtModelLocation (SGLocation * location)
502 location->getCachedTransformMatrix());
506 FGViewer::recalcOurOwnLocation (SGLocation * location, double lon_deg, double lat_deg, double alt_ft,
507 double roll_deg, double pitch_deg, double heading_deg)
509 // update from our own data...
510 dampEyeData(roll_deg, pitch_deg, heading_deg);
511 location->setPosition( lon_deg, lat_deg, alt_ft );
512 location->setOrientation( roll_deg, pitch_deg, heading_deg );
514 location->getTransformMatrix(globals->get_scenery()->get_center()));
517 // recalc() is done every time one of the setters is called (making the
518 // cached data "dirty") on the next "get". It calculates all the outputs
523 if (_type == FG_LOOKFROM) {
532 // recalculate for LookFrom view type...
534 FGViewer::recalcLookFrom ()
537 sgVec3 right, forward;
539 sgVec3 position_offset; // eye position offsets (xyz)
543 // Update location data...
545 // update or data from model location
546 updateFromModelLocation(_location);
548 // update from our own data...
549 recalcOurOwnLocation( _location, _lon_deg, _lat_deg, _alt_ft,
550 _roll_deg, _pitch_deg, _heading_deg );
553 // copy data from location class to local items...
556 // make sg vectors view up, right and forward vectors from LOCAL
557 sgSetVec3( _view_up, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
558 sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
559 sgSetVec3( forward, -LOCAL[0][0], -LOCAL[0][1], -LOCAL[0][2] );
562 // Note that when in "lookfrom" view the "view up" vector is always applied
563 // to the viewer. View up is based on verticle of the aircraft itself. (see
564 // "LOCAL" matrix above)
566 // Orientation Offsets matrix
567 MakeVIEW_OFFSET( VIEW_OFFSET,
568 _heading_offset_deg * SG_DEGREES_TO_RADIANS, _view_up,
569 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
571 // Make the VIEW matrix.
572 sgSetVec4(VIEW[0], right[0], right[1], right[2],SG_ZERO);
573 sgSetVec4(VIEW[1], forward[0], forward[1], forward[2],SG_ZERO);
574 sgSetVec4(VIEW[2], _view_up[0], _view_up[1], _view_up[2],SG_ZERO);
575 sgSetVec4(VIEW[3], SG_ZERO, SG_ZERO, SG_ZERO,SG_ONE);
577 // rotate model or local matrix to get a matrix to apply Eye Position Offsets
578 sgMat4 VIEW_UP; // L0 forward L1 right L2 up
579 sgCopyVec4(VIEW_UP[0], LOCAL[1]);
580 sgCopyVec4(VIEW_UP[1], LOCAL[2]);
581 sgCopyVec4(VIEW_UP[2], LOCAL[0]);
582 sgZeroVec4(VIEW_UP[3]);
584 // Eye Position Offsets to vector
585 sgSetVec3( position_offset, _x_offset_m, _y_offset_m, _z_offset_m );
586 sgXformVec3( position_offset, position_offset, VIEW_UP);
588 // add the offsets including rotations to the translation vector
589 sgAddVec3( _view_pos, position_offset );
591 // multiply the OFFSETS (for heading and pitch) into the VIEW
592 sgPostMultMat4(VIEW, VIEW_OFFSET);
594 // add the position data to the matrix
595 sgSetVec4(VIEW[3], _view_pos[0], _view_pos[1], _view_pos[2],SG_ONE);
600 FGViewer::recalcLookAt ()
604 sgVec3 eye_pos, at_pos;
605 sgVec3 position_offset; // eye position offsets (xyz)
606 sgVec3 target_position_offset; // target position offsets (xyz)
608 // The position vectors originate from the view point or target location
609 // depending on the type of view.
613 // Update location data for target...
615 // update or data from model location
616 updateAtModelLocation(_target_location);
618 // if not model then calculate our own target position...
619 recalcOurOwnLocation( _target_location, _target_lon_deg, _target_lat_deg, _target_alt_ft,
620 _target_roll_deg, _target_pitch_deg, _target_heading_deg );
622 // calculate the "at" target object positon relative to eye or view's tile center...
624 sgdSetVec3(dVec3, _location->get_tile_center()[0], _location->get_tile_center()[1], _location->get_tile_center()[2]);
626 _target_location->get_absolute_view_pos(globals->get_scenery()->get_center()),
628 sgSetVec3(at_pos, dVec3[0], dVec3[1], dVec3[2]);
630 // Update location data for eye...
632 // update or data from model location
633 updateFromModelLocation(_location);
635 // update from our own data, just the rotation here...
636 recalcOurOwnLocation( _location, _lon_deg, _lat_deg, _alt_ft,
637 _roll_deg, _pitch_deg, _heading_deg );
639 // save the eye positon...
640 sgCopyVec3(eye_pos, _location->get_view_pos());
642 // copy data from location class to local items...
645 // make sg vectors view up, right and forward vectors from LOCAL
646 sgSetVec3( _view_up, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
647 sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
649 // rotate model or local matrix to get a matrix to apply Eye Position Offsets
650 sgMat4 VIEW_UP; // L0 forward L1 right L2 up
651 sgCopyVec4(VIEW_UP[0], LOCAL[1]);
652 sgCopyVec4(VIEW_UP[1], LOCAL[2]);
653 sgCopyVec4(VIEW_UP[2], LOCAL[0]);
654 sgZeroVec4(VIEW_UP[3]);
656 // get Orientation Offsets matrix
657 MakeVIEW_OFFSET( VIEW_OFFSET,
658 (_heading_offset_deg - 180) * SG_DEGREES_TO_RADIANS, _view_up,
659 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
661 // add in the position offsets
662 sgSetVec3( position_offset, _y_offset_m, _x_offset_m, _z_offset_m );
663 sgXformVec3( position_offset, position_offset, VIEW_UP);
665 // apply the Orientation offsets
666 sgXformVec3( position_offset, position_offset, VIEW_OFFSET );
668 // add the Position offsets from object to the eye position
669 sgAddVec3( eye_pos, eye_pos, position_offset );
671 // add target offsets to at_position...
672 sgSetVec3(target_position_offset, _target_z_offset_m, _target_x_offset_m,
673 _target_y_offset_m );
674 sgXformVec3(target_position_offset, target_position_offset, ATLOCAL);
675 sgAddVec3( at_pos, at_pos, target_position_offset);
677 sgAddVec3( eye_pos, eye_pos, target_position_offset);
679 // Make the VIEW matrix for a "LOOKAT".
680 sgMakeLookAtMat4( VIEW, eye_pos, at_pos, _view_up );
684 // copy results from location class to viewer...
685 // FIXME: some of these should be changed to reference directly to SGLocation...
687 FGViewer::copyLocationData()
689 // Get our friendly vectors from the eye location...
690 sgCopyVec3(_zero_elev_view_pos, _location->get_zero_elev());
691 sgCopyVec3(_relative_view_pos, _location->get_view_pos());
692 sgdCopyVec3(_absolute_view_pos,
693 _location->get_absolute_view_pos(globals->get_scenery()->get_center()));
694 sgCopyMat4(UP, _location->getCachedUpMatrix());
695 sgCopyVec3(_world_up, _location->get_world_up());
696 // these are the vectors that the sun and moon code like to get...
697 sgCopyVec3(_surface_east, _location->get_surface_east());
698 sgCopyVec3(_surface_south, _location->get_surface_south());
700 // Update viewer's postion data for the eye location...
701 _lon_deg = _location->getLongitude_deg();
702 _lat_deg = _location->getLatitude_deg();
703 _alt_ft = _location->getAltitudeASL_ft();
704 _roll_deg = _location->getRoll_deg();
705 _pitch_deg = _location->getPitch_deg();
706 _heading_deg = _location->getHeading_deg();
708 // Update viewer's postion data for the target (at object) location
709 if (_type == FG_LOOKAT) {
710 _target_lon_deg = _target_location->getLongitude_deg();
711 _target_lat_deg = _target_location->getLatitude_deg();
712 _target_alt_ft = _target_location->getAltitudeASL_ft();
713 _target_roll_deg = _target_location->getRoll_deg();
714 _target_pitch_deg = _target_location->getPitch_deg();
715 _target_heading_deg = _target_location->getHeading_deg();
718 // copy coordinates to outputs for viewer...
719 sgCopyVec3(_zero_elev, _zero_elev_view_pos);
720 sgCopyVec3(_view_pos, _relative_view_pos);
724 FGViewer::dampEyeData (double &roll_deg, double &pitch_deg, double &heading_deg)
726 const double interval = 0.01;
728 static FGViewer *last_view = 0;
729 if (last_view != this) {
731 _damped_roll_deg = roll_deg;
732 _damped_pitch_deg = pitch_deg;
733 _damped_heading_deg = heading_deg;
738 if (_damp_sync < interval) {
739 if (_damp_roll > 0.0)
740 roll_deg = _damped_roll_deg;
741 if (_damp_pitch > 0.0)
742 pitch_deg = _damped_pitch_deg;
743 if (_damp_heading > 0.0)
744 heading_deg = _damped_heading_deg;
748 while (_damp_sync >= interval) {
749 _damp_sync -= interval;
752 if (_damp_roll > 0.0) {
753 d = _damped_roll_deg - roll_deg;
755 _damped_roll_deg -= 360.0;
757 _damped_roll_deg += 360.0;
758 roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
761 if (_damp_pitch > 0.0) {
762 d = _damped_pitch_deg - pitch_deg;
764 _damped_pitch_deg -= 360.0;
766 _damped_pitch_deg += 360.0;
767 pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
770 if (_damp_heading > 0.0) {
771 d = _damped_heading_deg - heading_deg;
773 _damped_heading_deg -= 360.0;
775 _damped_heading_deg += 360.0;
776 heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
782 FGViewer::get_h_fov()
784 switch (_scaling_type) {
785 case FG_SCALING_WIDTH: // h_fov == fov
788 if (_aspect_ratio < 1.0) {
793 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) / _aspect_ratio) *
794 SG_RADIANS_TO_DEGREES * 2;
805 FGViewer::get_v_fov()
807 switch (_scaling_type) {
808 case FG_SCALING_WIDTH: // h_fov == fov
809 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
810 SG_RADIANS_TO_DEGREES * 2;
812 if (_aspect_ratio < 1.0) {
814 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
815 SG_RADIANS_TO_DEGREES * 2;
827 FGViewer::update (double dt)
832 int dt_ms = int(dt * 1000);
833 for ( i = 0; i < dt_ms; i++ ) {
834 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
835 setHeadingOffset_deg( _goal_heading_offset_deg );
838 // move current_view.headingoffset towards
839 // current_view.goal_view_offset
840 if ( _goal_heading_offset_deg > _heading_offset_deg )
842 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
843 incHeadingOffset_deg( 0.5 );
845 incHeadingOffset_deg( -0.5 );
848 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
849 incHeadingOffset_deg( -0.5 );
851 incHeadingOffset_deg( 0.5 );
854 if ( _heading_offset_deg > 360 ) {
855 incHeadingOffset_deg( -360 );
856 } else if ( _heading_offset_deg < 0 ) {
857 incHeadingOffset_deg( 360 );
862 for ( i = 0; i < dt_ms; i++ ) {
863 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
864 setPitchOffset_deg( _goal_pitch_offset_deg );
867 // move current_view.pitch_offset_deg towards
868 // current_view.goal_pitch_offset
869 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
871 incPitchOffset_deg( 1.0 );
873 incPitchOffset_deg( -1.0 );
875 if ( _pitch_offset_deg > 90 ) {
876 setPitchOffset_deg(90);
877 } else if ( _pitch_offset_deg < -90 ) {
878 setPitchOffset_deg( -90 );