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/location.hxx>
43 #include <simgear/scene/model/placement.hxx>
44 #include <simgear/math/vector.hxx>
46 #include <Main/globals.hxx>
47 #include <Scenery/scenery.hxx>
48 #include <Model/acmodel.hxx>
52 #include "CameraGroup.hxx"
54 using namespace flightgear;
56 ////////////////////////////////////////////////////////////////////////
57 // Implementation of FGViewer.
58 ////////////////////////////////////////////////////////////////////////
61 FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index,
62 bool at_model, int at_model_index,
63 double damp_roll, double damp_pitch, double damp_heading,
64 double x_offset_m, double y_offset_m, double z_offset_m,
65 double heading_offset_deg, double pitch_offset_deg,
66 double roll_offset_deg,
67 double fov_deg, double aspect_ratio_multiplier,
68 double target_x_offset_m, double target_y_offset_m,
69 double target_z_offset_m, double near_m, bool internal ):
84 _scaling_type(FG_SCALING_MAX),
87 _cameraGroup(CameraGroup::getDefault())
89 _absolute_view_pos = SGVec3d(0, 0, 0);
91 _from_model = from_model;
92 _from_model_index = from_model_index;
94 _at_model_index = at_model_index;
99 _damp_roll = 1.0 / pow(10.0, fabs(damp_roll));
100 if (damp_pitch > 0.0)
101 _damp_pitch = 1.0 / pow(10.0, fabs(damp_pitch));
102 if (damp_heading > 0.0)
103 _damp_heading = 1.0 / pow(10.0, fabs(damp_heading));
105 _offset_m.x() = x_offset_m;
106 _offset_m.y() = y_offset_m;
107 _offset_m.z() = z_offset_m;
108 _heading_offset_deg = heading_offset_deg;
109 _pitch_offset_deg = pitch_offset_deg;
110 _roll_offset_deg = roll_offset_deg;
111 _goal_heading_offset_deg = heading_offset_deg;
112 _goal_pitch_offset_deg = pitch_offset_deg;
113 _goal_roll_offset_deg = roll_offset_deg;
119 _aspect_ratio_multiplier = aspect_ratio_multiplier;
120 _target_offset_m.x() = target_x_offset_m;
121 _target_offset_m.y() = target_y_offset_m;
122 _target_offset_m.z() = target_z_offset_m;
123 _ground_level_nearplane_m = near_m;
124 // a reasonable guess for init, so that the math doesn't blow up
129 FGViewer::~FGViewer( void ) {
136 _location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
138 _location = new SGLocation;
140 if ( _type == FG_LOOKAT ) {
142 _target_location = (SGLocation *) globals->get_aircraft_model()->get3DModel()->getSGLocation();
144 _target_location = (SGLocation *) new SGLocation;
159 FGViewer::setType ( int type )
168 FGViewer::setInternal ( bool internal )
170 _internal = internal;
174 FGViewer::setLongitude_deg (double lon_deg)
181 FGViewer::setLatitude_deg (double lat_deg)
188 FGViewer::setAltitude_ft (double alt_ft)
195 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
204 FGViewer::setTargetLongitude_deg (double lon_deg)
207 _target_lon_deg = lon_deg;
211 FGViewer::setTargetLatitude_deg (double lat_deg)
214 _target_lat_deg = lat_deg;
218 FGViewer::setTargetAltitude_ft (double alt_ft)
221 _target_alt_ft = alt_ft;
225 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
228 _target_lon_deg = lon_deg;
229 _target_lat_deg = lat_deg;
230 _target_alt_ft = alt_ft;
234 FGViewer::setRoll_deg (double roll_deg)
237 _roll_deg = roll_deg;
241 FGViewer::setPitch_deg (double pitch_deg)
244 _pitch_deg = pitch_deg;
248 FGViewer::setHeading_deg (double heading_deg)
251 _heading_deg = heading_deg;
255 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
258 _roll_deg = roll_deg;
259 _pitch_deg = pitch_deg;
260 _heading_deg = heading_deg;
264 FGViewer::setTargetRoll_deg (double target_roll_deg)
267 _target_roll_deg = target_roll_deg;
271 FGViewer::setTargetPitch_deg (double target_pitch_deg)
274 _target_pitch_deg = target_pitch_deg;
278 FGViewer::setTargetHeading_deg (double target_heading_deg)
281 _target_heading_deg = target_heading_deg;
285 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
288 _target_roll_deg = target_roll_deg;
289 _target_pitch_deg = target_pitch_deg;
290 _target_heading_deg = target_heading_deg;
294 FGViewer::setXOffset_m (double x_offset_m)
297 _offset_m.x() = x_offset_m;
301 FGViewer::setYOffset_m (double y_offset_m)
304 _offset_m.y() = y_offset_m;
308 FGViewer::setZOffset_m (double z_offset_m)
311 _offset_m.z() = z_offset_m;
315 FGViewer::setTargetXOffset_m (double target_x_offset_m)
318 _target_offset_m.x() = target_x_offset_m;
322 FGViewer::setTargetYOffset_m (double target_y_offset_m)
325 _target_offset_m.y() = target_y_offset_m;
329 FGViewer::setTargetZOffset_m (double target_z_offset_m)
332 _target_offset_m.z() = target_z_offset_m;
336 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
339 _offset_m.x() = x_offset_m;
340 _offset_m.y() = y_offset_m;
341 _offset_m.z() = z_offset_m;
345 FGViewer::setRollOffset_deg (double roll_offset_deg)
348 _roll_offset_deg = roll_offset_deg;
352 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
355 _pitch_offset_deg = pitch_offset_deg;
359 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
362 _heading_offset_deg = heading_offset_deg;
366 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
369 _goal_roll_offset_deg = goal_roll_offset_deg;
373 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
376 _goal_pitch_offset_deg = goal_pitch_offset_deg;
377 if ( _goal_pitch_offset_deg < -90 ) {
378 _goal_pitch_offset_deg = -90.0;
380 if ( _goal_pitch_offset_deg > 90.0 ) {
381 _goal_pitch_offset_deg = 90.0;
387 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
390 _goal_heading_offset_deg = goal_heading_offset_deg;
391 while ( _goal_heading_offset_deg < 0.0 ) {
392 _goal_heading_offset_deg += 360;
394 while ( _goal_heading_offset_deg > 360 ) {
395 _goal_heading_offset_deg -= 360;
400 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
403 _roll_offset_deg = roll_offset_deg;
404 _pitch_offset_deg = pitch_offset_deg;
405 _heading_offset_deg = heading_offset_deg;
408 // recalc() is done every time one of the setters is called (making the
409 // cached data "dirty") on the next "get". It calculates all the outputs
414 if (_type == FG_LOOKFROM) {
420 SGGeod geodEyePoint = SGGeod::fromCart(_absolute_view_pos);
421 geodEyePoint.setElevationM(0);
422 _zero_elev = SGVec3d::fromGeod(geodEyePoint);
424 SGQuatd hlOr = SGQuatd::fromLonLat(geodEyePoint);
425 _surface_south = toVec3f(hlOr.backTransform(-SGVec3d::e1()));
426 _surface_east = toVec3f(hlOr.backTransform(SGVec3d::e2()));
427 _world_up = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
429 // Update viewer's postion data for the eye location...
430 _lon_deg = _location->getLongitude_deg();
431 _lat_deg = _location->getLatitude_deg();
432 _alt_ft = _location->getAltitudeASL_ft();
433 _roll_deg = _location->getRoll_deg();
434 _pitch_deg = _location->getPitch_deg();
435 _heading_deg = _location->getHeading_deg();
437 // Update viewer's postion data for the target (at object) location
438 if (_type == FG_LOOKAT) {
439 _target_lon_deg = _target_location->getLongitude_deg();
440 _target_lat_deg = _target_location->getLatitude_deg();
441 _target_alt_ft = _target_location->getAltitudeASL_ft();
442 _target_roll_deg = _target_location->getRoll_deg();
443 _target_pitch_deg = _target_location->getPitch_deg();
444 _target_heading_deg = _target_location->getHeading_deg();
450 // recalculate for LookFrom view type...
452 FGViewer::recalcLookFrom ()
454 // Update location data ...
455 if ( !_from_model ) {
456 _location->setPosition( _lon_deg, _lat_deg, _alt_ft );
457 _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
458 _location->getTransformMatrix();
460 double lat = _location->getLatitude_deg();
461 double lon = _location->getLongitude_deg();
462 double alt = _location->getAltitudeASL_ft();
463 double head = _location->getHeading_deg();
464 double pitch = _location->getPitch_deg();
465 double roll = _location->getRoll_deg();
466 if ( !_from_model ) {
467 // update from our own data...
468 dampEyeData(roll, pitch, head);
471 // The geodetic position of our base view position
472 SGGeod geodPos = SGGeod::fromDegFt(lon, lat, alt);
473 // The rotation rotating from the earth centerd frame to
474 // the horizontal local OpenGL frame
475 SGQuatd hlOr = SGQuatd::viewHL(geodPos);
477 // the rotation from the horizontal local frame to the basic view orientation
478 SGQuatd hlToBody = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
479 hlToBody = SGQuatd::simToView(hlToBody);
481 // The cartesian position of the basic view coordinate
482 SGVec3d position = SGVec3d::fromGeod(geodPos);
483 // the rotation offset, don't know why heading is negative here ...
484 SGQuatd viewOffsetOr = SGQuatd::simToView(
485 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg, _pitch_offset_deg,
488 // Compute the eyepoints orientation and position
489 // wrt the earth centered frame - that is global coorinates
490 SGQuatd ec2body = hlOr*hlToBody;
491 _absolute_view_pos = position + ec2body.backTransform(_offset_m);
492 mViewOrientation = ec2body*viewOffsetOr;
496 FGViewer::recalcLookAt ()
498 // The geodetic position of our target to look at
499 SGGeod geodTargetPos;
500 SGQuatd geodTargetOr;
502 geodTargetPos = SGGeod::fromDegFt(_target_location->getLongitude_deg(),
503 _target_location->getLatitude_deg(),
504 _target_location->getAltitudeASL_ft());
505 double head = _target_location->getHeading_deg();
506 double pitch = _target_location->getPitch_deg();
507 double roll = _target_location->getRoll_deg();
508 geodTargetOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
510 dampEyeData(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
511 _target_location->setPosition( _target_lon_deg, _target_lat_deg, _target_alt_ft );
512 _target_location->setOrientation( _target_roll_deg, _target_pitch_deg, _target_heading_deg );
513 _target_location->getTransformMatrix();
515 // if not model then calculate our own target position...
516 geodTargetPos = SGGeod::fromDegFt(_target_lon_deg,
519 geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
523 SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(geodTargetPos);
529 geodEyePos = SGGeod::fromDegFt(_location->getLongitude_deg(),
530 _location->getLatitude_deg(),
531 _location->getAltitudeASL_ft());
532 double head = _location->getHeading_deg();
533 double pitch = _location->getPitch_deg();
534 double roll = _location->getRoll_deg();
535 geodEyeOr = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
537 dampEyeData(_roll_deg, _pitch_deg, _heading_deg);
538 _location->setPosition( _lon_deg, _lat_deg, _alt_ft );
539 _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
540 _location->getTransformMatrix();
542 // update from our own data, just the rotation here...
543 geodEyePos = SGGeod::fromDegFt(_lon_deg, _lat_deg, _alt_ft);
544 geodEyeOr = SGQuatd::fromYawPitchRollDeg(_heading_deg,
548 SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(geodEyePos);
550 // the rotation offset, don't know why heading is negative here ...
551 SGQuatd eyeOffsetOr =
552 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg + 180, _pitch_offset_deg,
555 // Offsets to the eye position
556 SGVec3d eyeOff(-_offset_m.z(), _offset_m.x(), -_offset_m.y());
557 SGQuatd ec2eye = geodEyeHlOr*geodEyeOr;
558 SGVec3d eyeCart = SGVec3d::fromGeod(geodEyePos);
559 eyeCart += (ec2eye*eyeOffsetOr).backTransform(eyeOff);
561 SGVec3d atCart = SGVec3d::fromGeod(geodTargetPos);
563 // add target offsets to at_position...
564 SGVec3d target_pos_off(-_target_offset_m.z(), _target_offset_m.x(),
565 -_target_offset_m.y());
566 target_pos_off = (geodTargetHlOr*geodTargetOr).backTransform(target_pos_off);
567 atCart += target_pos_off;
568 eyeCart += target_pos_off;
570 // Compute the eyepoints orientation and position
571 // wrt the earth centered frame - that is global coorinates
572 _absolute_view_pos = eyeCart;
574 // the view direction
575 SGVec3d dir = normalize(atCart - eyeCart);
577 SGVec3d up = ec2eye.backTransform(SGVec3d(0, 0, -1));
578 // rotate dir to the 0-th unit vector
579 // rotate up to 2-th unit vector
580 mViewOrientation = SGQuatd::fromRotateTo(-dir, 2, up, 1);
584 FGViewer::dampEyeData(double &roll_deg, double &pitch_deg, double &heading_deg)
586 const double interval = 0.01;
588 static FGViewer *last_view = 0;
589 if (last_view != this) {
591 _damped_roll_deg = roll_deg;
592 _damped_pitch_deg = pitch_deg;
593 _damped_heading_deg = heading_deg;
598 if (_damp_sync < interval) {
599 if (_damp_roll > 0.0)
600 roll_deg = _damped_roll_deg;
601 if (_damp_pitch > 0.0)
602 pitch_deg = _damped_pitch_deg;
603 if (_damp_heading > 0.0)
604 heading_deg = _damped_heading_deg;
608 while (_damp_sync >= interval) {
609 _damp_sync -= interval;
612 if (_damp_roll > 0.0) {
613 d = _damped_roll_deg - roll_deg;
615 _damped_roll_deg -= 360.0;
617 _damped_roll_deg += 360.0;
618 roll_deg = _damped_roll_deg = roll_deg * _damp_roll + _damped_roll_deg * (1 - _damp_roll);
621 if (_damp_pitch > 0.0) {
622 d = _damped_pitch_deg - pitch_deg;
624 _damped_pitch_deg -= 360.0;
626 _damped_pitch_deg += 360.0;
627 pitch_deg = _damped_pitch_deg = pitch_deg * _damp_pitch + _damped_pitch_deg * (1 - _damp_pitch);
630 if (_damp_heading > 0.0) {
631 d = _damped_heading_deg - heading_deg;
633 _damped_heading_deg -= 360.0;
635 _damped_heading_deg += 360.0;
636 heading_deg = _damped_heading_deg = heading_deg * _damp_heading + _damped_heading_deg * (1 - _damp_heading);
642 FGViewer::get_h_fov()
644 switch (_scaling_type) {
645 case FG_SCALING_WIDTH: // h_fov == fov
648 if (_aspect_ratio < 1.0) {
654 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
655 / (_aspect_ratio*_aspect_ratio_multiplier))
656 * SG_RADIANS_TO_DEGREES * 2;
667 FGViewer::get_v_fov()
669 switch (_scaling_type) {
670 case FG_SCALING_WIDTH: // h_fov == fov
672 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
673 * (_aspect_ratio*_aspect_ratio_multiplier))
674 * SG_RADIANS_TO_DEGREES * 2;
676 if (_aspect_ratio < 1.0) {
679 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
680 * (_aspect_ratio*_aspect_ratio_multiplier))
681 * SG_RADIANS_TO_DEGREES * 2;
693 FGViewer::update (double dt)
698 int dt_ms = int(dt * 1000);
699 for ( i = 0; i < dt_ms; i++ ) {
700 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
701 setHeadingOffset_deg( _goal_heading_offset_deg );
704 // move current_view.headingoffset towards
705 // current_view.goal_view_offset
706 if ( _goal_heading_offset_deg > _heading_offset_deg )
708 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
709 incHeadingOffset_deg( 0.5 );
711 incHeadingOffset_deg( -0.5 );
714 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
715 incHeadingOffset_deg( -0.5 );
717 incHeadingOffset_deg( 0.5 );
720 if ( _heading_offset_deg > 360 ) {
721 incHeadingOffset_deg( -360 );
722 } else if ( _heading_offset_deg < 0 ) {
723 incHeadingOffset_deg( 360 );
728 for ( i = 0; i < dt_ms; i++ ) {
729 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
730 setPitchOffset_deg( _goal_pitch_offset_deg );
733 // move current_view.pitch_offset_deg towards
734 // current_view.goal_pitch_offset
735 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
737 incPitchOffset_deg( 1.0 );
739 incPitchOffset_deg( -1.0 );
741 if ( _pitch_offset_deg > 90 ) {
742 setPitchOffset_deg(90);
743 } else if ( _pitch_offset_deg < -90 ) {
744 setPitchOffset_deg( -90 );
750 for ( i = 0; i < dt_ms; i++ ) {
751 if ( fabs( _goal_roll_offset_deg - _roll_offset_deg ) < 1 ) {
752 setRollOffset_deg( _goal_roll_offset_deg );
755 // move current_view.roll_offset_deg towards
756 // current_view.goal_roll_offset
757 if ( _goal_roll_offset_deg > _roll_offset_deg )
759 incRollOffset_deg( 1.0 );
761 incRollOffset_deg( -1.0 );
763 if ( _roll_offset_deg > 90 ) {
764 setRollOffset_deg(90);
765 } else if ( _roll_offset_deg < -90 ) {
766 setRollOffset_deg( -90 );
771 _cameraGroup->update(_absolute_view_pos.osg(), mViewOrientation.osg());
772 _cameraGroup->setCameraParameters(get_v_fov(), get_aspect_ratio());