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/math/polar3d.hxx>
41 #include <simgear/math/sg_geodesy.hxx>
42 #include <simgear/scene/model/placement.hxx>
43 #include <simgear/math/vector.hxx>
45 #include <Main/globals.hxx>
46 #include <Scenery/scenery.hxx>
47 #include <Model/acmodel.hxx>
51 #include "CameraGroup.hxx"
53 using namespace flightgear;
55 ////////////////////////////////////////////////////////////////////////
56 // Implementation of FGViewer.
57 ////////////////////////////////////////////////////////////////////////
60 FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index,
61 bool at_model, int at_model_index,
62 double damp_roll, double damp_pitch, double damp_heading,
63 double x_offset_m, double y_offset_m, double z_offset_m,
64 double heading_offset_deg, double pitch_offset_deg,
65 double roll_offset_deg,
66 double fov_deg, double aspect_ratio_multiplier,
67 double target_x_offset_m, double target_y_offset_m,
68 double target_z_offset_m, double near_m, bool internal ):
83 _scaling_type(FG_SCALING_MAX),
84 _cameraGroup(CameraGroup::getDefault())
86 _absolute_view_pos = SGVec3d(0, 0, 0);
88 _from_model = from_model;
89 _from_model_index = from_model_index;
91 _at_model_index = at_model_index;
96 _damp_roll = 1.0 / pow(10.0, fabs(damp_roll));
98 _damp_pitch = 1.0 / pow(10.0, fabs(damp_pitch));
99 if (damp_heading > 0.0)
100 _damp_heading = 1.0 / pow(10.0, fabs(damp_heading));
102 _offset_m.x() = x_offset_m;
103 _offset_m.y() = y_offset_m;
104 _offset_m.z() = z_offset_m;
105 _heading_offset_deg = heading_offset_deg;
106 _pitch_offset_deg = pitch_offset_deg;
107 _roll_offset_deg = roll_offset_deg;
108 _goal_heading_offset_deg = heading_offset_deg;
109 _goal_pitch_offset_deg = pitch_offset_deg;
110 _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 ...
414 double lat, lon, alt, head, pitch, roll;
416 SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
417 lat = placement->getLatitudeDeg();
418 lon = placement->getLongitudeDeg();
419 alt = placement->getElevationFt();
421 head = placement->getHeadingDeg();
422 pitch = placement->getPitchDeg();
423 roll = placement->getRollDeg();
432 if ( !_from_model ) {
433 // update from our own data...
434 dampEyeData(roll, pitch, head);
437 // The geodetic position of our base view position
438 SGGeod geodPos = SGGeod::fromDegFt(lon, lat, alt);
439 // The rotation rotating from the earth centerd frame to
440 // the horizontal local OpenGL frame
441 SGQuatd hlOr = SGQuatd::viewHL(geodPos);
443 // the rotation from the horizontal local frame to the basic view orientation
444 SGQuatd hlToBody = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
445 hlToBody = SGQuatd::simToView(hlToBody);
447 // The cartesian position of the basic view coordinate
448 SGVec3d position = SGVec3d::fromGeod(geodPos);
449 // the rotation offset, don't know why heading is negative here ...
450 SGQuatd viewOffsetOr = SGQuatd::simToView(
451 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg, _pitch_offset_deg,
454 // Compute the eyepoints orientation and position
455 // wrt the earth centered frame - that is global coorinates
456 SGQuatd ec2body = hlOr*hlToBody;
457 _absolute_view_pos = position + ec2body.backTransform(_offset_m);
458 mViewOrientation = ec2body*viewOffsetOr;
462 FGViewer::recalcLookAt ()
464 // The geodetic position of our target to look at
465 SGGeod geodTargetPos;
466 SGQuatd geodTargetOr;
468 SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
469 double lat = placement->getLatitudeDeg();
470 double lon = placement->getLongitudeDeg();
471 double alt = placement->getElevationFt();
472 geodTargetPos = SGGeod::fromDegFt(lon, lat, alt);
474 double head = placement->getHeadingDeg();
475 double pitch = placement->getPitchDeg();
476 double roll = placement->getRollDeg();
477 geodTargetOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
479 dampEyeData(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
481 // if not model then calculate our own target position...
482 geodTargetPos = SGGeod::fromDegFt(_target_lon_deg,
485 geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
489 SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(geodTargetPos);
495 SGModelPlacement* placement = globals->get_aircraft_model()->get3DModel();
496 double lat = placement->getLatitudeDeg();
497 double lon = placement->getLongitudeDeg();
498 double alt = placement->getElevationFt();
499 geodEyePos = SGGeod::fromDegFt(lon, lat, alt);
501 double head = placement->getHeadingDeg();
502 double pitch = placement->getPitchDeg();
503 double roll = placement->getRollDeg();
504 geodEyeOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
506 dampEyeData(_roll_deg, _pitch_deg, _heading_deg);
508 // update from our own data, just the rotation here...
509 geodEyePos = SGGeod::fromDegFt(_lon_deg, _lat_deg, _alt_ft);
510 geodEyeOr = SGQuatd::fromYawPitchRollDeg(_heading_deg,
514 SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(geodEyePos);
516 // the rotation offset, don't know why heading is negative here ...
517 SGQuatd eyeOffsetOr =
518 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg + 180, _pitch_offset_deg,
521 // Offsets to the eye position
522 SGVec3d eyeOff(-_offset_m.z(), _offset_m.x(), -_offset_m.y());
523 SGQuatd ec2eye = geodEyeHlOr*geodEyeOr;
524 SGVec3d eyeCart = SGVec3d::fromGeod(geodEyePos);
525 eyeCart += (ec2eye*eyeOffsetOr).backTransform(eyeOff);
527 SGVec3d atCart = SGVec3d::fromGeod(geodTargetPos);
529 // add target offsets to at_position...
530 SGVec3d target_pos_off(-_target_offset_m.z(), _target_offset_m.x(),
531 -_target_offset_m.y());
532 target_pos_off = (geodTargetHlOr*geodTargetOr).backTransform(target_pos_off);
533 atCart += target_pos_off;
534 eyeCart += target_pos_off;
536 // Compute the eyepoints orientation and position
537 // wrt the earth centered frame - that is global coorinates
538 _absolute_view_pos = eyeCart;
540 // the view direction
541 SGVec3d dir = normalize(atCart - eyeCart);
543 SGVec3d up = ec2eye.backTransform(SGVec3d(0, 0, -1));
544 // rotate dir to the 0-th unit vector
545 // rotate up to 2-th unit vector
546 mViewOrientation = SGQuatd::fromRotateTo(-dir, 2, up, 1);
550 FGViewer::dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg)
552 const double interval = 0.01;
554 static FGViewer *last_view = 0;
555 if (last_view != this) {
557 _damped_roll_deg = roll_deg;
558 _damped_pitch_deg = pitch_deg;
559 _damped_heading_deg = heading_deg;
564 if (_damp_sync < interval) {
565 if (_damp_roll > 0.0)
566 roll_deg = _damped_roll_deg;
567 if (_damp_pitch > 0.0)
568 pitch_deg = _damped_pitch_deg;
569 if (_damp_heading > 0.0)
570 heading_deg = _damped_heading_deg;
574 while (_damp_sync >= interval) {
575 _damp_sync -= interval;
578 if (_damp_roll > 0.0) {
579 d = _damped_roll_deg - roll_deg;
581 _damped_roll_deg -= 360.0;
583 _damped_roll_deg += 360.0;
584 roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
587 if (_damp_pitch > 0.0) {
588 d = _damped_pitch_deg - pitch_deg;
590 _damped_pitch_deg -= 360.0;
592 _damped_pitch_deg += 360.0;
593 pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
596 if (_damp_heading > 0.0) {
597 d = _damped_heading_deg - heading_deg;
599 _damped_heading_deg -= 360.0;
601 _damped_heading_deg += 360.0;
602 heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
608 FGViewer::get_h_fov()
610 switch (_scaling_type) {
611 case FG_SCALING_WIDTH: // h_fov == fov
614 if (_aspect_ratio < 1.0) {
620 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
621 / (_aspect_ratio*_aspect_ratio_multiplier))
622 * SG_RADIANS_TO_DEGREES * 2;
633 FGViewer::get_v_fov()
635 switch (_scaling_type) {
636 case FG_SCALING_WIDTH: // h_fov == fov
638 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
639 * (_aspect_ratio*_aspect_ratio_multiplier))
640 * SG_RADIANS_TO_DEGREES * 2;
642 if (_aspect_ratio < 1.0) {
645 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
646 * (_aspect_ratio*_aspect_ratio_multiplier))
647 * SG_RADIANS_TO_DEGREES * 2;
659 FGViewer::update (double dt)
664 int dt_ms = int(dt * 1000);
665 for ( i = 0; i < dt_ms; i++ ) {
666 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
667 setHeadingOffset_deg( _goal_heading_offset_deg );
670 // move current_view.headingoffset towards
671 // current_view.goal_view_offset
672 if ( _goal_heading_offset_deg > _heading_offset_deg )
674 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
675 incHeadingOffset_deg( 0.5 );
677 incHeadingOffset_deg( -0.5 );
680 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
681 incHeadingOffset_deg( -0.5 );
683 incHeadingOffset_deg( 0.5 );
686 if ( _heading_offset_deg > 360 ) {
687 incHeadingOffset_deg( -360 );
688 } else if ( _heading_offset_deg < 0 ) {
689 incHeadingOffset_deg( 360 );
694 for ( i = 0; i < dt_ms; i++ ) {
695 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
696 setPitchOffset_deg( _goal_pitch_offset_deg );
699 // move current_view.pitch_offset_deg towards
700 // current_view.goal_pitch_offset
701 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
703 incPitchOffset_deg( 1.0 );
705 incPitchOffset_deg( -1.0 );
707 if ( _pitch_offset_deg > 90 ) {
708 setPitchOffset_deg(90);
709 } else if ( _pitch_offset_deg < -90 ) {
710 setPitchOffset_deg( -90 );
716 for ( i = 0; i < dt_ms; i++ ) {
717 if ( fabs( _goal_roll_offset_deg - _roll_offset_deg ) < 1 ) {
718 setRollOffset_deg( _goal_roll_offset_deg );
721 // move current_view.roll_offset_deg towards
722 // current_view.goal_roll_offset
723 if ( _goal_roll_offset_deg > _roll_offset_deg )
725 incRollOffset_deg( 1.0 );
727 incRollOffset_deg( -1.0 );
729 if ( _roll_offset_deg > 90 ) {
730 setRollOffset_deg(90);
731 } else if ( _roll_offset_deg < -90 ) {
732 setRollOffset_deg( -90 );
737 _cameraGroup->update(_absolute_view_pos.osg(), mViewOrientation.osg());
738 _cameraGroup->setCameraParameters(get_v_fov(), get_aspect_ratio());