1 // viewmgr.cxx -- class for managing all the views in the flightgear world.
3 // Written by Curtis Olson, started October 2000.
4 // partially rewritten by Jim Wilson March 2002
6 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include "viewmgr.hxx"
30 #include <string.h> // strcmp
32 #include <simgear/compiler.h>
33 #include <Main/fg_props.hxx>
37 FGViewMgr::FGViewMgr( void ) :
41 view_number(fgGetNode("/sim/current-view/view-number", true)),
42 config_list(fgGetNode("/sim", true)->getChildren("view")),
43 abs_viewer_position(SGVec3d::zeros()),
45 current_view_orientation(SGQuatd::zeros()),
46 current_view_or_offset(SGQuatd::zeros())
48 globals->set_viewmgr(this);
52 FGViewMgr::~FGViewMgr( void )
54 globals->set_viewmgr(NULL);
61 SG_LOG(SG_VIEW, SG_WARN, "duplicate init of view manager");
68 current_x_offs = fgGetNode("/sim/current-view/x-offset-m", true);
69 current_y_offs = fgGetNode("/sim/current-view/y-offset-m", true);
70 current_z_offs = fgGetNode("/sim/current-view/z-offset-m", true);
71 target_x_offs = fgGetNode("/sim/current-view/target-x-offset-m", true);
72 target_y_offs = fgGetNode("/sim/current-view/target-y-offset-m", true);
73 target_z_offs = fgGetNode("/sim/current-view/target-z-offset-m", true);
75 double aspect_ratio_multiplier
76 = fgGetDouble("/sim/current-view/aspect-ratio-multiplier");
78 for (unsigned int i = 0; i < config_list.size(); i++) {
79 SGPropertyNode *n = config_list[i];
80 SGPropertyNode *config = n->getChild("config", 0, true);
82 // find out if this is an internal view (e.g. in cockpit, low near plane)
83 bool internal = n->getBoolValue("internal", false);
86 // this is assumed to be an aircraft model...we will need to read
87 // model-from-type as well.
89 // find out if this is a model we are looking from...
90 bool from_model = config->getBoolValue("from-model");
91 int from_model_index = config->getIntValue("from-model-idx");
93 double x_offset_m = config->getDoubleValue("x-offset-m");
94 double y_offset_m = config->getDoubleValue("y-offset-m");
95 double z_offset_m = config->getDoubleValue("z-offset-m");
97 double heading_offset_deg = config->getDoubleValue("heading-offset-deg");
98 config->setDoubleValue("heading-offset-deg", heading_offset_deg);
99 double pitch_offset_deg = config->getDoubleValue("pitch-offset-deg");
100 config->setDoubleValue("pitch-offset-deg", pitch_offset_deg);
101 double roll_offset_deg = config->getDoubleValue("roll-offset-deg");
102 config->setDoubleValue("roll-offset-deg", roll_offset_deg);
104 double fov_deg = config->getDoubleValue("default-field-of-view-deg");
105 double near_m = config->getDoubleValue("ground-level-nearplane-m");
107 // supporting two types "lookat" = 1 and "lookfrom" = 0
108 const char *type = n->getStringValue("type");
109 if (!strcmp(type, "lookat")) {
111 bool at_model = config->getBoolValue("at-model");
112 int at_model_index = config->getIntValue("at-model-idx");
114 double damp_roll = config->getDoubleValue("at-model-roll-damping");
115 double damp_pitch = config->getDoubleValue("at-model-pitch-damping");
116 double damp_heading = config->getDoubleValue("at-model-heading-damping");
118 double target_x_offset_m = config->getDoubleValue("target-x-offset-m");
119 double target_y_offset_m = config->getDoubleValue("target-y-offset-m");
120 double target_z_offset_m = config->getDoubleValue("target-z-offset-m");
122 add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
123 at_model, at_model_index,
124 damp_roll, damp_pitch, damp_heading,
125 x_offset_m, y_offset_m,z_offset_m,
126 heading_offset_deg, pitch_offset_deg,
127 roll_offset_deg, fov_deg, aspect_ratio_multiplier,
128 target_x_offset_m, target_y_offset_m,
129 target_z_offset_m, near_m, internal ));
131 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index,
132 false, 0, 0.0, 0.0, 0.0,
133 x_offset_m, y_offset_m, z_offset_m,
134 heading_offset_deg, pitch_offset_deg,
135 roll_offset_deg, fov_deg, aspect_ratio_multiplier,
136 0, 0, 0, near_m, internal ));
145 FGViewMgr::shutdown()
157 int current_view_index = current;
159 // reset offsets and fov to configuration defaults
160 for (unsigned int i = 0; i < config_list.size(); i++) {
161 SGPropertyNode *n = config_list[i];
164 fgSetDouble("/sim/current-view/x-offset-m",
165 n->getDoubleValue("config/x-offset-m"));
166 fgSetDouble("/sim/current-view/y-offset-m",
167 n->getDoubleValue("config/y-offset-m"));
168 fgSetDouble("/sim/current-view/z-offset-m",
169 n->getDoubleValue("config/z-offset-m"));
170 fgSetDouble("/sim/current-view/pitch-offset-deg",
171 n->getDoubleValue("config/pitch-offset-deg"));
172 fgSetDouble("/sim/current-view/heading-offset-deg",
173 n->getDoubleValue("config/heading-offset-deg"));
174 fgSetDouble("/sim/current-view/roll-offset-deg",
175 n->getDoubleValue("config/roll-offset-deg"));
177 double fov_deg = n->getDoubleValue("config/default-field-of-view-deg");
180 fgSetDouble("/sim/current-view/field-of-view", fov_deg);
182 // target offsets for lookat mode only...
183 fgSetDouble("/sim/current-view/target-x-offset-m",
184 n->getDoubleValue("config/target-x-offset-m"));
185 fgSetDouble("/sim/current-view/target-y-offset-m",
186 n->getDoubleValue("config/target-y-offset-m"));
187 fgSetDouble("/sim/current-view/target-z-offset-m",
188 n->getDoubleValue("config/target-z-offset-m"));
190 setView(current_view_index);
193 typedef double (FGViewMgr::*double_getter)() const;
198 // view-manager code was designed to init before bind, so
199 // this is a no-op; init() calls the real bind() impl below
205 // these are bound to the current view properties
206 _tiedProperties.setRoot(fgGetNode("/sim/current-view", true));
207 _tiedProperties.Tie("heading-offset-deg", this,
208 &FGViewMgr::getViewHeadingOffset_deg,
209 &FGViewMgr::setViewHeadingOffset_deg);
210 fgSetArchivable("/sim/current-view/heading-offset-deg");
211 _tiedProperties.Tie("goal-heading-offset-deg", this,
212 &FGViewMgr::getViewGoalHeadingOffset_deg,
213 &FGViewMgr::setViewGoalHeadingOffset_deg);
214 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
215 _tiedProperties.Tie("pitch-offset-deg", this,
216 &FGViewMgr::getViewPitchOffset_deg,
217 &FGViewMgr::setViewPitchOffset_deg);
218 fgSetArchivable("/sim/current-view/pitch-offset-deg");
219 _tiedProperties.Tie("goal-pitch-offset-deg", this,
220 &FGViewMgr::getGoalViewPitchOffset_deg,
221 &FGViewMgr::setGoalViewPitchOffset_deg);
222 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
223 _tiedProperties.Tie("roll-offset-deg", this,
224 &FGViewMgr::getViewRollOffset_deg,
225 &FGViewMgr::setViewRollOffset_deg);
226 fgSetArchivable("/sim/current-view/roll-offset-deg");
227 _tiedProperties.Tie("goal-roll-offset-deg", this,
228 &FGViewMgr::getGoalViewRollOffset_deg,
229 &FGViewMgr::setGoalViewRollOffset_deg);
230 fgSetArchivable("/sim/current-view/goal-roll-offset-deg");
232 _tiedProperties.Tie("view-number", this,
233 &FGViewMgr::getView, &FGViewMgr::setView);
234 SGPropertyNode* view_number =
235 _tiedProperties.getRoot()->getNode("view-number");
236 view_number->setAttribute(SGPropertyNode::ARCHIVE, false);
238 // Keep view on reset/reinit
239 view_number->setAttribute(SGPropertyNode::PRESERVE, true);
241 _tiedProperties.Tie("axes/long", this,
242 (double_getter)0, &FGViewMgr::setViewAxisLong);
243 fgSetArchivable("/sim/current-view/axes/long");
245 _tiedProperties.Tie("axes/lat", this,
246 (double_getter)0, &FGViewMgr::setViewAxisLat);
247 fgSetArchivable("/sim/current-view/axes/lat");
249 _tiedProperties.Tie("field-of-view", this,
250 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
251 fgSetArchivable("/sim/current-view/field-of-view");
253 _tiedProperties.Tie("aspect-ratio-multiplier", this,
254 &FGViewMgr::getARM_deg, &FGViewMgr::setARM_deg);
255 fgSetArchivable("/sim/current-view/field-of-view");
257 _tiedProperties.Tie("ground-level-nearplane-m", this,
258 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
259 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
261 SGPropertyNode *n = fgGetNode("/sim/current-view", true);
262 _tiedProperties.Tie(n->getNode("viewer-x-m", true),SGRawValuePointer<double>(&abs_viewer_position[0]));
263 _tiedProperties.Tie(n->getNode("viewer-y-m", true),SGRawValuePointer<double>(&abs_viewer_position[1]));
264 _tiedProperties.Tie(n->getNode("viewer-z-m", true),SGRawValuePointer<double>(&abs_viewer_position[2]));
266 _tiedProperties.Tie("viewer-lon-deg", this, &FGViewMgr::getViewLon_deg);
267 _tiedProperties.Tie("viewer-lat-deg", this, &FGViewMgr::getViewLat_deg);
268 _tiedProperties.Tie("viewer-elev-ft", this, &FGViewMgr::getViewElev_ft);
271 _tiedProperties.Tie("debug/orientation-w", this,
272 &FGViewMgr::getCurrentViewOrientation_w);
273 _tiedProperties.Tie("debug/orientation-x", this,
274 &FGViewMgr::getCurrentViewOrientation_x);
275 _tiedProperties.Tie("debug/orientation-y", this,
276 &FGViewMgr::getCurrentViewOrientation_y);
277 _tiedProperties.Tie("debug/orientation-z", this,
278 &FGViewMgr::getCurrentViewOrientation_z);
280 _tiedProperties.Tie("debug/orientation_offset-w", this,
281 &FGViewMgr::getCurrentViewOrOffset_w);
282 _tiedProperties.Tie("debug/orientation_offset-x", this,
283 &FGViewMgr::getCurrentViewOrOffset_x);
284 _tiedProperties.Tie("debug/orientation_offset-y", this,
285 &FGViewMgr::getCurrentViewOrOffset_y);
286 _tiedProperties.Tie("debug/orientation_offset-z", this,
287 &FGViewMgr::getCurrentViewOrOffset_z);
289 _tiedProperties.Tie("debug/frame-w", this,
290 &FGViewMgr::getCurrentViewFrame_w);
291 _tiedProperties.Tie("debug/frame-x", this,
292 &FGViewMgr::getCurrentViewFrame_x);
293 _tiedProperties.Tie("debug/frame-y", this,
294 &FGViewMgr::getCurrentViewFrame_y);
295 _tiedProperties.Tie("debug/frame-z", this,
296 &FGViewMgr::getCurrentViewFrame_z);
302 _tiedProperties.Untie();
308 FGViewMgr::update (double dt)
310 FGViewer* currentView = (FGViewer *)get_current_view();
311 if (!currentView) return;
313 SGPropertyNode *n = config_list[current];
314 SGPropertyNode *config = n->getChild("config", 0, true);
315 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
317 // Set up view location and orientation
319 if (!config->getBoolValue("from-model")) {
320 lon_deg = fgGetDouble(config->getStringValue("eye-lon-deg-path"));
321 lat_deg = fgGetDouble(config->getStringValue("eye-lat-deg-path"));
322 alt_ft = fgGetDouble(config->getStringValue("eye-alt-ft-path"));
323 roll_deg = fgGetDouble(config->getStringValue("eye-roll-deg-path"));
324 pitch_deg = fgGetDouble(config->getStringValue("eye-pitch-deg-path"));
325 heading_deg = fgGetDouble(config->getStringValue("eye-heading-deg-path"));
327 currentView->setPosition(lon_deg, lat_deg, alt_ft);
328 currentView->setOrientation(roll_deg, pitch_deg, heading_deg);
330 // force recalc in viewer
331 currentView->set_dirty();
334 // if lookat (type 1) then get target data...
335 if (currentView->getType() == FG_LOOKAT) {
336 if (!config->getBoolValue("from-model")) {
337 lon_deg = fgGetDouble(config->getStringValue("target-lon-deg-path"));
338 lat_deg = fgGetDouble(config->getStringValue("target-lat-deg-path"));
339 alt_ft = fgGetDouble(config->getStringValue("target-alt-ft-path"));
340 roll_deg = fgGetDouble(config->getStringValue("target-roll-deg-path"));
341 pitch_deg = fgGetDouble(config->getStringValue("target-pitch-deg-path"));
342 heading_deg = fgGetDouble(config->getStringValue("target-heading-deg-path"));
344 currentView->setTargetPosition(lon_deg, lat_deg, alt_ft);
345 currentView->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
347 currentView->set_dirty();
351 setViewXOffset_m(current_x_offs->getDoubleValue());
352 setViewYOffset_m(current_y_offs->getDoubleValue());
353 setViewZOffset_m(current_z_offs->getDoubleValue());
355 setViewTargetXOffset_m(target_x_offs->getDoubleValue());
356 setViewTargetYOffset_m(target_y_offs->getDoubleValue());
357 setViewTargetZOffset_m(target_z_offs->getDoubleValue());
359 current_view_orientation = currentView->getViewOrientation();
360 current_view_or_offset = currentView->getViewOrientationOffset();
362 // Update the current view
364 currentView->update(dt);
365 abs_viewer_position = currentView->getViewPosition();
367 // expose the raw (OpenGL) orientation to the property tree,
368 // for the sound-manager
369 for (int i=0; i<4; ++i) {
370 _tiedProperties.getRoot()->getChild("raw-orientation", i, true)->setDoubleValue(current_view_orientation[i]);
375 FGViewMgr::copyToCurrent()
381 SGPropertyNode *n = config_list[current];
382 fgSetString("/sim/current-view/name", n->getStringValue("name"));
383 fgSetString("/sim/current-view/type", n->getStringValue("type"));
385 // copy certain view config data for default values
386 fgSetDouble("/sim/current-view/config/heading-offset-deg",
387 n->getDoubleValue("config/default-heading-offset-deg"));
388 fgSetDouble("/sim/current-view/config/pitch-offset-deg",
389 n->getDoubleValue("config/pitch-offset-deg"));
390 fgSetDouble("/sim/current-view/config/roll-offset-deg",
391 n->getDoubleValue("config/roll-offset-deg"));
392 fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
393 n->getDoubleValue("config/default-field-of-view-deg"));
394 fgSetBool("/sim/current-view/config/from-model",
395 n->getBoolValue("config/from-model"));
398 fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
399 fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
400 fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
402 fgSetDouble("/sim/current-view/goal-heading-offset-deg",
403 get_current_view()->getGoalHeadingOffset_deg());
404 fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
405 get_current_view()->getGoalPitchOffset_deg());
406 fgSetDouble("/sim/current-view/goal-roll-offset-deg",
407 get_current_view()->getRollOffset_deg());
408 fgSetDouble("/sim/current-view/heading-offset-deg",
409 get_current_view()->getHeadingOffset_deg());
410 fgSetDouble("/sim/current-view/pitch-offset-deg",
411 get_current_view()->getPitchOffset_deg());
412 fgSetDouble("/sim/current-view/roll-offset-deg",
413 get_current_view()->getRollOffset_deg());
414 fgSetDouble("/sim/current-view/target-x-offset-m",
415 get_current_view()->getTargetXOffset_m());
416 fgSetDouble("/sim/current-view/target-y-offset-m",
417 get_current_view()->getTargetYOffset_m());
418 fgSetDouble("/sim/current-view/target-z-offset-m",
419 get_current_view()->getTargetZOffset_m());
420 fgSetBool("/sim/current-view/internal",
421 get_current_view()->getInternal());
424 void FGViewMgr::clear()
430 FGViewMgr::get_current_view()
432 if ( current < (int)views.size() ) {
433 return views[current];
440 FGViewMgr::get_current_view() const
442 if ( current < (int)views.size() ) {
443 return views[current];
451 FGViewMgr::get_view( int i )
453 if ( i < 0 ) { i = 0; }
454 if ( i >= (int)views.size() ) { i = views.size() - 1; }
459 FGViewMgr::get_view( int i ) const
461 if ( i < 0 ) { i = 0; }
462 if ( i >= (int)views.size() ) { i = views.size() - 1; }
467 FGViewMgr::next_view()
469 setView((current+1 < (int)views.size()) ? (current + 1) : 0);
470 view_number->fireValueChanged();
471 return views[current];
475 FGViewMgr::prev_view()
477 setView((0 < current) ? (current - 1) : (views.size() - 1));
478 view_number->fireValueChanged();
479 return views[current];
483 FGViewMgr::add_view( FGViewer * v )
490 FGViewMgr::getViewHeadingOffset_deg () const
492 const FGViewer * view = get_current_view();
493 return (view == 0 ? 0 : view->getHeadingOffset_deg());
497 FGViewMgr::setViewHeadingOffset_deg (double offset)
499 FGViewer * view = get_current_view();
501 view->setGoalHeadingOffset_deg(offset);
502 view->setHeadingOffset_deg(offset);
507 FGViewMgr::getViewGoalHeadingOffset_deg () const
509 const FGViewer * view = get_current_view();
510 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
514 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
516 FGViewer * view = get_current_view();
518 view->setGoalHeadingOffset_deg(offset);
522 FGViewMgr::getViewPitchOffset_deg () const
524 const FGViewer * view = get_current_view();
525 return (view == 0 ? 0 : view->getPitchOffset_deg());
529 FGViewMgr::setViewPitchOffset_deg (double tilt)
531 FGViewer * view = get_current_view();
533 view->setGoalPitchOffset_deg(tilt);
534 view->setPitchOffset_deg(tilt);
539 FGViewMgr::getGoalViewPitchOffset_deg () const
541 const FGViewer * view = get_current_view();
542 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
546 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
548 FGViewer * view = get_current_view();
550 view->setGoalPitchOffset_deg(tilt);
554 FGViewMgr::getViewRollOffset_deg () const
556 const FGViewer * view = get_current_view();
557 return (view == 0 ? 0 : view->getRollOffset_deg());
561 FGViewMgr::setViewRollOffset_deg (double tilt)
563 FGViewer * view = get_current_view();
565 view->setGoalRollOffset_deg(tilt);
566 view->setRollOffset_deg(tilt);
571 FGViewMgr::getGoalViewRollOffset_deg () const
573 const FGViewer * view = get_current_view();
574 return (view == 0 ? 0 : view->getGoalRollOffset_deg());
578 FGViewMgr::setGoalViewRollOffset_deg (double tilt)
580 FGViewer * view = get_current_view();
582 view->setGoalRollOffset_deg(tilt);
586 FGViewMgr::getViewXOffset_m () const
588 const FGViewer * view = get_current_view();
590 return ((FGViewer *)view)->getXOffset_m();
597 FGViewMgr::setViewXOffset_m (double x)
599 FGViewer * view = get_current_view();
601 view->setXOffset_m(x);
606 FGViewMgr::getViewYOffset_m () const
608 const FGViewer * view = get_current_view();
610 return ((FGViewer *)view)->getYOffset_m();
617 FGViewMgr::setViewYOffset_m (double y)
619 FGViewer * view = get_current_view();
621 view->setYOffset_m(y);
626 FGViewMgr::getViewZOffset_m () const
628 const FGViewer * view = get_current_view();
630 return ((FGViewer *)view)->getZOffset_m();
637 FGViewMgr::setViewZOffset_m (double z)
639 FGViewer * view = get_current_view();
641 view->setZOffset_m(z);
646 FGViewMgr::getViewTargetXOffset_m () const
648 const FGViewer * view = get_current_view();
650 return ((FGViewer *)view)->getTargetXOffset_m();
657 FGViewMgr::setViewTargetXOffset_m (double x)
659 FGViewer * view = get_current_view();
661 view->setTargetXOffset_m(x);
666 FGViewMgr::getViewTargetYOffset_m () const
668 const FGViewer * view = get_current_view();
670 return ((FGViewer *)view)->getTargetYOffset_m();
677 FGViewMgr::setViewTargetYOffset_m (double y)
679 FGViewer * view = get_current_view();
681 view->setTargetYOffset_m(y);
686 FGViewMgr::getViewTargetZOffset_m () const
688 const FGViewer * view = get_current_view();
690 return ((FGViewer *)view)->getTargetZOffset_m();
697 FGViewMgr::setViewTargetZOffset_m (double z)
699 FGViewer * view = get_current_view();
701 view->setTargetZOffset_m(z);
706 FGViewMgr::getView () const
712 FGViewMgr::setView (int newview)
714 // negative numbers -> set view with node index -newview
716 for (int i = 0; i < (int)config_list.size(); i++) {
717 int index = -config_list[i]->getIndex();
718 if (index == newview)
725 // if newview number too low wrap to last view...
727 newview = (int)views.size() - 1;
729 // if newview number to high wrap to zero...
730 if (newview >= (int)views.size())
741 FGViewMgr::getFOV_deg () const
743 const FGViewer * view = get_current_view();
744 return (view == 0 ? 0 : view->get_fov());
748 FGViewMgr::setFOV_deg (double fov)
750 FGViewer * view = get_current_view();
756 FGViewMgr::getARM_deg () const
758 const FGViewer * view = get_current_view();
759 return (view == 0 ? 0 : view->get_aspect_ratio_multiplier());
763 FGViewMgr::setARM_deg (double aspect_ratio_multiplier)
765 FGViewer * view = get_current_view();
767 view->set_aspect_ratio_multiplier(aspect_ratio_multiplier);
771 FGViewMgr::getNear_m () const
773 const FGViewer * view = get_current_view();
774 return (view == 0 ? 0.5f : view->getNear_m());
778 FGViewMgr::setNear_m (double near_m)
780 FGViewer * view = get_current_view();
782 view->setNear_m(near_m);
786 FGViewMgr::setViewAxisLong (double axis)
792 FGViewMgr::setViewAxisLat (double axis)
798 FGViewMgr::getViewLon_deg() const
800 const FGViewer* view = get_current_view();
801 return (view != NULL) ? view->getPosition().getLongitudeDeg() : 0.0;
805 FGViewMgr::getViewLat_deg() const
807 const FGViewer* view = get_current_view();
808 return (view != NULL) ? view->getPosition().getLatitudeDeg() : 0.0;
812 FGViewMgr::getViewElev_ft() const
814 const FGViewer* view = get_current_view();
815 return (view != NULL) ? view->getPosition().getElevationFt() : 0.0;
819 // reference frame orientation.
820 // This is the view orientation you get when you have no
821 // view offset, i.e. the offset operator is the identity.
823 // For example, in the familiar "cockpit lookfrom" view,
824 // the reference frame is equal to the aircraft attitude,
825 // i.e. it is the view looking towards 12:00 straight ahead.
827 // FIXME: Somebody needs to figure out what is the reference
828 // frame view for the other view modes.
830 // Conceptually, this quat represents a rotation relative
831 // to the ECEF reference orientation, as described at
832 // http://www.av8n.com/physics/coords.htm#sec-orientation
834 // See the NOTE concerning reference orientations, below.
836 // The components of this quat are expressed in
837 // the conventional aviation basis set,
838 // i.e. x=forward, y=starboard, z=bottom
839 double FGViewMgr::getCurrentViewFrame_w() const{
840 return ((current_view_orientation*conj(fsb2sta())*conj(current_view_or_offset))).w();
842 double FGViewMgr::getCurrentViewFrame_x() const{
843 return ((current_view_orientation*conj(fsb2sta())*conj(current_view_or_offset))).x();
845 double FGViewMgr::getCurrentViewFrame_y() const{
846 return ((current_view_orientation*conj(fsb2sta())*conj(current_view_or_offset))).y();
848 double FGViewMgr::getCurrentViewFrame_z() const{
849 return ((current_view_orientation*conj(fsb2sta())*conj(current_view_or_offset))).z();
854 // This rotation takes you from the aforementioned
855 // reference frame view orientation to whatever
856 // actual current view orientation is.
858 // The components of this quaternion are expressed in
859 // the conventional aviation basis set,
860 // i.e. x=forward, y=starboard, z=bottom
861 double FGViewMgr::getCurrentViewOrOffset_w() const{
862 return current_view_or_offset.w();
864 double FGViewMgr::getCurrentViewOrOffset_x() const{
865 return current_view_or_offset.x();
867 double FGViewMgr::getCurrentViewOrOffset_y() const{
868 return current_view_or_offset.y();
870 double FGViewMgr::getCurrentViewOrOffset_z() const{
871 return current_view_or_offset.z();
875 // current view orientation.
876 // This is a rotation relative to the earth-centered (ec)
879 // NOTE: Here we remove a factor of fsb2sta so that
880 // the components of this quat are displayed using the
881 // conventional ECEF basis set. This is *not* the way
882 // the view orientation is stored in the views[] array,
883 // but is easier for non-graphics hackers to understand.
884 // If we did not remove this factor of fsb2sta here and
885 // in getCurrentViewFrame, that would be equivalent to
886 // the following peculiar reference orientation:
887 // Suppose you are over the Gulf of Guinea, at (lat,lon) = (0,0).
888 // Then the reference frame orientation can be achieved via:
889 // -- The aircraft X-axis (nose) headed south.
890 // -- The aircraft Y-axis (starboard wingtip) pointing up.
891 // -- The aircraft Z-axis (belly) pointing west.
892 // To say the same thing in other words, and perhaps more to the
893 // point: If we use the OpenGL camera orientation conventions,
894 // i.e. Xprime=starboard, Yprime=top, Zprime=aft, then the
895 // aforementioned peculiar reference orientation at (lat,lon)
896 // = (0,0) can be described as:
897 // -- aircraft Xprime axis (starboard) pointed up
898 // -- aircraft Yprime axis (top) pointed east
899 // -- aircraft Zprime axis (aft) pointed north
900 // meaning the OpenGL axes are aligned with the ECEF axes.
901 double FGViewMgr::getCurrentViewOrientation_w() const{
902 return (current_view_orientation * conj(fsb2sta())).w();
904 double FGViewMgr::getCurrentViewOrientation_x() const{
905 return (current_view_orientation * conj(fsb2sta())).x();
907 double FGViewMgr::getCurrentViewOrientation_y() const{
908 return (current_view_orientation * conj(fsb2sta())).y();
910 double FGViewMgr::getCurrentViewOrientation_z() const{
911 return (current_view_orientation * conj(fsb2sta())).z();
915 FGViewMgr::do_axes ()
917 // Take no action when hat is centered
918 if ( ( axis_long < 0.01 ) &&
919 ( axis_long > -0.01 ) &&
920 ( axis_lat < 0.01 ) &&
925 double viewDir = 999;
927 /* Do all the quick and easy cases */
928 if (axis_long < 0) { // Longitudinal axis forward
929 if (axis_lat == axis_long)
930 viewDir = fgGetDouble("/sim/view/config/front-left-direction-deg");
931 else if (axis_lat == - axis_long)
932 viewDir = fgGetDouble("/sim/view/config/front-right-direction-deg");
933 else if (axis_lat == 0)
934 viewDir = fgGetDouble("/sim/view/config/front-direction-deg");
935 } else if (axis_long > 0) { // Longitudinal axis backward
936 if (axis_lat == - axis_long)
937 viewDir = fgGetDouble("/sim/view/config/back-left-direction-deg");
938 else if (axis_lat == axis_long)
939 viewDir = fgGetDouble("/sim/view/config/back-right-direction-deg");
940 else if (axis_lat == 0)
941 viewDir = fgGetDouble("/sim/view/config/back-direction-deg");
942 } else if (axis_long == 0) { // Longitudinal axis neutral
944 viewDir = fgGetDouble("/sim/view/config/left-direction-deg");
945 else if (axis_lat > 0)
946 viewDir = fgGetDouble("/sim/view/config/right-direction-deg");
947 else return; /* And assertion failure maybe? */
950 // Do all the difficult cases
952 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
953 if ( viewDir < -1 ) viewDir += 360;
955 get_current_view()->setGoalHeadingOffset_deg(viewDir);