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