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