]> git.mxchange.org Git - flightgear.git/blob - src/Viewer/viewmgr.cxx
Kill off some globals.
[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/sound/soundmgr_openal.hxx>
34 #include <Main/fg_props.hxx>
35 #include "viewer.hxx"
36
37 // Constructor
38 FGViewMgr::FGViewMgr( void ) :
39   axis_long(0),
40   axis_lat(0),
41   inited(false),
42   view_number(fgGetNode("/sim/current-view/view-number", true)),
43   config_list(fgGetNode("/sim", true)->getChildren("view")),
44   abs_viewer_position(SGVec3d::zeros()),
45   current(0),
46   current_view_orientation(SGQuatd::zeros()),
47   current_view_or_offset(SGQuatd::zeros()),
48   smgr(globals->get_soundmgr())
49 {
50 }
51
52 // Destructor
53 FGViewMgr::~FGViewMgr( void ) {
54 }
55
56 void
57 FGViewMgr::init ()
58 {
59   if (inited) {
60     SG_LOG(SG_VIEW, SG_WARN, "duplicate init of view manager");
61     return;
62   }
63   
64   inited = true;
65   
66   double aspect_ratio_multiplier
67       = fgGetDouble("/sim/current-view/aspect-ratio-multiplier");
68
69   for (unsigned int i = 0; i < config_list.size(); i++) {
70     SGPropertyNode *n = config_list[i];
71
72     // find out if this is an internal view (e.g. in cockpit, low near plane)
73     bool internal = n->getBoolValue("internal", false);
74
75     // FIXME:
76     // this is assumed to be an aircraft model...we will need to read
77     // model-from-type as well.
78
79     // find out if this is a model we are looking from...
80     bool from_model = n->getBoolValue("config/from-model");
81     int from_model_index = n->getIntValue("config/from-model-idx");
82
83     double x_offset_m = n->getDoubleValue("config/x-offset-m");
84     double y_offset_m = n->getDoubleValue("config/y-offset-m");
85     double z_offset_m = n->getDoubleValue("config/z-offset-m");
86
87     double heading_offset_deg = n->getDoubleValue("config/heading-offset-deg");
88     n->setDoubleValue("config/heading-offset-deg", heading_offset_deg);
89     double pitch_offset_deg = n->getDoubleValue("config/pitch-offset-deg");
90     n->setDoubleValue("config/pitch-offset-deg", pitch_offset_deg);
91     double roll_offset_deg = n->getDoubleValue("config/roll-offset-deg");
92     n->setDoubleValue("config/roll-offset-deg", roll_offset_deg);
93
94     double fov_deg = n->getDoubleValue("config/default-field-of-view-deg");
95     double near_m = n->getDoubleValue("config/ground-level-nearplane-m");
96
97     // supporting two types "lookat" = 1 and "lookfrom" = 0
98     const char *type = n->getStringValue("type");
99     if (!strcmp(type, "lookat")) {
100
101       bool at_model = n->getBoolValue("config/at-model");
102       int at_model_index = n->getIntValue("config/at-model-idx");
103
104       double damp_roll = n->getDoubleValue("config/at-model-roll-damping");
105       double damp_pitch = n->getDoubleValue("config/at-model-pitch-damping");
106       double damp_heading = n->getDoubleValue("config/at-model-heading-damping");
107
108       double target_x_offset_m = n->getDoubleValue("config/target-x-offset-m");
109       double target_y_offset_m = n->getDoubleValue("config/target-y-offset-m");
110       double target_z_offset_m = n->getDoubleValue("config/target-z-offset-m");
111
112       add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
113                               at_model, at_model_index,
114                               damp_roll, damp_pitch, damp_heading,
115                               x_offset_m, y_offset_m,z_offset_m,
116                               heading_offset_deg, pitch_offset_deg,
117                               roll_offset_deg, fov_deg, aspect_ratio_multiplier,
118                               target_x_offset_m, target_y_offset_m,
119                               target_z_offset_m, near_m, internal ));
120     } else {
121       add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index,
122                               false, 0, 0.0, 0.0, 0.0,
123                               x_offset_m, y_offset_m, z_offset_m,
124                               heading_offset_deg, pitch_offset_deg,
125                               roll_offset_deg, fov_deg, aspect_ratio_multiplier,
126                               0, 0, 0, near_m, internal ));
127     }
128   }
129
130   copyToCurrent();
131   do_bind();
132 }
133
134 void
135 FGViewMgr::reinit ()
136 {
137   // reset offsets and fov to configuration defaults
138   for (unsigned int i = 0; i < config_list.size(); i++) {
139     SGPropertyNode *n = config_list[i];
140     setView(i);
141
142     fgSetDouble("/sim/current-view/x-offset-m",
143         n->getDoubleValue("config/x-offset-m"));
144     fgSetDouble("/sim/current-view/y-offset-m",
145         n->getDoubleValue("config/y-offset-m"));
146     fgSetDouble("/sim/current-view/z-offset-m",
147         n->getDoubleValue("config/z-offset-m"));
148     fgSetDouble("/sim/current-view/pitch-offset-deg",
149         n->getDoubleValue("config/pitch-offset-deg"));
150     fgSetDouble("/sim/current-view/heading-offset-deg",
151         n->getDoubleValue("config/heading-offset-deg"));
152     fgSetDouble("/sim/current-view/roll-offset-deg",
153         n->getDoubleValue("config/roll-offset-deg"));
154
155     double fov_deg = n->getDoubleValue("config/default-field-of-view-deg");
156     if (fov_deg < 10.0)
157       fov_deg = 55.0;
158     fgSetDouble("/sim/current-view/field-of-view", fov_deg);
159
160     // target offsets for lookat mode only...
161     fgSetDouble("/sim/current-view/target-x-offset-m",
162         n->getDoubleValue("config/target-x-offset-m"));
163     fgSetDouble("/sim/current-view/target-y-offset-m",
164         n->getDoubleValue("config/target-y-offset-m"));
165     fgSetDouble("/sim/current-view/target-z-offset-m",
166         n->getDoubleValue("config/target-z-offset-m"));
167   }
168   setView(0);
169 }
170
171 typedef double (FGViewMgr::*double_getter)() const;
172
173 void
174 FGViewMgr::bind()
175 {
176   // view-manager code was designed to init before bind, so
177   // this is a no-op; init() calls the real bind() impl below
178 }
179
180 void
181 FGViewMgr::do_bind()
182 {
183   velocityNorthFPS = fgGetNode("velocities/speed-north-fps", true);
184   velocityEastFPS = fgGetNode("velocities/speed-east-fps", true);
185   velocityDownFPS = fgGetNode("velocities/speed-down-fps", true);
186   
187   // these are bound to the current view properties
188   _tiedProperties.setRoot(fgGetNode("/sim/current-view", true));
189   _tiedProperties.Tie("heading-offset-deg", this,
190                       &FGViewMgr::getViewHeadingOffset_deg,
191                       &FGViewMgr::setViewHeadingOffset_deg);
192   fgSetArchivable("/sim/current-view/heading-offset-deg");
193   _tiedProperties.Tie("goal-heading-offset-deg", this,
194                       &FGViewMgr::getViewGoalHeadingOffset_deg,
195                       &FGViewMgr::setViewGoalHeadingOffset_deg);
196   fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
197   _tiedProperties.Tie("pitch-offset-deg", this,
198                       &FGViewMgr::getViewPitchOffset_deg,
199                       &FGViewMgr::setViewPitchOffset_deg);
200   fgSetArchivable("/sim/current-view/pitch-offset-deg");
201   _tiedProperties.Tie("goal-pitch-offset-deg", this,
202                       &FGViewMgr::getGoalViewPitchOffset_deg,
203                       &FGViewMgr::setGoalViewPitchOffset_deg);
204   fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
205   _tiedProperties.Tie("roll-offset-deg", this,
206                       &FGViewMgr::getViewRollOffset_deg,
207                       &FGViewMgr::setViewRollOffset_deg);
208   fgSetArchivable("/sim/current-view/roll-offset-deg");
209   _tiedProperties.Tie("goal-roll-offset-deg", this,
210                       &FGViewMgr::getGoalViewRollOffset_deg,
211                       &FGViewMgr::setGoalViewRollOffset_deg);
212   fgSetArchivable("/sim/current-view/goal-roll-offset-deg");
213
214   _tiedProperties.Tie("view-number", this,
215                       &FGViewMgr::getView, &FGViewMgr::setView);
216   fgSetArchivable("/sim/current-view/view-number", false);
217
218   _tiedProperties.Tie("axes/long", this,
219                       (double_getter)0, &FGViewMgr::setViewAxisLong);
220   fgSetArchivable("/sim/current-view/axes/long");
221
222   _tiedProperties.Tie("axes/lat", this,
223                       (double_getter)0, &FGViewMgr::setViewAxisLat);
224   fgSetArchivable("/sim/current-view/axes/lat");
225
226   _tiedProperties.Tie("field-of-view", this,
227                       &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
228   fgSetArchivable("/sim/current-view/field-of-view");
229
230   _tiedProperties.Tie("aspect-ratio-multiplier", this,
231                       &FGViewMgr::getARM_deg, &FGViewMgr::setARM_deg);
232   fgSetArchivable("/sim/current-view/field-of-view");
233
234   _tiedProperties.Tie("ground-level-nearplane-m", this,
235                       &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
236   fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
237
238   SGPropertyNode *n = fgGetNode("/sim/current-view", true);
239   _tiedProperties.Tie(n->getNode("viewer-x-m", true),SGRawValuePointer<double>(&abs_viewer_position[0]));
240   _tiedProperties.Tie(n->getNode("viewer-y-m", true),SGRawValuePointer<double>(&abs_viewer_position[1]));
241   _tiedProperties.Tie(n->getNode("viewer-z-m", true),SGRawValuePointer<double>(&abs_viewer_position[2]));
242
243   _tiedProperties.Tie("debug/orientation-w", this,
244                       &FGViewMgr::getCurrentViewOrientation_w);
245   _tiedProperties.Tie("debug/orientation-x", this,
246                       &FGViewMgr::getCurrentViewOrientation_x);
247   _tiedProperties.Tie("debug/orientation-y", this,
248                       &FGViewMgr::getCurrentViewOrientation_y);
249   _tiedProperties.Tie("debug/orientation-z", this,
250                       &FGViewMgr::getCurrentViewOrientation_z);
251
252   _tiedProperties.Tie("debug/orientation_offset-w", this,
253                       &FGViewMgr::getCurrentViewOrOffset_w);
254   _tiedProperties.Tie("debug/orientation_offset-x", this,
255                       &FGViewMgr::getCurrentViewOrOffset_x);
256   _tiedProperties.Tie("debug/orientation_offset-y", this,
257                       &FGViewMgr::getCurrentViewOrOffset_y);
258   _tiedProperties.Tie("debug/orientation_offset-z", this,
259                       &FGViewMgr::getCurrentViewOrOffset_z);
260
261   _tiedProperties.Tie("debug/frame-w", this,
262                       &FGViewMgr::getCurrentViewFrame_w);
263   _tiedProperties.Tie("debug/frame-x", this,
264                       &FGViewMgr::getCurrentViewFrame_x);
265   _tiedProperties.Tie("debug/frame-y", this,
266                       &FGViewMgr::getCurrentViewFrame_y);
267   _tiedProperties.Tie("debug/frame-z", this,
268                       &FGViewMgr::getCurrentViewFrame_z);
269 }
270
271 void
272 FGViewMgr::unbind ()
273 {
274   _tiedProperties.Untie();
275 }
276
277 void
278 FGViewMgr::update (double dt)
279 {
280   FGViewer *loop_view = (FGViewer *)get_current_view();
281   if (loop_view == 0) return;
282
283   SGPropertyNode *n = config_list[current];
284   double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
285
286   // Set up view location and orientation
287
288   if (!n->getBoolValue("config/from-model")) {
289     lon_deg = fgGetDouble(n->getStringValue("config/eye-lon-deg-path"));
290     lat_deg = fgGetDouble(n->getStringValue("config/eye-lat-deg-path"));
291     alt_ft = fgGetDouble(n->getStringValue("config/eye-alt-ft-path"));
292     roll_deg = fgGetDouble(n->getStringValue("config/eye-roll-deg-path"));
293     pitch_deg = fgGetDouble(n->getStringValue("config/eye-pitch-deg-path"));
294     heading_deg = fgGetDouble(n->getStringValue("config/eye-heading-deg-path"));
295
296     loop_view->setPosition(lon_deg, lat_deg, alt_ft);
297     loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
298   } else {
299     // force recalc in viewer
300     loop_view->set_dirty();
301   }
302
303   // if lookat (type 1) then get target data...
304   if (loop_view->getType() == FG_LOOKAT) {
305     if (!n->getBoolValue("config/from-model")) {
306       lon_deg = fgGetDouble(n->getStringValue("config/target-lon-deg-path"));
307       lat_deg = fgGetDouble(n->getStringValue("config/target-lat-deg-path"));
308       alt_ft = fgGetDouble(n->getStringValue("config/target-alt-ft-path"));
309       roll_deg = fgGetDouble(n->getStringValue("config/target-roll-deg-path"));
310       pitch_deg = fgGetDouble(n->getStringValue("config/target-pitch-deg-path"));
311       heading_deg = fgGetDouble(n->getStringValue("config/target-heading-deg-path"));
312
313       loop_view->setTargetPosition(lon_deg, lat_deg, alt_ft);
314       loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
315     } else {
316       loop_view->set_dirty();
317     }
318   }
319
320   setViewXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
321   setViewYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
322   setViewZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
323
324   setViewTargetXOffset_m(fgGetDouble("/sim/current-view/target-x-offset-m"));
325   setViewTargetYOffset_m(fgGetDouble("/sim/current-view/target-y-offset-m"));
326   setViewTargetZOffset_m(fgGetDouble("/sim/current-view/target-z-offset-m"));
327
328   current_view_orientation = loop_view->getViewOrientation();
329   current_view_or_offset = loop_view->getViewOrientationOffset();
330
331   // Update the current view
332   do_axes();
333   loop_view->update(dt);
334   abs_viewer_position = loop_view->getViewPosition();
335
336   // update audio listener values
337   // set the viewer position in Cartesian coordinates in meters
338   smgr->set_position( abs_viewer_position, loop_view->getPosition() );
339   smgr->set_orientation( current_view_orientation );
340
341   // get the model velocity
342   SGVec3d velocity = SGVec3d::zeros();
343   if ( !stationary() ) {
344     velocity = SGVec3d( velocityNorthFPS->getDoubleValue(),
345                         velocityEastFPS->getDoubleValue(),
346                         velocityDownFPS->getDoubleValue() );
347   }
348   smgr->set_velocity( velocity );
349 }
350
351 void
352 FGViewMgr::copyToCurrent()
353 {
354   if (!inited) {
355     return;
356   }
357   
358     SGPropertyNode *n = config_list[current];
359     fgSetString("/sim/current-view/name", n->getStringValue("name"));
360     fgSetString("/sim/current-view/type", n->getStringValue("type"));
361
362     // copy certain view config data for default values
363     fgSetDouble("/sim/current-view/config/heading-offset-deg",
364                 n->getDoubleValue("config/default-heading-offset-deg"));
365     fgSetDouble("/sim/current-view/config/pitch-offset-deg",
366                 n->getDoubleValue("config/pitch-offset-deg"));
367     fgSetDouble("/sim/current-view/config/roll-offset-deg",
368                 n->getDoubleValue("config/roll-offset-deg"));
369     fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
370                 n->getDoubleValue("config/default-field-of-view-deg"));
371     fgSetBool("/sim/current-view/config/from-model",
372                 n->getBoolValue("config/from-model"));
373
374     // copy view data
375     fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
376     fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
377     fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
378
379     fgSetDouble("/sim/current-view/goal-heading-offset-deg",
380                 get_current_view()->getGoalHeadingOffset_deg());
381     fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
382                 get_current_view()->getGoalPitchOffset_deg());
383     fgSetDouble("/sim/current-view/goal-roll-offset-deg",
384                 get_current_view()->getRollOffset_deg());
385     fgSetDouble("/sim/current-view/heading-offset-deg",
386                 get_current_view()->getHeadingOffset_deg());
387     fgSetDouble("/sim/current-view/pitch-offset-deg",
388                 get_current_view()->getPitchOffset_deg());
389     fgSetDouble("/sim/current-view/roll-offset-deg",
390                 get_current_view()->getRollOffset_deg());
391     fgSetDouble("/sim/current-view/target-x-offset-m",
392                 get_current_view()->getTargetXOffset_m());
393     fgSetDouble("/sim/current-view/target-y-offset-m",
394                 get_current_view()->getTargetYOffset_m());
395     fgSetDouble("/sim/current-view/target-z-offset-m",
396                 get_current_view()->getTargetZOffset_m());
397     fgSetBool("/sim/current-view/internal",
398                 get_current_view()->getInternal());
399 }
400
401 void FGViewMgr::clear()
402 {
403   views.clear();
404 }
405
406 FGViewer*
407 FGViewMgr::get_current_view()
408 {
409         if ( current < (int)views.size() ) {
410             return views[current];
411         } else {
412             return NULL;
413         }
414 }
415
416 const FGViewer*
417 FGViewMgr::get_current_view() const
418 {
419         if ( current < (int)views.size() ) {
420             return views[current];
421         } else {
422             return NULL;
423         }
424 }
425
426
427 FGViewer*
428 FGViewMgr::get_view( int i )
429 {
430         if ( i < 0 ) { i = 0; }
431         if ( i >= (int)views.size() ) { i = views.size() - 1; }
432         return views[i];
433 }
434
435 const FGViewer*
436 FGViewMgr::get_view( int i ) const
437 {
438         if ( i < 0 ) { i = 0; }
439         if ( i >= (int)views.size() ) { i = views.size() - 1; }
440         return views[i];
441 }
442
443 FGViewer*
444 FGViewMgr::next_view()
445 {
446         setView((current+1 < (int)views.size()) ? (current + 1) : 0);
447         view_number->fireValueChanged();
448         return views[current];
449 }
450
451 FGViewer*
452 FGViewMgr::prev_view()
453 {
454         setView((0 < current) ? (current - 1) : (views.size() - 1));
455         view_number->fireValueChanged();
456         return views[current];
457 }
458
459 void
460 FGViewMgr::add_view( FGViewer * v )
461 {
462   views.push_back(v);
463   v->init();
464 }
465     
466 double
467 FGViewMgr::getViewHeadingOffset_deg () const
468 {
469   const FGViewer * view = get_current_view();
470   return (view == 0 ? 0 : view->getHeadingOffset_deg());
471 }
472
473 void
474 FGViewMgr::setViewHeadingOffset_deg (double offset)
475 {
476   FGViewer * view = get_current_view();
477   if (view != 0) {
478     view->setGoalHeadingOffset_deg(offset);
479     view->setHeadingOffset_deg(offset);
480   }
481 }
482
483 double
484 FGViewMgr::getViewGoalHeadingOffset_deg () const
485 {
486   const FGViewer * view = get_current_view();
487   return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
488 }
489
490 void
491 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
492 {
493   FGViewer * view = get_current_view();
494   if (view != 0)
495     view->setGoalHeadingOffset_deg(offset);
496 }
497
498 double
499 FGViewMgr::getViewPitchOffset_deg () const
500 {
501   const FGViewer * view = get_current_view();
502   return (view == 0 ? 0 : view->getPitchOffset_deg());
503 }
504
505 void
506 FGViewMgr::setViewPitchOffset_deg (double tilt)
507 {
508   FGViewer * view = get_current_view();
509   if (view != 0) {
510     view->setGoalPitchOffset_deg(tilt);
511     view->setPitchOffset_deg(tilt);
512   }
513 }
514
515 double
516 FGViewMgr::getGoalViewPitchOffset_deg () const
517 {
518   const FGViewer * view = get_current_view();
519   return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
520 }
521
522 void
523 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
524 {
525   FGViewer * view = get_current_view();
526   if (view != 0)
527     view->setGoalPitchOffset_deg(tilt);
528 }
529
530 double
531 FGViewMgr::getViewRollOffset_deg () const
532 {
533   const FGViewer * view = get_current_view();
534   return (view == 0 ? 0 : view->getRollOffset_deg());
535 }
536
537 void
538 FGViewMgr::setViewRollOffset_deg (double tilt)
539 {
540   FGViewer * view = get_current_view();
541   if (view != 0) {
542     view->setGoalRollOffset_deg(tilt);
543     view->setRollOffset_deg(tilt);
544   }
545 }
546
547 double
548 FGViewMgr::getGoalViewRollOffset_deg () const
549 {
550   const FGViewer * view = get_current_view();
551   return (view == 0 ? 0 : view->getGoalRollOffset_deg());
552 }
553
554 void
555 FGViewMgr::setGoalViewRollOffset_deg (double tilt)
556 {
557   FGViewer * view = get_current_view();
558   if (view != 0)
559     view->setGoalRollOffset_deg(tilt);
560 }
561
562 double
563 FGViewMgr::getViewXOffset_m () const
564 {
565   const FGViewer * view = get_current_view();
566   if (view != 0) {
567     return ((FGViewer *)view)->getXOffset_m();
568   } else {
569     return 0;
570   }
571 }
572
573 void
574 FGViewMgr::setViewXOffset_m (double x)
575 {
576   FGViewer * view = get_current_view();
577   if (view != 0) {
578     view->setXOffset_m(x);
579   }
580 }
581
582 double
583 FGViewMgr::getViewYOffset_m () const
584 {
585   const FGViewer * view = get_current_view();
586   if (view != 0) {
587     return ((FGViewer *)view)->getYOffset_m();
588   } else {
589     return 0;
590   }
591 }
592
593 void
594 FGViewMgr::setViewYOffset_m (double y)
595 {
596   FGViewer * view = get_current_view();
597   if (view != 0) {
598     view->setYOffset_m(y);
599   }
600 }
601
602 double
603 FGViewMgr::getViewZOffset_m () const
604 {
605   const FGViewer * view = get_current_view();
606   if (view != 0) {
607     return ((FGViewer *)view)->getZOffset_m();
608   } else {
609     return 0;
610   }
611 }
612
613 void
614 FGViewMgr::setViewZOffset_m (double z)
615 {
616   FGViewer * view = get_current_view();
617   if (view != 0) {
618     view->setZOffset_m(z);
619   }
620 }
621
622 bool
623 FGViewMgr::stationary () const
624 {
625   const FGViewer * view = get_current_view();
626   if (view != 0) {
627     if (((FGViewer *)view)->getXOffset_m() == 0.0 &&
628         ((FGViewer *)view)->getYOffset_m() == 0.0 &&
629         ((FGViewer *)view)->getZOffset_m() == 0.0)
630       return true;
631   }
632
633   return false;
634 }
635
636 double
637 FGViewMgr::getViewTargetXOffset_m () const
638 {
639   const FGViewer * view = get_current_view();
640   if (view != 0) {
641     return ((FGViewer *)view)->getTargetXOffset_m();
642   } else {
643     return 0;
644   }
645 }
646
647 void
648 FGViewMgr::setViewTargetXOffset_m (double x)
649 {
650   FGViewer * view = get_current_view();
651   if (view != 0) {
652     view->setTargetXOffset_m(x);
653   }
654 }
655
656 double
657 FGViewMgr::getViewTargetYOffset_m () const
658 {
659   const FGViewer * view = get_current_view();
660   if (view != 0) {
661     return ((FGViewer *)view)->getTargetYOffset_m();
662   } else {
663     return 0;
664   }
665 }
666
667 void
668 FGViewMgr::setViewTargetYOffset_m (double y)
669 {
670   FGViewer * view = get_current_view();
671   if (view != 0) {
672     view->setTargetYOffset_m(y);
673   }
674 }
675
676 double
677 FGViewMgr::getViewTargetZOffset_m () const
678 {
679   const FGViewer * view = get_current_view();
680   if (view != 0) {
681     return ((FGViewer *)view)->getTargetZOffset_m();
682   } else {
683     return 0;
684   }
685 }
686
687 void
688 FGViewMgr::setViewTargetZOffset_m (double z)
689 {
690   FGViewer * view = get_current_view();
691   if (view != 0) {
692     view->setTargetZOffset_m(z);
693   }
694 }
695
696 int
697 FGViewMgr::getView () const
698 {
699   return ( current );
700 }
701
702 void
703 FGViewMgr::setView (int newview)
704 {
705   // negative numbers -> set view with node index -newview
706   if (newview < 0) {
707     for (int i = 0; i < (int)config_list.size(); i++) {
708       int index = -config_list[i]->getIndex();
709       if (index == newview)
710         newview = i;
711     }
712     if (newview < 0)
713       return;
714   }
715
716   // if newview number too low wrap to last view...
717   if (newview < 0)
718     newview = (int)views.size() - 1;
719
720   // if newview number to high wrap to zero...
721   if (newview >= (int)views.size())
722     newview = 0;
723
724   // set new view
725   current = newview;
726   // copy in view data
727   copyToCurrent();
728 }
729
730
731 double
732 FGViewMgr::getFOV_deg () const
733 {
734   const FGViewer * view = get_current_view();
735   return (view == 0 ? 0 : view->get_fov());
736 }
737
738 void
739 FGViewMgr::setFOV_deg (double fov)
740 {
741   FGViewer * view = get_current_view();
742   if (view != 0)
743     view->set_fov(fov);
744 }
745
746 double
747 FGViewMgr::getARM_deg () const
748 {
749   const FGViewer * view = get_current_view();
750   return (view == 0 ? 0 : view->get_aspect_ratio_multiplier());
751 }
752
753 void
754 FGViewMgr::setARM_deg (double aspect_ratio_multiplier)
755 {  
756   FGViewer * view = get_current_view();
757   if (view != 0)
758     view->set_aspect_ratio_multiplier(aspect_ratio_multiplier);
759 }
760
761 double
762 FGViewMgr::getNear_m () const
763 {
764   const FGViewer * view = get_current_view();
765   return (view == 0 ? 0.5f : view->getNear_m());
766 }
767
768 void
769 FGViewMgr::setNear_m (double near_m)
770 {
771   FGViewer * view = get_current_view();
772   if (view != 0)
773     view->setNear_m(near_m);
774 }
775
776 void
777 FGViewMgr::setViewAxisLong (double axis)
778 {
779   axis_long = axis;
780 }
781
782 void
783 FGViewMgr::setViewAxisLat (double axis)
784 {
785   axis_lat = axis;
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 }