1 // viewmgr.cxx -- class for managing all the views in the flightgear world.
3 // Written by Curtis Olson, started October 2000.
4 // partially rewritten by Jim Wilson March 2002
6 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <string.h> // strcmp
28 #include "viewmgr.hxx"
29 #include "fg_props.hxx"
33 FGViewMgr::FGViewMgr( void ) :
42 FGViewMgr::~FGViewMgr( void ) {
49 string viewpath, nodepath, strdata;
50 bool from_model = false;
51 bool at_model = false;
52 int from_model_index = 0;
53 int at_model_index = 0;
55 double damp_roll = 0.0, damp_pitch = 0.0, damp_heading = 0.0;
56 double x_offset_m, y_offset_m, z_offset_m, fov_deg;
57 double heading_offset_deg, pitch_offset_deg, roll_offset_deg;
58 double target_x_offset_m, target_y_offset_m, target_z_offset_m;
62 double aspect_ratio_multiplier
63 = fgGetDouble("/sim/current-view/aspect-ratio-multiplier");
65 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
66 viewpath = "/sim/view";
67 sprintf(stridx, "[%d]", i);
70 // find out what type of view this is...
73 strdata = fgGetString(nodepath.c_str());
75 // find out if this is an internal view (e.g. in cockpit, low near plane)
76 internal = false; // default
78 nodepath += "/internal";
79 internal = fgGetBool(nodepath.c_str());
82 // this is assumed to be an aircraft model...we will need to read
83 // model-from-type as well.
84 // find out if this is a model we are looking from...
86 nodepath += "/config/from-model";
87 from_model = fgGetBool(nodepath.c_str());
89 // get model index (which model)
92 nodepath += "/config/from-model-idx";
93 from_model_index = fgGetInt(nodepath.c_str());
96 if ( strcmp("lookat",strdata.c_str()) == 0 ) {
97 // find out if this is a model we are looking at...
99 nodepath += "/config/at-model";
100 at_model = fgGetBool(nodepath.c_str());
102 // get model index (which model)
105 nodepath += "/config/at-model-idx";
106 at_model_index = fgGetInt(nodepath.c_str());
109 nodepath += "/config/at-model-roll-damping";
110 damp_roll = fgGetDouble(nodepath.c_str(), 0.0);
112 nodepath += "/config/at-model-pitch-damping";
113 damp_pitch = fgGetDouble(nodepath.c_str(), 0.0);
115 nodepath += "/config/at-model-heading-damping";
116 damp_heading = fgGetDouble(nodepath.c_str(), 0.0);
121 nodepath += "/config/x-offset-m";
122 x_offset_m = fgGetDouble(nodepath.c_str());
124 nodepath += "/config/y-offset-m";
125 y_offset_m = fgGetDouble(nodepath.c_str());
127 nodepath += "/config/z-offset-m";
128 z_offset_m = fgGetDouble(nodepath.c_str());
130 nodepath += "/config/pitch-offset-deg";
131 pitch_offset_deg = fgGetDouble(nodepath.c_str());
132 fgSetDouble(nodepath.c_str(),pitch_offset_deg);
134 nodepath += "/config/heading-offset-deg";
135 heading_offset_deg = fgGetDouble(nodepath.c_str());
136 fgSetDouble(nodepath.c_str(),heading_offset_deg);
138 nodepath += "/config/roll-offset-deg";
139 roll_offset_deg = fgGetDouble(nodepath.c_str());
140 fgSetDouble(nodepath.c_str(),roll_offset_deg);
142 nodepath += "/config/default-field-of-view-deg";
143 fov_deg = fgGetDouble(nodepath.c_str());
145 // target offsets for lookat mode only...
147 nodepath += "/config/target-x-offset-m";
148 target_x_offset_m = fgGetDouble(nodepath.c_str());
150 nodepath += "/config/target-y-offset-m";
151 target_y_offset_m = fgGetDouble(nodepath.c_str());
153 nodepath += "/config/target-z-offset-m";
154 target_z_offset_m = fgGetDouble(nodepath.c_str());
157 nodepath += "/config/ground-level-nearplane-m";
158 near_m = fgGetDouble(nodepath.c_str());
160 // supporting two types now "lookat" = 1 and "lookfrom" = 0
161 if ( strcmp("lookat",strdata.c_str()) == 0 )
162 add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
163 at_model, at_model_index,
164 damp_roll, damp_pitch, damp_heading,
165 x_offset_m, y_offset_m,z_offset_m,
166 heading_offset_deg, pitch_offset_deg,
167 roll_offset_deg, fov_deg, aspect_ratio_multiplier,
168 target_x_offset_m, target_y_offset_m,
169 target_z_offset_m, near_m, internal ));
171 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index,
172 false, 0, 0.0, 0.0, 0.0,
173 x_offset_m, y_offset_m, z_offset_m,
174 heading_offset_deg, pitch_offset_deg,
175 roll_offset_deg, fov_deg, aspect_ratio_multiplier,
176 0, 0, 0, near_m, internal ));
187 string viewpath, nodepath, strdata;
190 // reset offsets and fov to configuration defaults
192 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
193 viewpath = "/sim/view";
194 sprintf(stridx, "[%d]", i);
200 nodepath += "/config/x-offset-m";
201 fgSetDouble("/sim/current-view/x-offset-m",fgGetDouble(nodepath.c_str()));
204 nodepath += "/config/y-offset-m";
205 fgSetDouble("/sim/current-view/y-offset-m",fgGetDouble(nodepath.c_str()));
208 nodepath += "/config/z-offset-m";
209 fgSetDouble("/sim/current-view/z-offset-m",fgGetDouble(nodepath.c_str()));
212 nodepath += "/config/pitch-offset-deg";
213 fgSetDouble("/sim/current-view/pitch-offset-deg",
214 fgGetDouble(nodepath.c_str()));
217 nodepath += "/config/heading-offset-deg";
218 fgSetDouble("/sim/current-view/heading-offset-deg",
219 fgGetDouble(nodepath.c_str()));
222 nodepath += "/config/roll-offset-deg";
223 fgSetDouble("/sim/current-view/roll-offset-deg",
224 fgGetDouble(nodepath.c_str()));
227 nodepath += "/config/default-field-of-view-deg";
228 fov_deg = fgGetDouble(nodepath.c_str());
229 if (fov_deg < 10.0) {
232 fgSetDouble("/sim/current-view/field-of-view",fov_deg);
234 // target offsets for lookat mode only...
236 nodepath += "/config/target-x-offset-m";
237 fgSetDouble("/sim/current-view/target-x-offset-deg",
238 fgGetDouble(nodepath.c_str()));
241 nodepath += "/config/target-y-offset-m";
242 fgSetDouble("/sim/current-view/target-y-offset-deg",
243 fgGetDouble(nodepath.c_str()));
246 nodepath += "/config/target-z-offset-m";
247 fgSetDouble("/sim/current-view/target-z-offset-deg",
248 fgGetDouble(nodepath.c_str()));
256 typedef double (FGViewMgr::*double_getter)() const;
261 // these are bound to the current view properties
262 fgTie("/sim/current-view/heading-offset-deg", this,
263 &FGViewMgr::getViewHeadingOffset_deg,
264 &FGViewMgr::setViewHeadingOffset_deg);
265 fgSetArchivable("/sim/current-view/heading-offset-deg");
266 fgTie("/sim/current-view/goal-heading-offset-deg", this,
267 &FGViewMgr::getViewGoalHeadingOffset_deg,
268 &FGViewMgr::setViewGoalHeadingOffset_deg);
269 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
270 fgTie("/sim/current-view/pitch-offset-deg", this,
271 &FGViewMgr::getViewPitchOffset_deg,
272 &FGViewMgr::setViewPitchOffset_deg);
273 fgSetArchivable("/sim/current-view/pitch-offset-deg");
274 fgTie("/sim/current-view/goal-pitch-offset-deg", this,
275 &FGViewMgr::getGoalViewPitchOffset_deg,
276 &FGViewMgr::setGoalViewPitchOffset_deg);
277 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
278 fgTie("/sim/current-view/roll-offset-deg", this,
279 &FGViewMgr::getViewRollOffset_deg,
280 &FGViewMgr::setViewRollOffset_deg);
281 fgSetArchivable("/sim/current-view/roll-offset-deg");
282 fgTie("/sim/current-view/goal-roll-offset-deg", this,
283 &FGViewMgr::getGoalViewRollOffset_deg,
284 &FGViewMgr::setGoalViewRollOffset_deg);
285 fgSetArchivable("/sim/current-view/goal-roll-offset-deg");
287 fgTie("/sim/current-view/view-number", this,
288 &FGViewMgr::getView, &FGViewMgr::setView);
289 fgSetArchivable("/sim/current-view/view-number", FALSE);
291 fgTie("/sim/current-view/axes/long", this,
292 (double_getter)0, &FGViewMgr::setViewAxisLong);
293 fgSetArchivable("/sim/current-view/axes/long");
295 fgTie("/sim/current-view/axes/lat", this,
296 (double_getter)0, &FGViewMgr::setViewAxisLat);
297 fgSetArchivable("/sim/current-view/axes/lat");
299 fgTie("/sim/current-view/field-of-view", this,
300 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
301 fgSetArchivable("/sim/current-view/field-of-view");
303 fgTie("/sim/current-view/aspect-ratio-multiplier", this,
304 &FGViewMgr::getARM_deg, &FGViewMgr::setARM_deg);
305 fgSetArchivable("/sim/current-view/field-of-view");
307 fgTie("/sim/current-view/ground-level-nearplane-m", this,
308 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
309 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
317 // need to redo these bindings to the new locations (move to viewer?)
318 fgUntie("/sim/current-view/heading-offset-deg");
319 fgUntie("/sim/current-view/goal-heading-offset-deg");
320 fgUntie("/sim/current-view/pitch-offset-deg");
321 fgUntie("/sim/current-view/goal-pitch-offset-deg");
322 fgUntie("/sim/current-view/field-of-view");
323 fgUntie("/sim/current-view/aspect-ratio-multiplier");
324 fgUntie("/sim/current-view/view-number");
325 fgUntie("/sim/current-view/axes/long");
326 fgUntie("/sim/current-view/axes/lat");
327 fgUntie("/sim/current-view/ground-level-nearplane-m");
331 FGViewMgr::update (double dt)
334 string viewpath, nodepath;
335 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
337 FGViewer * view = get_current_view();
343 viewpath = "/sim/view";
344 sprintf(stridx, "[%d]", i);
347 FGViewer *loop_view = (FGViewer *)get_view( i );
349 // Set up view location and orientation
352 nodepath += "/config/from-model";
353 if (!fgGetBool(nodepath.c_str())) {
355 nodepath += "/config/eye-lon-deg-path";
356 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
358 nodepath += "/config/eye-lat-deg-path";
359 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
361 nodepath += "/config/eye-alt-ft-path";
362 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
364 nodepath += "/config/eye-roll-deg-path";
365 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
367 nodepath += "/config/eye-pitch-deg-path";
368 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
370 nodepath += "/config/eye-heading-deg-path";
371 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
372 loop_view->setPosition(lon_deg, lat_deg, alt_ft);
373 loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
375 // force recalc in viewer
376 loop_view->set_dirty();
379 // if lookat (type 1) then get target data...
380 if (loop_view->getType() == FG_LOOKAT) {
382 nodepath += "/config/from-model";
383 if (!fgGetBool(nodepath.c_str())) {
385 nodepath += "/config/target-lon-deg-path";
386 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
388 nodepath += "/config/target-lat-deg-path";
389 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
391 nodepath += "/config/target-alt-ft-path";
392 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
394 nodepath += "/config/target-roll-deg-path";
395 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
397 nodepath += "/config/target-pitch-deg-path";
398 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
400 nodepath += "/config/target-heading-deg-path";
401 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
403 loop_view->setTargetPosition(lon_deg, lat_deg, alt_ft);
404 loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
406 loop_view->set_dirty();
410 setViewXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
411 setViewYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
412 setViewZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
414 setViewTargetXOffset_m(fgGetDouble("/sim/current-view/target-x-offset-m"));
415 setViewTargetYOffset_m(fgGetDouble("/sim/current-view/target-y-offset-m"));
416 setViewTargetZOffset_m(fgGetDouble("/sim/current-view/target-z-offset-m"));
418 // Update the current view
424 FGViewMgr::copyToCurrent()
427 string viewpath, nodepath;
430 viewpath = "/sim/view";
431 sprintf(stridx, "[%d]", i);
434 // copy certain view config data for default values
436 nodepath += "/config/default-heading-offset-deg";
437 fgSetDouble("/sim/current-view/config/heading-offset-deg",
438 fgGetDouble(nodepath.c_str()));
441 nodepath += "/config/pitch-offset-deg";
442 fgSetDouble("/sim/current-view/config/pitch-offset-deg",
443 fgGetDouble(nodepath.c_str()));
446 nodepath += "/config/roll-offset-deg";
447 fgSetDouble("/sim/current-view/config/roll-offset-deg",
448 fgGetDouble(nodepath.c_str()));
451 nodepath += "/config/default-field-of-view-deg";
452 fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
453 fgGetDouble(nodepath.c_str()));
456 nodepath += "/config/from-model";
457 fgSetBool("/sim/current-view/config/from-model",
458 fgGetBool(nodepath.c_str()));
462 fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
463 fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
464 fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
465 fgSetDouble("/sim/current-view/goal-heading-offset-deg",
466 get_current_view()->getGoalHeadingOffset_deg());
467 fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
468 get_current_view()->getGoalPitchOffset_deg());
469 fgSetDouble("/sim/current-view/goal-roll-offset-deg",
470 get_current_view()->getRollOffset_deg());
471 fgSetDouble("/sim/current-view/heading-offset-deg",
472 get_current_view()->getHeadingOffset_deg());
473 fgSetDouble("/sim/current-view/pitch-offset-deg",
474 get_current_view()->getPitchOffset_deg());
475 fgSetDouble("/sim/current-view/roll-offset-deg",
476 get_current_view()->getRollOffset_deg());
477 fgSetDouble("/sim/current-view/target-x-offset-m",
478 get_current_view()->getTargetXOffset_m());
479 fgSetDouble("/sim/current-view/target-y-offset-m",
480 get_current_view()->getTargetYOffset_m());
481 fgSetDouble("/sim/current-view/target-z-offset-m",
482 get_current_view()->getTargetZOffset_m());
484 fgSetBool("/sim/current-view/internal",
485 get_current_view()->getInternal());
491 FGViewMgr::getViewHeadingOffset_deg () const
493 const FGViewer * view = get_current_view();
494 return (view == 0 ? 0 : view->getHeadingOffset_deg());
498 FGViewMgr::setViewHeadingOffset_deg (double offset)
500 FGViewer * view = get_current_view();
502 view->setGoalHeadingOffset_deg(offset);
503 view->setHeadingOffset_deg(offset);
508 FGViewMgr::getViewGoalHeadingOffset_deg () const
510 const FGViewer * view = get_current_view();
511 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
515 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
517 FGViewer * view = get_current_view();
519 view->setGoalHeadingOffset_deg(offset);
523 FGViewMgr::getViewPitchOffset_deg () const
525 const FGViewer * view = get_current_view();
526 return (view == 0 ? 0 : view->getPitchOffset_deg());
530 FGViewMgr::setViewPitchOffset_deg (double tilt)
532 FGViewer * view = get_current_view();
534 view->setGoalPitchOffset_deg(tilt);
535 view->setPitchOffset_deg(tilt);
540 FGViewMgr::getGoalViewPitchOffset_deg () const
542 const FGViewer * view = get_current_view();
543 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
547 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
549 FGViewer * view = get_current_view();
551 view->setGoalPitchOffset_deg(tilt);
555 FGViewMgr::getViewRollOffset_deg () const
557 const FGViewer * view = get_current_view();
558 return (view == 0 ? 0 : view->getRollOffset_deg());
562 FGViewMgr::setViewRollOffset_deg (double tilt)
564 FGViewer * view = get_current_view();
566 view->setGoalRollOffset_deg(tilt);
567 view->setRollOffset_deg(tilt);
572 FGViewMgr::getGoalViewRollOffset_deg () const
574 const FGViewer * view = get_current_view();
575 return (view == 0 ? 0 : view->getGoalRollOffset_deg());
579 FGViewMgr::setGoalViewRollOffset_deg (double tilt)
581 FGViewer * view = get_current_view();
583 view->setGoalRollOffset_deg(tilt);
587 FGViewMgr::getViewXOffset_m () const
589 const FGViewer * view = get_current_view();
591 return ((FGViewer *)view)->getXOffset_m();
598 FGViewMgr::setViewXOffset_m (double x)
600 FGViewer * view = get_current_view();
602 view->setXOffset_m(x);
607 FGViewMgr::getViewYOffset_m () const
609 const FGViewer * view = get_current_view();
611 return ((FGViewer *)view)->getYOffset_m();
618 FGViewMgr::setViewYOffset_m (double y)
620 FGViewer * view = get_current_view();
622 view->setYOffset_m(y);
627 FGViewMgr::getViewZOffset_m () const
629 const FGViewer * view = get_current_view();
631 return ((FGViewer *)view)->getZOffset_m();
638 FGViewMgr::setViewZOffset_m (double z)
640 FGViewer * view = get_current_view();
642 view->setZOffset_m(z);
647 FGViewMgr::getViewTargetXOffset_m () const
649 const FGViewer * view = get_current_view();
651 return ((FGViewer *)view)->getTargetXOffset_m();
658 FGViewMgr::setViewTargetXOffset_m (double x)
660 FGViewer * view = get_current_view();
662 view->setTargetXOffset_m(x);
667 FGViewMgr::getViewTargetYOffset_m () const
669 const FGViewer * view = get_current_view();
671 return ((FGViewer *)view)->getTargetYOffset_m();
678 FGViewMgr::setViewTargetYOffset_m (double y)
680 FGViewer * view = get_current_view();
682 view->setTargetYOffset_m(y);
687 FGViewMgr::getViewTargetZOffset_m () const
689 const FGViewer * view = get_current_view();
691 return ((FGViewer *)view)->getTargetZOffset_m();
698 FGViewMgr::setViewTargetZOffset_m (double z)
700 FGViewer * view = get_current_view();
702 view->setTargetZOffset_m(z);
707 FGViewMgr::getView () const
713 FGViewMgr::setView (int newview )
715 // if newview number too low wrap to last view...
717 newview = (int)views.size() -1;
719 // if newview number to high wrap to zero...
720 if ( newview > ((int)views.size() -1) ) {
731 FGViewMgr::getFOV_deg () const
733 const FGViewer * view = get_current_view();
734 return (view == 0 ? 0 : view->get_fov());
738 FGViewMgr::setFOV_deg (double fov)
740 FGViewer * view = get_current_view();
746 FGViewMgr::getARM_deg () const
748 const FGViewer * view = get_current_view();
749 return (view == 0 ? 0 : view->get_aspect_ratio_multiplier());
753 FGViewMgr::setARM_deg (double aspect_ratio_multiplier)
755 FGViewer * view = get_current_view();
757 view->set_aspect_ratio_multiplier(aspect_ratio_multiplier);
761 FGViewMgr::getNear_m () const
763 const FGViewer * view = get_current_view();
764 return (view == 0 ? 0.5f : view->getNear_m());
768 FGViewMgr::setNear_m (double near_m)
770 FGViewer * view = get_current_view();
772 view->setNear_m(near_m);
776 FGViewMgr::setViewAxisLong (double axis)
782 FGViewMgr::setViewAxisLat (double axis)
788 FGViewMgr::do_axes ()
790 // Take no action when hat is centered
791 if ( ( axis_long < 0.01 ) &&
792 ( axis_long > -0.01 ) &&
793 ( axis_lat < 0.01 ) &&
798 double viewDir = 999;
800 /* Do all the quick and easy cases */
801 if (axis_long < 0) { // Longitudinal axis forward
802 if (axis_lat == axis_long)
803 viewDir = fgGetDouble("/sim/view/config/front-left-direction-deg");
804 else if (axis_lat == - axis_long)
805 viewDir = fgGetDouble("/sim/view/config/front-right-direction-deg");
806 else if (axis_lat == 0)
807 viewDir = fgGetDouble("/sim/view/config/front-direction-deg");
808 } else if (axis_long > 0) { // Longitudinal axis backward
809 if (axis_lat == - axis_long)
810 viewDir = fgGetDouble("/sim/view/config/back-left-direction-deg");
811 else if (axis_lat == axis_long)
812 viewDir = fgGetDouble("/sim/view/config/back-right-direction-deg");
813 else if (axis_lat == 0)
814 viewDir = fgGetDouble("/sim/view/config/back-direction-deg");
815 } else if (axis_long == 0) { // Longitudinal axis neutral
817 viewDir = fgGetDouble("/sim/view/config/left-direction-deg");
818 else if (axis_lat > 0)
819 viewDir = fgGetDouble("/sim/view/config/right-direction-deg");
820 else return; /* And assertion failure maybe? */
823 // Do all the difficult cases
825 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
826 if ( viewDir < -1 ) viewDir += 360;
828 get_current_view()->setGoalHeadingOffset_deg(viewDir);