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