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::setXOffset_m (double x_offset_m)
347 _x_offset_m = x_offset_m;
351 FGViewer::setYOffset_m (double y_offset_m)
354 _y_offset_m = y_offset_m;
358 FGViewer::setZOffset_m (double z_offset_m)
361 _z_offset_m = z_offset_m;
365 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
368 _x_offset_m = x_offset_m;
369 _y_offset_m = y_offset_m;
370 _z_offset_m = z_offset_m;
374 FGViewer::setRollOffset_deg (double roll_offset_deg)
377 _roll_offset_deg = roll_offset_deg;
381 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
384 _pitch_offset_deg = pitch_offset_deg;
388 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
391 _heading_offset_deg = heading_offset_deg;
395 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
398 _goal_roll_offset_deg = goal_roll_offset_deg;
402 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
405 _goal_pitch_offset_deg = goal_pitch_offset_deg;
406 if ( _goal_pitch_offset_deg < -90 ) {
407 _goal_pitch_offset_deg = -90.0;
409 if ( _goal_pitch_offset_deg > 90.0 ) {
410 _goal_pitch_offset_deg = 90.0;
416 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
419 _goal_heading_offset_deg = goal_heading_offset_deg;
420 while ( _goal_heading_offset_deg < 0.0 ) {
421 _goal_heading_offset_deg += 360;
423 while ( _goal_heading_offset_deg > 360 ) {
424 _goal_heading_offset_deg -= 360;
429 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
432 _roll_offset_deg = roll_offset_deg;
433 _pitch_offset_deg = pitch_offset_deg;
434 _heading_offset_deg = heading_offset_deg;
438 FGViewer::get_absolute_view_pos ()
442 return _absolute_view_pos;
446 FGViewer::getRelativeViewPos ()
450 return _relative_view_pos;
454 FGViewer::getZeroElevViewPos ()
458 return _zero_elev_view_pos;
462 // recalc() is done every time one of the setters is called (making the
463 // cached data "dirty") on the next "get". It calculates all the outputs
468 sgVec3 minus_z, right, forward, tilt;
469 sgMat4 tmpROT; // temp rotation work matrices
470 sgMat4 VIEW_HEADINGOFFSET, VIEW_PITCHOFFSET;
471 sgVec3 tmpVec3; // temp work vector (3)
474 // The position vectors originate from the view point or target location
475 // depending on the type of view.
476 // FIXME: In particular this routine will need to support both locations
477 // and chase view (aka lookat) is only unique in that the
478 // eye position is calculated in relation to the object's position.
479 // FIXME: Later note: actually the object (target) info needs to be held
480 // by the model class.
481 if (_type == FG_RPH) {
482 // position is the location of the pilot
483 recalcPositionVectors( _lon_deg, _lat_deg, _alt_ft );
484 // Make the world up rotation matrix for rph
485 sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
487 // position is the location of the object being looked at
488 recalcPositionVectors( _target_lon_deg, _target_lat_deg, _target_alt_ft );
489 // Make the world up rotation matrix for lookat
490 sgMakeRotMat4( UP, _target_lon_deg, 0.0, -_target_lat_deg );
492 // the coordinates generated by the above "recalcPositionVectors"
493 sgCopyVec3(_zero_elev, _zero_elev_view_pos);
494 sgCopyVec3(_view_pos, _relative_view_pos);
498 // get the world up radial vector from planet center
499 // (ie. effect of aircraft location on earth "sphere" approximation)
500 sgSetVec3( _world_up, UP[0][0], UP[0][1], UP[0][2] );
504 // Creat local matrix with current geodetic position. Converting
505 // the orientation (pitch/roll/heading) to vectors.
506 fgMakeLOCAL( LOCAL, _pitch_deg * SG_DEGREES_TO_RADIANS,
507 _roll_deg * SG_DEGREES_TO_RADIANS,
508 -_heading_deg * SG_DEGREES_TO_RADIANS);
509 // Adjust LOCAL to current world_up vector (adjustment for planet location)
510 MakeWithWorldUp( LOCAL, UP, LOCAL );
511 // copy the LOCAL matrix to COCKPIT_ROT for publication...
512 sgCopyMat4( LOCAL_ROT, LOCAL );
514 // make sg vectors view up, right and forward vectors from LOCAL
515 sgSetVec3( _view_up, LOCAL[0][0], LOCAL[0][1], LOCAL[0][2] );
516 sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
517 sgSetVec3( forward, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
521 // create xyz offsets Vector
522 sgVec3 position_offset;
523 sgSetVec3( position_offset, _y_offset_m, _x_offset_m, _z_offset_m );
528 // Looking up/down left/right in pilot view (lookfrom mode)
529 // or Floating Rotatation around the object in chase view (lookat mode).
530 // Generate the offset matrix to be applied using offset angles:
531 if (_type == FG_LOOKAT) {
532 // Note that when in "chase view" the offset is in relation to the
533 // orientation heading (_heading_deg) of the model being looked at as
534 // it is used to rotate around the model.
535 MakeVIEW_OFFSET( VIEW_OFFSET,
536 (_heading_offset_deg - _heading_deg) * SG_DEGREES_TO_RADIANS, _world_up,
537 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
539 if (_type == FG_RPH) {
540 // generate the view offset matrix using orientation offsets
541 MakeVIEW_OFFSET( VIEW_OFFSET,
542 _heading_offset_deg * SG_DEGREES_TO_RADIANS, _view_up,
543 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
548 if (_type == FG_LOOKAT) {
550 // transfrom "offset" and "orientation offset" to vector
551 sgXformVec3( position_offset, position_offset, UP );
552 sgXformVec3( position_offset, position_offset, VIEW_OFFSET );
554 sgVec3 object_pos, eye_pos;
555 // copy to coordinates to object...
556 sgCopyVec3( object_pos, _view_pos );
558 // add the offsets from object to the coordinates to get "eye"
559 sgAddVec3( eye_pos, _view_pos, position_offset );
561 // Make the VIEW matrix for "lookat".
562 sgMakeLookAtMat4( VIEW, eye_pos, object_pos, _view_up );
565 if (_type == FG_RPH) {
567 sgXformVec3( position_offset, position_offset, LOCAL);
568 // add the offsets including rotations to the coordinates
569 sgAddVec3( _view_pos, position_offset );
571 // Make the VIEW matrix.
572 VIEW[0][0] = right[0];
573 VIEW[0][1] = right[1];
574 VIEW[0][2] = right[2];
576 VIEW[1][0] = forward[0];
577 VIEW[1][1] = forward[1];
578 VIEW[1][2] = forward[2];
580 VIEW[2][0] = _view_up[0];
581 VIEW[2][1] = _view_up[1];
582 VIEW[2][2] = _view_up[2];
588 // multiply the OFFSETS (for heading and pitch) into the VIEW
589 sgPostMultMat4(VIEW, VIEW_OFFSET);
591 // add the position data to the matrix
592 VIEW[3][0] = _view_pos[0];
593 VIEW[3][1] = _view_pos[1];
594 VIEW[3][2] = _view_pos[2];
599 // the VIEW matrix includes both rotation and translation. Let's
600 // knock out the translation part to make the VIEW_ROT matrix
601 sgCopyMat4( VIEW_ROT, VIEW );
602 VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
604 // Given a vector pointing straight down (-Z), map into onto the
605 // local plane representing "horizontal". This should give us the
606 // local direction for moving "south".
607 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
609 sgmap_vec_onto_cur_surface_plane(_world_up, _view_pos, minus_z,
611 sgNormalizeVec3(_surface_south);
613 // now calculate the surface east vector
615 sgNegateVec3(world_down, _world_up);
616 sgVectorProductVec3(_surface_east, _surface_south, world_down);
622 FGViewer::recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const
624 double sea_level_radius_m;
628 // Convert from geodetic to geocentric
630 sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
631 alt_ft * SG_FEET_TO_METER,
635 // Calculate the cartesian coordinates
636 // of point directly below at sea level.
637 // aka Zero Elevation Position
638 Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
641 Point3D tmp = sgPolarToCart3d(p) - scenery.get_next_center();
642 sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
644 // Calculate the absolute view position
645 // in fgfs coordinates.
646 // aka Absolute View Position
647 p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
648 tmp = sgPolarToCart3d(p);
649 sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
651 // Calculate the relative view position
652 // from the scenery center.
653 // aka Relative View Position
654 sgdVec3 scenery_center;
655 sgdSetVec3(scenery_center,
656 scenery.get_next_center().x(),
657 scenery.get_next_center().y(),
658 scenery.get_next_center().z());
660 sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
661 sgSetVec3(_relative_view_pos, view_pos);
666 FGViewer::get_h_fov()
668 switch (_scaling_type) {
669 case FG_SCALING_WIDTH: // h_fov == fov
672 if (_aspect_ratio < 1.0) {
677 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) / _aspect_ratio) *
678 SG_RADIANS_TO_DEGREES * 2;
686 FGViewer::get_v_fov()
688 switch (_scaling_type) {
689 case FG_SCALING_WIDTH: // h_fov == fov
690 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
691 SG_RADIANS_TO_DEGREES * 2;
693 if (_aspect_ratio < 1.0) {
695 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
696 SG_RADIANS_TO_DEGREES * 2;
707 FGViewer::update (int dt)
710 for ( i = 0; i < dt; i++ ) {
711 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
712 setHeadingOffset_deg( _goal_heading_offset_deg );
715 // move current_view.headingoffset towards
716 // current_view.goal_view_offset
717 if ( _goal_heading_offset_deg > _heading_offset_deg )
719 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
720 incHeadingOffset_deg( 0.5 );
722 incHeadingOffset_deg( -0.5 );
725 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
726 incHeadingOffset_deg( -0.5 );
728 incHeadingOffset_deg( 0.5 );
731 if ( _heading_offset_deg > 360 ) {
732 incHeadingOffset_deg( -360 );
733 } else if ( _heading_offset_deg < 0 ) {
734 incHeadingOffset_deg( 360 );
739 for ( i = 0; i < dt; i++ ) {
740 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
741 setPitchOffset_deg( _goal_pitch_offset_deg );
744 // move current_view.pitch_offset_deg towards
745 // current_view.goal_pitch_offset
746 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
748 incPitchOffset_deg( 1.0 );
750 incPitchOffset_deg( -1.0 );
752 if ( _pitch_offset_deg > 90 ) {
753 setPitchOffset_deg(90);
754 } else if ( _pitch_offset_deg < -90 ) {
755 setPitchOffset_deg( -90 );