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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <simgear/compiler.h>
32 #include "fg_props.hxx"
38 #include <simgear/debug/logstream.hxx>
39 #include <simgear/constants.h>
40 #include <simgear/scene/model/placement.hxx>
41 #include <simgear/math/vector.hxx>
43 #include <Main/globals.hxx>
44 #include <Scenery/scenery.hxx>
45 #include <Model/acmodel.hxx>
49 #include "CameraGroup.hxx"
51 using namespace flightgear;
53 ////////////////////////////////////////////////////////////////////////
54 // Implementation of FGViewer.
55 ////////////////////////////////////////////////////////////////////////
58 FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index,
59 bool at_model, int at_model_index,
60 double damp_roll, double damp_pitch, double damp_heading,
61 double x_offset_m, double y_offset_m, double z_offset_m,
62 double heading_offset_deg, double pitch_offset_deg,
63 double roll_offset_deg,
64 double fov_deg, double aspect_ratio_multiplier,
65 double target_x_offset_m, double target_y_offset_m,
66 double target_z_offset_m, double near_m, bool internal ):
81 _scaling_type(FG_SCALING_MAX),
82 _cameraGroup(CameraGroup::getDefault())
84 _absolute_view_pos = SGVec3d(0, 0, 0);
86 _from_model = from_model;
87 _from_model_index = from_model_index;
89 _at_model_index = at_model_index;
94 _damp_roll = 1.0 / pow(10.0, fabs(damp_roll));
96 _damp_pitch = 1.0 / pow(10.0, fabs(damp_pitch));
97 if (damp_heading > 0.0)
98 _damp_heading = 1.0 / pow(10.0, fabs(damp_heading));
100 _offset_m.x() = x_offset_m;
101 _offset_m.y() = y_offset_m;
102 _offset_m.z() = z_offset_m;
103 _heading_offset_deg = heading_offset_deg;
104 _pitch_offset_deg = pitch_offset_deg;
105 _roll_offset_deg = roll_offset_deg;
106 _goal_heading_offset_deg = heading_offset_deg;
107 _goal_pitch_offset_deg = pitch_offset_deg;
108 _goal_roll_offset_deg = roll_offset_deg;
115 _aspect_ratio_multiplier = aspect_ratio_multiplier;
116 _target_offset_m.x() = target_x_offset_m;
117 _target_offset_m.y() = target_y_offset_m;
118 _target_offset_m.z() = target_z_offset_m;
119 _ground_level_nearplane_m = near_m;
120 // a reasonable guess for init, so that the math doesn't blow up
125 FGViewer::~FGViewer( void ) {
144 FGViewer::setType ( int type )
153 FGViewer::setInternal ( bool internal )
155 _internal = internal;
159 FGViewer::setLongitude_deg (double lon_deg)
166 FGViewer::setLatitude_deg (double lat_deg)
173 FGViewer::setAltitude_ft (double alt_ft)
180 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
189 FGViewer::setTargetLongitude_deg (double lon_deg)
192 _target_lon_deg = lon_deg;
196 FGViewer::setTargetLatitude_deg (double lat_deg)
199 _target_lat_deg = lat_deg;
203 FGViewer::setTargetAltitude_ft (double alt_ft)
206 _target_alt_ft = alt_ft;
210 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
213 _target_lon_deg = lon_deg;
214 _target_lat_deg = lat_deg;
215 _target_alt_ft = alt_ft;
219 FGViewer::setRoll_deg (double roll_deg)
222 _roll_deg = roll_deg;
226 FGViewer::setPitch_deg (double pitch_deg)
229 _pitch_deg = pitch_deg;
233 FGViewer::setHeading_deg (double heading_deg)
236 _heading_deg = heading_deg;
240 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
243 _roll_deg = roll_deg;
244 _pitch_deg = pitch_deg;
245 _heading_deg = heading_deg;
249 FGViewer::setTargetRoll_deg (double target_roll_deg)
252 _target_roll_deg = target_roll_deg;
256 FGViewer::setTargetPitch_deg (double target_pitch_deg)
259 _target_pitch_deg = target_pitch_deg;
263 FGViewer::setTargetHeading_deg (double target_heading_deg)
266 _target_heading_deg = target_heading_deg;
270 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
273 _target_roll_deg = target_roll_deg;
274 _target_pitch_deg = target_pitch_deg;
275 _target_heading_deg = target_heading_deg;
279 FGViewer::setXOffset_m (double x_offset_m)
282 _offset_m.x() = x_offset_m;
286 FGViewer::setYOffset_m (double y_offset_m)
289 _offset_m.y() = y_offset_m;
293 FGViewer::setZOffset_m (double z_offset_m)
296 _offset_m.z() = z_offset_m;
300 FGViewer::setTargetXOffset_m (double target_x_offset_m)
303 _target_offset_m.x() = target_x_offset_m;
307 FGViewer::setTargetYOffset_m (double target_y_offset_m)
310 _target_offset_m.y() = target_y_offset_m;
314 FGViewer::setTargetZOffset_m (double target_z_offset_m)
317 _target_offset_m.z() = target_z_offset_m;
321 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
324 _offset_m.x() = x_offset_m;
325 _offset_m.y() = y_offset_m;
326 _offset_m.z() = z_offset_m;
330 FGViewer::setRollOffset_deg (double roll_offset_deg)
333 _roll_offset_deg = roll_offset_deg;
337 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
340 _pitch_offset_deg = pitch_offset_deg;
344 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
347 _heading_offset_deg = heading_offset_deg;
351 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
354 _goal_roll_offset_deg = goal_roll_offset_deg;
358 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
361 _goal_pitch_offset_deg = goal_pitch_offset_deg;
362 if ( _goal_pitch_offset_deg < -90 ) {
363 _goal_pitch_offset_deg = -90.0;
365 if ( _goal_pitch_offset_deg > 90.0 ) {
366 _goal_pitch_offset_deg = 90.0;
372 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
375 _goal_heading_offset_deg = goal_heading_offset_deg;
376 while ( _goal_heading_offset_deg < 0.0 ) {
377 _goal_heading_offset_deg += 360;
379 while ( _goal_heading_offset_deg > 360 ) {
380 _goal_heading_offset_deg -= 360;
385 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
388 _roll_offset_deg = roll_offset_deg;
389 _pitch_offset_deg = pitch_offset_deg;
390 _heading_offset_deg = heading_offset_deg;
393 // recalc() is done every time one of the setters is called (making the
394 // cached data "dirty") on the next "get". It calculates all the outputs
399 if (_type == FG_LOOKFROM) {
408 // recalculate for LookFrom view type...
410 FGViewer::recalcLookFrom ()
412 // Update location data ...
414 SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
415 _lat_deg = placement->getLatitudeDeg();
416 _lon_deg = placement->getLongitudeDeg();
417 _alt_ft = placement->getElevationFt();
419 _heading_deg = placement->getHeadingDeg();
420 _pitch_deg = placement->getPitchDeg();
421 _roll_deg = placement->getRollDeg();
423 double lat = _lat_deg;
424 double lon = _lon_deg;
425 double alt = _alt_ft;
426 double head = _heading_deg;
427 double pitch = _pitch_deg;
428 double roll = _roll_deg;
429 if ( !_from_model ) {
430 // update from our own data...
431 dampEyeData(roll, pitch, head);
434 // The geodetic position of our base view position
435 SGGeod geodPos = SGGeod::fromDegFt(lon, lat, alt);
436 // The rotation rotating from the earth centerd frame to
437 // the horizontal local OpenGL frame
438 SGQuatd hlOr = SGQuatd::viewHL(geodPos);
440 // the rotation from the horizontal local frame to the basic view orientation
441 SGQuatd hlToBody = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
442 hlToBody = SGQuatd::simToView(hlToBody);
444 // The cartesian position of the basic view coordinate
445 SGVec3d position = SGVec3d::fromGeod(geodPos);
446 // the rotation offset, don't know why heading is negative here ...
447 SGQuatd viewOffsetOr = SGQuatd::simToView(
448 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg, _pitch_offset_deg,
451 // Compute the eyepoints orientation and position
452 // wrt the earth centered frame - that is global coorinates
453 SGQuatd ec2body = hlOr*hlToBody;
454 _absolute_view_pos = position + ec2body.backTransform(_offset_m);
455 mViewOrientation = ec2body*viewOffsetOr;
459 FGViewer::recalcLookAt ()
461 // The geodetic position of our target to look at
463 SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
464 _target_lat_deg = placement->getLatitudeDeg();
465 _target_lon_deg = placement->getLongitudeDeg();
466 _target_alt_ft = placement->getElevationFt();
467 _target_heading_deg = placement->getHeadingDeg();
468 _target_pitch_deg = placement->getPitchDeg();
469 _target_roll_deg = placement->getRollDeg();
471 // if not model then calculate our own target position...
472 dampEyeData(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
475 SGGeod geodTargetPos = SGGeod::fromDegFt(_target_lon_deg,
478 SGQuatd geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
481 SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(geodTargetPos);
485 SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
486 _lat_deg = placement->getLatitudeDeg();
487 _lon_deg = placement->getLongitudeDeg();
488 _alt_ft = placement->getElevationFt();
489 _heading_deg = placement->getHeadingDeg();
490 _pitch_deg = placement->getPitchDeg();
491 _roll_deg = placement->getRollDeg();
493 // update from our own data, just the rotation here...
494 dampEyeData(_roll_deg, _pitch_deg, _heading_deg);
496 SGGeod geodEyePos = SGGeod::fromDegFt(_lon_deg, _lat_deg, _alt_ft);
497 SGQuatd geodEyeOr = SGQuatd::fromYawPitchRollDeg(_heading_deg,
500 SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(geodEyePos);
502 // the rotation offset, don't know why heading is negative here ...
503 SGQuatd eyeOffsetOr =
504 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg + 180, _pitch_offset_deg,
507 // Offsets to the eye position
508 SGVec3d eyeOff(-_offset_m.z(), _offset_m.x(), -_offset_m.y());
509 SGQuatd ec2eye = geodEyeHlOr*geodEyeOr;
510 SGVec3d eyeCart = SGVec3d::fromGeod(geodEyePos);
511 eyeCart += (ec2eye*eyeOffsetOr).backTransform(eyeOff);
513 SGVec3d atCart = SGVec3d::fromGeod(geodTargetPos);
515 // add target offsets to at_position...
516 SGVec3d target_pos_off(-_target_offset_m.z(), _target_offset_m.x(),
517 -_target_offset_m.y());
518 target_pos_off = (geodTargetHlOr*geodTargetOr).backTransform(target_pos_off);
519 atCart += target_pos_off;
520 eyeCart += target_pos_off;
522 // Compute the eyepoints orientation and position
523 // wrt the earth centered frame - that is global coorinates
524 _absolute_view_pos = eyeCart;
526 // the view direction
527 SGVec3d dir = normalize(atCart - eyeCart);
529 SGVec3d up = ec2eye.backTransform(SGVec3d(0, 0, -1));
530 // rotate dir to the 0-th unit vector
531 // rotate up to 2-th unit vector
532 mViewOrientation = SGQuatd::fromRotateTo(-dir, 2, up, 1);
536 FGViewer::dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg)
538 const double interval = 0.01;
540 static FGViewer *last_view = 0;
541 if (last_view != this) {
543 _damped_roll_deg = roll_deg;
544 _damped_pitch_deg = pitch_deg;
545 _damped_heading_deg = heading_deg;
550 if (_damp_sync < interval) {
551 if (_damp_roll > 0.0)
552 roll_deg = _damped_roll_deg;
553 if (_damp_pitch > 0.0)
554 pitch_deg = _damped_pitch_deg;
555 if (_damp_heading > 0.0)
556 heading_deg = _damped_heading_deg;
560 while (_damp_sync >= interval) {
561 _damp_sync -= interval;
564 if (_damp_roll > 0.0) {
565 d = _damped_roll_deg - roll_deg;
567 _damped_roll_deg -= 360.0;
569 _damped_roll_deg += 360.0;
570 roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
573 if (_damp_pitch > 0.0) {
574 d = _damped_pitch_deg - pitch_deg;
576 _damped_pitch_deg -= 360.0;
578 _damped_pitch_deg += 360.0;
579 pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
582 if (_damp_heading > 0.0) {
583 d = _damped_heading_deg - heading_deg;
585 _damped_heading_deg -= 360.0;
587 _damped_heading_deg += 360.0;
588 heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
594 FGViewer::get_h_fov()
596 switch (_scaling_type) {
597 case FG_SCALING_WIDTH: // h_fov == fov
600 if (_aspect_ratio < 1.0) {
606 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
607 / (_aspect_ratio*_aspect_ratio_multiplier))
608 * SG_RADIANS_TO_DEGREES * 2;
619 FGViewer::get_v_fov()
621 switch (_scaling_type) {
622 case FG_SCALING_WIDTH: // h_fov == fov
624 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
625 * (_aspect_ratio*_aspect_ratio_multiplier))
626 * SG_RADIANS_TO_DEGREES * 2;
628 if (_aspect_ratio < 1.0) {
631 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
632 * (_aspect_ratio*_aspect_ratio_multiplier))
633 * SG_RADIANS_TO_DEGREES * 2;
645 FGViewer::update (double dt)
650 int dt_ms = int(dt * 1000);
651 for ( i = 0; i < dt_ms; i++ ) {
652 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
653 setHeadingOffset_deg( _goal_heading_offset_deg );
656 // move current_view.headingoffset towards
657 // current_view.goal_view_offset
658 if ( _goal_heading_offset_deg > _heading_offset_deg )
660 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
661 incHeadingOffset_deg( 0.5 );
663 incHeadingOffset_deg( -0.5 );
666 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
667 incHeadingOffset_deg( -0.5 );
669 incHeadingOffset_deg( 0.5 );
672 if ( _heading_offset_deg > 360 ) {
673 incHeadingOffset_deg( -360 );
674 } else if ( _heading_offset_deg < 0 ) {
675 incHeadingOffset_deg( 360 );
680 for ( i = 0; i < dt_ms; i++ ) {
681 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
682 setPitchOffset_deg( _goal_pitch_offset_deg );
685 // move current_view.pitch_offset_deg towards
686 // current_view.goal_pitch_offset
687 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
689 incPitchOffset_deg( 1.0 );
691 incPitchOffset_deg( -1.0 );
693 if ( _pitch_offset_deg > 90 ) {
694 setPitchOffset_deg(90);
695 } else if ( _pitch_offset_deg < -90 ) {
696 setPitchOffset_deg( -90 );
702 for ( i = 0; i < dt_ms; i++ ) {
703 if ( fabs( _goal_roll_offset_deg - _roll_offset_deg ) < 1 ) {
704 setRollOffset_deg( _goal_roll_offset_deg );
707 // move current_view.roll_offset_deg towards
708 // current_view.goal_roll_offset
709 if ( _goal_roll_offset_deg > _roll_offset_deg )
711 incRollOffset_deg( 1.0 );
713 incRollOffset_deg( -1.0 );
715 if ( _roll_offset_deg > 90 ) {
716 setRollOffset_deg(90);
717 } else if ( _roll_offset_deg < -90 ) {
718 setRollOffset_deg( -90 );
723 _cameraGroup->update(_absolute_view_pos.osg(), mViewOrientation.osg());
724 _cameraGroup->setCameraParameters(get_v_fov(), get_aspect_ratio());