| 1 | // view.hxx -- class for managing a view in the flightgear world. |
| 2 | // |
| 3 | // Written by Curtis Olson, started August 1997. |
| 4 | // overhaul started October 2000. |
| 5 | // partially rewritten by Jim Wilson [email protected] using interface |
| 6 | // by David Megginson March 2002 |
| 7 | // |
| 8 | // Copyright (C) 1997 - 2000 Curtis L. Olson - http://www.flightgear.org/~curt |
| 9 | // |
| 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. |
| 14 | // |
| 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. |
| 19 | // |
| 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. |
| 23 | // |
| 24 | // $Id$ |
| 25 | |
| 26 | |
| 27 | #ifndef _VIEWER_HXX |
| 28 | #define _VIEWER_HXX |
| 29 | |
| 30 | |
| 31 | #include <simgear/compiler.h> |
| 32 | #include <simgear/constants.h> |
| 33 | |
| 34 | #include <simgear/props/props.hxx> |
| 35 | #include <simgear/props/tiedpropertylist.hxx> |
| 36 | #include <simgear/structure/subsystem_mgr.hxx> |
| 37 | #include <simgear/math/SGMath.hxx> |
| 38 | |
| 39 | #define FG_FOV_MIN 0.1 |
| 40 | #define FG_FOV_MAX 179.9 |
| 41 | |
| 42 | namespace flightgear |
| 43 | { |
| 44 | |
| 45 | // Define a structure containing view information |
| 46 | class View : public SGSubsystem |
| 47 | { |
| 48 | public: |
| 49 | enum ScalingType { // nominal Field Of View actually applies to ... |
| 50 | FG_SCALING_WIDTH, // window width |
| 51 | FG_SCALING_MAX // max(width, height) |
| 52 | // FG_SCALING_G_MEAN, // geometric_mean(width, height) |
| 53 | // FG_SCALING_INDEPENDENT // whole screen |
| 54 | }; |
| 55 | |
| 56 | enum ViewType { |
| 57 | FG_LOOKFROM = 0, |
| 58 | FG_LOOKAT = 1 |
| 59 | }; |
| 60 | |
| 61 | // view_index is to allow us to look up corresponding view information for |
| 62 | // multiplayer aircraft. |
| 63 | static View* createFromProperties(SGPropertyNode_ptr props, int view_index=-1); |
| 64 | |
| 65 | // Destructor |
| 66 | virtual ~View(); |
| 67 | |
| 68 | ////////////////////////////////////////////////////////////////////// |
| 69 | // Part 1: standard SGSubsystem implementation. |
| 70 | ////////////////////////////////////////////////////////////////////// |
| 71 | |
| 72 | void init() override; |
| 73 | void bind() override; |
| 74 | void unbind() override; |
| 75 | void update(double dt) override; |
| 76 | |
| 77 | // Subsystem identification. |
| 78 | static const char* staticSubsystemClassId() { return "view"; } |
| 79 | |
| 80 | ////////////////////////////////////////////////////////////////////// |
| 81 | // Part 2: user settings. |
| 82 | ////////////////////////////////////////////////////////////////////// |
| 83 | |
| 84 | void resetOffsetsAndFOV(); |
| 85 | |
| 86 | ViewType getType() const { return _type; } |
| 87 | void setType( int type ); |
| 88 | |
| 89 | bool getInternal() const { return _internal; } |
| 90 | void setInternal( bool internal ); |
| 91 | |
| 92 | const std::string& getName() const { return _name; } |
| 93 | |
| 94 | // Reference geodetic position of view from position... |
| 95 | // These are the actual aircraft position (pilot in |
| 96 | // pilot view, model in model view). |
| 97 | // FIXME: the model view position (ie target positions) |
| 98 | // should be in the model class. |
| 99 | |
| 100 | |
| 101 | const SGGeod& getPosition() const { return _position; } |
| 102 | |
| 103 | // Reference geodetic target position... |
| 104 | const SGGeod& getTargetPosition() const { return _target; } |
| 105 | |
| 106 | |
| 107 | |
| 108 | // Position offsets from reference |
| 109 | // These offsets position they "eye" in the scene according to a given |
| 110 | // location. For example in pilot view they are used to position the |
| 111 | // head inside the aircraft. |
| 112 | // Note that in pilot view these are applied "before" the orientation |
| 113 | // rotations (see below) so that the orientation rotations have the |
| 114 | // effect of the pilot staying in his seat and "looking out" in |
| 115 | // different directions. |
| 116 | // In chase view these are applied "after" the application of the |
| 117 | // orientation rotations listed below. This has the effect of the |
| 118 | // eye moving around and "looking at" the object (model) from |
| 119 | // different angles. |
| 120 | SGVec3d getOffset_m () const { return _offset_m; } |
| 121 | double getXOffset_m () const { return _offset_m.x(); } |
| 122 | double getYOffset_m () const { return _offset_m.y(); } |
| 123 | double getZOffset_m () const { return _offset_m.z(); } |
| 124 | double getTargetXOffset_m () const { return _target_offset_m.x(); } |
| 125 | double getTargetYOffset_m () const { return _target_offset_m.y(); } |
| 126 | double getTargetZOffset_m () const { return _target_offset_m.z(); } |
| 127 | void setXOffset_m (double x_offset_m); |
| 128 | void setYOffset_m (double y_offset_m); |
| 129 | void setZOffset_m (double z_offset_m); |
| 130 | void setTargetXOffset_m (double x_offset_m); |
| 131 | void setTargetYOffset_m (double y_offset_m); |
| 132 | void setTargetZOffset_m (double z_offset_m); |
| 133 | void setPositionOffsets (double x_offset_m, |
| 134 | double y_offset_m, |
| 135 | double z_offset_m); |
| 136 | double getAdjustXOffset_m () const { return _adjust_offset_m.x(); } |
| 137 | double getAdjustYOffset_m () const { return _adjust_offset_m.y(); } |
| 138 | double getAdjustZOffset_m () const { return _adjust_offset_m.z(); } |
| 139 | void setAdjustXOffset_m (double x_adjust_offset_m); |
| 140 | void setAdjustYOffset_m (double y_adjust_offset_m); |
| 141 | void setAdjustZOffset_m (double z_adjust_offset_m); |
| 142 | |
| 143 | // Reference orientation rotations... |
| 144 | // These are rotations that represent the plane attitude effect on |
| 145 | // the view (in Pilot view). IE The view frustrum rotates as the plane |
| 146 | // turns, pitches, and rolls. |
| 147 | // In model view (lookat/chaseview) these end up changing the angle that |
| 148 | // the eye is looking at the ojbect (ie the model). |
| 149 | // FIXME: the FGModel class should have its own version of these so that |
| 150 | // it can generate it's own model rotations. |
| 151 | double getHeading_deg () const {return _heading_deg; } |
| 152 | |
| 153 | |
| 154 | // Orientation offsets rotations from reference orientation. |
| 155 | // Goal settings are for smooth transition from prior |
| 156 | // offset when changing view direction. |
| 157 | // These offsets are in ADDITION to the orientation rotations listed |
| 158 | // above. |
| 159 | // In pilot view they are applied after the position offsets in order to |
| 160 | // give the effect of the pilot looking around. |
| 161 | // In lookat view they are applied before the position offsets so that |
| 162 | // the effect is the eye moving around looking at the object (ie the model) |
| 163 | // from different angles. |
| 164 | double getRollOffset_deg () const { return _roll_offset_deg; } |
| 165 | double getPitchOffset_deg () const { return _pitch_offset_deg; } |
| 166 | double getHeadingOffset_deg () const { return _heading_offset_deg; } |
| 167 | |
| 168 | void setGoalHeadingOffset_deg (double goal_heading_offset_deg); |
| 169 | void setHeadingOffset_deg (double heading_offset_deg); |
| 170 | |
| 171 | ////////////////////////////////////////////////////////////////////// |
| 172 | // Part 3: output vectors and matrices in FlightGear coordinates. |
| 173 | ////////////////////////////////////////////////////////////////////// |
| 174 | |
| 175 | // Vectors and positions... |
| 176 | |
| 177 | const SGVec3d& getViewPosition() { if ( _dirty ) { recalc(); } return _absolute_view_pos; } |
| 178 | const SGQuatd& getViewOrientation() { if ( _dirty ) { recalc(); } return mViewOrientation; } |
| 179 | const SGQuatd& getViewOrientationOffset() { if ( _dirty ) { recalc(); } return mViewOffsetOr; } |
| 180 | |
| 181 | ////////////////////////////////////////////////////////////////////// |
| 182 | // Part 4: View and frustrum data setters and getters |
| 183 | ////////////////////////////////////////////////////////////////////// |
| 184 | |
| 185 | double get_fov() const { return _fov_deg; } |
| 186 | double get_h_fov(); // Get horizontal fov, in degrees. |
| 187 | double get_v_fov(); // Get vertical fov, in degrees. |
| 188 | |
| 189 | // this is currently just a wrapper for the default CameraGroups' aspect |
| 190 | double get_aspect_ratio() const; |
| 191 | |
| 192 | ////////////////////////////////////////////////////////////////////// |
| 193 | // Part 5: misc setters and getters |
| 194 | ////////////////////////////////////////////////////////////////////// |
| 195 | |
| 196 | void set_dirty() { _dirty = true; } |
| 197 | |
| 198 | private: |
| 199 | // Constructor |
| 200 | View( ViewType Type, bool from_model, int from_model_index, |
| 201 | bool at_model, int at_model_index, |
| 202 | double damp_roll, double damp_pitch, double damp_heading, |
| 203 | double x_offset_m, double y_offset_m, double z_offset_m, |
| 204 | double heading_offset_deg, double pitch_offset_deg, |
| 205 | double roll_offset_deg, |
| 206 | double fov_deg, double aspect_ratio_multiplier, |
| 207 | double target_x_offset_m, double target_y_offset_m, |
| 208 | double target_z_offset_m, double near_m, bool internal, |
| 209 | bool lookat_agl, double lookat_agl_damping, int view_index ); |
| 210 | |
| 211 | void set_clean() { _dirty = false; } |
| 212 | |
| 213 | void setHeadingOffset_deg_property (double heading_offset_deg); |
| 214 | void setPitchOffset_deg_property(double pitch_offset_deg); |
| 215 | void setRollOffset_deg_property(double roll_offset_deg); |
| 216 | |
| 217 | void setPosition (const SGGeod& geod); |
| 218 | void setTargetPosition (const SGGeod& geod); |
| 219 | |
| 220 | double getAbsolutePosition_x() const; |
| 221 | double getAbsolutePosition_y() const; |
| 222 | double getAbsolutePosition_z() const; |
| 223 | |
| 224 | double getRawOrientation_w() const; |
| 225 | double getRawOrientation_x() const; |
| 226 | double getRawOrientation_y() const; |
| 227 | double getRawOrientation_z() const; |
| 228 | |
| 229 | // quaternion accessors, for debugging: |
| 230 | double getFrame_w() const; |
| 231 | double getFrame_x() const; |
| 232 | double getFrame_y() const; |
| 233 | double getFrame_z() const; |
| 234 | |
| 235 | double getOrientation_w() const; |
| 236 | double getOrientation_x() const; |
| 237 | double getOrientation_y() const; |
| 238 | double getOrientation_z() const; |
| 239 | |
| 240 | double getOrOffset_w() const; |
| 241 | double getOrOffset_x() const; |
| 242 | double getOrOffset_y() const; |
| 243 | double getOrOffset_z() const; |
| 244 | |
| 245 | double getLon_deg() const; |
| 246 | double getLat_deg() const; |
| 247 | double getElev_ft() const; |
| 248 | |
| 249 | // Reference orientation rotations... |
| 250 | // These are rotations that represent the plane attitude effect on |
| 251 | // the view (in Pilot view). IE The view frustrum rotates as the plane |
| 252 | // turns, pitches, and rolls. |
| 253 | // In model view (lookat/chaseview) these end up changing the angle that |
| 254 | // the eye is looking at the ojbect (ie the model). |
| 255 | // FIXME: the FGModel class should have its own version of these so that |
| 256 | // it can generate it's own model rotations. |
| 257 | double getRoll_deg () const { return _roll_deg; } |
| 258 | double getPitch_deg () const {return _pitch_deg; } |
| 259 | void setRoll_deg (double roll_deg); |
| 260 | void setPitch_deg (double pitch_deg); |
| 261 | void setHeading_deg (double heading_deg); |
| 262 | void setOrientation (double roll_deg, double pitch_deg, double heading_deg); |
| 263 | double getTargetRoll_deg () const { return _target_roll_deg; } |
| 264 | double getTargetPitch_deg () const {return _target_pitch_deg; } |
| 265 | double getTargetHeading_deg () const {return _target_heading_deg; } |
| 266 | void setTargetRoll_deg (double roll_deg); |
| 267 | void setTargetPitch_deg (double pitch_deg); |
| 268 | void setTargetHeading_deg (double heading_deg); |
| 269 | void setTargetOrientation (double roll_deg, double pitch_deg, double heading_deg); |
| 270 | |
| 271 | void handleAGL(); |
| 272 | |
| 273 | // Orientation offsets rotations from reference orientation. |
| 274 | // Goal settings are for smooth transition from prior |
| 275 | // offset when changing view direction. |
| 276 | // These offsets are in ADDITION to the orientation rotations listed |
| 277 | // above. |
| 278 | // In pilot view they are applied after the position offsets in order to |
| 279 | // give the effect of the pilot looking around. |
| 280 | // In lookat view they are applied before the position offsets so that |
| 281 | // the effect is the eye moving around looking at the object (ie the model) |
| 282 | // from different angles. |
| 283 | double getGoalRollOffset_deg () const { return _goal_roll_offset_deg; } |
| 284 | double getGoalPitchOffset_deg () const { return _goal_pitch_offset_deg; } |
| 285 | double getGoalHeadingOffset_deg () const {return _goal_heading_offset_deg; } |
| 286 | void setRollOffset_deg (double roll_offset_deg); |
| 287 | void setPitchOffset_deg (double pitch_offset_deg); |
| 288 | void setGoalRollOffset_deg (double goal_roll_offset_deg); |
| 289 | void setGoalPitchOffset_deg (double goal_pitch_offset_deg); |
| 290 | void setOrientationOffsets (double roll_offset_deg, |
| 291 | double heading_offset_deg, |
| 292 | double pitch_offset_deg); |
| 293 | |
| 294 | void set_aspect_ratio_multiplier( double m ) { |
| 295 | _aspect_ratio_multiplier = m; |
| 296 | } |
| 297 | double get_aspect_ratio_multiplier() const { |
| 298 | return _aspect_ratio_multiplier; |
| 299 | } |
| 300 | |
| 301 | double getNear_m () const { return _ground_level_nearplane_m; } |
| 302 | void setNear_m (double near_m) { |
| 303 | _ground_level_nearplane_m = near_m; |
| 304 | } |
| 305 | |
| 306 | void set_fov( double fov_deg ) { |
| 307 | _fov_deg = fov_deg; |
| 308 | } |
| 309 | |
| 310 | double get_fov_user() const { return _fov_user_deg; } |
| 311 | void set_fov_user( double fov_deg ) { _fov_user_deg = fov_deg; } |
| 312 | |
| 313 | ////////////////////////////////////////////////////////////////// |
| 314 | // private data // |
| 315 | ////////////////////////////////////////////////////////////////// |
| 316 | |
| 317 | SGPropertyNode_ptr _config; |
| 318 | std::string _name, _typeString; |
| 319 | |
| 320 | // flag forcing a recalc of derived view parameters |
| 321 | bool _dirty; |
| 322 | |
| 323 | simgear::TiedPropertyList _tiedProperties; |
| 324 | |
| 325 | SGQuatd mViewOrientation; |
| 326 | SGQuatd mViewOffsetOr; |
| 327 | SGVec3d _absolute_view_pos; |
| 328 | |
| 329 | SGGeod _position; |
| 330 | SGGeod _target; |
| 331 | |
| 332 | double _roll_deg; |
| 333 | double _pitch_deg; |
| 334 | double _heading_deg; |
| 335 | double _target_roll_deg; |
| 336 | double _target_pitch_deg; |
| 337 | double _target_heading_deg; |
| 338 | |
| 339 | double _configRollOffsetDeg, |
| 340 | _configHeadingOffsetDeg, |
| 341 | _configPitchOffsetDeg; |
| 342 | |
| 343 | SGVec3d _dampTarget; ///< current target value we are damping towards |
| 344 | SGVec3d _dampOutput; ///< current output of damping filter |
| 345 | SGVec3d _dampFactor; ///< weighting of the damping filter |
| 346 | |
| 347 | /* Generic damping support. */ |
| 348 | struct Damping { |
| 349 | |
| 350 | Damping(double factor, double min, double max); |
| 351 | void setTarget(double target); |
| 352 | void update(double dt, void* id); |
| 353 | double get(); |
| 354 | void updateTarget(double& io); |
| 355 | void reset(double target); |
| 356 | |
| 357 | private: |
| 358 | void* _id; |
| 359 | double _min; |
| 360 | double _max; |
| 361 | double _target; |
| 362 | double _factor; |
| 363 | double _current; |
| 364 | }; |
| 365 | |
| 366 | Damping _lookat_agl_damping; |
| 367 | double _lookat_agl_ground_altitude; |
| 368 | |
| 369 | // Position offsets from FDM origin. The X axis is positive |
| 370 | // out the tail, Y is out the right wing, and Z is positive up. |
| 371 | // distance in meters |
| 372 | SGVec3d _offset_m; |
| 373 | SGVec3d _configOffset_m; |
| 374 | |
| 375 | SGVec3d _adjust_offset_m; |
| 376 | |
| 377 | // Target offsets from FDM origin (for "lookat" targets) The X |
| 378 | // axis is positive out the tail, Y is out the right wing, and Z |
| 379 | // is positive up. distance in meters |
| 380 | SGVec3d _target_offset_m; |
| 381 | SGVec3d _configTargetOffset_m; |
| 382 | |
| 383 | // orientation offsets from reference (_goal* are for smoothed transitions) |
| 384 | double _roll_offset_deg; |
| 385 | double _pitch_offset_deg; |
| 386 | double _heading_offset_deg; |
| 387 | double _goal_roll_offset_deg; |
| 388 | double _goal_pitch_offset_deg; |
| 389 | double _goal_heading_offset_deg; |
| 390 | |
| 391 | // used to set nearplane when at ground level for this view |
| 392 | double _ground_level_nearplane_m; |
| 393 | |
| 394 | ViewType _type; |
| 395 | ScalingType _scaling_type; |
| 396 | |
| 397 | // internal view (e.g. cockpit) flag |
| 398 | bool _internal; |
| 399 | |
| 400 | // Dynamically update view angle and field of view so that we always |
| 401 | // include the target and the ground below it. |
| 402 | bool _lookat_agl; |
| 403 | |
| 404 | int _view_index; |
| 405 | |
| 406 | // view is looking from a model |
| 407 | bool _from_model; |
| 408 | int _from_model_index; // number of model (for multi model) |
| 409 | |
| 410 | // view is looking at a model |
| 411 | bool _at_model; |
| 412 | int _at_model_index; // number of model (for multi model) |
| 413 | |
| 414 | // Field of view as requested by user. Usually copied directly into the |
| 415 | // actual field of view, except for Tower AGL view. |
| 416 | double _fov_user_deg; |
| 417 | |
| 418 | // the nominal field of view (angle, in degrees) |
| 419 | double _fov_deg; |
| 420 | double _configFOV_deg; |
| 421 | // default = 1.0, this value is user configurable and is |
| 422 | // multiplied into the aspect_ratio to get the actual vertical fov |
| 423 | double _aspect_ratio_multiplier; |
| 424 | |
| 425 | ////////////////////////////////////////////////////////////////// |
| 426 | // private functions // |
| 427 | ////////////////////////////////////////////////////////////////// |
| 428 | |
| 429 | void recalc (); |
| 430 | void recalcLookFrom(); |
| 431 | void recalcLookAt(); |
| 432 | |
| 433 | void setDampTarget(double h, double p, double r); |
| 434 | void getDampOutput(double& roll, double& pitch, double& heading); |
| 435 | |
| 436 | void updateDampOutput(double dt); |
| 437 | |
| 438 | // add to _heading_offset_deg |
| 439 | inline void incHeadingOffset_deg( double amt ) { |
| 440 | set_dirty(); |
| 441 | _heading_offset_deg += amt; |
| 442 | } |
| 443 | |
| 444 | // add to _pitch_offset_deg |
| 445 | inline void incPitchOffset_deg( double amt ) { |
| 446 | set_dirty(); |
| 447 | _pitch_offset_deg += amt; |
| 448 | } |
| 449 | |
| 450 | // add to _roll_offset_deg |
| 451 | inline void incRollOffset_deg( double amt ) { |
| 452 | set_dirty(); |
| 453 | _roll_offset_deg += amt; |
| 454 | } |
| 455 | }; // of class View |
| 456 | |
| 457 | } // of namespace flightgear |
| 458 | |
| 459 | #endif // _VIEWER_HXX |