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 // Implementation of FGViewer.
51 ////////////////////////////////////////////////////////////////////////
54 FGViewer::FGViewer( void ):
55 scalingType(FG_SCALING_MAX),
70 _heading_offset_deg(0),
73 _goal_heading_offset_deg(0.0),
74 _goal_pitch_offset_deg(0.0)
76 sgdZeroVec3(_absolute_view_pos);
77 sea_level_radius = SG_EQUATORIAL_RADIUS_M;
78 //a reasonable guess for init, so that the math doesn't blow up
83 FGViewer::~FGViewer( void ) {
89 if ( _type == FG_LOOKAT ) {
90 set_reverse_view_offset(true);
93 if ( _type == FG_RPH ) {
94 set_reverse_view_offset(false);
109 FGViewer::setType ( int type )
118 FGViewer::setLongitude_deg (double lon_deg)
125 FGViewer::setLatitude_deg (double lat_deg)
132 FGViewer::setAltitude_ft (double alt_ft)
139 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
148 FGViewer::setTargetLongitude_deg (double lon_deg)
151 _target_lon_deg = lon_deg;
155 FGViewer::setTargetLatitude_deg (double lat_deg)
158 _target_lat_deg = lat_deg;
162 FGViewer::setTargetAltitude_ft (double alt_ft)
165 _target_alt_ft = alt_ft;
169 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
172 _target_lon_deg = lon_deg;
173 _target_lat_deg = lat_deg;
174 _target_alt_ft = alt_ft;
178 FGViewer::setRoll_deg (double roll_deg)
181 _roll_deg = roll_deg;
185 FGViewer::setPitch_deg (double pitch_deg)
188 _pitch_deg = pitch_deg;
192 FGViewer::setHeading_deg (double heading_deg)
195 _heading_deg = heading_deg;
199 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
202 _roll_deg = roll_deg;
203 _pitch_deg = pitch_deg;
204 _heading_deg = heading_deg;
208 FGViewer::setXOffset_m (double x_offset_m)
211 _x_offset_m = x_offset_m;
215 FGViewer::setYOffset_m (double y_offset_m)
218 _y_offset_m = y_offset_m;
222 FGViewer::setZOffset_m (double z_offset_m)
225 _z_offset_m = z_offset_m;
229 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
232 _x_offset_m = x_offset_m;
233 _y_offset_m = y_offset_m;
234 _z_offset_m = z_offset_m;
238 FGViewer::setRollOffset_deg (double roll_offset_deg)
241 _roll_offset_deg = roll_offset_deg;
245 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
248 _pitch_offset_deg = pitch_offset_deg;
252 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
255 _heading_offset_deg = heading_offset_deg;
259 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
262 _goal_roll_offset_deg = goal_roll_offset_deg;
266 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
269 _goal_pitch_offset_deg = goal_pitch_offset_deg;
270 while ( _goal_pitch_offset_deg < -90 ) {
271 _goal_pitch_offset_deg = -90.0;
273 while ( _goal_pitch_offset_deg > 90.0 ) {
274 _goal_pitch_offset_deg = 90.0;
280 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
283 _goal_heading_offset_deg = goal_heading_offset_deg;
284 while ( _goal_heading_offset_deg < 0.0 ) {
285 _goal_heading_offset_deg += 360;
287 while ( _goal_heading_offset_deg > 360 ) {
288 _goal_heading_offset_deg -= 360;
293 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
296 _roll_offset_deg = roll_offset_deg;
297 _pitch_offset_deg = pitch_offset_deg;
298 _heading_offset_deg = heading_offset_deg;
302 FGViewer::get_absolute_view_pos ()
306 return _absolute_view_pos;
310 FGViewer::getRelativeViewPos ()
314 return _relative_view_pos;
318 FGViewer::getZeroElevViewPos ()
322 return _zero_elev_view_pos;
326 // recalc() is done every time one of the setters is called (making the
327 // cached data "dirty") on the next "get". It calculates all the outputs
332 sgVec3 minus_z, right, forward, tilt;
334 sgMat4 tmpROT; // temp rotation work matrices
335 sgVec3 tmpVec3; // temp work vector (3)
338 // The position vectors originate from the view point or target location
339 // depending on the type of view.
341 if (_type == FG_RPH) {
342 recalcPositionVectors( _lon_deg, _lat_deg, _alt_ft );
344 recalcPositionVectors( _target_lon_deg, _target_lat_deg, _target_alt_ft );
347 sgCopyVec3(zero_elev, _zero_elev_view_pos);
348 sgCopyVec3(view_pos, _relative_view_pos);
350 if (_type == FG_LOOKAT) {
352 // Make the world up rotation matrix for lookat
353 sgMakeRotMat4( UP, _target_lon_deg, 0.0, -_target_lat_deg );
355 // get the world up verctor from the worldup rotation matrix
356 sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
358 sgCopyVec3( view_up, world_up );
361 // create offset vector
362 sgVec3 lookat_offset;
363 sgSetVec3( lookat_offset, _x_offset_m, _y_offset_m, _z_offset_m );
365 // Apply heading orientation and orientation offset to lookat_offset...
366 sgMakeRotMat4( tmpROT, _heading_offset_deg -_heading_deg, world_up);
367 sgXformVec3( lookat_offset, lookat_offset, UP );
368 sgXformVec3( lookat_offset, lookat_offset, tmpROT );
370 // Apply orientation offset tilt...
371 // FIXME: Need to get and use a "right" vector instead of 1-0-0
372 sgSetVec3 (tmpVec3, 1, 0, 0);
373 sgMakeRotMat4( tmpROT, _pitch_offset_deg, tmpVec3 );
374 sgXformPnt3( lookat_offset, lookat_offset, tmpROT );
376 // add the offsets including rotations to the coordinates
377 sgAddVec3( view_pos, lookat_offset );
379 // Make the VIEW matrix.
380 fgMakeLookAtMat4( VIEW, view_pos, view_forward, view_up );
383 // the VIEW matrix includes both rotation and translation. Let's
384 // knock out the translation part to make the VIEW_ROT matrix
385 sgCopyMat4( VIEW_ROT, VIEW );
386 VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
390 if (_type == FG_RPH) {
392 // code to calculate LOCAL matrix calculated from Phi, Theta, and
393 // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
396 fgMakeLOCAL( LOCAL, _pitch_deg * SG_DEGREES_TO_RADIANS,
397 _roll_deg * SG_DEGREES_TO_RADIANS,
398 -_heading_deg * SG_DEGREES_TO_RADIANS);
400 // Make the world up rotation matrix for pilot view
401 sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
403 // get the world up verctor from the worldup rotation matrix
404 sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
406 // VIEWo becomes the rotation matrix with world_up incorporated
407 sgCopyMat4( VIEWo, LOCAL );
408 sgPostMultMat4( VIEWo, UP );
410 // generate the sg view up and forward vectors
411 sgSetVec3( view_up, VIEWo[0][0], VIEWo[0][1], VIEWo[0][2] );
412 sgSetVec3( right, VIEWo[1][0], VIEWo[1][1], VIEWo[1][2] );
413 sgSetVec3( forward, VIEWo[2][0], VIEWo[2][1], VIEWo[2][2] );
415 // apply the offsets in world coordinates
416 sgVec3 pilot_offset_world;
417 sgSetVec3( pilot_offset_world,
418 _z_offset_m, _y_offset_m, -_x_offset_m );
419 sgXformVec3( pilot_offset_world, pilot_offset_world, VIEWo );
421 // generate the view offset matrix using orientation offset (heading)
422 sgMakeRotMat4( VIEW_OFFSET, _heading_offset_deg, view_up );
424 // create a tilt matrix using orientation offset (pitch)
426 sgMakeRotMat4( VIEW_TILT, _pitch_offset_deg, right );
427 sgPreMultMat4(VIEW_OFFSET, VIEW_TILT);
428 sgXformVec3( view_forward, forward, VIEW_OFFSET );
429 SG_LOG( SG_VIEW, SG_DEBUG, "(RPH) view forward = "
430 << view_forward[0] << "," << view_forward[1] << ","
431 << view_forward[2] );
433 // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
434 fgMakeViewRot( VIEW_ROT, VIEW_OFFSET, VIEWo );
437 sgAddVec3( trans_vec, view_pos, pilot_offset_world );
439 // VIEW = VIEW_ROT * TRANS
440 sgCopyMat4( VIEW, VIEW_ROT );
441 sgPostMultMat4ByTransMat4( VIEW, trans_vec );
445 // Given a vector pointing straight down (-Z), map into onto the
446 // local plane representing "horizontal". This should give us the
447 // local direction for moving "south".
448 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
450 sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
452 sgNormalizeVec3(surface_south);
454 // now calculate the surface east vector
456 sgNegateVec3(world_down, world_up);
457 sgVectorProductVec3(surface_east, surface_south, world_down);
463 FGViewer::recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const
465 double sea_level_radius_m;
469 // Convert from geodetic to geocentric
471 sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
472 alt_ft * SG_FEET_TO_METER,
476 // Calculate the cartesian coordinates
477 // of point directly below at sea level.
478 // aka Zero Elevation Position
479 Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
482 Point3D tmp = sgPolarToCart3d(p) - scenery.get_next_center();
483 sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
485 // Calculate the absolute view position
486 // in fgfs coordinates.
487 // aka Absolute View Position
488 p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
489 tmp = sgPolarToCart3d(p);
490 sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
492 // Calculate the relative view position
493 // from the scenery center.
494 // aka Relative View Position
495 sgdVec3 scenery_center;
496 sgdSetVec3(scenery_center,
497 scenery.get_next_center().x(),
498 scenery.get_next_center().y(),
499 scenery.get_next_center().z());
501 sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
502 sgSetVec3(_relative_view_pos, view_pos);
507 FGViewer::get_h_fov()
509 switch (scalingType) {
510 case FG_SCALING_WIDTH: // h_fov == fov
513 if (aspect_ratio < 1.0) {
518 return atan(tan(fov/2 * SG_DEGREES_TO_RADIANS) / aspect_ratio) *
519 SG_RADIANS_TO_DEGREES * 2;
527 FGViewer::get_v_fov()
529 switch (scalingType) {
530 case FG_SCALING_WIDTH: // h_fov == fov
531 return atan(tan(fov/2 * SG_DEGREES_TO_RADIANS) * aspect_ratio) *
532 SG_RADIANS_TO_DEGREES * 2;
534 if (aspect_ratio < 1.0) {
536 return atan(tan(fov/2 * SG_DEGREES_TO_RADIANS) * aspect_ratio) *
537 SG_RADIANS_TO_DEGREES * 2;
548 FGViewer::update (int dt)
551 for ( i = 0; i < dt; i++ ) {
552 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
553 setHeadingOffset_deg( _goal_heading_offset_deg );
556 // move current_view.headingoffset towards
557 // current_view.goal_view_offset
558 if ( _goal_heading_offset_deg > _heading_offset_deg )
560 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
561 inc_view_offset( 0.5 );
563 inc_view_offset( -0.5 );
566 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
567 inc_view_offset( -0.5 );
569 inc_view_offset( 0.5 );
572 if ( _heading_offset_deg > 360 ) {
573 inc_view_offset( -360 );
574 } else if ( _heading_offset_deg < 0 ) {
575 inc_view_offset( 360 );
580 for ( i = 0; i < dt; i++ ) {
581 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
582 setPitchOffset_deg( _goal_pitch_offset_deg );
585 // move current_view.pitch_offset_deg towards
586 // current_view.goal_view_tilt
587 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
589 if ( _goal_pitch_offset_deg - _pitch_offset_deg < 0 ){
590 inc_view_tilt( 1.0 );
592 inc_view_tilt( -1.0 );
595 if ( _pitch_offset_deg - _goal_pitch_offset_deg < 0 ){
596 inc_view_tilt( -1.0 );
598 inc_view_tilt( 1.0 );
601 if ( _pitch_offset_deg > 90 ) {
602 setPitchOffset_deg(90);
603 } else if ( _pitch_offset_deg < -90 ) {
604 setPitchOffset_deg( -90 );
611 void FGViewer::fgMakeLookAtMat4 ( sgMat4 dst, const sgVec3 eye, const sgVec3 center,
615 // 1) In order to compute the line of sight, the eye point must not be equal
616 // to the center point.
617 // 2) The up vector must not be parallel to the line of sight from the eye
618 // to the center point.
620 /* Compute the direction vectors */
623 /* Y vector = center - eye */
624 sgSubVec3 ( y, center, eye ) ;
627 sgCopyVec3 ( z, up ) ;
629 /* X vector = Y cross Z */
630 sgVectorProductVec3 ( x, y, z ) ;
632 /* Recompute Z = X cross Y */
633 sgVectorProductVec3 ( z, x, y ) ;
635 /* Normalize everything */
636 sgNormaliseVec3 ( x ) ;
637 sgNormaliseVec3 ( y ) ;
638 sgNormaliseVec3 ( z ) ;
640 /* Build the matrix */
641 #define M(row,col) dst[row][col]
642 M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = 0.0;
643 M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = 0.0;
644 M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = 0.0;
645 M(3,0) = eye[0]; M(3,1) = eye[1]; M(3,2) = eye[2]; M(3,3) = 1.0;
648 /* end from lookat */
651 // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
652 // This takes advantage of the fact that VIEWo and VIEW_OFFSET
653 // only have entries in the upper 3x3 block
654 // and that LARC_TO_SSG is just a shift of rows NHV
655 void FGViewer::fgMakeViewRot( sgMat4 dst, const sgMat4 m1, const sgMat4 m2 )
657 for ( int j = 0 ; j < 3 ; j++ ) {
658 dst[2][j] = m2[0][0] * m1[0][j] +
659 m2[0][1] * m1[1][j] +
662 dst[0][j] = m2[1][0] * m1[0][j] +
663 m2[1][1] * m1[1][j] +
666 dst[1][j] = m2[2][0] * m1[0][j] +
667 m2[2][1] * m1[1][j] +
680 void FGViewer::fgMakeLOCAL( sgMat4 dst, const double Theta,
681 const double Phi, const double Psi)
683 SGfloat cosTheta = (SGfloat) cos(Theta);
684 SGfloat sinTheta = (SGfloat) sin(Theta);
685 SGfloat cosPhi = (SGfloat) cos(Phi);
686 SGfloat sinPhi = (SGfloat) sin(Phi);
687 SGfloat sinPsi = (SGfloat) sin(Psi) ;
688 SGfloat cosPsi = (SGfloat) cos(Psi) ;
690 dst[0][0] = cosPhi * cosTheta;
691 dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
692 dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
695 dst[1][0] = -sinPhi * cosTheta;
696 dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
697 dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
698 dst[1][3] = SG_ZERO ;
700 dst[2][0] = sinTheta;
701 dst[2][1] = cosTheta * -sinPsi;
702 dst[2][2] = cosTheta * cosPsi;