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