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),
57 goal_view_offset(0.0),
72 _heading_offset_deg(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::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
262 _roll_offset_deg = roll_offset_deg;
263 _pitch_offset_deg = pitch_offset_deg;
264 _heading_offset_deg = heading_offset_deg;
268 FGViewer::get_absolute_view_pos ()
272 return _absolute_view_pos;
276 FGViewer::getRelativeViewPos ()
280 return _relative_view_pos;
284 FGViewer::getZeroElevViewPos ()
288 return _zero_elev_view_pos;
292 // recalc() is done every time one of the setters is called (making the
293 // cached data "dirty"). It calculates all the outputs for viewer.
297 sgVec3 minus_z, right, forward, tilt;
299 sgMat4 tmpROT; // temp rotation work matrices
300 sgVec3 tmpVec3; // temp work vector (3)
303 // The position vectors originate from the view point or target location
304 // depending on the type of view.
306 if (_type == FG_RPH) {
307 recalcPositionVectors( _lon_deg, _lat_deg, _alt_ft );
309 recalcPositionVectors( _target_lon_deg, _target_lat_deg, _target_alt_ft );
312 sgCopyVec3(zero_elev, _zero_elev_view_pos);
313 sgCopyVec3(view_pos, _relative_view_pos);
315 if (_type == FG_LOOKAT) {
317 // Make the world up rotation matrix for lookat
318 sgMakeRotMat4( UP, _target_lon_deg, 0.0, -_target_lat_deg );
320 // get the world up verctor from the worldup rotation matrix
321 sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
323 sgCopyVec3( view_up, world_up );
326 // create offset vector
327 sgVec3 lookat_offset;
328 sgSetVec3( lookat_offset, _x_offset_m, _y_offset_m, _z_offset_m );
330 // Apply heading orientation and orientation offset to lookat_offset...
331 sgMakeRotMat4( tmpROT, _heading_offset_deg -_heading_deg, world_up);
332 sgXformVec3( lookat_offset, lookat_offset, UP );
333 sgXformVec3( lookat_offset, lookat_offset, tmpROT );
335 // Apply orientation offset tilt...
336 // FIXME: Need to get and use a "right" vector instead of 1-0-0
337 sgSetVec3 (tmpVec3, 1, 0, 0);
338 sgMakeRotMat4( tmpROT, _pitch_offset_deg, tmpVec3 );
339 sgXformPnt3( lookat_offset, lookat_offset, tmpROT );
341 // add the offsets including rotations to the coordinates
342 sgAddVec3( view_pos, lookat_offset );
344 // Make the VIEW matrix.
345 fgMakeLookAtMat4( VIEW, view_pos, view_forward, view_up );
348 // the VIEW matrix includes both rotation and translation. Let's
349 // knock out the translation part to make the VIEW_ROT matrix
350 sgCopyMat4( VIEW_ROT, VIEW );
351 VIEW_ROT[3][0] = VIEW_ROT[3][1] = VIEW_ROT[3][2] = 0.0;
355 if (_type == FG_RPH) {
357 // code to calculate LOCAL matrix calculated from Phi, Theta, and
358 // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
361 fgMakeLOCAL( LOCAL, _pitch_deg * SG_DEGREES_TO_RADIANS,
362 _roll_deg * SG_DEGREES_TO_RADIANS,
363 -_heading_deg * SG_DEGREES_TO_RADIANS);
365 // Make the world up rotation matrix for pilot view
366 sgMakeRotMat4( UP, _lon_deg, 0.0, -_lat_deg );
368 // get the world up verctor from the worldup rotation matrix
369 sgSetVec3( world_up, UP[0][0], UP[0][1], UP[0][2] );
371 // VIEWo becomes the rotation matrix with world_up incorporated
372 sgCopyMat4( VIEWo, LOCAL );
373 sgPostMultMat4( VIEWo, UP );
375 // generate the sg view up and forward vectors
376 sgSetVec3( view_up, VIEWo[0][0], VIEWo[0][1], VIEWo[0][2] );
377 sgSetVec3( right, VIEWo[1][0], VIEWo[1][1], VIEWo[1][2] );
378 sgSetVec3( forward, VIEWo[2][0], VIEWo[2][1], VIEWo[2][2] );
380 // apply the offsets in world coordinates
381 sgVec3 pilot_offset_world;
382 sgSetVec3( pilot_offset_world,
383 _z_offset_m, _y_offset_m, -_x_offset_m );
384 sgXformVec3( pilot_offset_world, pilot_offset_world, VIEWo );
386 // generate the view offset matrix using orientation offset (heading)
387 sgMakeRotMat4( VIEW_OFFSET, _heading_offset_deg, view_up );
389 // create a tilt matrix using orientation offset (pitch)
391 sgMakeRotMat4( VIEW_TILT, _pitch_offset_deg, right );
392 sgPreMultMat4(VIEW_OFFSET, VIEW_TILT);
393 sgXformVec3( view_forward, forward, VIEW_OFFSET );
394 SG_LOG( SG_VIEW, SG_DEBUG, "(RPH) view forward = "
395 << view_forward[0] << "," << view_forward[1] << ","
396 << view_forward[2] );
398 // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
399 fgMakeViewRot( VIEW_ROT, VIEW_OFFSET, VIEWo );
402 sgAddVec3( trans_vec, view_pos, pilot_offset_world );
404 // VIEW = VIEW_ROT * TRANS
405 sgCopyMat4( VIEW, VIEW_ROT );
406 sgPostMultMat4ByTransMat4( VIEW, trans_vec );
410 // Given a vector pointing straight down (-Z), map into onto the
411 // local plane representing "horizontal". This should give us the
412 // local direction for moving "south".
413 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
415 sgmap_vec_onto_cur_surface_plane(world_up, view_pos, minus_z,
417 sgNormalizeVec3(surface_south);
419 // now calculate the surface east vector
421 sgNegateVec3(world_down, world_up);
422 sgVectorProductVec3(surface_east, surface_south, world_down);
428 FGViewer::recalcPositionVectors (double lon_deg, double lat_deg, double alt_ft) const
430 double sea_level_radius_m;
434 // Convert from geodetic to geocentric
436 sgGeodToGeoc(lat_deg * SGD_DEGREES_TO_RADIANS,
437 alt_ft * SG_FEET_TO_METER,
441 // Calculate the cartesian coordinates
442 // of point directly below at sea level.
443 // aka Zero Elevation Position
444 Point3D p = Point3D(lon_deg * SG_DEGREES_TO_RADIANS,
447 Point3D tmp = sgPolarToCart3d(p) - scenery.get_next_center();
448 sgSetVec3(_zero_elev_view_pos, tmp[0], tmp[1], tmp[2]);
450 // Calculate the absolute view position
451 // in fgfs coordinates.
452 // aka Absolute View Position
453 p.setz(p.radius() + alt_ft * SG_FEET_TO_METER);
454 tmp = sgPolarToCart3d(p);
455 sgdSetVec3(_absolute_view_pos, tmp[0], tmp[1], tmp[2]);
457 // Calculate the relative view position
458 // from the scenery center.
459 // aka Relative View Position
460 sgdVec3 scenery_center;
461 sgdSetVec3(scenery_center,
462 scenery.get_next_center().x(),
463 scenery.get_next_center().y(),
464 scenery.get_next_center().z());
466 sgdSubVec3(view_pos, _absolute_view_pos, scenery_center);
467 sgSetVec3(_relative_view_pos, view_pos);
472 FGViewer::get_h_fov()
474 switch (scalingType) {
475 case FG_SCALING_WIDTH: // h_fov == fov
478 if (aspect_ratio < 1.0) {
483 return atan(tan(fov/2 * SG_DEGREES_TO_RADIANS) / aspect_ratio) *
484 SG_RADIANS_TO_DEGREES * 2;
492 FGViewer::get_v_fov()
494 switch (scalingType) {
495 case FG_SCALING_WIDTH: // h_fov == fov
496 return atan(tan(fov/2 * SG_DEGREES_TO_RADIANS) * aspect_ratio) *
497 SG_RADIANS_TO_DEGREES * 2;
499 if (aspect_ratio < 1.0) {
501 return atan(tan(fov/2 * SG_DEGREES_TO_RADIANS) * aspect_ratio) *
502 SG_RADIANS_TO_DEGREES * 2;
513 FGViewer::update (int dt)
516 for ( i = 0; i < dt; i++ ) {
517 if ( fabs(get_goal_view_offset() - getHeadingOffset_deg()) < 1 ) {
518 setHeadingOffset_deg( get_goal_view_offset() );
521 // move current_view.headingoffset towards
522 // current_view.goal_view_offset
523 if ( get_goal_view_offset() > getHeadingOffset_deg() )
525 if ( get_goal_view_offset() - getHeadingOffset_deg() < 180 ){
526 inc_view_offset( 0.5 );
528 inc_view_offset( -0.5 );
531 if ( getHeadingOffset_deg() - get_goal_view_offset() < 180 ){
532 inc_view_offset( -0.5 );
534 inc_view_offset( 0.5 );
537 if ( getHeadingOffset_deg() > 360 ) {
538 inc_view_offset( -360 );
539 } else if ( getHeadingOffset_deg() < 0 ) {
540 inc_view_offset( 360 );
545 for ( i = 0; i < dt; i++ ) {
546 if ( fabs(get_goal_view_tilt() - getPitchOffset_deg()) < 1 ) {
547 setPitchOffset_deg( get_goal_view_tilt() );
550 // move current_view.pitch_offset_deg towards
551 // current_view.goal_view_tilt
552 if ( get_goal_view_tilt() > getPitchOffset_deg() )
554 if ( get_goal_view_tilt() - getPitchOffset_deg() < 0 ){
555 inc_view_tilt( 1.0 );
557 inc_view_tilt( -1.0 );
560 if ( getPitchOffset_deg() - get_goal_view_tilt() < 0 ){
561 inc_view_tilt( -1.0 );
563 inc_view_tilt( 1.0 );
566 if ( getPitchOffset_deg() > 90 ) {
567 setPitchOffset_deg(90);
568 } else if ( getPitchOffset_deg() < -90 ) {
569 setPitchOffset_deg( -90 );
576 void FGViewer::fgMakeLookAtMat4 ( sgMat4 dst, const sgVec3 eye, const sgVec3 center,
580 // 1) In order to compute the line of sight, the eye point must not be equal
581 // to the center point.
582 // 2) The up vector must not be parallel to the line of sight from the eye
583 // to the center point.
585 /* Compute the direction vectors */
588 /* Y vector = center - eye */
589 sgSubVec3 ( y, center, eye ) ;
592 sgCopyVec3 ( z, up ) ;
594 /* X vector = Y cross Z */
595 sgVectorProductVec3 ( x, y, z ) ;
597 /* Recompute Z = X cross Y */
598 sgVectorProductVec3 ( z, x, y ) ;
600 /* Normalize everything */
601 sgNormaliseVec3 ( x ) ;
602 sgNormaliseVec3 ( y ) ;
603 sgNormaliseVec3 ( z ) ;
605 /* Build the matrix */
606 #define M(row,col) dst[row][col]
607 M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = 0.0;
608 M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = 0.0;
609 M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = 0.0;
610 M(3,0) = eye[0]; M(3,1) = eye[1]; M(3,2) = eye[2]; M(3,3) = 1.0;
613 /* end from lookat */
616 // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
617 // This takes advantage of the fact that VIEWo and VIEW_OFFSET
618 // only have entries in the upper 3x3 block
619 // and that LARC_TO_SSG is just a shift of rows NHV
620 void FGViewer::fgMakeViewRot( sgMat4 dst, const sgMat4 m1, const sgMat4 m2 )
622 for ( int j = 0 ; j < 3 ; j++ ) {
623 dst[2][j] = m2[0][0] * m1[0][j] +
624 m2[0][1] * m1[1][j] +
627 dst[0][j] = m2[1][0] * m1[0][j] +
628 m2[1][1] * m1[1][j] +
631 dst[1][j] = m2[2][0] * m1[0][j] +
632 m2[2][1] * m1[1][j] +
645 void FGViewer::fgMakeLOCAL( sgMat4 dst, const double Theta,
646 const double Phi, const double Psi)
648 SGfloat cosTheta = (SGfloat) cos(Theta);
649 SGfloat sinTheta = (SGfloat) sin(Theta);
650 SGfloat cosPhi = (SGfloat) cos(Phi);
651 SGfloat sinPhi = (SGfloat) sin(Phi);
652 SGfloat sinPsi = (SGfloat) sin(Psi) ;
653 SGfloat cosPsi = (SGfloat) cos(Psi) ;
655 dst[0][0] = cosPhi * cosTheta;
656 dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
657 dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
660 dst[1][0] = -sinPhi * cosTheta;
661 dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
662 dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
663 dst[1][3] = SG_ZERO ;
665 dst[2][0] = sinTheta;
666 dst[2][1] = cosTheta * -sinPsi;
667 dst[2][2] = cosTheta * cosPsi;