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>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/constants.h>
35 #include <simgear/math/point3d.hxx>
36 #include <simgear/math/polar3d.hxx>
37 #include <simgear/math/sg_geodesy.hxx>
39 #include <Scenery/scenery.hxx>
42 #include <simgear/math/vector.hxx>
43 #include "globals.hxx"
49 //////////////////////////////////////////////////////////////////
50 // Norman's Optimized matrix rotators! //
51 //////////////////////////////////////////////////////////////////
53 static void fgMakeLOCAL( sgMat4 dst, const double Theta,
54 const double Phi, const double Psi)
56 SGfloat cosTheta = (SGfloat) cos(Theta);
57 SGfloat sinTheta = (SGfloat) sin(Theta);
58 SGfloat cosPhi = (SGfloat) cos(Phi);
59 SGfloat sinPhi = (SGfloat) sin(Phi);
60 SGfloat sinPsi = (SGfloat) sin(Psi) ;
61 SGfloat cosPsi = (SGfloat) cos(Psi) ;
63 dst[0][0] = cosPhi * cosTheta;
64 dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
65 dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
68 dst[1][0] = -sinPhi * cosTheta;
69 dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
70 dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
74 dst[2][1] = cosTheta * -sinPsi;
75 dst[2][2] = cosTheta * cosPsi;
85 // Since these are pure rotation matrices we can save some bookwork
86 // by considering them to be 3x3 until the very end -- NHV
87 static void MakeVIEW_OFFSET( sgMat4 dst,
88 const float angle1, const sgVec3 axis1,
89 const float angle2, const sgVec3 axis2 )
91 // make rotmatrix1 from angle and axis
92 float s = (float) sin ( angle1 ) ;
93 float c = (float) cos ( angle1 ) ;
94 float t = SG_ONE - c ;
97 float tmp = t * axis1[0];
98 mat1[0][0] = tmp * axis1[0] + c ;
99 mat1[0][1] = tmp * axis1[1] + s * axis1[2] ;
100 mat1[0][2] = tmp * axis1[2] - s * axis1[1] ;
103 mat1[1][0] = tmp * axis1[0] - s * axis1[2] ;
104 mat1[1][1] = tmp * axis1[1] + c ;
105 mat1[1][2] = tmp * axis1[2] + s * axis1[0] ;
108 mat1[2][0] = tmp * axis1[0] + s * axis1[1] ;
109 mat1[2][1] = tmp * axis1[1] - s * axis1[0] ;
110 mat1[2][2] = tmp * axis1[2] + c ;
112 // make rotmatrix2 from angle and axis
113 s = (float) sin ( angle2 ) ;
114 c = (float) cos ( angle2 ) ;
119 mat2[0][0] = tmp * axis2[0] + c ;
120 mat2[0][1] = tmp * axis2[1] + s * axis2[2] ;
121 mat2[0][2] = tmp * axis2[2] - s * axis2[1] ;
124 mat2[1][0] = tmp * axis2[0] - s * axis2[2] ;
125 mat2[1][1] = tmp * axis2[1] + c ;
126 mat2[1][2] = tmp * axis2[2] + s * axis2[0] ;
129 mat2[2][0] = tmp * axis2[0] + s * axis2[1] ;
130 mat2[2][1] = tmp * axis2[1] - s * axis2[0] ;
131 mat2[2][2] = tmp * axis2[2] + c ;
134 for ( int j = 0 ; j < 3 ; j++ ) {
135 dst[0][j] = mat2[0][0] * mat1[0][j] +
136 mat2[0][1] * mat1[1][j] +
137 mat2[0][2] * mat1[2][j];
139 dst[1][j] = mat2[1][0] * mat1[0][j] +
140 mat2[1][1] * mat1[1][j] +
141 mat2[1][2] * mat1[2][j];
143 dst[2][j] = mat2[2][0] * mat1[0][j] +
144 mat2[2][1] * mat1[1][j] +
145 mat2[2][2] * mat1[2][j];
147 // fill in 4x4 matrix elements
157 // Taking advantage of the 3x3 nature of this -- NHV
158 inline static void MakeWithWorldUp( sgMat4 dst, const sgMat4 UP, const sgMat4 LOCAL )
165 tmp[0][0] = a*LOCAL[0][0] + b*LOCAL[0][1] + c*LOCAL[0][2] ;
166 tmp[1][0] = a*LOCAL[1][0] + b*LOCAL[1][1] + c*LOCAL[1][2] ;
167 tmp[2][0] = a*LOCAL[2][0] + b*LOCAL[2][1] + c*LOCAL[2][2] ;
168 tmp[3][0] = SG_ZERO ;
173 tmp[0][1] = a*LOCAL[0][0] + b*LOCAL[0][1] + c*LOCAL[0][2] ;
174 tmp[1][1] = a*LOCAL[1][0] + b*LOCAL[1][1] + c*LOCAL[1][2] ;
175 tmp[2][1] = a*LOCAL[2][0] + b*LOCAL[2][1] + c*LOCAL[2][2] ;
176 tmp[3][1] = SG_ZERO ;
180 tmp[0][2] = a*LOCAL[0][0] + c*LOCAL[0][2] ;
181 tmp[1][2] = a*LOCAL[1][0] + c*LOCAL[1][2] ;
182 tmp[2][2] = a*LOCAL[2][0] + c*LOCAL[2][2] ;
183 tmp[3][2] = SG_ZERO ;
185 tmp[0][3] = SG_ZERO ;
186 tmp[1][3] = SG_ZERO ;
187 tmp[2][3] = SG_ZERO ;
189 sgCopyMat4(dst, tmp);
193 ////////////////////////////////////////////////////////////////////////
194 // Implementation of FGViewer.
195 ////////////////////////////////////////////////////////////////////////
198 FGViewer::FGViewer( void ):
199 _scaling_type(FG_SCALING_MAX),
214 _heading_offset_deg(0),
215 _pitch_offset_deg(0),
217 _goal_heading_offset_deg(0.0),
218 _goal_pitch_offset_deg(0.0)
220 sgdZeroVec3(_absolute_view_pos);
221 //a reasonable guess for init, so that the math doesn't blow up
226 FGViewer::~FGViewer( void ) {
245 FGViewer::setType ( int type )
254 FGViewer::setLongitude_deg (double lon_deg)
261 FGViewer::setLatitude_deg (double lat_deg)
268 FGViewer::setAltitude_ft (double alt_ft)
275 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
284 FGViewer::setTargetLongitude_deg (double lon_deg)
287 _target_lon_deg = lon_deg;
291 FGViewer::setTargetLatitude_deg (double lat_deg)
294 _target_lat_deg = lat_deg;
298 FGViewer::setTargetAltitude_ft (double alt_ft)
301 _target_alt_ft = alt_ft;
305 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
308 _target_lon_deg = lon_deg;
309 _target_lat_deg = lat_deg;
310 _target_alt_ft = alt_ft;
314 FGViewer::setRoll_deg (double roll_deg)
317 _roll_deg = roll_deg;
321 FGViewer::setPitch_deg (double pitch_deg)
324 _pitch_deg = pitch_deg;
328 FGViewer::setHeading_deg (double heading_deg)
331 _heading_deg = heading_deg;
335 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
338 _roll_deg = roll_deg;
339 _pitch_deg = pitch_deg;
340 _heading_deg = heading_deg;
344 FGViewer::setTargetRoll_deg (double target_roll_deg)
347 _target_roll_deg = target_roll_deg;
351 FGViewer::setTargetPitch_deg (double target_pitch_deg)
354 _target_pitch_deg = target_pitch_deg;
358 FGViewer::setTargetHeading_deg (double target_heading_deg)
361 _target_heading_deg = target_heading_deg;
365 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
368 _target_roll_deg = target_roll_deg;
369 _target_pitch_deg = target_pitch_deg;
370 _target_heading_deg = target_heading_deg;
374 FGViewer::setXOffset_m (double x_offset_m)
377 _x_offset_m = x_offset_m;
381 FGViewer::setYOffset_m (double y_offset_m)
384 _y_offset_m = y_offset_m;
388 FGViewer::setZOffset_m (double z_offset_m)
391 _z_offset_m = z_offset_m;
395 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
398 _x_offset_m = x_offset_m;
399 _y_offset_m = y_offset_m;
400 _z_offset_m = z_offset_m;
404 FGViewer::setRollOffset_deg (double roll_offset_deg)
407 _roll_offset_deg = roll_offset_deg;
411 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
414 _pitch_offset_deg = pitch_offset_deg;
418 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
421 _heading_offset_deg = heading_offset_deg;
425 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
428 _goal_roll_offset_deg = goal_roll_offset_deg;
432 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
435 _goal_pitch_offset_deg = goal_pitch_offset_deg;
436 if ( _goal_pitch_offset_deg < -90 ) {
437 _goal_pitch_offset_deg = -90.0;
439 if ( _goal_pitch_offset_deg > 90.0 ) {
440 _goal_pitch_offset_deg = 90.0;
446 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
449 _goal_heading_offset_deg = goal_heading_offset_deg;
450 while ( _goal_heading_offset_deg < 0.0 ) {
451 _goal_heading_offset_deg += 360;
453 while ( _goal_heading_offset_deg > 360 ) {
454 _goal_heading_offset_deg -= 360;
459 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
462 _roll_offset_deg = roll_offset_deg;
463 _pitch_offset_deg = pitch_offset_deg;
464 _heading_offset_deg = heading_offset_deg;
468 FGViewer::get_absolute_view_pos ()
472 return _absolute_view_pos;
476 FGViewer::getRelativeViewPos ()
480 return _relative_view_pos;
484 FGViewer::getZeroElevViewPos ()
488 return _zero_elev_view_pos;
492 // recalc() is done every time one of the setters is called (making the
493 // cached data "dirty") on the next "get". It calculates all the outputs
498 sgVec3 minus_z, right, forward, tilt;
499 sgMat4 tmpROT; // temp rotation work matrices
500 sgMat4 VIEW_HEADINGOFFSET, VIEW_PITCHOFFSET;
501 sgVec3 tmpVec3; // temp work vector (3)
502 sgVec3 eye_pos, object_pos;
504 // The position vectors originate from the view point or target location
505 // depending on the type of view.
506 // FIXME: Later note: actually the object (target) info needs to be held
507 // by the model class.
509 if (_type == FG_RPH) {
510 // eye position is the location of the pilot
511 recalcPositionVectors( _lon_deg, _lat_deg, _alt_ft );
513 // eye position is now calculated based on lon/lat;
514 recalcPositionVectors( _lon_deg, _lat_deg, _alt_ft );
515 sgCopyVec3(eye_pos, _relative_view_pos);
517 // object position is the location of the object being looked at
518 recalcPositionVectors( _target_lon_deg, _target_lat_deg, _target_alt_ft );
520 // the coordinates generated by the above "recalcPositionVectors"
521 sgCopyVec3(_zero_elev, _zero_elev_view_pos);
522 sgCopyVec3(_view_pos, _relative_view_pos);
524 // Make the world up rotation matrix for eye positioin...
525 sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
528 // get the world up radial vector from planet center
529 // (ie. effect of aircraft location on earth "sphere" approximation)
530 sgSetVec3( _world_up, UP[0][0], UP[0][1], UP[0][2] );
534 // Creat local matrix with current geodetic position. Converting
535 // the orientation (pitch/roll/heading) to vectors.
536 fgMakeLOCAL( LOCAL, _pitch_deg * SG_DEGREES_TO_RADIANS,
537 _roll_deg * SG_DEGREES_TO_RADIANS,
538 -_heading_deg * SG_DEGREES_TO_RADIANS);
539 // Adjust LOCAL to current world_up vector (adjustment for planet location)
540 MakeWithWorldUp( LOCAL, UP, LOCAL );
541 // copy the LOCAL matrix to COCKPIT_ROT for publication...
542 sgCopyMat4( LOCAL_ROT, LOCAL );
544 // make sg vectors view up, right and forward vectors from LOCAL
545 sgSetVec3( _view_up, LOCAL[0][0], LOCAL[0][1], LOCAL[0][2] );
546 sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
547 sgSetVec3( forward, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
551 // create xyz offsets Vector
552 sgVec3 position_offset;
553 sgSetVec3( position_offset, _y_offset_m, _x_offset_m, _z_offset_m );
557 // Looking up/down left/right in pilot view (lookfrom mode)
558 // or Floating Rotatation around the object in chase view (lookat mode).
559 // Generate the offset matrix to be applied using offset angles:
560 if (_type == FG_LOOKAT) {
561 // Note that when in "lookat" view the "world up" vector is always applied
562 // to the viewer. World up is based on verticle at a given lon/lat (see
563 // matrix "UP" above).
564 MakeVIEW_OFFSET( VIEW_OFFSET,
565 _heading_offset_deg * SG_DEGREES_TO_RADIANS, _world_up,
566 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
568 if (_type == FG_RPH) {
569 // Note that when in "lookfrom" view the "view up" vector is always applied
570 // to the viewer. View up is based on verticle of the aircraft itself. (see
571 // "LOCAL" matrix above)
572 MakeVIEW_OFFSET( VIEW_OFFSET,
573 _heading_offset_deg * SG_DEGREES_TO_RADIANS, _view_up,
574 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
579 if (_type == FG_LOOKAT) {
581 // transfrom "offset" and "orientation offset" to vector
582 sgXformVec3( position_offset, position_offset, UP );
584 // add heading to offset so that the eye does heading as such...
585 sgMakeRotMat4(tmpROT, -_heading_deg, _world_up);
586 sgPostMultMat4(VIEW_OFFSET, tmpROT);
587 sgXformVec3( position_offset, position_offset, VIEW_OFFSET );
589 // add the offsets from object to the eye position
590 sgAddVec3( eye_pos, eye_pos, position_offset );
592 sgCopyVec3( object_pos, _view_pos );
594 // Make the VIEW matrix for "lookat".
595 sgMakeLookAtMat4( VIEW, eye_pos, object_pos, _view_up );
598 if (_type == FG_RPH) {
600 sgXformVec3( position_offset, position_offset, LOCAL);
601 // add the offsets including rotations to the coordinates
602 sgAddVec3( _view_pos, position_offset );
604 // Make the VIEW matrix.
605 VIEW[0][0] = right[0];
606 VIEW[0][1] = right[1];
607 VIEW[0][2] = right[2];
609 VIEW[1][0] = forward[0];
610 VIEW[1][1] = forward[1];
611 VIEW[1][2] = forward[2];
613 VIEW[2][0] = _view_up[0];
614 VIEW[2][1] = _view_up[1];
615 VIEW[2][2] = _view_up[2];
621 // multiply the OFFSETS (for heading and pitch) into the VIEW
622 sgPostMultMat4(VIEW, VIEW_OFFSET);
624 // add the position data to the matrix
625 VIEW[3][0] = _view_pos[0];
626 VIEW[3][1] = _view_pos[1];
627 VIEW[3][2] = _view_pos[2];
632 // the VIEW matrix includes both rotation and translation. Let's
633 // knock out the translation part to make the VIEW_ROT matrix
634 sgCopyMat4( VIEW_ROT, VIEW );
635 VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
637 // Given a vector pointing straight down (-Z), map into onto the
638 // local plane representing "horizontal". This should give us the
639 // local direction for moving "south".
640 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
642 sgmap_vec_onto_cur_surface_plane(_world_up, _view_pos, minus_z,
644 sgNormalizeVec3(_surface_south);
646 // now calculate the surface east vector
648 sgNegateVec3(world_down, _world_up);
649 sgVectorProductVec3(_surface_east, _surface_south, world_down);
655 FGViewer::recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const
657 double sea_level_radius_m;
661 // Convert from geodetic to geocentric
663 sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
664 alt_ft * SG_FEET_TO_METER,
668 // Calculate the cartesian coordinates
669 // of point directly below at sea level.
670 // aka Zero Elevation Position
671 Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
674 Point3D tmp = sgPolarToCart3d(p) - scenery.get_next_center();
675 sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
677 // Calculate the absolute view position
678 // in fgfs coordinates.
679 // aka Absolute View Position
680 p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
681 tmp = sgPolarToCart3d(p);
682 sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
684 // Calculate the relative view position
685 // from the scenery center.
686 // aka Relative View Position
687 sgdVec3 scenery_center;
688 sgdSetVec3(scenery_center,
689 scenery.get_next_center().x(),
690 scenery.get_next_center().y(),
691 scenery.get_next_center().z());
693 sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
694 sgSetVec3(_relative_view_pos, view_pos);
699 FGViewer::get_h_fov()
701 switch (_scaling_type) {
702 case FG_SCALING_WIDTH: // h_fov == fov
705 if (_aspect_ratio < 1.0) {
710 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) / _aspect_ratio) *
711 SG_RADIANS_TO_DEGREES * 2;
719 FGViewer::get_v_fov()
721 switch (_scaling_type) {
722 case FG_SCALING_WIDTH: // h_fov == fov
723 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
724 SG_RADIANS_TO_DEGREES * 2;
726 if (_aspect_ratio < 1.0) {
728 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
729 SG_RADIANS_TO_DEGREES * 2;
740 FGViewer::update (int dt)
743 for ( i = 0; i < dt; i++ ) {
744 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
745 setHeadingOffset_deg( _goal_heading_offset_deg );
748 // move current_view.headingoffset towards
749 // current_view.goal_view_offset
750 if ( _goal_heading_offset_deg > _heading_offset_deg )
752 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
753 incHeadingOffset_deg( 0.5 );
755 incHeadingOffset_deg( -0.5 );
758 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
759 incHeadingOffset_deg( -0.5 );
761 incHeadingOffset_deg( 0.5 );
764 if ( _heading_offset_deg > 360 ) {
765 incHeadingOffset_deg( -360 );
766 } else if ( _heading_offset_deg < 0 ) {
767 incHeadingOffset_deg( 360 );
772 for ( i = 0; i < dt; i++ ) {
773 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
774 setPitchOffset_deg( _goal_pitch_offset_deg );
777 // move current_view.pitch_offset_deg towards
778 // current_view.goal_pitch_offset
779 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
781 incPitchOffset_deg( 1.0 );
783 incPitchOffset_deg( -1.0 );
785 if ( _pitch_offset_deg > 90 ) {
786 setPitchOffset_deg(90);
787 } else if ( _pitch_offset_deg < -90 ) {
788 setPitchOffset_deg( -90 );