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>
43 #include <simgear/math/vector.hxx>
44 #include <Main/globals.hxx>
45 #include <Model/acmodel.hxx>
46 #include <Model/model.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 x_offset_m, double y_offset_m, double z_offset_m,
137 double heading_offset_deg, double pitch_offset_deg,
138 double roll_offset_deg, double fov_deg,
139 double target_x_offset_m, double target_y_offset_m,
140 double target_z_offset_m, double near_m ):
151 _scaling_type(FG_SCALING_MAX)
153 sgdZeroVec3(_absolute_view_pos);
155 _from_model = from_model;
156 _from_model_index = from_model_index;
157 _at_model = at_model;
158 _at_model_index = at_model_index;
159 _x_offset_m = x_offset_m;
160 _y_offset_m = y_offset_m;
161 _z_offset_m = z_offset_m;
162 _heading_offset_deg = heading_offset_deg;
163 _pitch_offset_deg = pitch_offset_deg;
164 _roll_offset_deg = roll_offset_deg;
165 _goal_heading_offset_deg = heading_offset_deg;
166 _goal_pitch_offset_deg = pitch_offset_deg;
167 _goal_roll_offset_deg = roll_offset_deg;
173 _target_x_offset_m = target_x_offset_m;
174 _target_y_offset_m = target_y_offset_m;
175 _target_z_offset_m = target_z_offset_m;
176 _ground_level_nearplane_m = near_m;
177 // a reasonable guess for init, so that the math doesn't blow up
182 FGViewer::~FGViewer( void ) {
189 _location = (FGLocation *) globals->get_aircraft_model()->get3DModel()->getFGLocation();
191 _location = (FGLocation *) new FGLocation;
193 if ( _type == FG_LOOKAT ) {
195 _target_location = (FGLocation *) globals->get_aircraft_model()->get3DModel()->getFGLocation();
197 _target_location = (FGLocation *) new FGLocation;
212 FGViewer::setType ( int type )
221 FGViewer::setLongitude_deg (double lon_deg)
228 FGViewer::setLatitude_deg (double lat_deg)
235 FGViewer::setAltitude_ft (double alt_ft)
242 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
251 FGViewer::setTargetLongitude_deg (double lon_deg)
254 _target_lon_deg = lon_deg;
258 FGViewer::setTargetLatitude_deg (double lat_deg)
261 _target_lat_deg = lat_deg;
265 FGViewer::setTargetAltitude_ft (double alt_ft)
268 _target_alt_ft = alt_ft;
272 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
275 _target_lon_deg = lon_deg;
276 _target_lat_deg = lat_deg;
277 _target_alt_ft = alt_ft;
281 FGViewer::setRoll_deg (double roll_deg)
284 _roll_deg = roll_deg;
288 FGViewer::setPitch_deg (double pitch_deg)
291 _pitch_deg = pitch_deg;
295 FGViewer::setHeading_deg (double heading_deg)
298 _heading_deg = heading_deg;
302 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
305 _roll_deg = roll_deg;
306 _pitch_deg = pitch_deg;
307 _heading_deg = heading_deg;
311 FGViewer::setTargetRoll_deg (double target_roll_deg)
314 _target_roll_deg = target_roll_deg;
318 FGViewer::setTargetPitch_deg (double target_pitch_deg)
321 _target_pitch_deg = target_pitch_deg;
325 FGViewer::setTargetHeading_deg (double target_heading_deg)
328 _target_heading_deg = target_heading_deg;
332 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
335 _target_roll_deg = target_roll_deg;
336 _target_pitch_deg = target_pitch_deg;
337 _target_heading_deg = target_heading_deg;
341 FGViewer::setXOffset_m (double x_offset_m)
344 _x_offset_m = x_offset_m;
348 FGViewer::setYOffset_m (double y_offset_m)
351 _y_offset_m = y_offset_m;
355 FGViewer::setZOffset_m (double z_offset_m)
358 _z_offset_m = z_offset_m;
362 FGViewer::setTargetXOffset_m (double target_x_offset_m)
365 _target_x_offset_m = target_x_offset_m;
369 FGViewer::setTargetYOffset_m (double target_y_offset_m)
372 _target_y_offset_m = target_y_offset_m;
376 FGViewer::setTargetZOffset_m (double target_z_offset_m)
379 _target_z_offset_m = target_z_offset_m;
383 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
386 _x_offset_m = x_offset_m;
387 _y_offset_m = y_offset_m;
388 _z_offset_m = z_offset_m;
392 FGViewer::setRollOffset_deg (double roll_offset_deg)
395 _roll_offset_deg = roll_offset_deg;
399 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
402 _pitch_offset_deg = pitch_offset_deg;
406 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
409 _heading_offset_deg = heading_offset_deg;
413 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
416 _goal_roll_offset_deg = goal_roll_offset_deg;
420 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
423 _goal_pitch_offset_deg = goal_pitch_offset_deg;
424 if ( _goal_pitch_offset_deg < -90 ) {
425 _goal_pitch_offset_deg = -90.0;
427 if ( _goal_pitch_offset_deg > 90.0 ) {
428 _goal_pitch_offset_deg = 90.0;
434 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
437 _goal_heading_offset_deg = goal_heading_offset_deg;
438 while ( _goal_heading_offset_deg < 0.0 ) {
439 _goal_heading_offset_deg += 360;
441 while ( _goal_heading_offset_deg > 360 ) {
442 _goal_heading_offset_deg -= 360;
447 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
450 _roll_offset_deg = roll_offset_deg;
451 _pitch_offset_deg = pitch_offset_deg;
452 _heading_offset_deg = heading_offset_deg;
456 FGViewer::get_absolute_view_pos ()
460 return _absolute_view_pos;
464 FGViewer::getRelativeViewPos ()
468 return _relative_view_pos;
472 FGViewer::getZeroElevViewPos ()
476 return _zero_elev_view_pos;
480 FGViewer::updateFromModelLocation (FGLocation * location)
482 sgCopyMat4(LOCAL, location->getCachedTransformMatrix());
486 FGViewer::updateAtModelLocation (FGLocation * location)
488 sgCopyMat4(ATLOCAL, location->getCachedTransformMatrix());
492 FGViewer::recalcOurOwnLocation (FGLocation * location, double lon_deg, double lat_deg, double alt_ft,
493 double roll_deg, double pitch_deg, double heading_deg)
495 // update from our own data...
496 location->setPosition( lon_deg, lat_deg, alt_ft );
497 location->setOrientation( roll_deg, pitch_deg, heading_deg );
498 sgCopyMat4(LOCAL, location->getTransformMatrix());
501 // recalc() is done every time one of the setters is called (making the
502 // cached data "dirty") on the next "get". It calculates all the outputs
507 if (_type == FG_LOOKFROM) {
516 // recalculate for LookFrom view type...
518 FGViewer::recalcLookFrom ()
521 sgVec3 right, forward;
523 sgVec3 position_offset; // eye position offsets (xyz)
527 // Update location data...
529 // update or data from model location
530 updateFromModelLocation(_location);
532 // update from our own data...
533 recalcOurOwnLocation( _location, _lon_deg, _lat_deg, _alt_ft,
534 _roll_deg, _pitch_deg, _heading_deg );
537 // copy data from location class to local items...
540 // make sg vectors view up, right and forward vectors from LOCAL
541 sgSetVec3( _view_up, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
542 sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
543 sgSetVec3( forward, -LOCAL[0][0], -LOCAL[0][1], -LOCAL[0][2] );
546 // Note that when in "lookfrom" view the "view up" vector is always applied
547 // to the viewer. View up is based on verticle of the aircraft itself. (see
548 // "LOCAL" matrix above)
550 // Orientation Offsets matrix
551 MakeVIEW_OFFSET( VIEW_OFFSET,
552 _heading_offset_deg * SG_DEGREES_TO_RADIANS, _view_up,
553 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
555 // Make the VIEW matrix.
556 sgSetVec4(VIEW[0], right[0], right[1], right[2],SG_ZERO);
557 sgSetVec4(VIEW[1], forward[0], forward[1], forward[2],SG_ZERO);
558 sgSetVec4(VIEW[2], _view_up[0], _view_up[1], _view_up[2],SG_ZERO);
559 sgSetVec4(VIEW[3], SG_ZERO, SG_ZERO, SG_ZERO,SG_ONE);
561 // rotate model or local matrix to get a matrix to apply Eye Position Offsets
562 sgMat4 VIEW_UP; // L0 forward L1 right L2 up
563 sgCopyVec4(VIEW_UP[0], LOCAL[1]);
564 sgCopyVec4(VIEW_UP[1], LOCAL[2]);
565 sgCopyVec4(VIEW_UP[2], LOCAL[0]);
566 sgZeroVec4(VIEW_UP[3]);
568 // Eye Position Offsets to vector
569 sgSetVec3( position_offset, _x_offset_m, _y_offset_m, _z_offset_m );
570 sgXformVec3( position_offset, position_offset, VIEW_UP);
572 // add the offsets including rotations to the translation vector
573 sgAddVec3( _view_pos, position_offset );
575 // multiply the OFFSETS (for heading and pitch) into the VIEW
576 sgPostMultMat4(VIEW, VIEW_OFFSET);
578 // add the position data to the matrix
579 sgSetVec4(VIEW[3], _view_pos[0], _view_pos[1], _view_pos[2],SG_ONE);
584 FGViewer::recalcLookAt ()
588 sgVec3 eye_pos, at_pos;
589 sgVec3 position_offset; // eye position offsets (xyz)
590 sgVec3 target_position_offset; // target position offsets (xyz)
592 // The position vectors originate from the view point or target location
593 // depending on the type of view.
597 // Update location data for target...
599 // update or data from model location
600 updateAtModelLocation(_target_location);
602 // if not model then calculate our own target position...
603 recalcOurOwnLocation( _target_location, _target_lon_deg, _target_lat_deg, _target_alt_ft,
604 _target_roll_deg, _target_pitch_deg, _target_heading_deg );
606 // calculate the "at" target object positon relative to eye or view's tile center...
608 sgdSetVec3(dVec3, _location->get_tile_center()[0], _location->get_tile_center()[1], _location->get_tile_center()[2]);
609 sgdSubVec3(dVec3, _target_location->get_absolute_view_pos(), dVec3 );
610 sgSetVec3(at_pos, dVec3[0], dVec3[1], dVec3[2]);
612 // Update location data for eye...
614 // update or data from model location
615 updateFromModelLocation(_location);
617 // update from our own data, just the rotation here...
618 recalcOurOwnLocation( _location, _lon_deg, _lat_deg, _alt_ft,
619 _roll_deg, _pitch_deg, _heading_deg );
621 // save the eye positon...
622 sgCopyVec3(eye_pos, _location->get_view_pos());
624 // copy data from location class to local items...
627 // make sg vectors view up, right and forward vectors from LOCAL
628 sgSetVec3( _view_up, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
629 sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
631 // rotate model or local matrix to get a matrix to apply Eye Position Offsets
632 sgMat4 VIEW_UP; // L0 forward L1 right L2 up
633 sgCopyVec4(VIEW_UP[0], LOCAL[1]);
634 sgCopyVec4(VIEW_UP[1], LOCAL[2]);
635 sgCopyVec4(VIEW_UP[2], LOCAL[0]);
636 sgZeroVec4(VIEW_UP[3]);
638 // get Orientation Offsets matrix
639 MakeVIEW_OFFSET( VIEW_OFFSET,
640 (_heading_offset_deg - 180) * SG_DEGREES_TO_RADIANS, _view_up,
641 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
643 // add in the position offsets
644 sgSetVec3( position_offset, _y_offset_m, _x_offset_m, _z_offset_m );
645 sgXformVec3( position_offset, position_offset, VIEW_UP);
647 // apply the Orientation offsets
648 sgXformVec3( position_offset, position_offset, VIEW_OFFSET );
650 // add the Position offsets from object to the eye position
651 sgAddVec3( eye_pos, eye_pos, position_offset );
653 // add target offsets to at_position...
654 sgSetVec3(target_position_offset, _target_z_offset_m, _target_x_offset_m, _target_y_offset_m );
655 sgXformVec3(target_position_offset, target_position_offset, ATLOCAL);
656 sgAddVec3( at_pos, at_pos, target_position_offset);
658 // Make the VIEW matrix for a "LOOKAT".
659 sgMakeLookAtMat4( VIEW, eye_pos, at_pos, _view_up );
663 // copy results from location class to viewer...
664 // FIXME: some of these should be changed to reference directly to FGLocation...
666 FGViewer::copyLocationData()
668 // Get our friendly vectors from the eye location...
669 sgCopyVec3(_zero_elev_view_pos, _location->get_zero_elev());
670 sgCopyVec3(_relative_view_pos, _location->get_view_pos());
671 sgdCopyVec3(_absolute_view_pos, _location->get_absolute_view_pos());
672 sgCopyMat4(UP, _location->getCachedUpMatrix());
673 sgCopyVec3(_world_up, _location->get_world_up());
674 // these are the vectors that the sun and moon code like to get...
675 sgCopyVec3(_surface_east, _location->get_surface_east());
676 sgCopyVec3(_surface_south, _location->get_surface_south());
678 // Update viewer's postion data for the eye location...
679 _lon_deg = _location->getLongitude_deg();
680 _lat_deg = _location->getLatitude_deg();
681 _alt_ft = _location->getAltitudeASL_ft();
682 _roll_deg = _location->getRoll_deg();
683 _pitch_deg = _location->getPitch_deg();
684 _heading_deg = _location->getHeading_deg();
686 // Update viewer's postion data for the target (at object) location
687 if (_type == FG_LOOKAT) {
688 _target_lon_deg = _target_location->getLongitude_deg();
689 _target_lat_deg = _target_location->getLatitude_deg();
690 _target_alt_ft = _target_location->getAltitudeASL_ft();
691 _target_roll_deg = _target_location->getRoll_deg();
692 _target_pitch_deg = _target_location->getPitch_deg();
693 _target_heading_deg = _target_location->getHeading_deg();
696 // copy coordinates to outputs for viewer...
697 sgCopyVec3(_zero_elev, _zero_elev_view_pos);
698 sgCopyVec3(_view_pos, _relative_view_pos);
702 FGViewer::get_h_fov()
704 switch (_scaling_type) {
705 case FG_SCALING_WIDTH: // h_fov == fov
708 if (_aspect_ratio < 1.0) {
713 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) / _aspect_ratio) *
714 SG_RADIANS_TO_DEGREES * 2;
725 FGViewer::get_v_fov()
727 switch (_scaling_type) {
728 case FG_SCALING_WIDTH: // h_fov == fov
729 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
730 SG_RADIANS_TO_DEGREES * 2;
732 if (_aspect_ratio < 1.0) {
734 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
735 SG_RADIANS_TO_DEGREES * 2;
747 FGViewer::update (double dt)
750 int dt_ms = int(dt * 1000);
751 for ( i = 0; i < dt_ms; i++ ) {
752 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
753 setHeadingOffset_deg( _goal_heading_offset_deg );
756 // move current_view.headingoffset towards
757 // current_view.goal_view_offset
758 if ( _goal_heading_offset_deg > _heading_offset_deg )
760 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
761 incHeadingOffset_deg( 0.5 );
763 incHeadingOffset_deg( -0.5 );
766 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
767 incHeadingOffset_deg( -0.5 );
769 incHeadingOffset_deg( 0.5 );
772 if ( _heading_offset_deg > 360 ) {
773 incHeadingOffset_deg( -360 );
774 } else if ( _heading_offset_deg < 0 ) {
775 incHeadingOffset_deg( 360 );
780 for ( i = 0; i < dt_ms; i++ ) {
781 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
782 setPitchOffset_deg( _goal_pitch_offset_deg );
785 // move current_view.pitch_offset_deg towards
786 // current_view.goal_pitch_offset
787 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
789 incPitchOffset_deg( 1.0 );
791 incPitchOffset_deg( -1.0 );
793 if ( _pitch_offset_deg > 90 ) {
794 setPitchOffset_deg(90);
795 } else if ( _pitch_offset_deg < -90 ) {
796 setPitchOffset_deg( -90 );