]> git.mxchange.org Git - flightgear.git/blob - src/Viewer/viewmgr.cxx
commradio: improvements for atis speech
[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 <Main/fg_props.hxx>
34 #include "viewer.hxx"
35
36 // Constructor
37 FGViewMgr::FGViewMgr( void ) :
38   axis_long(0),
39   axis_lat(0),
40   inited(false),
41   view_number(fgGetNode("/sim/current-view/view-number", true)),
42   config_list(fgGetNode("/sim", true)->getChildren("view")),
43   abs_viewer_position(SGVec3d::zeros()),
44   current(0),
45   current_view_orientation(SGQuatd::zeros()),
46   current_view_or_offset(SGQuatd::zeros())
47 {
48   globals->set_viewmgr(this);
49 }
50
51 // Destructor
52 FGViewMgr::~FGViewMgr( void )
53 {
54   globals->set_viewmgr(NULL);
55 }
56
57 void
58 FGViewMgr::init ()
59 {
60   if (inited) {
61     SG_LOG(SG_VIEW, SG_WARN, "duplicate init of view manager");
62     return;
63   }
64   
65   inited = true;
66   
67   // stash properties
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);
74
75   double aspect_ratio_multiplier
76       = fgGetDouble("/sim/current-view/aspect-ratio-multiplier");
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     // find out if this is an internal view (e.g. in cockpit, low near plane)
83     bool internal = n->getBoolValue("internal", false);
84
85     // FIXME:
86     // this is assumed to be an aircraft model...we will need to read
87     // model-from-type as well.
88
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");
92
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");
96
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);
103
104     double fov_deg = config->getDoubleValue("default-field-of-view-deg");
105     double near_m = config->getDoubleValue("ground-level-nearplane-m");
106
107     // supporting two types "lookat" = 1 and "lookfrom" = 0
108     const char *type = n->getStringValue("type");
109     if (!strcmp(type, "lookat")) {
110
111       bool at_model = config->getBoolValue("at-model");
112       int at_model_index = config->getIntValue("at-model-idx");
113
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");
117
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");
121
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 ));
130     } else {
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 ));
137     }
138   }
139
140   copyToCurrent();
141   do_bind();
142 }
143
144 void
145 FGViewMgr::shutdown()
146 {
147     if (!inited) {
148         return;
149     }
150     
151     inited = false;
152 }
153
154 void
155 FGViewMgr::reinit ()
156 {
157   int current_view_index = current;
158
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];
162     setView(i);
163
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"));
176
177     double fov_deg = n->getDoubleValue("config/default-field-of-view-deg");
178     if (fov_deg < 10.0)
179       fov_deg = 55.0;
180     fgSetDouble("/sim/current-view/field-of-view", fov_deg);
181
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"));
189   }
190   setView(current_view_index);
191 }
192
193 typedef double (FGViewMgr::*double_getter)() const;
194
195 void
196 FGViewMgr::bind()
197 {
198   // view-manager code was designed to init before bind, so
199   // this is a no-op; init() calls the real bind() impl below
200 }
201
202 void
203 FGViewMgr::do_bind()
204 {  
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");
231
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);
237
238   // Keep view on reset/reinit
239   view_number->setAttribute(SGPropertyNode::PRESERVE, true);
240
241   _tiedProperties.Tie("axes/long", this,
242                       (double_getter)0, &FGViewMgr::setViewAxisLong);
243   fgSetArchivable("/sim/current-view/axes/long");
244
245   _tiedProperties.Tie("axes/lat", this,
246                       (double_getter)0, &FGViewMgr::setViewAxisLat);
247   fgSetArchivable("/sim/current-view/axes/lat");
248
249   _tiedProperties.Tie("field-of-view", this,
250                       &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
251   fgSetArchivable("/sim/current-view/field-of-view");
252
253   _tiedProperties.Tie("aspect-ratio-multiplier", this,
254                       &FGViewMgr::getARM_deg, &FGViewMgr::setARM_deg);
255   fgSetArchivable("/sim/current-view/field-of-view");
256
257   _tiedProperties.Tie("ground-level-nearplane-m", this,
258                       &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
259   fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
260
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]));
265
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);
269   
270   
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);
279
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);
288
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);
297 }
298
299 void
300 FGViewMgr::unbind ()
301 {
302   _tiedProperties.Untie();
303     config_list.clear();
304     view_number.clear();
305 }
306
307 void
308 FGViewMgr::update (double dt)
309 {
310   FGViewer* currentView = (FGViewer *)get_current_view();
311   if (!currentView) return;
312
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;
316
317   // Set up view location and orientation
318
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"));
326
327     currentView->setPosition(lon_deg, lat_deg, alt_ft);
328     currentView->setOrientation(roll_deg, pitch_deg, heading_deg);
329   } else {
330     // force recalc in viewer
331     currentView->set_dirty();
332   }
333
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"));
343
344       currentView->setTargetPosition(lon_deg, lat_deg, alt_ft);
345       currentView->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
346     } else {
347       currentView->set_dirty();
348     }
349   }
350
351   setViewXOffset_m(current_x_offs->getDoubleValue());
352   setViewYOffset_m(current_y_offs->getDoubleValue());
353   setViewZOffset_m(current_z_offs->getDoubleValue());
354
355   setViewTargetXOffset_m(target_x_offs->getDoubleValue());
356   setViewTargetYOffset_m(target_y_offs->getDoubleValue());
357   setViewTargetZOffset_m(target_z_offs->getDoubleValue());
358
359   current_view_orientation = currentView->getViewOrientation();
360   current_view_or_offset = currentView->getViewOrientationOffset();
361
362   // Update the current view
363   do_axes();
364   currentView->update(dt);
365   abs_viewer_position = currentView->getViewPosition();
366   
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]);
371   }
372 }
373
374 void
375 FGViewMgr::copyToCurrent()
376 {
377     if (!inited) {
378         return;
379     }
380   
381     SGPropertyNode *n = config_list[current];
382     fgSetString("/sim/current-view/name", n->getStringValue("name"));
383     fgSetString("/sim/current-view/type", n->getStringValue("type"));
384
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"));
396
397     // copy view data
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());
401
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());
422 }
423
424 void FGViewMgr::clear()
425 {
426     views.clear();
427 }
428
429 FGViewer*
430 FGViewMgr::get_current_view()
431 {
432     if ( current < (int)views.size() ) {
433         return views[current];
434     } else {
435         return NULL;
436     }
437 }
438
439 const FGViewer*
440 FGViewMgr::get_current_view() const
441 {
442     if ( current < (int)views.size() ) {
443         return views[current];
444     } else {
445         return NULL;
446     }
447 }
448
449
450 FGViewer*
451 FGViewMgr::get_view( int i )
452 {
453     if ( i < 0 ) { i = 0; }
454     if ( i >= (int)views.size() ) { i = views.size() - 1; }
455     return views[i];
456 }
457
458 const FGViewer*
459 FGViewMgr::get_view( int i ) const
460 {
461     if ( i < 0 ) { i = 0; }
462     if ( i >= (int)views.size() ) { i = views.size() - 1; }
463     return views[i];
464 }
465
466 FGViewer*
467 FGViewMgr::next_view()
468 {
469     setView((current+1 < (int)views.size()) ? (current + 1) : 0);
470     view_number->fireValueChanged();
471     return views[current];
472 }
473
474 FGViewer*
475 FGViewMgr::prev_view()
476 {
477     setView((0 < current) ? (current - 1) : (views.size() - 1));
478     view_number->fireValueChanged();
479     return views[current];
480 }
481
482 void
483 FGViewMgr::add_view( FGViewer * v )
484 {
485   views.push_back(v);
486   v->init();
487 }
488     
489 double
490 FGViewMgr::getViewHeadingOffset_deg () const
491 {
492   const FGViewer * view = get_current_view();
493   return (view == 0 ? 0 : view->getHeadingOffset_deg());
494 }
495
496 void
497 FGViewMgr::setViewHeadingOffset_deg (double offset)
498 {
499   FGViewer * view = get_current_view();
500   if (view != 0) {
501     view->setGoalHeadingOffset_deg(offset);
502     view->setHeadingOffset_deg(offset);
503   }
504 }
505
506 double
507 FGViewMgr::getViewGoalHeadingOffset_deg () const
508 {
509   const FGViewer * view = get_current_view();
510   return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
511 }
512
513 void
514 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
515 {
516   FGViewer * view = get_current_view();
517   if (view != 0)
518     view->setGoalHeadingOffset_deg(offset);
519 }
520
521 double
522 FGViewMgr::getViewPitchOffset_deg () const
523 {
524   const FGViewer * view = get_current_view();
525   return (view == 0 ? 0 : view->getPitchOffset_deg());
526 }
527
528 void
529 FGViewMgr::setViewPitchOffset_deg (double tilt)
530 {
531   FGViewer * view = get_current_view();
532   if (view != 0) {
533     view->setGoalPitchOffset_deg(tilt);
534     view->setPitchOffset_deg(tilt);
535   }
536 }
537
538 double
539 FGViewMgr::getGoalViewPitchOffset_deg () const
540 {
541   const FGViewer * view = get_current_view();
542   return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
543 }
544
545 void
546 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
547 {
548   FGViewer * view = get_current_view();
549   if (view != 0)
550     view->setGoalPitchOffset_deg(tilt);
551 }
552
553 double
554 FGViewMgr::getViewRollOffset_deg () const
555 {
556   const FGViewer * view = get_current_view();
557   return (view == 0 ? 0 : view->getRollOffset_deg());
558 }
559
560 void
561 FGViewMgr::setViewRollOffset_deg (double tilt)
562 {
563   FGViewer * view = get_current_view();
564   if (view != 0) {
565     view->setGoalRollOffset_deg(tilt);
566     view->setRollOffset_deg(tilt);
567   }
568 }
569
570 double
571 FGViewMgr::getGoalViewRollOffset_deg () const
572 {
573   const FGViewer * view = get_current_view();
574   return (view == 0 ? 0 : view->getGoalRollOffset_deg());
575 }
576
577 void
578 FGViewMgr::setGoalViewRollOffset_deg (double tilt)
579 {
580   FGViewer * view = get_current_view();
581   if (view != 0)
582     view->setGoalRollOffset_deg(tilt);
583 }
584
585 double
586 FGViewMgr::getViewXOffset_m () const
587 {
588   const FGViewer * view = get_current_view();
589   if (view != 0) {
590     return ((FGViewer *)view)->getXOffset_m();
591   } else {
592     return 0;
593   }
594 }
595
596 void
597 FGViewMgr::setViewXOffset_m (double x)
598 {
599   FGViewer * view = get_current_view();
600   if (view != 0) {
601     view->setXOffset_m(x);
602   }
603 }
604
605 double
606 FGViewMgr::getViewYOffset_m () const
607 {
608   const FGViewer * view = get_current_view();
609   if (view != 0) {
610     return ((FGViewer *)view)->getYOffset_m();
611   } else {
612     return 0;
613   }
614 }
615
616 void
617 FGViewMgr::setViewYOffset_m (double y)
618 {
619   FGViewer * view = get_current_view();
620   if (view != 0) {
621     view->setYOffset_m(y);
622   }
623 }
624
625 double
626 FGViewMgr::getViewZOffset_m () const
627 {
628   const FGViewer * view = get_current_view();
629   if (view != 0) {
630     return ((FGViewer *)view)->getZOffset_m();
631   } else {
632     return 0;
633   }
634 }
635
636 void
637 FGViewMgr::setViewZOffset_m (double z)
638 {
639   FGViewer * view = get_current_view();
640   if (view != 0) {
641     view->setZOffset_m(z);
642   }
643 }
644
645 double
646 FGViewMgr::getViewTargetXOffset_m () const
647 {
648   const FGViewer * view = get_current_view();
649   if (view != 0) {
650     return ((FGViewer *)view)->getTargetXOffset_m();
651   } else {
652     return 0;
653   }
654 }
655
656 void
657 FGViewMgr::setViewTargetXOffset_m (double x)
658 {
659   FGViewer * view = get_current_view();
660   if (view != 0) {
661     view->setTargetXOffset_m(x);
662   }
663 }
664
665 double
666 FGViewMgr::getViewTargetYOffset_m () const
667 {
668   const FGViewer * view = get_current_view();
669   if (view != 0) {
670     return ((FGViewer *)view)->getTargetYOffset_m();
671   } else {
672     return 0;
673   }
674 }
675
676 void
677 FGViewMgr::setViewTargetYOffset_m (double y)
678 {
679   FGViewer * view = get_current_view();
680   if (view != 0) {
681     view->setTargetYOffset_m(y);
682   }
683 }
684
685 double
686 FGViewMgr::getViewTargetZOffset_m () const
687 {
688   const FGViewer * view = get_current_view();
689   if (view != 0) {
690     return ((FGViewer *)view)->getTargetZOffset_m();
691   } else {
692     return 0;
693   }
694 }
695
696 void
697 FGViewMgr::setViewTargetZOffset_m (double z)
698 {
699   FGViewer * view = get_current_view();
700   if (view != 0) {
701     view->setTargetZOffset_m(z);
702   }
703 }
704
705 int
706 FGViewMgr::getView () const
707 {
708   return ( current );
709 }
710
711 void
712 FGViewMgr::setView (int newview)
713 {
714   // negative numbers -> set view with node index -newview
715   if (newview < 0) {
716     for (int i = 0; i < (int)config_list.size(); i++) {
717       int index = -config_list[i]->getIndex();
718       if (index == newview)
719         newview = i;
720     }
721     if (newview < 0)
722       return;
723   }
724
725   // if newview number too low wrap to last view...
726   if (newview < 0)
727     newview = (int)views.size() - 1;
728
729   // if newview number to high wrap to zero...
730   if (newview >= (int)views.size())
731     newview = 0;
732
733   // set new view
734   current = newview;
735   // copy in view data
736   copyToCurrent();
737 }
738
739
740 double
741 FGViewMgr::getFOV_deg () const
742 {
743   const FGViewer * view = get_current_view();
744   return (view == 0 ? 0 : view->get_fov());
745 }
746
747 void
748 FGViewMgr::setFOV_deg (double fov)
749 {
750   FGViewer * view = get_current_view();
751   if (view != 0)
752     view->set_fov(fov);
753 }
754
755 double
756 FGViewMgr::getARM_deg () const
757 {
758   const FGViewer * view = get_current_view();
759   return (view == 0 ? 0 : view->get_aspect_ratio_multiplier());
760 }
761
762 void
763 FGViewMgr::setARM_deg (double aspect_ratio_multiplier)
764 {  
765   FGViewer * view = get_current_view();
766   if (view != 0)
767     view->set_aspect_ratio_multiplier(aspect_ratio_multiplier);
768 }
769
770 double
771 FGViewMgr::getNear_m () const
772 {
773   const FGViewer * view = get_current_view();
774   return (view == 0 ? 0.5f : view->getNear_m());
775 }
776
777 void
778 FGViewMgr::setNear_m (double near_m)
779 {
780   FGViewer * view = get_current_view();
781   if (view != 0)
782     view->setNear_m(near_m);
783 }
784
785 void
786 FGViewMgr::setViewAxisLong (double axis)
787 {
788   axis_long = axis;
789 }
790
791 void
792 FGViewMgr::setViewAxisLat (double axis)
793 {
794   axis_lat = axis;
795 }
796
797 double
798 FGViewMgr::getViewLon_deg() const
799 {
800   const FGViewer* view = get_current_view();
801   return (view != NULL) ? view->getPosition().getLongitudeDeg() : 0.0;
802 }
803
804 double
805 FGViewMgr::getViewLat_deg() const
806 {
807   const FGViewer* view = get_current_view();
808   return (view != NULL) ? view->getPosition().getLatitudeDeg() : 0.0;
809 }
810
811 double
812 FGViewMgr::getViewElev_ft() const
813 {
814   const FGViewer* view = get_current_view();
815   return (view != NULL) ? view->getPosition().getElevationFt() : 0.0;
816 }
817
818
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.
822 // 
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.
826 //
827 // FIXME:  Somebody needs to figure out what is the reference
828 // frame view for the other view modes.
829 // 
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
833 //
834 // See the NOTE concerning reference orientations, below.
835 //
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();
841 }
842 double FGViewMgr::getCurrentViewFrame_x() const{
843   return ((current_view_orientation*conj(fsb2sta())*conj(current_view_or_offset))).x();
844 }
845 double FGViewMgr::getCurrentViewFrame_y() const{
846   return ((current_view_orientation*conj(fsb2sta())*conj(current_view_or_offset))).y();
847 }
848 double FGViewMgr::getCurrentViewFrame_z() const{
849   return ((current_view_orientation*conj(fsb2sta())*conj(current_view_or_offset))).z();
850 }
851
852
853 // view offset.
854 // This rotation takes you from the aforementioned
855 // reference frame view orientation to whatever
856 // actual current view orientation is.
857 //
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();
863 }
864 double FGViewMgr::getCurrentViewOrOffset_x() const{
865    return current_view_or_offset.x();
866 }
867 double FGViewMgr::getCurrentViewOrOffset_y() const{
868    return current_view_or_offset.y();
869 }
870 double FGViewMgr::getCurrentViewOrOffset_z() const{
871    return current_view_or_offset.z();
872 }
873
874
875 // current view orientation.
876 // This is a rotation relative to the earth-centered (ec)
877 // reference frame.
878 // 
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();
903 }
904 double FGViewMgr::getCurrentViewOrientation_x() const{
905   return (current_view_orientation * conj(fsb2sta())).x();
906 }
907 double FGViewMgr::getCurrentViewOrientation_y() const{
908   return (current_view_orientation * conj(fsb2sta())).y();
909 }
910 double FGViewMgr::getCurrentViewOrientation_z() const{
911   return (current_view_orientation * conj(fsb2sta())).z();
912 }
913
914 void
915 FGViewMgr::do_axes ()
916 {
917                 // Take no action when hat is centered
918   if ( ( axis_long <  0.01 ) &&
919        ( axis_long > -0.01 ) &&
920        ( axis_lat  <  0.01 ) &&
921        ( axis_lat  > -0.01 )
922      )
923     return;
924
925   double viewDir = 999;
926
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
943     if (axis_lat < 0)
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? */
948   }
949
950                 // Do all the difficult cases
951   if ( viewDir > 900 )
952     viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
953   if ( viewDir < -1 ) viewDir += 360;
954
955   get_current_view()->setGoalHeadingOffset_deg(viewDir);
956 }