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