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.
32 #include <simgear/compiler.h>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/constants.h>
37 #include <simgear/scene/model/placement.hxx>
38 #include <simgear/scene/util/OsgMath.hxx>
40 #include <Main/fg_props.hxx>
41 #include <Main/globals.hxx>
42 #include "CameraGroup.hxx"
44 using namespace flightgear;
46 ////////////////////////////////////////////////////////////////////////
47 // Implementation of FGViewer.
48 ////////////////////////////////////////////////////////////////////////
51 FGViewer::FGViewer( fgViewType Type, bool from_model, int from_model_index,
52 bool at_model, int at_model_index,
53 double damp_roll, double damp_pitch, double damp_heading,
54 double x_offset_m, double y_offset_m, double z_offset_m,
55 double heading_offset_deg, double pitch_offset_deg,
56 double roll_offset_deg,
57 double fov_deg, double aspect_ratio_multiplier,
58 double target_x_offset_m, double target_y_offset_m,
59 double target_z_offset_m, double near_m, bool internal ):
66 _target_heading_deg(0),
67 _scaling_type(FG_SCALING_MAX),
68 _cameraGroup(CameraGroup::getDefault())
70 _absolute_view_pos = SGVec3d(0, 0, 0);
72 _from_model = from_model;
73 _from_model_index = from_model_index;
75 _at_model_index = at_model_index;
79 _dampFactor = SGVec3d::zeros();
80 _dampOutput = SGVec3d::zeros();
81 _dampTarget = SGVec3d::zeros();
84 _dampFactor[0] = 1.0 / pow(10.0, fabs(damp_roll));
86 _dampFactor[1] = 1.0 / pow(10.0, fabs(damp_pitch));
87 if (damp_heading > 0.0)
88 _dampFactor[2] = 1.0 / pow(10.0, fabs(damp_heading));
90 _offset_m.x() = x_offset_m;
91 _offset_m.y() = y_offset_m;
92 _offset_m.z() = z_offset_m;
93 _heading_offset_deg = heading_offset_deg;
94 _pitch_offset_deg = pitch_offset_deg;
95 _roll_offset_deg = roll_offset_deg;
96 _goal_heading_offset_deg = heading_offset_deg;
97 _goal_pitch_offset_deg = pitch_offset_deg;
98 _goal_roll_offset_deg = roll_offset_deg;
105 _aspect_ratio_multiplier = aspect_ratio_multiplier;
106 _target_offset_m.x() = target_x_offset_m;
107 _target_offset_m.y() = target_y_offset_m;
108 _target_offset_m.z() = target_z_offset_m;
109 _ground_level_nearplane_m = near_m;
110 // a reasonable guess for init, so that the math doesn't blow up
115 FGViewer::~FGViewer( void ) {
134 FGViewer::setType ( int type )
143 FGViewer::setInternal ( bool internal )
145 _internal = internal;
149 FGViewer::setPosition (double lon_deg, double lat_deg, double alt_ft)
152 _position = SGGeod::fromDegFt(lon_deg, lat_deg, alt_ft);
156 FGViewer::setTargetPosition (double lon_deg, double lat_deg, double alt_ft)
159 _target = SGGeod::fromDegFt(lon_deg, lat_deg, alt_ft);
163 FGViewer::setRoll_deg (double roll_deg)
166 _roll_deg = roll_deg;
170 FGViewer::setPitch_deg (double pitch_deg)
173 _pitch_deg = pitch_deg;
177 FGViewer::setHeading_deg (double heading_deg)
180 _heading_deg = heading_deg;
184 FGViewer::setOrientation (double roll_deg, double pitch_deg, double heading_deg)
187 _roll_deg = roll_deg;
188 _pitch_deg = pitch_deg;
189 _heading_deg = heading_deg;
193 FGViewer::setTargetRoll_deg (double target_roll_deg)
196 _target_roll_deg = target_roll_deg;
200 FGViewer::setTargetPitch_deg (double target_pitch_deg)
203 _target_pitch_deg = target_pitch_deg;
207 FGViewer::setTargetHeading_deg (double target_heading_deg)
210 _target_heading_deg = target_heading_deg;
214 FGViewer::setTargetOrientation (double target_roll_deg, double target_pitch_deg, double target_heading_deg)
217 _target_roll_deg = target_roll_deg;
218 _target_pitch_deg = target_pitch_deg;
219 _target_heading_deg = target_heading_deg;
223 FGViewer::setXOffset_m (double x_offset_m)
226 _offset_m.x() = x_offset_m;
230 FGViewer::setYOffset_m (double y_offset_m)
233 _offset_m.y() = y_offset_m;
237 FGViewer::setZOffset_m (double z_offset_m)
240 _offset_m.z() = z_offset_m;
244 FGViewer::setTargetXOffset_m (double target_x_offset_m)
247 _target_offset_m.x() = target_x_offset_m;
251 FGViewer::setTargetYOffset_m (double target_y_offset_m)
254 _target_offset_m.y() = target_y_offset_m;
258 FGViewer::setTargetZOffset_m (double target_z_offset_m)
261 _target_offset_m.z() = target_z_offset_m;
265 FGViewer::setPositionOffsets (double x_offset_m, double y_offset_m, double z_offset_m)
268 _offset_m.x() = x_offset_m;
269 _offset_m.y() = y_offset_m;
270 _offset_m.z() = z_offset_m;
274 FGViewer::setRollOffset_deg (double roll_offset_deg)
277 _roll_offset_deg = roll_offset_deg;
281 FGViewer::setPitchOffset_deg (double pitch_offset_deg)
284 _pitch_offset_deg = pitch_offset_deg;
288 FGViewer::setHeadingOffset_deg (double heading_offset_deg)
291 if (_at_model && (_offset_m.x() == 0.0)&&(_offset_m.z() == 0.0))
293 /* avoid optical effects (e.g. rotating sky) when "looking at" with
294 * heading offsets x==z==0 (view heading cannot change). */
295 _heading_offset_deg = 0.0;
298 _heading_offset_deg = heading_offset_deg;
302 FGViewer::setGoalRollOffset_deg (double goal_roll_offset_deg)
305 _goal_roll_offset_deg = goal_roll_offset_deg;
309 FGViewer::setGoalPitchOffset_deg (double goal_pitch_offset_deg)
312 _goal_pitch_offset_deg = goal_pitch_offset_deg;
313 /* The angle is set to 1/1000th of a degree from the poles to avoid the
314 * singularity where the azimuthal angle becomes undefined, inducing optical
315 * artefacts. The arbitrary angle offset is visually unnoticeable while
316 * avoiding any possible floating point truncation artefacts. */
317 if ( _goal_pitch_offset_deg < -89.999 ) {
318 _goal_pitch_offset_deg = -89.999;
320 if ( _goal_pitch_offset_deg > 89.999 ) {
321 _goal_pitch_offset_deg = 89.999;
327 FGViewer::setGoalHeadingOffset_deg (double goal_heading_offset_deg)
330 if (_at_model && (_offset_m.x() == 0.0)&&(_offset_m.z() == 0.0))
332 /* avoid optical effects (e.g. rotating sky) when "looking at" with
333 * heading offsets x==z==0 (view heading cannot change). */
334 _goal_heading_offset_deg = 0.0;
338 _goal_heading_offset_deg = goal_heading_offset_deg;
339 while ( _goal_heading_offset_deg < 0.0 ) {
340 _goal_heading_offset_deg += 360;
342 while ( _goal_heading_offset_deg > 360 ) {
343 _goal_heading_offset_deg -= 360;
348 FGViewer::setOrientationOffsets (double roll_offset_deg, double pitch_offset_deg, double heading_offset_deg)
351 _roll_offset_deg = roll_offset_deg;
352 _pitch_offset_deg = pitch_offset_deg;
353 _heading_offset_deg = heading_offset_deg;
356 // recalc() is done every time one of the setters is called (making the
357 // cached data "dirty") on the next "get". It calculates all the outputs
362 if (_type == FG_LOOKFROM) {
371 // recalculate for LookFrom view type...
373 FGViewer::recalcLookFrom ()
375 // Update location data ...
377 _position = globals->get_aircraft_position();
378 globals->get_aircraft_orientation(_heading_deg, _pitch_deg, _roll_deg);
381 double head = _heading_deg;
382 double pitch = _pitch_deg;
383 double roll = _roll_deg;
384 if ( !_from_model ) {
385 // update from our own data...
386 setDampTarget(roll, pitch, head);
387 getDampOutput(roll, pitch, head);
390 // The rotation rotating from the earth centerd frame to
391 // the horizontal local frame
392 SGQuatd hlOr = SGQuatd::fromLonLat(_position);
394 // The rotation from the horizontal local frame to the basic view orientation
395 SGQuatd hlToBody = SGQuatd::fromYawPitchRollDeg(head, pitch, roll);
397 // The rotation offset, don't know why heading is negative here ...
399 = SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg, _pitch_offset_deg,
402 // Compute the eyepoints orientation and position
403 // wrt the earth centered frame - that is global coorinates
404 SGQuatd ec2body = hlOr*hlToBody;
406 // The cartesian position of the basic view coordinate
407 SGVec3d position = SGVec3d::fromGeod(_position);
409 // This is rotates the x-forward, y-right, z-down coordinate system the where
410 // simulation runs into the OpenGL camera system with x-right, y-up, z-back.
411 SGQuatd q(-0.5, -0.5, 0.5, 0.5);
413 _absolute_view_pos = position + (ec2body*q).backTransform(_offset_m);
414 mViewOrientation = ec2body*mViewOffsetOr*q;
418 FGViewer::recalcLookAt ()
420 // The geodetic position of our target to look at
422 _target = globals->get_aircraft_position();
423 globals->get_aircraft_orientation(_target_heading_deg,
427 // if not model then calculate our own target position...
428 setDampTarget(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
429 getDampOutput(_target_roll_deg, _target_pitch_deg, _target_heading_deg);
432 SGQuatd geodTargetOr = SGQuatd::fromYawPitchRollDeg(_target_heading_deg,
435 SGQuatd geodTargetHlOr = SGQuatd::fromLonLat(_target);
439 _position = globals->get_aircraft_position();
440 globals->get_aircraft_orientation(_heading_deg, _pitch_deg, _roll_deg);
442 // update from our own data, just the rotation here...
443 setDampTarget(_roll_deg, _pitch_deg, _heading_deg);
444 getDampOutput(_roll_deg, _pitch_deg, _heading_deg);
446 SGQuatd geodEyeOr = SGQuatd::fromYawPitchRollDeg(_heading_deg, _pitch_deg, _roll_deg);
447 SGQuatd geodEyeHlOr = SGQuatd::fromLonLat(_position);
449 // the rotation offset, don't know why heading is negative here ...
451 SGQuatd::fromYawPitchRollDeg(-_heading_offset_deg + 180, _pitch_offset_deg,
454 // Offsets to the eye position
455 SGVec3d eyeOff(-_offset_m.z(), _offset_m.x(), -_offset_m.y());
456 SGQuatd ec2eye = geodEyeHlOr*geodEyeOr;
457 SGVec3d eyeCart = SGVec3d::fromGeod(_position);
458 eyeCart += (ec2eye*mViewOffsetOr).backTransform(eyeOff);
460 SGVec3d atCart = SGVec3d::fromGeod(_target);
462 // add target offsets to at_position...
463 SGVec3d target_pos_off(-_target_offset_m.z(), _target_offset_m.x(),
464 -_target_offset_m.y());
465 target_pos_off = (geodTargetHlOr*geodTargetOr).backTransform(target_pos_off);
466 atCart += target_pos_off;
467 eyeCart += target_pos_off;
469 // Compute the eyepoints orientation and position
470 // wrt the earth centered frame - that is global coorinates
471 _absolute_view_pos = eyeCart;
473 // the view direction
474 SGVec3d dir = normalize(atCart - eyeCart);
476 SGVec3d up = ec2eye.backTransform(SGVec3d(0, 0, -1));
477 // rotate -dir to the 2-th unit vector
478 // rotate up to 1-th unit vector
479 // Note that this matches the OpenGL camera coordinate system
480 // with x-right, y-up, z-back.
481 mViewOrientation = SGQuatd::fromRotateTo(-dir, 2, up, 1);
485 FGViewer::setDampTarget(double roll, double pitch, double heading)
487 _dampTarget = SGVec3d(roll, pitch, heading);
491 FGViewer::getDampOutput(double& roll, double& pitch, double& heading)
493 roll = _dampOutput[0];
494 pitch = _dampOutput[1];
495 heading = _dampOutput[2];
500 FGViewer::updateDampOutput(double dt)
502 static FGViewer *last_view = 0;
503 if ((last_view != this) || (dt > 1.0)) {
504 _dampOutput = _dampTarget;
509 const double interval = 0.01;
510 while (dt > interval) {
512 for (unsigned int i=0; i<3; ++i) {
513 if (_dampFactor[i] <= 0.0) {
514 // axis is un-damped, set output to target directly
515 _dampOutput[i] = _dampTarget[i];
519 double d = _dampOutput[i] - _dampTarget[i];
521 _dampOutput[i] -= 360.0;
522 } else if (d < -180.0) {
523 _dampOutput[i] += 360.0;
526 _dampOutput[i] = (_dampTarget[i] * _dampFactor[i]) +
527 (_dampOutput[i] * (1.0 - _dampFactor[i]));
528 } // of axis iteration
531 } // of dt subdivision by interval
535 FGViewer::get_h_fov()
537 double aspectRatio = _cameraGroup->getMasterAspectRatio();
538 switch (_scaling_type) {
539 case FG_SCALING_WIDTH: // h_fov == fov
542 if (aspectRatio < 1.0) {
548 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
549 / (aspectRatio*_aspect_ratio_multiplier))
550 * SG_RADIANS_TO_DEGREES * 2;
561 FGViewer::get_v_fov()
563 double aspectRatio = _cameraGroup->getMasterAspectRatio();
564 switch (_scaling_type) {
565 case FG_SCALING_WIDTH: // h_fov == fov
567 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
568 * (aspectRatio*_aspect_ratio_multiplier))
569 * SG_RADIANS_TO_DEGREES * 2;
571 if (aspectRatio < 1.0) {
574 atan(tan(_fov_deg/2 * SG_DEGREES_TO_RADIANS)
575 * (aspectRatio*_aspect_ratio_multiplier))
576 * SG_RADIANS_TO_DEGREES * 2;
588 FGViewer::update (double dt)
590 updateDampOutput(dt);
593 int dt_ms = int(dt * 1000);
594 for ( i = 0; i < dt_ms; i++ ) {
595 if ( fabs( _goal_heading_offset_deg - _heading_offset_deg) < 1 ) {
596 setHeadingOffset_deg( _goal_heading_offset_deg );
599 // move current_view.headingoffset towards
600 // current_view.goal_view_offset
601 if ( _goal_heading_offset_deg > _heading_offset_deg )
603 if ( _goal_heading_offset_deg - _heading_offset_deg < 180 ){
604 incHeadingOffset_deg( 0.5 );
606 incHeadingOffset_deg( -0.5 );
609 if ( _heading_offset_deg - _goal_heading_offset_deg < 180 ){
610 incHeadingOffset_deg( -0.5 );
612 incHeadingOffset_deg( 0.5 );
615 if ( _heading_offset_deg > 360 ) {
616 incHeadingOffset_deg( -360 );
617 } else if ( _heading_offset_deg < 0 ) {
618 incHeadingOffset_deg( 360 );
623 for ( i = 0; i < dt_ms; i++ ) {
624 if ( fabs( _goal_pitch_offset_deg - _pitch_offset_deg ) < 1 ) {
625 setPitchOffset_deg( _goal_pitch_offset_deg );
628 // move current_view.pitch_offset_deg towards
629 // current_view.goal_pitch_offset
630 if ( _goal_pitch_offset_deg > _pitch_offset_deg )
632 incPitchOffset_deg( 1.0 );
634 incPitchOffset_deg( -1.0 );
636 if ( _pitch_offset_deg > 90 ) {
637 setPitchOffset_deg(90);
638 } else if ( _pitch_offset_deg < -90 ) {
639 setPitchOffset_deg( -90 );
645 for ( i = 0; i < dt_ms; i++ ) {
646 if ( fabs( _goal_roll_offset_deg - _roll_offset_deg ) < 1 ) {
647 setRollOffset_deg( _goal_roll_offset_deg );
650 // move current_view.roll_offset_deg towards
651 // current_view.goal_roll_offset
652 if ( _goal_roll_offset_deg > _roll_offset_deg )
654 incRollOffset_deg( 1.0 );
656 incRollOffset_deg( -1.0 );
658 if ( _roll_offset_deg > 90 ) {
659 setRollOffset_deg(90);
660 } else if ( _roll_offset_deg < -90 ) {
661 setRollOffset_deg( -90 );
667 _cameraGroup->update(toOsg(_absolute_view_pos), toOsg(mViewOrientation));
668 _cameraGroup->setCameraParameters(get_v_fov(), get_aspect_ratio());
671 double FGViewer::get_aspect_ratio() const
673 return _cameraGroup->getMasterAspectRatio();