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, bool internal ):
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;
165 _internal = internal;
168 _damp_roll = 1.0 / pow(10, fabs(damp_roll));
169 if (damp_pitch > 0.0)
170 _damp_pitch = 1.0 / pow(10, fabs(damp_pitch));
171 if (damp_heading > 0.0)
172 _damp_heading = 1.0 / pow(10, fabs(damp_heading));
174 _x_offset_m = x_offset_m;
175 _y_offset_m = y_offset_m;
176 _z_offset_m = z_offset_m;
177 _heading_offset_deg = heading_offset_deg;
178 _pitch_offset_deg = pitch_offset_deg;
179 _roll_offset_deg = roll_offset_deg;
180 _goal_heading_offset_deg = heading_offset_deg;
181 _goal_pitch_offset_deg = pitch_offset_deg;
182 _goal_roll_offset_deg = roll_offset_deg;
188 _target_x_offset_m = target_x_offset_m;
189 _target_y_offset_m = target_y_offset_m;
190 _target_z_offset_m = target_z_offset_m;
191 _ground_level_nearplane_m = near_m;
192 // a reasonable guess for init, so that the math doesn't blow up
197 FGViewer::~FGViewer( void ) {
204 _location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
206 _location = (SGLocation *) new SGLocation;
208 if ( _type == FG_LOOKAT ) {
210 _target_location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
212 _target_location = (SGLocation *) new SGLocation;
227 FGViewer::setType ( int type )
236 FGViewer::setInternal ( bool internal )
238 _internal = internal;
242 FGViewer::setLongitude_deg (double lon_deg)
249 FGViewer::setLatitude_deg (double lat_deg)
256 FGViewer::setAltitude_ft (double alt_ft)
263 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
272 FGViewer::setTargetLongitude_deg (double lon_deg)
275 _target_lon_deg = lon_deg;
279 FGViewer::setTargetLatitude_deg (double lat_deg)
282 _target_lat_deg = lat_deg;
286 FGViewer::setTargetAltitude_ft (double alt_ft)
289 _target_alt_ft = alt_ft;
293 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
296 _target_lon_deg = lon_deg;
297 _target_lat_deg = lat_deg;
298 _target_alt_ft = alt_ft;
302 FGViewer::setRoll_deg (double roll_deg)
305 _roll_deg = roll_deg;
309 FGViewer::setPitch_deg (double pitch_deg)
312 _pitch_deg = pitch_deg;
316 FGViewer::setHeading_deg (double heading_deg)
319 _heading_deg = heading_deg;
323 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
326 _roll_deg = roll_deg;
327 _pitch_deg = pitch_deg;
328 _heading_deg = heading_deg;
332 FGViewer::setTargetRoll_deg (double target_roll_deg)
335 _target_roll_deg = target_roll_deg;
339 FGViewer::setTargetPitch_deg (double target_pitch_deg)
342 _target_pitch_deg = target_pitch_deg;
346 FGViewer::setTargetHeading_deg (double target_heading_deg)
349 _target_heading_deg = target_heading_deg;
353 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
356 _target_roll_deg = target_roll_deg;
357 _target_pitch_deg = target_pitch_deg;
358 _target_heading_deg = target_heading_deg;
362 FGViewer::setXOffset_m (double x_offset_m)
365 _x_offset_m = x_offset_m;
369 FGViewer::setYOffset_m (double y_offset_m)
372 _y_offset_m = y_offset_m;
376 FGViewer::setZOffset_m (double z_offset_m)
379 _z_offset_m = z_offset_m;
383 FGViewer::setTargetXOffset_m (double target_x_offset_m)
386 _target_x_offset_m = target_x_offset_m;
390 FGViewer::setTargetYOffset_m (double target_y_offset_m)
393 _target_y_offset_m = target_y_offset_m;
397 FGViewer::setTargetZOffset_m (double target_z_offset_m)
400 _target_z_offset_m = target_z_offset_m;
404 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
407 _x_offset_m = x_offset_m;
408 _y_offset_m = y_offset_m;
409 _z_offset_m = z_offset_m;
413 FGViewer::setRollOffset_deg (double roll_offset_deg)
416 _roll_offset_deg = roll_offset_deg;
420 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
423 _pitch_offset_deg = pitch_offset_deg;
427 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
430 _heading_offset_deg = heading_offset_deg;
434 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
437 _goal_roll_offset_deg = goal_roll_offset_deg;
441 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
444 _goal_pitch_offset_deg = goal_pitch_offset_deg;
445 if ( _goal_pitch_offset_deg < -90 ) {
446 _goal_pitch_offset_deg = -90.0;
448 if ( _goal_pitch_offset_deg > 90.0 ) {
449 _goal_pitch_offset_deg = 90.0;
455 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
458 _goal_heading_offset_deg = goal_heading_offset_deg;
459 while ( _goal_heading_offset_deg < 0.0 ) {
460 _goal_heading_offset_deg += 360;
462 while ( _goal_heading_offset_deg > 360 ) {
463 _goal_heading_offset_deg -= 360;
468 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
471 _roll_offset_deg = roll_offset_deg;
472 _pitch_offset_deg = pitch_offset_deg;
473 _heading_offset_deg = heading_offset_deg;
477 FGViewer::get_absolute_view_pos ()
481 return _absolute_view_pos;
485 FGViewer::getRelativeViewPos ()
489 return _relative_view_pos;
493 FGViewer::getZeroElevViewPos ()
497 return _zero_elev_view_pos;
501 FGViewer::updateFromModelLocation (SGLocation * location)
503 sgCopyMat4(LOCAL, location->getCachedTransformMatrix());
507 FGViewer::updateAtModelLocation (SGLocation * location)
510 location->getCachedTransformMatrix());
514 FGViewer::recalcOurOwnLocation (SGLocation * location, double lon_deg, double lat_deg, double alt_ft,
515 double roll_deg, double pitch_deg, double heading_deg)
517 // update from our own data...
518 dampEyeData(roll_deg, pitch_deg, heading_deg);
519 location->setPosition( lon_deg, lat_deg, alt_ft );
520 location->setOrientation( roll_deg, pitch_deg, heading_deg );
522 location->getTransformMatrix(globals->get_scenery()->get_center()));
525 // recalc() is done every time one of the setters is called (making the
526 // cached data "dirty") on the next "get". It calculates all the outputs
531 if (_type == FG_LOOKFROM) {
540 // recalculate for LookFrom view type...
542 FGViewer::recalcLookFrom ()
545 sgVec3 right, forward;
547 sgVec3 position_offset; // eye position offsets (xyz)
551 // Update location data...
553 // update or data from model location
554 updateFromModelLocation(_location);
556 // update from our own data...
557 recalcOurOwnLocation( _location, _lon_deg, _lat_deg, _alt_ft,
558 _roll_deg, _pitch_deg, _heading_deg );
561 // copy data from location class to local items...
564 // make sg vectors view up, right and forward vectors from LOCAL
565 sgSetVec3( _view_up, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
566 sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
567 sgSetVec3( forward, -LOCAL[0][0], -LOCAL[0][1], -LOCAL[0][2] );
570 // Note that when in "lookfrom" view the "view up" vector is always applied
571 // to the viewer. View up is based on verticle of the aircraft itself. (see
572 // "LOCAL" matrix above)
574 // Orientation Offsets matrix
575 MakeVIEW_OFFSET( VIEW_OFFSET,
576 _heading_offset_deg * SG_DEGREES_TO_RADIANS, _view_up,
577 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
579 // Make the VIEW matrix.
580 sgSetVec4(VIEW[0], right[0], right[1], right[2],SG_ZERO);
581 sgSetVec4(VIEW[1], forward[0], forward[1], forward[2],SG_ZERO);
582 sgSetVec4(VIEW[2], _view_up[0], _view_up[1], _view_up[2],SG_ZERO);
583 sgSetVec4(VIEW[3], SG_ZERO, SG_ZERO, SG_ZERO,SG_ONE);
585 // rotate model or local matrix to get a matrix to apply Eye Position Offsets
586 sgMat4 VIEW_UP; // L0 forward L1 right L2 up
587 sgCopyVec4(VIEW_UP[0], LOCAL[1]);
588 sgCopyVec4(VIEW_UP[1], LOCAL[2]);
589 sgCopyVec4(VIEW_UP[2], LOCAL[0]);
590 sgZeroVec4(VIEW_UP[3]);
592 // Eye Position Offsets to vector
593 sgSetVec3( position_offset, _x_offset_m, _y_offset_m, _z_offset_m );
594 sgXformVec3( position_offset, position_offset, VIEW_UP);
596 // add the offsets including rotations to the translation vector
597 sgAddVec3( _view_pos, position_offset );
599 // multiply the OFFSETS (for heading and pitch) into the VIEW
600 sgPostMultMat4(VIEW, VIEW_OFFSET);
602 // add the position data to the matrix
603 sgSetVec4(VIEW[3], _view_pos[0], _view_pos[1], _view_pos[2],SG_ONE);
608 FGViewer::recalcLookAt ()
612 sgVec3 eye_pos, at_pos;
613 sgVec3 position_offset; // eye position offsets (xyz)
614 sgVec3 target_position_offset; // target position offsets (xyz)
616 // The position vectors originate from the view point or target location
617 // depending on the type of view.
621 // Update location data for target...
623 // update or data from model location
624 updateAtModelLocation(_target_location);
626 // if not model then calculate our own target position...
627 recalcOurOwnLocation( _target_location, _target_lon_deg, _target_lat_deg, _target_alt_ft,
628 _target_roll_deg, _target_pitch_deg, _target_heading_deg );
630 // calculate the "at" target object positon relative to eye or view's tile center...
632 sgdSetVec3(dVec3, _location->get_tile_center()[0], _location->get_tile_center()[1], _location->get_tile_center()[2]);
634 _target_location->get_absolute_view_pos(globals->get_scenery()->get_center()),
636 sgSetVec3(at_pos, dVec3[0], dVec3[1], dVec3[2]);
638 // Update location data for eye...
640 // update or data from model location
641 updateFromModelLocation(_location);
643 // update from our own data, just the rotation here...
644 recalcOurOwnLocation( _location, _lon_deg, _lat_deg, _alt_ft,
645 _roll_deg, _pitch_deg, _heading_deg );
647 // save the eye positon...
648 sgCopyVec3(eye_pos, _location->get_view_pos());
650 // copy data from location class to local items...
653 // make sg vectors view up, right and forward vectors from LOCAL
654 sgSetVec3( _view_up, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
655 sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
657 // rotate model or local matrix to get a matrix to apply Eye Position Offsets
658 sgMat4 VIEW_UP; // L0 forward L1 right L2 up
659 sgCopyVec4(VIEW_UP[0], LOCAL[1]);
660 sgCopyVec4(VIEW_UP[1], LOCAL[2]);
661 sgCopyVec4(VIEW_UP[2], LOCAL[0]);
662 sgZeroVec4(VIEW_UP[3]);
664 // get Orientation Offsets matrix
665 MakeVIEW_OFFSET( VIEW_OFFSET,
666 (_heading_offset_deg - 180) * SG_DEGREES_TO_RADIANS, _view_up,
667 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
669 // add in the position offsets
670 sgSetVec3( position_offset, _y_offset_m, _x_offset_m, _z_offset_m );
671 sgXformVec3( position_offset, position_offset, VIEW_UP);
673 // apply the Orientation offsets
674 sgXformVec3( position_offset, position_offset, VIEW_OFFSET );
676 // add the Position offsets from object to the eye position
677 sgAddVec3( eye_pos, eye_pos, position_offset );
679 // add target offsets to at_position...
680 sgSetVec3(target_position_offset, _target_z_offset_m, _target_x_offset_m,
681 _target_y_offset_m );
682 sgXformVec3(target_position_offset, target_position_offset, ATLOCAL);
683 sgAddVec3( at_pos, at_pos, target_position_offset);
685 sgAddVec3( eye_pos, eye_pos, target_position_offset);
687 // Make the VIEW matrix for a "LOOKAT".
688 sgMakeLookAtMat4( VIEW, eye_pos, at_pos, _view_up );
692 // copy results from location class to viewer...
693 // FIXME: some of these should be changed to reference directly to SGLocation...
695 FGViewer::copyLocationData()
697 // Get our friendly vectors from the eye location...
698 sgCopyVec3(_zero_elev_view_pos, _location->get_zero_elev());
699 sgCopyVec3(_relative_view_pos, _location->get_view_pos());
700 sgdCopyVec3(_absolute_view_pos,
701 _location->get_absolute_view_pos(globals->get_scenery()->get_center()));
702 sgCopyMat4(UP, _location->getCachedUpMatrix());
703 sgCopyVec3(_world_up, _location->get_world_up());
704 // these are the vectors that the sun and moon code like to get...
705 sgCopyVec3(_surface_east, _location->get_surface_east());
706 sgCopyVec3(_surface_south, _location->get_surface_south());
708 // Update viewer's postion data for the eye location...
709 _lon_deg = _location->getLongitude_deg();
710 _lat_deg = _location->getLatitude_deg();
711 _alt_ft = _location->getAltitudeASL_ft();
712 _roll_deg = _location->getRoll_deg();
713 _pitch_deg = _location->getPitch_deg();
714 _heading_deg = _location->getHeading_deg();
716 // Update viewer's postion data for the target (at object) location
717 if (_type == FG_LOOKAT) {
718 _target_lon_deg = _target_location->getLongitude_deg();
719 _target_lat_deg = _target_location->getLatitude_deg();
720 _target_alt_ft = _target_location->getAltitudeASL_ft();
721 _target_roll_deg = _target_location->getRoll_deg();
722 _target_pitch_deg = _target_location->getPitch_deg();
723 _target_heading_deg = _target_location->getHeading_deg();
726 // copy coordinates to outputs for viewer...
727 sgCopyVec3(_zero_elev, _zero_elev_view_pos);
728 sgCopyVec3(_view_pos, _relative_view_pos);
732 FGViewer::dampEyeData (double &roll_deg, double &pitch_deg, double &heading_deg)
734 const double interval = 0.01;
736 static FGViewer *last_view = 0;
737 if (last_view != this) {
739 _damped_roll_deg = roll_deg;
740 _damped_pitch_deg = pitch_deg;
741 _damped_heading_deg = heading_deg;
746 if (_damp_sync < interval) {
747 if (_damp_roll > 0.0)
748 roll_deg = _damped_roll_deg;
749 if (_damp_pitch > 0.0)
750 pitch_deg = _damped_pitch_deg;
751 if (_damp_heading > 0.0)
752 heading_deg = _damped_heading_deg;
756 while (_damp_sync >= interval) {
757 _damp_sync -= interval;
760 if (_damp_roll > 0.0) {
761 d = _damped_roll_deg - roll_deg;
763 _damped_roll_deg -= 360.0;
765 _damped_roll_deg += 360.0;
766 roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
769 if (_damp_pitch > 0.0) {
770 d = _damped_pitch_deg - pitch_deg;
772 _damped_pitch_deg -= 360.0;
774 _damped_pitch_deg += 360.0;
775 pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
778 if (_damp_heading > 0.0) {
779 d = _damped_heading_deg - heading_deg;
781 _damped_heading_deg -= 360.0;
783 _damped_heading_deg += 360.0;
784 heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
790 FGViewer::get_h_fov()
792 switch (_scaling_type) {
793 case FG_SCALING_WIDTH: // h_fov == fov
796 if (_aspect_ratio < 1.0) {
801 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) / _aspect_ratio) *
802 SG_RADIANS_TO_DEGREES * 2;
813 FGViewer::get_v_fov()
815 switch (_scaling_type) {
816 case FG_SCALING_WIDTH: // h_fov == fov
817 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
818 SG_RADIANS_TO_DEGREES * 2;
820 if (_aspect_ratio < 1.0) {
822 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
823 SG_RADIANS_TO_DEGREES * 2;
835 FGViewer::update (double dt)
840 int dt_ms = int(dt * 1000);
841 for ( i = 0; i < dt_ms; i++ ) {
842 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
843 setHeadingOffset_deg( _goal_heading_offset_deg );
846 // move current_view.headingoffset towards
847 // current_view.goal_view_offset
848 if ( _goal_heading_offset_deg > _heading_offset_deg )
850 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
851 incHeadingOffset_deg( 0.5 );
853 incHeadingOffset_deg( -0.5 );
856 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
857 incHeadingOffset_deg( -0.5 );
859 incHeadingOffset_deg( 0.5 );
862 if ( _heading_offset_deg > 360 ) {
863 incHeadingOffset_deg( -360 );
864 } else if ( _heading_offset_deg < 0 ) {
865 incHeadingOffset_deg( 360 );
870 for ( i = 0; i < dt_ms; i++ ) {
871 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
872 setPitchOffset_deg( _goal_pitch_offset_deg );
875 // move current_view.pitch_offset_deg towards
876 // current_view.goal_pitch_offset
877 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
879 incPitchOffset_deg( 1.0 );
881 incPitchOffset_deg( -1.0 );
883 if ( _pitch_offset_deg > 90 ) {
884 setPitchOffset_deg(90);
885 } else if ( _pitch_offset_deg < -90 ) {
886 setPitchOffset_deg( -90 );