]> git.mxchange.org Git - flightgear.git/blob - src/Viewer/viewmgr.cxx
View can created itself from config properties
[flightgear.git] / src / Viewer / viewmgr.cxx
1 // viewmgr.cxx -- class for managing all the views in the flightgear world.
2 //
3 // Written by Curtis Olson, started October 2000.
4 //   partially rewritten by Jim Wilson March 2002
5 //
6 // Copyright (C) 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "viewmgr.hxx"
29
30 #include <string.h>        // strcmp
31
32 #include <simgear/compiler.h>
33 #include <simgear/scene/util/OsgMath.hxx>
34
35 #include <Main/fg_props.hxx>
36 #include "viewer.hxx"
37
38 #include "CameraGroup.hxx"
39
40 // Constructor
41 FGViewMgr::FGViewMgr( void ) :
42   axis_long(0),
43   axis_lat(0),
44   inited(false),
45   view_number(fgGetNode("/sim/current-view/view-number", true)),
46   config_list(fgGetNode("/sim", true)->getChildren("view")),
47   abs_viewer_position(SGVec3d::zeros()),
48   current(0),
49   current_view_orientation(SGQuatd::zeros()),
50   current_view_or_offset(SGQuatd::zeros())
51 {
52 }
53
54 // Destructor
55 FGViewMgr::~FGViewMgr( void )
56 {
57 }
58
59 void
60 FGViewMgr::init ()
61 {
62   if (inited) {
63     SG_LOG(SG_VIEW, SG_WARN, "duplicate init of view manager");
64     return;
65   }
66   
67   inited = true;
68   
69   // stash properties
70   current_x_offs = fgGetNode("/sim/current-view/x-offset-m", true);
71   current_y_offs = fgGetNode("/sim/current-view/y-offset-m", true);
72   current_z_offs = fgGetNode("/sim/current-view/z-offset-m", true);
73   target_x_offs  = fgGetNode("/sim/current-view/target-x-offset-m", true);
74   target_y_offs  = fgGetNode("/sim/current-view/target-y-offset-m", true);
75   target_z_offs  = fgGetNode("/sim/current-view/target-z-offset-m", true);
76
77
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);
81
82       flightgear::View* v = flightgear::View::createFromProperties(config);
83       if (v) {
84           add_view(v);
85       }
86   }
87
88   copyToCurrent();
89   do_bind();
90 }
91
92 void
93 FGViewMgr::postinit()
94 {
95     // force update now so many properties of the current view are valid,
96     // eg view position and orientation (as exposed via globals)
97     update(0.0);
98 }
99
100 void
101 FGViewMgr::shutdown()
102 {
103     if (!inited) {
104         return;
105     }
106     
107     inited = false;
108 }
109
110 void
111 FGViewMgr::reinit ()
112 {
113   int current_view_index = current;
114
115   // reset offsets and fov to configuration defaults
116   for (unsigned int i = 0; i < config_list.size(); i++) {
117     SGPropertyNode *n = config_list[i];
118     setView(i);
119
120     fgSetDouble("/sim/current-view/x-offset-m",
121         n->getDoubleValue("config/x-offset-m"));
122     fgSetDouble("/sim/current-view/y-offset-m",
123         n->getDoubleValue("config/y-offset-m"));
124     fgSetDouble("/sim/current-view/z-offset-m",
125         n->getDoubleValue("config/z-offset-m"));
126     fgSetDouble("/sim/current-view/pitch-offset-deg",
127         n->getDoubleValue("config/pitch-offset-deg"));
128     fgSetDouble("/sim/current-view/heading-offset-deg",
129         n->getDoubleValue("config/heading-offset-deg"));
130     fgSetDouble("/sim/current-view/roll-offset-deg",
131         n->getDoubleValue("config/roll-offset-deg"));
132
133     double fov_deg = n->getDoubleValue("config/default-field-of-view-deg");
134     if (fov_deg < 10.0)
135       fov_deg = 55.0;
136     fgSetDouble("/sim/current-view/field-of-view", fov_deg);
137
138     // target offsets for lookat mode only...
139     fgSetDouble("/sim/current-view/target-x-offset-m",
140         n->getDoubleValue("config/target-x-offset-m"));
141     fgSetDouble("/sim/current-view/target-y-offset-m",
142         n->getDoubleValue("config/target-y-offset-m"));
143     fgSetDouble("/sim/current-view/target-z-offset-m",
144         n->getDoubleValue("config/target-z-offset-m"));
145   }
146   setView(current_view_index);
147 }
148
149 typedef double (FGViewMgr::*double_getter)() const;
150
151 void
152 FGViewMgr::bind()
153 {
154   // view-manager code was designed to init before bind, so
155   // this is a no-op; init() calls the real bind() impl below
156 }
157
158 void
159 FGViewMgr::do_bind()
160 {  
161   // these are bound to the current view properties
162   _tiedProperties.setRoot(fgGetNode("/sim/current-view", true));
163   _tiedProperties.Tie("heading-offset-deg", this,
164                       &FGViewMgr::getViewHeadingOffset_deg,
165                       &FGViewMgr::setViewHeadingOffset_deg);
166   fgSetArchivable("/sim/current-view/heading-offset-deg");
167   _tiedProperties.Tie("goal-heading-offset-deg", this,
168                       &FGViewMgr::getViewGoalHeadingOffset_deg,
169                       &FGViewMgr::setViewGoalHeadingOffset_deg);
170   fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
171   _tiedProperties.Tie("pitch-offset-deg", this,
172                       &FGViewMgr::getViewPitchOffset_deg,
173                       &FGViewMgr::setViewPitchOffset_deg);
174   fgSetArchivable("/sim/current-view/pitch-offset-deg");
175   _tiedProperties.Tie("goal-pitch-offset-deg", this,
176                       &FGViewMgr::getGoalViewPitchOffset_deg,
177                       &FGViewMgr::setGoalViewPitchOffset_deg);
178   fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
179   _tiedProperties.Tie("roll-offset-deg", this,
180                       &FGViewMgr::getViewRollOffset_deg,
181                       &FGViewMgr::setViewRollOffset_deg);
182   fgSetArchivable("/sim/current-view/roll-offset-deg");
183   _tiedProperties.Tie("goal-roll-offset-deg", this,
184                       &FGViewMgr::getGoalViewRollOffset_deg,
185                       &FGViewMgr::setGoalViewRollOffset_deg);
186   fgSetArchivable("/sim/current-view/goal-roll-offset-deg");
187
188   _tiedProperties.Tie("view-number", this,
189                       &FGViewMgr::getView, &FGViewMgr::setView);
190   SGPropertyNode* view_number =
191     _tiedProperties.getRoot()->getNode("view-number");
192   view_number->setAttribute(SGPropertyNode::ARCHIVE, false);
193
194   // Keep view on reset/reinit
195   view_number->setAttribute(SGPropertyNode::PRESERVE, true);
196
197   _tiedProperties.Tie("axes/long", this,
198                       (double_getter)0, &FGViewMgr::setViewAxisLong);
199   fgSetArchivable("/sim/current-view/axes/long");
200
201   _tiedProperties.Tie("axes/lat", this,
202                       (double_getter)0, &FGViewMgr::setViewAxisLat);
203   fgSetArchivable("/sim/current-view/axes/lat");
204
205   _tiedProperties.Tie("field-of-view", this,
206                       &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
207   fgSetArchivable("/sim/current-view/field-of-view");
208
209   _tiedProperties.Tie("aspect-ratio-multiplier", this,
210                       &FGViewMgr::getARM_deg, &FGViewMgr::setARM_deg);
211   fgSetArchivable("/sim/current-view/field-of-view");
212
213   _tiedProperties.Tie("ground-level-nearplane-m", this,
214                       &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
215   fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
216
217   SGPropertyNode *n = fgGetNode("/sim/current-view", true);
218   _tiedProperties.Tie(n->getNode("viewer-x-m", true),SGRawValuePointer<double>(&abs_viewer_position[0]));
219   _tiedProperties.Tie(n->getNode("viewer-y-m", true),SGRawValuePointer<double>(&abs_viewer_position[1]));
220   _tiedProperties.Tie(n->getNode("viewer-z-m", true),SGRawValuePointer<double>(&abs_viewer_position[2]));
221
222   _tiedProperties.Tie("viewer-lon-deg", this, &FGViewMgr::getViewLon_deg);
223   _tiedProperties.Tie("viewer-lat-deg", this, &FGViewMgr::getViewLat_deg);
224   _tiedProperties.Tie("viewer-elev-ft", this, &FGViewMgr::getViewElev_ft);
225   
226   
227   _tiedProperties.Tie("debug/orientation-w", this,
228                       &FGViewMgr::getCurrentViewOrientation_w);
229   _tiedProperties.Tie("debug/orientation-x", this,
230                       &FGViewMgr::getCurrentViewOrientation_x);
231   _tiedProperties.Tie("debug/orientation-y", this,
232                       &FGViewMgr::getCurrentViewOrientation_y);
233   _tiedProperties.Tie("debug/orientation-z", this,
234                       &FGViewMgr::getCurrentViewOrientation_z);
235
236   _tiedProperties.Tie("debug/orientation_offset-w", this,
237                       &FGViewMgr::getCurrentViewOrOffset_w);
238   _tiedProperties.Tie("debug/orientation_offset-x", this,
239                       &FGViewMgr::getCurrentViewOrOffset_x);
240   _tiedProperties.Tie("debug/orientation_offset-y", this,
241                       &FGViewMgr::getCurrentViewOrOffset_y);
242   _tiedProperties.Tie("debug/orientation_offset-z", this,
243                       &FGViewMgr::getCurrentViewOrOffset_z);
244
245   _tiedProperties.Tie("debug/frame-w", this,
246                       &FGViewMgr::getCurrentViewFrame_w);
247   _tiedProperties.Tie("debug/frame-x", this,
248                       &FGViewMgr::getCurrentViewFrame_x);
249   _tiedProperties.Tie("debug/frame-y", this,
250                       &FGViewMgr::getCurrentViewFrame_y);
251   _tiedProperties.Tie("debug/frame-z", this,
252                       &FGViewMgr::getCurrentViewFrame_z);
253 }
254
255 void
256 FGViewMgr::unbind ()
257 {
258   _tiedProperties.Untie();
259     config_list.clear();
260     view_number.clear();
261 }
262
263 void
264 FGViewMgr::update (double dt)
265 {
266     flightgear::View* currentView = get_current_view();
267   if (!currentView) return;
268
269   SGPropertyNode *n = config_list[current];
270   SGPropertyNode *config = n->getChild("config", 0, true);
271   double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
272
273   // Set up view location and orientation
274
275   if (!config->getBoolValue("from-model")) {
276     lon_deg = fgGetDouble(config->getStringValue("eye-lon-deg-path"));
277     lat_deg = fgGetDouble(config->getStringValue("eye-lat-deg-path"));
278     alt_ft = fgGetDouble(config->getStringValue("eye-alt-ft-path"));
279     roll_deg = fgGetDouble(config->getStringValue("eye-roll-deg-path"));
280     pitch_deg = fgGetDouble(config->getStringValue("eye-pitch-deg-path"));
281     heading_deg = fgGetDouble(config->getStringValue("eye-heading-deg-path"));
282
283     currentView->setPosition(lon_deg, lat_deg, alt_ft);
284     currentView->setOrientation(roll_deg, pitch_deg, heading_deg);
285   } else {
286     // force recalc in viewer
287     currentView->set_dirty();
288   }
289
290   // if lookat (type 1) then get target data...
291     if (currentView->getType() == flightgear::View::FG_LOOKAT) {
292     if (!config->getBoolValue("from-model")) {
293       lon_deg = fgGetDouble(config->getStringValue("target-lon-deg-path"));
294       lat_deg = fgGetDouble(config->getStringValue("target-lat-deg-path"));
295       alt_ft = fgGetDouble(config->getStringValue("target-alt-ft-path"));
296       roll_deg = fgGetDouble(config->getStringValue("target-roll-deg-path"));
297       pitch_deg = fgGetDouble(config->getStringValue("target-pitch-deg-path"));
298       heading_deg = fgGetDouble(config->getStringValue("target-heading-deg-path"));
299
300       currentView->setTargetPosition(lon_deg, lat_deg, alt_ft);
301       currentView->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
302     } else {
303       currentView->set_dirty();
304     }
305   }
306
307   setViewXOffset_m(current_x_offs->getDoubleValue());
308   setViewYOffset_m(current_y_offs->getDoubleValue());
309   setViewZOffset_m(current_z_offs->getDoubleValue());
310
311   setViewTargetXOffset_m(target_x_offs->getDoubleValue());
312   setViewTargetYOffset_m(target_y_offs->getDoubleValue());
313   setViewTargetZOffset_m(target_z_offs->getDoubleValue());
314
315   current_view_orientation = currentView->getViewOrientation();
316   current_view_or_offset = currentView->getViewOrientationOffset();
317
318   // Update the current view
319   do_axes();
320   currentView->update(dt);
321   abs_viewer_position = currentView->getViewPosition();
322
323
324 // update the camera now
325     osg::ref_ptr<flightgear::CameraGroup> cameraGroup = flightgear::CameraGroup::getDefault();
326     cameraGroup->update(toOsg(abs_viewer_position),
327                         toOsg(currentView->getViewOrientation()));
328     cameraGroup->setCameraParameters(currentView->get_v_fov(),
329                                      cameraGroup->getMasterAspectRatio());
330
331   // expose the raw (OpenGL) orientation to the property tree,
332   // for the sound-manager
333   for (int i=0; i<4; ++i) {
334     _tiedProperties.getRoot()->getChild("raw-orientation", i, true)->setDoubleValue(current_view_orientation[i]);
335   }
336 }
337
338 void
339 FGViewMgr::copyToCurrent()
340 {
341     if (!inited) {
342         return;
343     }
344   
345     SGPropertyNode *n = config_list[current];
346     fgSetString("/sim/current-view/name", n->getStringValue("name"));
347     fgSetString("/sim/current-view/type", n->getStringValue("type"));
348
349     // copy certain view config data for default values
350     fgSetDouble("/sim/current-view/config/heading-offset-deg",
351                 n->getDoubleValue("config/default-heading-offset-deg"));
352     fgSetDouble("/sim/current-view/config/pitch-offset-deg",
353                 n->getDoubleValue("config/pitch-offset-deg"));
354     fgSetDouble("/sim/current-view/config/roll-offset-deg",
355                 n->getDoubleValue("config/roll-offset-deg"));
356     fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
357                 n->getDoubleValue("config/default-field-of-view-deg"));
358     fgSetBool("/sim/current-view/config/from-model",
359                 n->getBoolValue("config/from-model"));
360
361     // copy view data
362     fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
363     fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
364     fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
365
366     fgSetDouble("/sim/current-view/goal-heading-offset-deg",
367                 get_current_view()->getGoalHeadingOffset_deg());
368     fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
369                 get_current_view()->getGoalPitchOffset_deg());
370     fgSetDouble("/sim/current-view/goal-roll-offset-deg",
371                 get_current_view()->getRollOffset_deg());
372     fgSetDouble("/sim/current-view/heading-offset-deg",
373                 get_current_view()->getHeadingOffset_deg());
374     fgSetDouble("/sim/current-view/pitch-offset-deg",
375                 get_current_view()->getPitchOffset_deg());
376     fgSetDouble("/sim/current-view/roll-offset-deg",
377                 get_current_view()->getRollOffset_deg());
378     fgSetDouble("/sim/current-view/target-x-offset-m",
379                 get_current_view()->getTargetXOffset_m());
380     fgSetDouble("/sim/current-view/target-y-offset-m",
381                 get_current_view()->getTargetYOffset_m());
382     fgSetDouble("/sim/current-view/target-z-offset-m",
383                 get_current_view()->getTargetZOffset_m());
384     fgSetBool("/sim/current-view/internal",
385                 get_current_view()->getInternal());
386 }
387
388 void FGViewMgr::clear()
389 {
390     views.clear();
391 }
392
393 flightgear::View*
394 FGViewMgr::get_current_view()
395 {
396     if ( current < (int)views.size() ) {
397         return views[current];
398     } else {
399         return NULL;
400     }
401 }
402
403 const flightgear::View*
404 FGViewMgr::get_current_view() const
405 {
406     if ( current < (int)views.size() ) {
407         return views[current];
408     } else {
409         return NULL;
410     }
411 }
412
413
414 flightgear::View*
415 FGViewMgr::get_view( int i )
416 {
417     if ( i < 0 ) { i = 0; }
418     if ( i >= (int)views.size() ) { i = views.size() - 1; }
419     return views[i];
420 }
421
422 const flightgear::View*
423 FGViewMgr::get_view( int i ) const
424 {
425     if ( i < 0 ) { i = 0; }
426     if ( i >= (int)views.size() ) { i = views.size() - 1; }
427     return views[i];
428 }
429
430 flightgear::View*
431 FGViewMgr::next_view()
432 {
433     setView((current+1 < (int)views.size()) ? (current + 1) : 0);
434     view_number->fireValueChanged();
435     return views[current];
436 }
437
438 flightgear::View*
439 FGViewMgr::prev_view()
440 {
441     setView((0 < current) ? (current - 1) : (views.size() - 1));
442     view_number->fireValueChanged();
443     return views[current];
444 }
445
446 void
447 FGViewMgr::add_view( flightgear::View * v )
448 {
449   views.push_back(v);
450   v->init();
451 }
452     
453 double
454 FGViewMgr::getViewHeadingOffset_deg () const
455 {
456   const flightgear::View * view = get_current_view();
457   return (view == 0 ? 0 : view->getHeadingOffset_deg());
458 }
459
460 void
461 FGViewMgr::setViewHeadingOffset_deg (double offset)
462 {
463   flightgear::View * view = get_current_view();
464   if (view != 0) {
465     view->setGoalHeadingOffset_deg(offset);
466     view->setHeadingOffset_deg(offset);
467   }
468 }
469
470 double
471 FGViewMgr::getViewGoalHeadingOffset_deg () const
472 {
473   const flightgear::View * view = get_current_view();
474   return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
475 }
476
477 void
478 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
479 {
480   flightgear::View * view = get_current_view();
481   if (view != 0)
482     view->setGoalHeadingOffset_deg(offset);
483 }
484
485 double
486 FGViewMgr::getViewPitchOffset_deg () const
487 {
488   const flightgear::View * view = get_current_view();
489   return (view == 0 ? 0 : view->getPitchOffset_deg());
490 }
491
492 void
493 FGViewMgr::setViewPitchOffset_deg (double tilt)
494 {
495   flightgear::View * view = get_current_view();
496   if (view != 0) {
497     view->setGoalPitchOffset_deg(tilt);
498     view->setPitchOffset_deg(tilt);
499   }
500 }
501
502 double
503 FGViewMgr::getGoalViewPitchOffset_deg () const
504 {
505   const flightgear::View * view = get_current_view();
506   return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
507 }
508
509 void
510 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
511 {
512   flightgear::View * view = get_current_view();
513   if (view != 0)
514     view->setGoalPitchOffset_deg(tilt);
515 }
516
517 double
518 FGViewMgr::getViewRollOffset_deg () const
519 {
520   const flightgear::View * view = get_current_view();
521   return (view == 0 ? 0 : view->getRollOffset_deg());
522 }
523
524 void
525 FGViewMgr::setViewRollOffset_deg (double tilt)
526 {
527   flightgear::View * view = get_current_view();
528   if (view != 0) {
529     view->setGoalRollOffset_deg(tilt);
530     view->setRollOffset_deg(tilt);
531   }
532 }
533
534 double
535 FGViewMgr::getGoalViewRollOffset_deg () const
536 {
537   const flightgear::View * view = get_current_view();
538   return (view == 0 ? 0 : view->getGoalRollOffset_deg());
539 }
540
541 void
542 FGViewMgr::setGoalViewRollOffset_deg (double tilt)
543 {
544   flightgear::View * view = get_current_view();
545   if (view != 0)
546     view->setGoalRollOffset_deg(tilt);
547 }
548
549 double
550 FGViewMgr::getViewXOffset_m () const
551 {
552   const flightgear::View * view = get_current_view();
553   if (view != 0) {
554     return ((flightgear::View *)view)->getXOffset_m();
555   } else {
556     return 0;
557   }
558 }
559
560 void
561 FGViewMgr::setViewXOffset_m (double x)
562 {
563   flightgear::View * view = get_current_view();
564   if (view != 0) {
565     view->setXOffset_m(x);
566   }
567 }
568
569 double
570 FGViewMgr::getViewYOffset_m () const
571 {
572   const flightgear::View * view = get_current_view();
573   if (view != 0) {
574     return ((flightgear::View *)view)->getYOffset_m();
575   } else {
576     return 0;
577   }
578 }
579
580 void
581 FGViewMgr::setViewYOffset_m (double y)
582 {
583   flightgear::View * view = get_current_view();
584   if (view != 0) {
585     view->setYOffset_m(y);
586   }
587 }
588
589 double
590 FGViewMgr::getViewZOffset_m () const
591 {
592   const flightgear::View * view = get_current_view();
593   if (view != 0) {
594     return ((flightgear::View *)view)->getZOffset_m();
595   } else {
596     return 0;
597   }
598 }
599
600 void
601 FGViewMgr::setViewZOffset_m (double z)
602 {
603   flightgear::View * view = get_current_view();
604   if (view != 0) {
605     view->setZOffset_m(z);
606   }
607 }
608
609 double
610 FGViewMgr::getViewTargetXOffset_m () const
611 {
612   const flightgear::View * view = get_current_view();
613   if (view != 0) {
614     return ((flightgear::View *)view)->getTargetXOffset_m();
615   } else {
616     return 0;
617   }
618 }
619
620 void
621 FGViewMgr::setViewTargetXOffset_m (double x)
622 {
623   flightgear::View * view = get_current_view();
624   if (view != 0) {
625     view->setTargetXOffset_m(x);
626   }
627 }
628
629 double
630 FGViewMgr::getViewTargetYOffset_m () const
631 {
632   const flightgear::View * view = get_current_view();
633   if (view != 0) {
634     return ((flightgear::View *)view)->getTargetYOffset_m();
635   } else {
636     return 0;
637   }
638 }
639
640 void
641 FGViewMgr::setViewTargetYOffset_m (double y)
642 {
643   flightgear::View * view = get_current_view();
644   if (view != 0) {
645     view->setTargetYOffset_m(y);
646   }
647 }
648
649 double
650 FGViewMgr::getViewTargetZOffset_m () const
651 {
652   const flightgear::View * view = get_current_view();
653   if (view != 0) {
654     return ((flightgear::View *)view)->getTargetZOffset_m();
655   } else {
656     return 0;
657   }
658 }
659
660 void
661 FGViewMgr::setViewTargetZOffset_m (double z)
662 {
663   flightgear::View * view = get_current_view();
664   if (view != 0) {
665     view->setTargetZOffset_m(z);
666   }
667 }
668
669 int
670 FGViewMgr::getView () const
671 {
672   return ( current );
673 }
674
675 void
676 FGViewMgr::setView (int newview)
677 {
678   // negative numbers -> set view with node index -newview
679   if (newview < 0) {
680     for (int i = 0; i < (int)config_list.size(); i++) {
681       int index = -config_list[i]->getIndex();
682       if (index == newview)
683         newview = i;
684     }
685     if (newview < 0)
686       return;
687   }
688
689   // if newview number too low wrap to last view...
690   if (newview < 0)
691     newview = (int)views.size() - 1;
692
693   // if newview number to high wrap to zero...
694   if (newview >= (int)views.size())
695     newview = 0;
696
697   // set new view
698   current = newview;
699   // copy in view data
700   copyToCurrent();
701
702     // force an update now, to avoid returning bogus data.
703     // real fix would to be make all the accessors use the dirty mechanism
704     // on FGViewer, so update() is a no-op.
705     update(0.0);
706 }
707
708
709 double
710 FGViewMgr::getFOV_deg () const
711 {
712   const flightgear::View * view = get_current_view();
713   return (view == 0 ? 0 : view->get_fov());
714 }
715
716 void
717 FGViewMgr::setFOV_deg (double fov)
718 {
719   flightgear::View * view = get_current_view();
720   if (view != 0)
721     view->set_fov(fov);
722 }
723
724 double
725 FGViewMgr::getARM_deg () const
726 {
727   const flightgear::View * view = get_current_view();
728   return (view == 0 ? 0 : view->get_aspect_ratio_multiplier());
729 }
730
731 void
732 FGViewMgr::setARM_deg (double aspect_ratio_multiplier)
733 {  
734   flightgear::View * view = get_current_view();
735   if (view != 0)
736     view->set_aspect_ratio_multiplier(aspect_ratio_multiplier);
737 }
738
739 double
740 FGViewMgr::getNear_m () const
741 {
742   const flightgear::View * view = get_current_view();
743   return (view == 0 ? 0.5f : view->getNear_m());
744 }
745
746 void
747 FGViewMgr::setNear_m (double near_m)
748 {
749   flightgear::View * view = get_current_view();
750   if (view != 0)
751     view->setNear_m(near_m);
752 }
753
754 void
755 FGViewMgr::setViewAxisLong (double axis)
756 {
757   axis_long = axis;
758 }
759
760 void
761 FGViewMgr::setViewAxisLat (double axis)
762 {
763   axis_lat = axis;
764 }
765
766 double
767 FGViewMgr::getViewLon_deg() const
768 {
769   const flightgear::View* view = get_current_view();
770   return (view != NULL) ? view->getPosition().getLongitudeDeg() : 0.0;
771 }
772
773 double
774 FGViewMgr::getViewLat_deg() const
775 {
776   const flightgear::View* view = get_current_view();
777   return (view != NULL) ? view->getPosition().getLatitudeDeg() : 0.0;
778 }
779
780 double
781 FGViewMgr::getViewElev_ft() const
782 {
783   const flightgear::View* view = get_current_view();
784   return (view != NULL) ? view->getPosition().getElevationFt() : 0.0;
785 }
786
787
788 // reference frame orientation.
789 // This is the view orientation you get when you have no
790 // view offset, i.e. the offset operator is the identity.
791 // 
792 // For example, in the familiar "cockpit lookfrom" view,
793 // the reference frame is equal to the aircraft attitude,
794 // i.e. it is the view looking towards 12:00 straight ahead.
795 //
796 // FIXME:  Somebody needs to figure out what is the reference
797 // frame view for the other view modes.
798 // 
799 // Conceptually, this quat represents a rotation relative
800 // to the ECEF reference orientation, as described at
801 //    http://www.av8n.com/physics/coords.htm#sec-orientation
802 //
803 // See the NOTE concerning reference orientations, below.
804 //
805 // The components of this quat are expressed in 
806 // the conventional aviation basis set,
807 // i.e.  x=forward, y=starboard, z=bottom
808 double FGViewMgr::getCurrentViewFrame_w() const{
809   return ((current_view_orientation*conj(fsb2sta())*conj(current_view_or_offset))).w();
810 }
811 double FGViewMgr::getCurrentViewFrame_x() const{
812   return ((current_view_orientation*conj(fsb2sta())*conj(current_view_or_offset))).x();
813 }
814 double FGViewMgr::getCurrentViewFrame_y() const{
815   return ((current_view_orientation*conj(fsb2sta())*conj(current_view_or_offset))).y();
816 }
817 double FGViewMgr::getCurrentViewFrame_z() const{
818   return ((current_view_orientation*conj(fsb2sta())*conj(current_view_or_offset))).z();
819 }
820
821
822 // view offset.
823 // This rotation takes you from the aforementioned
824 // reference frame view orientation to whatever
825 // actual current view orientation is.
826 //
827 // The components of this quaternion are expressed in 
828 // the conventional aviation basis set,
829 // i.e.  x=forward, y=starboard, z=bottom
830 double FGViewMgr::getCurrentViewOrOffset_w() const{
831    return current_view_or_offset.w();
832 }
833 double FGViewMgr::getCurrentViewOrOffset_x() const{
834    return current_view_or_offset.x();
835 }
836 double FGViewMgr::getCurrentViewOrOffset_y() const{
837    return current_view_or_offset.y();
838 }
839 double FGViewMgr::getCurrentViewOrOffset_z() const{
840    return current_view_or_offset.z();
841 }
842
843
844 // current view orientation.
845 // This is a rotation relative to the earth-centered (ec)
846 // reference frame.
847 // 
848 // NOTE: Here we remove a factor of fsb2sta so that 
849 // the components of this quat are displayed using the 
850 // conventional ECEF basis set.  This is *not* the way
851 // the view orientation is stored in the views[] array,
852 // but is easier for non-graphics hackers to understand.
853 // If we did not remove this factor of fsb2sta here and
854 // in getCurrentViewFrame, that would be equivalent to
855 // the following peculiar reference orientation:
856 // Suppose you are over the Gulf of Guinea, at (lat,lon) = (0,0).
857 // Then the reference frame orientation can be achieved via:
858 //    -- The aircraft X-axis (nose) headed south.
859 //    -- The aircraft Y-axis (starboard wingtip) pointing up.
860 //    -- The aircraft Z-axis (belly) pointing west.
861 // To say the same thing in other words, and perhaps more to the
862 // point:  If we use the OpenGL camera orientation conventions, 
863 // i.e. Xprime=starboard, Yprime=top, Zprime=aft, then the
864 // aforementioned peculiar reference orientation at (lat,lon)
865 //  = (0,0) can be described as:
866 //    -- aircraft Xprime axis (starboard) pointed up
867 //    -- aircraft Yprime axis (top) pointed east
868 //    -- aircraft Zprime axis (aft) pointed north
869 // meaning the OpenGL axes are aligned with the ECEF axes.
870 double FGViewMgr::getCurrentViewOrientation_w() const{
871   return (current_view_orientation * conj(fsb2sta())).w();
872 }
873 double FGViewMgr::getCurrentViewOrientation_x() const{
874   return (current_view_orientation * conj(fsb2sta())).x();
875 }
876 double FGViewMgr::getCurrentViewOrientation_y() const{
877   return (current_view_orientation * conj(fsb2sta())).y();
878 }
879 double FGViewMgr::getCurrentViewOrientation_z() const{
880   return (current_view_orientation * conj(fsb2sta())).z();
881 }
882
883 void
884 FGViewMgr::do_axes ()
885 {
886                 // Take no action when hat is centered
887   if ( ( axis_long <  0.01 ) &&
888        ( axis_long > -0.01 ) &&
889        ( axis_lat  <  0.01 ) &&
890        ( axis_lat  > -0.01 )
891      )
892     return;
893
894   double viewDir = 999;
895
896   /* Do all the quick and easy cases */
897   if (axis_long < 0) {        // Longitudinal axis forward
898     if (axis_lat == axis_long)
899       viewDir = fgGetDouble("/sim/view/config/front-left-direction-deg");
900     else if (axis_lat == - axis_long)
901       viewDir = fgGetDouble("/sim/view/config/front-right-direction-deg");
902     else if (axis_lat == 0)
903       viewDir = fgGetDouble("/sim/view/config/front-direction-deg");
904   } else if (axis_long > 0) {    // Longitudinal axis backward
905     if (axis_lat == - axis_long)
906       viewDir = fgGetDouble("/sim/view/config/back-left-direction-deg");
907     else if (axis_lat == axis_long)
908       viewDir = fgGetDouble("/sim/view/config/back-right-direction-deg");
909     else if (axis_lat == 0)
910       viewDir = fgGetDouble("/sim/view/config/back-direction-deg");
911   } else if (axis_long == 0) {    // Longitudinal axis neutral
912     if (axis_lat < 0)
913       viewDir = fgGetDouble("/sim/view/config/left-direction-deg");
914     else if (axis_lat > 0)
915       viewDir = fgGetDouble("/sim/view/config/right-direction-deg");
916     else return; /* And assertion failure maybe? */
917   }
918
919                 // Do all the difficult cases
920   if ( viewDir > 900 )
921     viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
922   if ( viewDir < -1 ) viewDir += 360;
923
924   get_current_view()->setGoalHeadingOffset_deg(viewDir);
925 }