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