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>
42 //#include <Main/location.hxx>
44 #include <simgear/math/vector.hxx>
45 #include <Main/globals.hxx>
46 #include <Model/acmodel.hxx>
47 #include <Model/model.hxx>
52 //////////////////////////////////////////////////////////////////
53 // Norman's Optimized matrix rotators! //
54 //////////////////////////////////////////////////////////////////
57 // Since these are pure rotation matrices we can save some bookwork
58 // by considering them to be 3x3 until the very end -- NHV
59 static void MakeVIEW_OFFSET( sgMat4 dst,
60 const float angle1, const sgVec3 axis1,
61 const float angle2, const sgVec3 axis2 )
63 // make rotmatrix1 from angle and axis
64 float s = (float) sin ( angle1 ) ;
65 float c = (float) cos ( angle1 ) ;
66 float t = SG_ONE - c ;
69 float tmp = t * axis1[0];
70 mat1[0][0] = tmp * axis1[0] + c ;
71 mat1[0][1] = tmp * axis1[1] + s * axis1[2] ;
72 mat1[0][2] = tmp * axis1[2] - s * axis1[1] ;
75 mat1[1][0] = tmp * axis1[0] - s * axis1[2] ;
76 mat1[1][1] = tmp * axis1[1] + c ;
77 mat1[1][2] = tmp * axis1[2] + s * axis1[0] ;
80 mat1[2][0] = tmp * axis1[0] + s * axis1[1] ;
81 mat1[2][1] = tmp * axis1[1] - s * axis1[0] ;
82 mat1[2][2] = tmp * axis1[2] + c ;
84 // make rotmatrix2 from angle and axis
85 s = (float) sin ( angle2 ) ;
86 c = (float) cos ( angle2 ) ;
91 mat2[0][0] = tmp * axis2[0] + c ;
92 mat2[0][1] = tmp * axis2[1] + s * axis2[2] ;
93 mat2[0][2] = tmp * axis2[2] - s * axis2[1] ;
96 mat2[1][0] = tmp * axis2[0] - s * axis2[2] ;
97 mat2[1][1] = tmp * axis2[1] + c ;
98 mat2[1][2] = tmp * axis2[2] + s * axis2[0] ;
101 mat2[2][0] = tmp * axis2[0] + s * axis2[1] ;
102 mat2[2][1] = tmp * axis2[1] - s * axis2[0] ;
103 mat2[2][2] = tmp * axis2[2] + c ;
106 for ( int j = 0 ; j < 3 ; j++ ) {
107 dst[0][j] = mat2[0][0] * mat1[0][j] +
108 mat2[0][1] * mat1[1][j] +
109 mat2[0][2] * mat1[2][j];
111 dst[1][j] = mat2[1][0] * mat1[0][j] +
112 mat2[1][1] * mat1[1][j] +
113 mat2[1][2] * mat1[2][j];
115 dst[2][j] = mat2[2][0] * mat1[0][j] +
116 mat2[2][1] * mat1[1][j] +
117 mat2[2][2] * mat1[2][j];
119 // fill in 4x4 matrix elements
130 ////////////////////////////////////////////////////////////////////////
131 // Implementation of FGViewer.
132 ////////////////////////////////////////////////////////////////////////
135 FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index, bool at_model, int at_model_index, double x_offset_m, double y_offset_m, double z_offset_m, double near_m ):
136 _scaling_type(FG_SCALING_MAX),
148 _heading_offset_deg(0),
149 _pitch_offset_deg(0),
151 _goal_heading_offset_deg(0.0),
152 _goal_pitch_offset_deg(0.0)
154 sgdZeroVec3(_absolute_view_pos);
156 _from_model = from_model;
157 _from_model_index = from_model_index;
158 _at_model = at_model;
159 _at_model_index = at_model_index;
160 _x_offset_m = x_offset_m;
161 _y_offset_m = y_offset_m;
162 _z_offset_m = z_offset_m;
163 _ground_level_nearplane_m = near_m;
164 //a reasonable guess for init, so that the math doesn't blow up
169 FGViewer::~FGViewer( void ) {
176 _location = (FGLocation *) globals->get_aircraft_model()->get3DModel()->getFGLocation();
178 _location = (FGLocation *) new FGLocation;
180 if ( _type == FG_LOOKAT ) {
182 _target_location = (FGLocation *) globals->get_aircraft_model()->get3DModel()->getFGLocation();
184 _target_location = (FGLocation *) new FGLocation;
199 FGViewer::setType ( int type )
208 FGViewer::setLongitude_deg (double lon_deg)
215 FGViewer::setLatitude_deg (double lat_deg)
222 FGViewer::setAltitude_ft (double alt_ft)
229 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
238 FGViewer::setTargetLongitude_deg (double lon_deg)
241 _target_lon_deg = lon_deg;
245 FGViewer::setTargetLatitude_deg (double lat_deg)
248 _target_lat_deg = lat_deg;
252 FGViewer::setTargetAltitude_ft (double alt_ft)
255 _target_alt_ft = alt_ft;
259 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
262 _target_lon_deg = lon_deg;
263 _target_lat_deg = lat_deg;
264 _target_alt_ft = alt_ft;
268 FGViewer::setRoll_deg (double roll_deg)
271 _roll_deg = roll_deg;
275 FGViewer::setPitch_deg (double pitch_deg)
278 _pitch_deg = pitch_deg;
282 FGViewer::setHeading_deg (double heading_deg)
285 _heading_deg = heading_deg;
289 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
292 _roll_deg = roll_deg;
293 _pitch_deg = pitch_deg;
294 _heading_deg = heading_deg;
298 FGViewer::setTargetRoll_deg (double target_roll_deg)
301 _target_roll_deg = target_roll_deg;
305 FGViewer::setTargetPitch_deg (double target_pitch_deg)
308 _target_pitch_deg = target_pitch_deg;
312 FGViewer::setTargetHeading_deg (double target_heading_deg)
315 _target_heading_deg = target_heading_deg;
319 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
322 _target_roll_deg = target_roll_deg;
323 _target_pitch_deg = target_pitch_deg;
324 _target_heading_deg = target_heading_deg;
328 FGViewer::setXOffset_m (double x_offset_m)
331 _x_offset_m = x_offset_m;
335 FGViewer::setYOffset_m (double y_offset_m)
338 _y_offset_m = y_offset_m;
342 FGViewer::setZOffset_m (double z_offset_m)
345 _z_offset_m = z_offset_m;
349 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
352 _x_offset_m = x_offset_m;
353 _y_offset_m = y_offset_m;
354 _z_offset_m = z_offset_m;
358 FGViewer::setRollOffset_deg (double roll_offset_deg)
361 _roll_offset_deg = roll_offset_deg;
365 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
368 _pitch_offset_deg = pitch_offset_deg;
372 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
375 _heading_offset_deg = heading_offset_deg;
379 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
382 _goal_roll_offset_deg = goal_roll_offset_deg;
386 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
389 _goal_pitch_offset_deg = goal_pitch_offset_deg;
390 if ( _goal_pitch_offset_deg < -90 ) {
391 _goal_pitch_offset_deg = -90.0;
393 if ( _goal_pitch_offset_deg > 90.0 ) {
394 _goal_pitch_offset_deg = 90.0;
400 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
403 _goal_heading_offset_deg = goal_heading_offset_deg;
404 while ( _goal_heading_offset_deg < 0.0 ) {
405 _goal_heading_offset_deg += 360;
407 while ( _goal_heading_offset_deg > 360 ) {
408 _goal_heading_offset_deg -= 360;
413 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
416 _roll_offset_deg = roll_offset_deg;
417 _pitch_offset_deg = pitch_offset_deg;
418 _heading_offset_deg = heading_offset_deg;
422 FGViewer::get_absolute_view_pos ()
426 return _absolute_view_pos;
430 FGViewer::getRelativeViewPos ()
434 return _relative_view_pos;
438 FGViewer::getZeroElevViewPos ()
442 return _zero_elev_view_pos;
446 FGViewer::updateFromModelLocation (FGLocation * location)
448 sgCopyMat4(LOCAL, location->getCachedTransformMatrix());
449 _lon_deg = location->getLongitude_deg();
450 _lat_deg = location->getLatitude_deg();
451 _alt_ft = location->getAltitudeASL_ft();
452 _roll_deg = location->getRoll_deg();
453 _pitch_deg = location->getPitch_deg();
454 _heading_deg = location->getHeading_deg();
455 sgCopyVec3(_zero_elev_view_pos, location->get_zero_elev());
456 sgCopyVec3(_relative_view_pos, location->get_view_pos());
457 sgdCopyVec3(_absolute_view_pos, location->get_absolute_view_pos());
458 sgCopyMat4(UP, location->getCachedUpMatrix());
459 sgCopyVec3(_world_up, location->get_world_up());
460 // these are the vectors that the sun and moon code like to get...
461 sgCopyVec3(_surface_east, location->get_surface_east());
462 sgCopyVec3(_surface_south, location->get_surface_south());
466 FGViewer::recalcOurOwnLocation (double lon_deg, double lat_deg, double alt_ft,
467 double roll_deg, double pitch_deg, double heading_deg)
469 // update from our own data...
470 _location->setPosition( lon_deg, lat_deg, alt_ft );
471 _location->setOrientation( roll_deg, pitch_deg, heading_deg );
472 sgCopyMat4(LOCAL, _location->getTransformMatrix());
473 sgCopyVec3(_zero_elev_view_pos, _location->get_zero_elev());
474 sgCopyVec3(_relative_view_pos, _location->get_view_pos());
475 sgdCopyVec3(_absolute_view_pos, _location->get_absolute_view_pos());
476 // these are the vectors that the sun and moon code like to get...
477 sgCopyVec3(_surface_east, _location->get_surface_east());
478 sgCopyVec3(_surface_south, _location->get_surface_south());
481 // recalc() is done every time one of the setters is called (making the
482 // cached data "dirty") on the next "get". It calculates all the outputs
487 sgVec3 right, forward;
488 sgMat4 tmpROT; // temp rotation work matrices
489 sgVec3 eye_pos, at_pos;
490 sgVec3 position_offset; // eye position offsets (xyz)
492 // The position vectors originate from the view point or target location
493 // depending on the type of view.
495 if (_type == FG_LOOKFROM) {
498 // update or data from model location
499 updateFromModelLocation(_location);
501 // update from our own data...
502 recalcOurOwnLocation( _lon_deg, _lat_deg, _alt_ft,
503 _roll_deg, _pitch_deg, _heading_deg );
504 // get world up data from just recalced location
505 sgCopyMat4(UP, _location->getUpMatrix());
506 sgCopyVec3(_world_up, _location->get_world_up());
513 // update or data from model location
514 updateFromModelLocation(_location);
516 // update from our own data, just the rotation here...
517 recalcOurOwnLocation( _lon_deg, _lat_deg, _alt_ft,
518 _roll_deg, _pitch_deg, _heading_deg );
519 // get world up data from just recalced location
520 sgCopyMat4(UP, _location->getUpMatrix());
521 sgCopyVec3(_world_up, _location->get_world_up());
523 // save they eye positon...
524 sgCopyVec3(eye_pos, _location->get_view_pos());
525 // save the eye rotation before getting target values!!!
526 sgCopyMat4(tmpROT, LOCAL);
529 // update or data from model location
530 updateFromModelLocation(_target_location);
532 // if not model then calculate our own target position...
533 recalcOurOwnLocation( _target_lon_deg, _target_lat_deg, _target_alt_ft,
534 _target_roll_deg, _target_pitch_deg, _target_heading_deg );
536 // restore the eye rotation (the from position rotation)
537 sgCopyMat4(LOCAL, tmpROT);
541 // the coordinates generated by the above "recalcPositionVectors"
542 sgCopyVec3(_zero_elev, _zero_elev_view_pos);
543 sgCopyVec3(_view_pos, _relative_view_pos);
546 // Doing this last recalc here for published values...where the airplane is
547 // This should be per aircraft or model (for published values) before
548 // multiple FDM can be done.
549 // This info should come directly from the model (not through viewer),
550 // because in some cases there is no model directly assigned as a lookfrom
551 // position. The problem that arises is related to the FDM interface looking
552 // indirectly to the viewer to find the altitude of the aircraft on the runway.
554 // Note that recalcPositionVectors can be removed from viewer when this
555 // issue is addressed.
558 recalcPositionVectors(fgGetDouble("/position/longitude-deg"),
559 fgGetDouble("/position/latitude-deg"),
560 fgGetDouble("/position/altitude-ft"));
563 // make sg vectors view up, right and forward vectors from LOCAL
564 sgSetVec3( _view_up, LOCAL[2][0], LOCAL[2][1], LOCAL[2][2] );
565 sgSetVec3( right, LOCAL[1][0], LOCAL[1][1], LOCAL[1][2] );
566 sgSetVec3( forward, -LOCAL[0][0], -LOCAL[0][1], -LOCAL[0][2] );
568 if (_type == FG_LOOKAT) {
570 // Note that when in "lookat" view the "world up" vector is always applied
571 // to the viewer. World up is based on verticle at a given lon/lat (see
572 // matrix "UP" above).
574 // Orientation Offsets matrix
575 MakeVIEW_OFFSET( VIEW_OFFSET,
576 (_heading_offset_deg -_heading_deg) * SG_DEGREES_TO_RADIANS, _world_up,
577 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
579 // add in the Orientation Offsets here
580 sgSetVec3( position_offset, _x_offset_m, _y_offset_m, _z_offset_m );
581 sgXformVec3( position_offset, position_offset, UP);
583 sgXformVec3( position_offset, position_offset, VIEW_OFFSET );
585 // add the Position offsets from object to the eye position
586 sgAddVec3( eye_pos, eye_pos, position_offset );
588 // at position (what we are looking at)
589 sgCopyVec3( at_pos, _view_pos );
591 // Make the VIEW matrix for a "LOOKAT".
592 sgMakeLookAtMat4( VIEW, eye_pos, at_pos, _view_up );
595 if (_type == FG_LOOKFROM) {
597 // Note that when in "lookfrom" view the "view up" vector is always applied
598 // to the viewer. View up is based on verticle of the aircraft itself. (see
599 // "LOCAL" matrix above)
601 // Orientation Offsets matrix
602 MakeVIEW_OFFSET( VIEW_OFFSET,
603 _heading_offset_deg * SG_DEGREES_TO_RADIANS, _view_up,
604 _pitch_offset_deg * SG_DEGREES_TO_RADIANS, right );
606 // Make the VIEW matrix.
607 sgSetVec4(VIEW[0], right[0], right[1], right[2],SG_ZERO);
608 sgSetVec4(VIEW[1], forward[0], forward[1], forward[2],SG_ZERO);
609 sgSetVec4(VIEW[2], _view_up[0], _view_up[1], _view_up[2],SG_ZERO);
610 sgSetVec4(VIEW[3], SG_ZERO, SG_ZERO, SG_ZERO,SG_ONE);
612 // rotate matrix to get a matrix to apply Eye Position Offsets
613 sgMat4 VIEW_UP; // L0 forward L1 right L2 up
614 sgCopyVec4(VIEW_UP[0], LOCAL[1]);
615 sgCopyVec4(VIEW_UP[1], LOCAL[2]);
616 sgCopyVec4(VIEW_UP[2], LOCAL[0]);
617 sgZeroVec4(VIEW_UP[3]);
619 // Eye Position Offsets to vector
620 sgSetVec3( position_offset, _x_offset_m, _y_offset_m, _z_offset_m );
621 sgXformVec3( position_offset, position_offset, VIEW_UP);
623 // add the offsets including rotations to the translation vector
624 sgAddVec3( _view_pos, position_offset );
626 // multiply the OFFSETS (for heading and pitch) into the VIEW
627 sgPostMultMat4(VIEW, VIEW_OFFSET);
629 // add the position data to the matrix
630 sgSetVec4(VIEW[3], _view_pos[0], _view_pos[1], _view_pos[2],SG_ONE);
638 FGViewer::recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const
640 double sea_level_radius_m;
644 // Convert from geodetic to geocentric
646 sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
647 alt_ft * SG_FEET_TO_METER,
651 // Calculate the cartesian coordinates
652 // of point directly below at sea level.
653 // aka Zero Elevation Position
654 Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
657 Point3D tmp = sgPolarToCart3d(p) - globals->get_scenery()->get_next_center();
658 sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
660 // Calculate the absolute view position
661 // in fgfs coordinates.
662 // aka Absolute View Position
663 p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
664 tmp = sgPolarToCart3d(p);
665 sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
667 // Calculate the relative view position
668 // from the scenery center.
669 // aka Relative View Position
670 sgdVec3 scenery_center;
671 sgdSetVec3(scenery_center,
672 globals->get_scenery()->get_next_center().x(),
673 globals->get_scenery()->get_next_center().y(),
674 globals->get_scenery()->get_next_center().z());
676 sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
677 sgSetVec3(_relative_view_pos, view_pos);
682 FGViewer::get_h_fov()
684 switch (_scaling_type) {
685 case FG_SCALING_WIDTH: // h_fov == fov
688 if (_aspect_ratio < 1.0) {
693 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) / _aspect_ratio) *
694 SG_RADIANS_TO_DEGREES * 2;
703 FGViewer::get_v_fov()
705 switch (_scaling_type) {
706 case FG_SCALING_WIDTH: // h_fov == fov
707 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
708 SG_RADIANS_TO_DEGREES * 2;
710 if (_aspect_ratio < 1.0) {
712 return atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS) * _aspect_ratio) *
713 SG_RADIANS_TO_DEGREES * 2;
725 FGViewer::update (double dt)
728 int dt_ms = int(dt * 1000);
729 for ( i = 0; i < dt_ms; i++ ) {
730 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
731 setHeadingOffset_deg( _goal_heading_offset_deg );
734 // move current_view.headingoffset towards
735 // current_view.goal_view_offset
736 if ( _goal_heading_offset_deg > _heading_offset_deg )
738 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
739 incHeadingOffset_deg( 0.5 );
741 incHeadingOffset_deg( -0.5 );
744 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
745 incHeadingOffset_deg( -0.5 );
747 incHeadingOffset_deg( 0.5 );
750 if ( _heading_offset_deg > 360 ) {
751 incHeadingOffset_deg( -360 );
752 } else if ( _heading_offset_deg < 0 ) {
753 incHeadingOffset_deg( 360 );
758 for ( i = 0; i < dt_ms; i++ ) {
759 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
760 setPitchOffset_deg( _goal_pitch_offset_deg );
763 // move current_view.pitch_offset_deg towards
764 // current_view.goal_pitch_offset
765 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
767 incPitchOffset_deg( 1.0 );
769 incPitchOffset_deg( -1.0 );
771 if ( _pitch_offset_deg > 90 ) {
772 setPitchOffset_deg(90);
773 } else if ( _pitch_offset_deg < -90 ) {
774 setPitchOffset_deg( -90 );