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>
41 #include <Scenery/scenery.hxx>
44 #include <simgear/math/vector.hxx>
45 #include "globals.hxx"
51 //////////////////////////////////////////////////////////////////
52 // Norman's Optimized matrix rotators! //
53 //////////////////////////////////////////////////////////////////
55 static void fgMakeLOCAL( sgMat4 dst, const double Theta,
56 const double Phi, const double Psi)
58 SGfloat cosTheta = (SGfloat) cos(Theta);
59 SGfloat sinTheta = (SGfloat) sin(Theta);
60 SGfloat cosPhi = (SGfloat) cos(Phi);
61 SGfloat sinPhi = (SGfloat) sin(Phi);
62 SGfloat sinPsi = (SGfloat) sin(Psi) ;
63 SGfloat cosPsi = (SGfloat) cos(Psi) ;
65 dst[0][0] = cosPhi * cosTheta;
66 dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
67 dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
70 dst[1][0] = -sinPhi * cosTheta;
71 dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
72 dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
76 dst[2][1] = cosTheta * -sinPsi;
77 dst[2][2] = cosTheta * cosPsi;
87 // Since these are pure rotation matrices we can save some bookwork
88 // by considering them to be 3x3 until the very end -- NHV
89 static void MakeVIEW_OFFSET( sgMat4 dst,
90 const float angle1, const sgVec3 axis1,
91 const float angle2, const sgVec3 axis2 )
93 // make rotmatrix1 from angle and axis
94 float s = (float) sin ( angle1 ) ;
95 float c = (float) cos ( angle1 ) ;
96 float t = SG_ONE - c ;
99 float tmp = t * axis1[0];
100 mat1[0][0] = tmp * axis1[0] + c ;
101 mat1[0][1] = tmp * axis1[1] + s * axis1[2] ;
102 mat1[0][2] = tmp * axis1[2] - s * axis1[1] ;
105 mat1[1][0] = tmp * axis1[0] - s * axis1[2] ;
106 mat1[1][1] = tmp * axis1[1] + c ;
107 mat1[1][2] = tmp * axis1[2] + s * axis1[0] ;
110 mat1[2][0] = tmp * axis1[0] + s * axis1[1] ;
111 mat1[2][1] = tmp * axis1[1] - s * axis1[0] ;
112 mat1[2][2] = tmp * axis1[2] + c ;
114 // make rotmatrix2 from angle and axis
115 s = (float) sin ( angle2 ) ;
116 c = (float) cos ( angle2 ) ;
121 mat2[0][0] = tmp * axis2[0] + c ;
122 mat2[0][1] = tmp * axis2[1] + s * axis2[2] ;
123 mat2[0][2] = tmp * axis2[2] - s * axis2[1] ;
126 mat2[1][0] = tmp * axis2[0] - s * axis2[2] ;
127 mat2[1][1] = tmp * axis2[1] + c ;
128 mat2[1][2] = tmp * axis2[2] + s * axis2[0] ;
131 mat2[2][0] = tmp * axis2[0] + s * axis2[1] ;
132 mat2[2][1] = tmp * axis2[1] - s * axis2[0] ;
133 mat2[2][2] = tmp * axis2[2] + c ;
136 for ( int j = 0 ; j < 3 ; j++ ) {
137 dst[0][j] = mat2[0][0] * mat1[0][j] +
138 mat2[0][1] * mat1[1][j] +
139 mat2[0][2] * mat1[2][j];
141 dst[1][j] = mat2[1][0] * mat1[0][j] +
142 mat2[1][1] * mat1[1][j] +
143 mat2[1][2] * mat1[2][j];
145 dst[2][j] = mat2[2][0] * mat1[0][j] +
146 mat2[2][1] * mat1[1][j] +
147 mat2[2][2] * mat1[2][j];
149 // fill in 4x4 matrix elements
159 // Taking advantage of the 3x3 nature of this -- NHV
160 inline static void MakeWithWorldUp( sgMat4 dst, const sgMat4 UP, const sgMat4 LOCAL )
167 tmp[0][0] = a*LOCAL[0][0] + b*LOCAL[0][1] + c*LOCAL[0][2] ;
168 tmp[1][0] = a*LOCAL[1][0] + b*LOCAL[1][1] + c*LOCAL[1][2] ;
169 tmp[2][0] = a*LOCAL[2][0] + b*LOCAL[2][1] + c*LOCAL[2][2] ;
170 tmp[3][0] = SG_ZERO ;
175 tmp[0][1] = a*LOCAL[0][0] + b*LOCAL[0][1] + c*LOCAL[0][2] ;
176 tmp[1][1] = a*LOCAL[1][0] + b*LOCAL[1][1] + c*LOCAL[1][2] ;
177 tmp[2][1] = a*LOCAL[2][0] + b*LOCAL[2][1] + c*LOCAL[2][2] ;
178 tmp[3][1] = SG_ZERO ;
182 tmp[0][2] = a*LOCAL[0][0] + c*LOCAL[0][2] ;
183 tmp[1][2] = a*LOCAL[1][0] + c*LOCAL[1][2] ;
184 tmp[2][2] = a*LOCAL[2][0] + c*LOCAL[2][2] ;
185 tmp[3][2] = SG_ZERO ;
187 tmp[0][3] = SG_ZERO ;
188 tmp[1][3] = SG_ZERO ;
189 tmp[2][3] = SG_ZERO ;
191 sgCopyMat4(dst, tmp);
195 ////////////////////////////////////////////////////////////////////////
196 // Implementation of FGViewer.
197 ////////////////////////////////////////////////////////////////////////
200 FGViewer::FGViewer( void ):
201 _scaling_type(FG_SCALING_MAX),
216 _heading_offset_deg(0),
217 _pitch_offset_deg(0),
219 _goal_heading_offset_deg(0.0),
220 _goal_pitch_offset_deg(0.0)
222 sgdZeroVec3(_absolute_view_pos);
223 //a reasonable guess for init, so that the math doesn't blow up
228 FGViewer::~FGViewer( void ) {
247 FGViewer::setType ( int type )
256 FGViewer::setLongitude_deg (double lon_deg)
263 FGViewer::setLatitude_deg (double lat_deg)
270 FGViewer::setAltitude_ft (double alt_ft)
277 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
286 FGViewer::setTargetLongitude_deg (double lon_deg)
289 _target_lon_deg = lon_deg;
293 FGViewer::setTargetLatitude_deg (double lat_deg)
296 _target_lat_deg = lat_deg;
300 FGViewer::setTargetAltitude_ft (double alt_ft)
303 _target_alt_ft = alt_ft;
307 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
310 _target_lon_deg = lon_deg;
311 _target_lat_deg = lat_deg;
312 _target_alt_ft = alt_ft;
316 FGViewer::setRoll_deg (double roll_deg)
319 _roll_deg = roll_deg;
323 FGViewer::setPitch_deg (double pitch_deg)
326 _pitch_deg = pitch_deg;
330 FGViewer::setHeading_deg (double heading_deg)
333 _heading_deg = heading_deg;
337 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
340 _roll_deg = roll_deg;
341 _pitch_deg = pitch_deg;
342 _heading_deg = heading_deg;
346 FGViewer::setTargetRoll_deg (double target_roll_deg)
349 _target_roll_deg = target_roll_deg;
353 FGViewer::setTargetPitch_deg (double target_pitch_deg)
356 _target_pitch_deg = target_pitch_deg;
360 FGViewer::setTargetHeading_deg (double target_heading_deg)
363 _target_heading_deg = target_heading_deg;
367 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
370 _target_roll_deg = target_roll_deg;
371 _target_pitch_deg = target_pitch_deg;
372 _target_heading_deg = target_heading_deg;
376 FGViewer::setXOffset_m (double x_offset_m)
379 _x_offset_m = x_offset_m;
383 FGViewer::setYOffset_m (double y_offset_m)
386 _y_offset_m = y_offset_m;
390 FGViewer::setZOffset_m (double z_offset_m)
393 _z_offset_m = z_offset_m;
397 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
400 _x_offset_m = x_offset_m;
401 _y_offset_m = y_offset_m;
402 _z_offset_m = z_offset_m;
406 FGViewer::setRollOffset_deg (double roll_offset_deg)
409 _roll_offset_deg = roll_offset_deg;
413 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
416 _pitch_offset_deg = pitch_offset_deg;
420 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
423 _heading_offset_deg = heading_offset_deg;
427 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
430 _goal_roll_offset_deg = goal_roll_offset_deg;
434 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
437 _goal_pitch_offset_deg = goal_pitch_offset_deg;
438 if ( _goal_pitch_offset_deg < -90 ) {
439 _goal_pitch_offset_deg = -90.0;
441 if ( _goal_pitch_offset_deg > 90.0 ) {
442 _goal_pitch_offset_deg = 90.0;
448 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
451 _goal_heading_offset_deg = goal_heading_offset_deg;
452 while ( _goal_heading_offset_deg < 0.0 ) {
453 _goal_heading_offset_deg += 360;
455 while ( _goal_heading_offset_deg > 360 ) {
456 _goal_heading_offset_deg -= 360;
461 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
464 _roll_offset_deg = roll_offset_deg;
465 _pitch_offset_deg = pitch_offset_deg;
466 _heading_offset_deg = heading_offset_deg;
470 FGViewer::get_absolute_view_pos ()
474 return _absolute_view_pos;
478 FGViewer::getRelativeViewPos ()
482 return _relative_view_pos;
486 FGViewer::getZeroElevViewPos ()
490 return _zero_elev_view_pos;
494 // recalc() is done every time one of the setters is called (making the
495 // cached data "dirty") on the next "get". It calculates all the outputs
500 sgVec3 minus_z, right, forward, tilt;
501 sgMat4 tmpROT; // temp rotation work matrices
502 sgMat4 VIEW_HEADINGOFFSET, VIEW_PITCHOFFSET;
503 sgVec3 tmpVec3; // temp work vector (3)
504 sgVec3 eye_pos, object_pos;
506 // The position vectors originate from the view point or target location
507 // depending on the type of view.
508 // FIXME: Later note: actually the object (target) info needs to be held
509 // by the model class.
511 if (_type == FG_RPH) {
512 // eye position is the location of the pilot
513 recalcPositionVectors( _lon_deg, _lat_deg, _alt_ft );
515 // eye position is now calculated based on lon/lat;
516 recalcPositionVectors( _lon_deg, _lat_deg, _alt_ft );
517 sgCopyVec3(eye_pos, _relative_view_pos);
519 // object position is the location of the object being looked at
520 recalcPositionVectors( _target_lon_deg, _target_lat_deg, _target_alt_ft );
522 // the coordinates generated by the above "recalcPositionVectors"
523 sgCopyVec3(_zero_elev, _zero_elev_view_pos);
524 sgCopyVec3(_view_pos, _relative_view_pos);
527 // Doing this last recalc here for published values...where the airplane is
528 // This should be per aircraft or model (for published values) before
529 // multiple FDM can be done.
530 recalcPositionVectors(fgGetDouble("/position/longitude-deg"),
531 fgGetDouble("/position/latitude-deg"),
532 fgGetDouble("/position/altitude-ft"));
536 // Make the world up rotation matrix for eye positioin...
537 sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
540 // get the world up radial vector from planet center
541 // (ie. effect of aircraft location on earth "sphere" approximation)
542 sgSetVec3( _world_up, UP[0][0], UP[0][1], UP[0][2] );
546 // Creat local matrix with current geodetic position. Converting
547 // the orientation (pitch/roll/heading) to vectors.
548 fgMakeLOCAL( LOCAL, _pitch_deg * SG_DEGREES_TO_RADIANS,
549 _roll_deg * SG_DEGREES_TO_RADIANS,
550 -_heading_deg * SG_DEGREES_TO_RADIANS);
551 // Adjust LOCAL to current world_up vector (adjustment for planet location)
552 MakeWithWorldUp( LOCAL, UP, LOCAL );
553 // copy the LOCAL matrix to COCKPIT_ROT for publication...
554 sgCopyMat4( LOCAL_ROT, LOCAL );
556 // make sg vectors view up, right and forward vectors from LOCAL
557 sgSetVec3( _view_up, LOCAL[0][0], LOCAL[0][1], LOCAL[0][2] );
558 sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
559 sgSetVec3( forward, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
563 // create xyz offsets Vector
564 sgVec3 position_offset;
565 sgSetVec3( position_offset, _y_offset_m, _x_offset_m, _z_offset_m );
569 // Looking up/down left/right in pilot view (lookfrom mode)
570 // or Floating Rotatation around the object in chase view (lookat mode).
571 // Generate the offset matrix to be applied using offset angles:
572 if (_type == FG_LOOKAT) {
573 // Note that when in "lookat" view the "world up" vector is always applied
574 // to the viewer. World up is based on verticle at a given lon/lat (see
575 // matrix "UP" above).
576 MakeVIEW_OFFSET( VIEW_OFFSET,
577 _heading_offset_deg * SG_DEGREES_TO_RADIANS, _world_up,
578 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
580 if (_type == FG_RPH) {
581 // Note that when in "lookfrom" view the "view up" vector is always applied
582 // to the viewer. View up is based on verticle of the aircraft itself. (see
583 // "LOCAL" matrix above)
584 MakeVIEW_OFFSET( VIEW_OFFSET,
585 _heading_offset_deg * SG_DEGREES_TO_RADIANS, _view_up,
586 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
591 if (_type == FG_LOOKAT) {
593 // transfrom "offset" and "orientation offset" to vector
594 sgXformVec3( position_offset, position_offset, UP );
596 // add heading to offset so that the eye does heading as such...
597 sgMakeRotMat4(tmpROT, -_heading_deg, _world_up);
598 sgPostMultMat4(VIEW_OFFSET, tmpROT);
599 sgXformVec3( position_offset, position_offset, VIEW_OFFSET );
601 // add the offsets from object to the eye position
602 sgAddVec3( eye_pos, eye_pos, position_offset );
604 sgCopyVec3( object_pos, _view_pos );
606 // Make the VIEW matrix for "lookat".
607 sgMakeLookAtMat4( VIEW, eye_pos, object_pos, _view_up );
610 if (_type == FG_RPH) {
612 sgXformVec3( position_offset, position_offset, LOCAL);
613 // add the offsets including rotations to the coordinates
614 sgAddVec3( _view_pos, position_offset );
616 // Make the VIEW matrix.
617 VIEW[0][0] = right[0];
618 VIEW[0][1] = right[1];
619 VIEW[0][2] = right[2];
621 VIEW[1][0] = forward[0];
622 VIEW[1][1] = forward[1];
623 VIEW[1][2] = forward[2];
625 VIEW[2][0] = _view_up[0];
626 VIEW[2][1] = _view_up[1];
627 VIEW[2][2] = _view_up[2];
633 // multiply the OFFSETS (for heading and pitch) into the VIEW
634 sgPostMultMat4(VIEW, VIEW_OFFSET);
636 // add the position data to the matrix
637 VIEW[3][0] = _view_pos[0];
638 VIEW[3][1] = _view_pos[1];
639 VIEW[3][2] = _view_pos[2];
644 // the VIEW matrix includes both rotation and translation. Let's
645 // knock out the translation part to make the VIEW_ROT matrix
646 sgCopyMat4( VIEW_ROT, VIEW );
647 VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
649 // Given a vector pointing straight down (-Z), map into onto the
650 // local plane representing "horizontal". This should give us the
651 // local direction for moving "south".
652 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
654 sgmap_vec_onto_cur_surface_plane(_world_up, _view_pos, minus_z,
656 sgNormalizeVec3(_surface_south);
658 // now calculate the surface east vector
660 sgNegateVec3(world_down, _world_up);
661 sgVectorProductVec3(_surface_east, _surface_south, world_down);
667 FGViewer::recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const
669 double sea_level_radius_m;
673 // Convert from geodetic to geocentric
675 sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
676 alt_ft * SG_FEET_TO_METER,
680 // Calculate the cartesian coordinates
681 // of point directly below at sea level.
682 // aka Zero Elevation Position
683 Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
686 Point3D tmp = sgPolarToCart3d(p) - scenery.get_next_center();
687 sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
689 // Calculate the absolute view position
690 // in fgfs coordinates.
691 // aka Absolute View Position
692 p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
693 tmp = sgPolarToCart3d(p);
694 sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
696 // Calculate the relative view position
697 // from the scenery center.
698 // aka Relative View Position
699 sgdVec3 scenery_center;
700 sgdSetVec3(scenery_center,
701 scenery.get_next_center().x(),
702 scenery.get_next_center().y(),
703 scenery.get_next_center().z());
705 sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
706 sgSetVec3(_relative_view_pos, view_pos);
711 FGViewer::get_h_fov()
713 switch (_scaling_type) {
714 case FG_SCALING_WIDTH: // h_fov == fov
717 if (_aspect_ratio < 1.0) {
722 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) / _aspect_ratio) *
723 SG_RADIANS_TO_DEGREES * 2;
731 FGViewer::get_v_fov()
733 switch (_scaling_type) {
734 case FG_SCALING_WIDTH: // h_fov == fov
735 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
736 SG_RADIANS_TO_DEGREES * 2;
738 if (_aspect_ratio < 1.0) {
740 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
741 SG_RADIANS_TO_DEGREES * 2;
752 FGViewer::update (int dt)
755 for ( i = 0; i < dt; i++ ) {
756 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
757 setHeadingOffset_deg( _goal_heading_offset_deg );
760 // move current_view.headingoffset towards
761 // current_view.goal_view_offset
762 if ( _goal_heading_offset_deg > _heading_offset_deg )
764 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
765 incHeadingOffset_deg( 0.5 );
767 incHeadingOffset_deg( -0.5 );
770 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
771 incHeadingOffset_deg( -0.5 );
773 incHeadingOffset_deg( 0.5 );
776 if ( _heading_offset_deg > 360 ) {
777 incHeadingOffset_deg( -360 );
778 } else if ( _heading_offset_deg < 0 ) {
779 incHeadingOffset_deg( 360 );
784 for ( i = 0; i < dt; i++ ) {
785 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
786 setPitchOffset_deg( _goal_pitch_offset_deg );
789 // move current_view.pitch_offset_deg towards
790 // current_view.goal_pitch_offset
791 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
793 incPitchOffset_deg( 1.0 );
795 incPitchOffset_deg( -1.0 );
797 if ( _pitch_offset_deg > 90 ) {
798 setPitchOffset_deg(90);
799 } else if ( _pitch_offset_deg < -90 ) {
800 setPitchOffset_deg( -90 );