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