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),
83 _cameraGroup(CameraGroup::getDefault())
85 _absolute_view_pos = SGVec3d(0, 0, 0);
87 _from_model = from_model;
88 _from_model_index = from_model_index;
90 _at_model_index = at_model_index;
95 _damp_roll = 1.0 / pow(10.0, fabs(damp_roll));
97 _damp_pitch = 1.0 / pow(10.0, fabs(damp_pitch));
98 if (damp_heading > 0.0)
99 _damp_heading = 1.0 / pow(10.0, fabs(damp_heading));
101 _offset_m.x() = x_offset_m;
102 _offset_m.y() = y_offset_m;
103 _offset_m.z() = z_offset_m;
104 _heading_offset_deg = heading_offset_deg;
105 _pitch_offset_deg = pitch_offset_deg;
106 _roll_offset_deg = roll_offset_deg;
107 _goal_heading_offset_deg = heading_offset_deg;
108 _goal_pitch_offset_deg = pitch_offset_deg;
109 _goal_roll_offset_deg = roll_offset_deg;
116 _aspect_ratio_multiplier = aspect_ratio_multiplier;
117 _target_offset_m.x() = target_x_offset_m;
118 _target_offset_m.y() = target_y_offset_m;
119 _target_offset_m.z() = target_z_offset_m;
120 _ground_level_nearplane_m = near_m;
121 // a reasonable guess for init, so that the math doesn't blow up
126 FGViewer::~FGViewer( void ) {
145 FGViewer::setType ( int type )
154 FGViewer::setInternal ( bool internal )
156 _internal = internal;
160 FGViewer::setLongitude_deg (double lon_deg)
167 FGViewer::setLatitude_deg (double lat_deg)
174 FGViewer::setAltitude_ft (double alt_ft)
181 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
190 FGViewer::setTargetLongitude_deg (double lon_deg)
193 _target_lon_deg = lon_deg;
197 FGViewer::setTargetLatitude_deg (double lat_deg)
200 _target_lat_deg = lat_deg;
204 FGViewer::setTargetAltitude_ft (double alt_ft)
207 _target_alt_ft = alt_ft;
211 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
214 _target_lon_deg = lon_deg;
215 _target_lat_deg = lat_deg;
216 _target_alt_ft = alt_ft;
220 FGViewer::setRoll_deg (double roll_deg)
223 _roll_deg = roll_deg;
227 FGViewer::setPitch_deg (double pitch_deg)
230 _pitch_deg = pitch_deg;
234 FGViewer::setHeading_deg (double heading_deg)
237 _heading_deg = heading_deg;
241 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
244 _roll_deg = roll_deg;
245 _pitch_deg = pitch_deg;
246 _heading_deg = heading_deg;
250 FGViewer::setTargetRoll_deg (double target_roll_deg)
253 _target_roll_deg = target_roll_deg;
257 FGViewer::setTargetPitch_deg (double target_pitch_deg)
260 _target_pitch_deg = target_pitch_deg;
264 FGViewer::setTargetHeading_deg (double target_heading_deg)
267 _target_heading_deg = target_heading_deg;
271 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
274 _target_roll_deg = target_roll_deg;
275 _target_pitch_deg = target_pitch_deg;
276 _target_heading_deg = target_heading_deg;
280 FGViewer::setXOffset_m (double x_offset_m)
283 _offset_m.x() = x_offset_m;
287 FGViewer::setYOffset_m (double y_offset_m)
290 _offset_m.y() = y_offset_m;
294 FGViewer::setZOffset_m (double z_offset_m)
297 _offset_m.z() = z_offset_m;
301 FGViewer::setTargetXOffset_m (double target_x_offset_m)
304 _target_offset_m.x() = target_x_offset_m;
308 FGViewer::setTargetYOffset_m (double target_y_offset_m)
311 _target_offset_m.y() = target_y_offset_m;
315 FGViewer::setTargetZOffset_m (double target_z_offset_m)
318 _target_offset_m.z() = target_z_offset_m;
322 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
325 _offset_m.x() = x_offset_m;
326 _offset_m.y() = y_offset_m;
327 _offset_m.z() = z_offset_m;
331 FGViewer::setRollOffset_deg (double roll_offset_deg)
334 _roll_offset_deg = roll_offset_deg;
338 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
341 _pitch_offset_deg = pitch_offset_deg;
345 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
348 _heading_offset_deg = heading_offset_deg;
352 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
355 _goal_roll_offset_deg = goal_roll_offset_deg;
359 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
362 _goal_pitch_offset_deg = goal_pitch_offset_deg;
363 if ( _goal_pitch_offset_deg < -90 ) {
364 _goal_pitch_offset_deg = -90.0;
366 if ( _goal_pitch_offset_deg > 90.0 ) {
367 _goal_pitch_offset_deg = 90.0;
373 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
376 _goal_heading_offset_deg = goal_heading_offset_deg;
377 while ( _goal_heading_offset_deg < 0.0 ) {
378 _goal_heading_offset_deg += 360;
380 while ( _goal_heading_offset_deg > 360 ) {
381 _goal_heading_offset_deg -= 360;
386 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
389 _roll_offset_deg = roll_offset_deg;
390 _pitch_offset_deg = pitch_offset_deg;
391 _heading_offset_deg = heading_offset_deg;
394 // recalc() is done every time one of the setters is called (making the
395 // cached data "dirty") on the next "get". It calculates all the outputs
400 if (_type == FG_LOOKFROM) {
409 // recalculate for LookFrom view type...
411 FGViewer::recalcLookFrom ()
413 // Update location data ...
415 SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
416 _lat_deg = placement->getLatitudeDeg();
417 _lon_deg = placement->getLongitudeDeg();
418 _alt_ft = placement->getElevationFt();
420 _heading_deg = placement->getHeadingDeg();
421 _pitch_deg = placement->getPitchDeg();
422 _roll_deg = placement->getRollDeg();
424 double lat = _lat_deg;
425 double lon = _lon_deg;
426 double alt = _alt_ft;
427 double head = _heading_deg;
428 double pitch = _pitch_deg;
429 double roll = _roll_deg;
430 if ( !_from_model ) {
431 // update from our own data...
432 dampEyeData(roll, pitch, head);
435 // The geodetic position of our base view position
436 SGGeod geodPos = SGGeod::fromDegFt(lon, lat, alt);
437 // The rotation rotating from the earth centerd frame to
438 // the horizontal local OpenGL frame
439 SGQuatd hlOr = SGQuatd::viewHL(geodPos);
441 // the rotation from the horizontal local frame to the basic view orientation
442 SGQuatd hlToBody = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
443 hlToBody = SGQuatd::simToView(hlToBody);
445 // The cartesian position of the basic view coordinate
446 SGVec3d position = SGVec3d::fromGeod(geodPos);
447 // the rotation offset, don't know why heading is negative here ...
448 SGQuatd viewOffsetOr = SGQuatd::simToView(
449 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg, _pitch_offset_deg,
452 // Compute the eyepoints orientation and position
453 // wrt the earth centered frame - that is global coorinates
454 SGQuatd ec2body = hlOr*hlToBody;
455 _absolute_view_pos = position + ec2body.backTransform(_offset_m);
456 mViewOrientation = ec2body*viewOffsetOr;
460 FGViewer::recalcLookAt ()
462 // The geodetic position of our target to look at
464 SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
465 _target_lat_deg = placement->getLatitudeDeg();
466 _target_lon_deg = placement->getLongitudeDeg();
467 _target_alt_ft = placement->getElevationFt();
468 _target_heading_deg = placement->getHeadingDeg();
469 _target_pitch_deg = placement->getPitchDeg();
470 _target_roll_deg = placement->getRollDeg();
472 // if not model then calculate our own target position...
473 dampEyeData(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
476 SGGeod geodTargetPos = SGGeod::fromDegFt(_target_lon_deg,
479 SGQuatd geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
482 SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(geodTargetPos);
486 SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
487 _lat_deg = placement->getLatitudeDeg();
488 _lon_deg = placement->getLongitudeDeg();
489 _alt_ft = placement->getElevationFt();
490 _heading_deg = placement->getHeadingDeg();
491 _pitch_deg = placement->getPitchDeg();
492 _roll_deg = placement->getRollDeg();
494 // update from our own data, just the rotation here...
495 dampEyeData(_roll_deg, _pitch_deg, _heading_deg);
497 SGGeod geodEyePos = SGGeod::fromDegFt(_lon_deg, _lat_deg, _alt_ft);
498 SGQuatd geodEyeOr = SGQuatd::fromYawPitchRollDeg(_heading_deg,
501 SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(geodEyePos);
503 // the rotation offset, don't know why heading is negative here ...
504 SGQuatd eyeOffsetOr =
505 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg + 180, _pitch_offset_deg,
508 // Offsets to the eye position
509 SGVec3d eyeOff(-_offset_m.z(), _offset_m.x(), -_offset_m.y());
510 SGQuatd ec2eye = geodEyeHlOr*geodEyeOr;
511 SGVec3d eyeCart = SGVec3d::fromGeod(geodEyePos);
512 eyeCart += (ec2eye*eyeOffsetOr).backTransform(eyeOff);
514 SGVec3d atCart = SGVec3d::fromGeod(geodTargetPos);
516 // add target offsets to at_position...
517 SGVec3d target_pos_off(-_target_offset_m.z(), _target_offset_m.x(),
518 -_target_offset_m.y());
519 target_pos_off = (geodTargetHlOr*geodTargetOr).backTransform(target_pos_off);
520 atCart += target_pos_off;
521 eyeCart += target_pos_off;
523 // Compute the eyepoints orientation and position
524 // wrt the earth centered frame - that is global coorinates
525 _absolute_view_pos = eyeCart;
527 // the view direction
528 SGVec3d dir = normalize(atCart - eyeCart);
530 SGVec3d up = ec2eye.backTransform(SGVec3d(0, 0, -1));
531 // rotate dir to the 0-th unit vector
532 // rotate up to 2-th unit vector
533 mViewOrientation = SGQuatd::fromRotateTo(-dir, 2, up, 1);
537 FGViewer::dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg)
539 const double interval = 0.01;
541 static FGViewer *last_view = 0;
542 if (last_view != this) {
544 _damped_roll_deg = roll_deg;
545 _damped_pitch_deg = pitch_deg;
546 _damped_heading_deg = heading_deg;
551 if (_damp_sync < interval) {
552 if (_damp_roll > 0.0)
553 roll_deg = _damped_roll_deg;
554 if (_damp_pitch > 0.0)
555 pitch_deg = _damped_pitch_deg;
556 if (_damp_heading > 0.0)
557 heading_deg = _damped_heading_deg;
561 while (_damp_sync >= interval) {
562 _damp_sync -= interval;
565 if (_damp_roll > 0.0) {
566 d = _damped_roll_deg - roll_deg;
568 _damped_roll_deg -= 360.0;
570 _damped_roll_deg += 360.0;
571 roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
574 if (_damp_pitch > 0.0) {
575 d = _damped_pitch_deg - pitch_deg;
577 _damped_pitch_deg -= 360.0;
579 _damped_pitch_deg += 360.0;
580 pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
583 if (_damp_heading > 0.0) {
584 d = _damped_heading_deg - heading_deg;
586 _damped_heading_deg -= 360.0;
588 _damped_heading_deg += 360.0;
589 heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
595 FGViewer::get_h_fov()
597 switch (_scaling_type) {
598 case FG_SCALING_WIDTH: // h_fov == fov
601 if (_aspect_ratio < 1.0) {
607 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
608 / (_aspect_ratio*_aspect_ratio_multiplier))
609 * SG_RADIANS_TO_DEGREES * 2;
620 FGViewer::get_v_fov()
622 switch (_scaling_type) {
623 case FG_SCALING_WIDTH: // h_fov == fov
625 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
626 * (_aspect_ratio*_aspect_ratio_multiplier))
627 * SG_RADIANS_TO_DEGREES * 2;
629 if (_aspect_ratio < 1.0) {
632 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
633 * (_aspect_ratio*_aspect_ratio_multiplier))
634 * SG_RADIANS_TO_DEGREES * 2;
646 FGViewer::update (double dt)
651 int dt_ms = int(dt * 1000);
652 for ( i = 0; i < dt_ms; i++ ) {
653 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
654 setHeadingOffset_deg( _goal_heading_offset_deg );
657 // move current_view.headingoffset towards
658 // current_view.goal_view_offset
659 if ( _goal_heading_offset_deg > _heading_offset_deg )
661 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
662 incHeadingOffset_deg( 0.5 );
664 incHeadingOffset_deg( -0.5 );
667 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
668 incHeadingOffset_deg( -0.5 );
670 incHeadingOffset_deg( 0.5 );
673 if ( _heading_offset_deg > 360 ) {
674 incHeadingOffset_deg( -360 );
675 } else if ( _heading_offset_deg < 0 ) {
676 incHeadingOffset_deg( 360 );
681 for ( i = 0; i < dt_ms; i++ ) {
682 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
683 setPitchOffset_deg( _goal_pitch_offset_deg );
686 // move current_view.pitch_offset_deg towards
687 // current_view.goal_pitch_offset
688 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
690 incPitchOffset_deg( 1.0 );
692 incPitchOffset_deg( -1.0 );
694 if ( _pitch_offset_deg > 90 ) {
695 setPitchOffset_deg(90);
696 } else if ( _pitch_offset_deg < -90 ) {
697 setPitchOffset_deg( -90 );
703 for ( i = 0; i < dt_ms; i++ ) {
704 if ( fabs( _goal_roll_offset_deg - _roll_offset_deg ) < 1 ) {
705 setRollOffset_deg( _goal_roll_offset_deg );
708 // move current_view.roll_offset_deg towards
709 // current_view.goal_roll_offset
710 if ( _goal_roll_offset_deg > _roll_offset_deg )
712 incRollOffset_deg( 1.0 );
714 incRollOffset_deg( -1.0 );
716 if ( _roll_offset_deg > 90 ) {
717 setRollOffset_deg(90);
718 } else if ( _roll_offset_deg < -90 ) {
719 setRollOffset_deg( -90 );
724 _cameraGroup->update(_absolute_view_pos.osg(), mViewOrientation.osg());
725 _cameraGroup->setCameraParameters(get_v_fov(), get_aspect_ratio());