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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <string.h> // strcmp
32 #include <simgear/compiler.h>
34 #include <Model/acmodel.hxx>
36 #include "viewmgr.hxx"
37 #include "fg_props.hxx"
41 FGViewMgr::FGViewMgr( void ) :
50 FGViewMgr::~FGViewMgr( void ) {
57 string viewpath, nodepath, strdata;
58 bool from_model = false;
59 bool at_model = false;
60 int from_model_index = 0;
61 int at_model_index = 0;
63 double damp_roll = 0.0, damp_pitch = 0.0, damp_heading = 0.0;
64 double x_offset_m, y_offset_m, z_offset_m, fov_deg;
65 double heading_offset_deg, pitch_offset_deg, roll_offset_deg;
66 double target_x_offset_m, target_y_offset_m, target_z_offset_m;
70 double aspect_ratio_multiplier
71 = fgGetDouble("/sim/current-view/aspect-ratio-multiplier");
73 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
74 viewpath = "/sim/view";
75 sprintf(stridx, "[%d]", i);
78 // find out what type of view this is...
81 strdata = fgGetString(nodepath.c_str());
83 // find out if this is an internal view (e.g. in cockpit, low near plane)
84 internal = false; // default
86 nodepath += "/internal";
87 internal = fgGetBool(nodepath.c_str());
90 // this is assumed to be an aircraft model...we will need to read
91 // model-from-type as well.
92 // find out if this is a model we are looking from...
94 nodepath += "/config/from-model";
95 from_model = fgGetBool(nodepath.c_str());
97 // get model index (which model)
100 nodepath += "/config/from-model-idx";
101 from_model_index = fgGetInt(nodepath.c_str());
104 if ( strcmp("lookat",strdata.c_str()) == 0 ) {
105 // find out if this is a model we are looking at...
107 nodepath += "/config/at-model";
108 at_model = fgGetBool(nodepath.c_str());
110 // get model index (which model)
113 nodepath += "/config/at-model-idx";
114 at_model_index = fgGetInt(nodepath.c_str());
117 nodepath += "/config/at-model-roll-damping";
118 damp_roll = fgGetDouble(nodepath.c_str(), 0.0);
120 nodepath += "/config/at-model-pitch-damping";
121 damp_pitch = fgGetDouble(nodepath.c_str(), 0.0);
123 nodepath += "/config/at-model-heading-damping";
124 damp_heading = fgGetDouble(nodepath.c_str(), 0.0);
129 nodepath += "/config/x-offset-m";
130 x_offset_m = fgGetDouble(nodepath.c_str());
132 nodepath += "/config/y-offset-m";
133 y_offset_m = fgGetDouble(nodepath.c_str());
135 nodepath += "/config/z-offset-m";
136 z_offset_m = fgGetDouble(nodepath.c_str());
138 nodepath += "/config/pitch-offset-deg";
139 pitch_offset_deg = fgGetDouble(nodepath.c_str());
140 fgSetDouble(nodepath.c_str(),pitch_offset_deg);
142 nodepath += "/config/heading-offset-deg";
143 heading_offset_deg = fgGetDouble(nodepath.c_str());
144 fgSetDouble(nodepath.c_str(),heading_offset_deg);
146 nodepath += "/config/roll-offset-deg";
147 roll_offset_deg = fgGetDouble(nodepath.c_str());
148 fgSetDouble(nodepath.c_str(),roll_offset_deg);
150 nodepath += "/config/default-field-of-view-deg";
151 fov_deg = fgGetDouble(nodepath.c_str());
153 // target offsets for lookat mode only...
155 nodepath += "/config/target-x-offset-m";
156 target_x_offset_m = fgGetDouble(nodepath.c_str());
158 nodepath += "/config/target-y-offset-m";
159 target_y_offset_m = fgGetDouble(nodepath.c_str());
161 nodepath += "/config/target-z-offset-m";
162 target_z_offset_m = fgGetDouble(nodepath.c_str());
165 nodepath += "/config/ground-level-nearplane-m";
166 near_m = fgGetDouble(nodepath.c_str());
168 // supporting two types now "lookat" = 1 and "lookfrom" = 0
169 if ( strcmp("lookat",strdata.c_str()) == 0 )
170 add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
171 at_model, at_model_index,
172 damp_roll, damp_pitch, damp_heading,
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 target_x_offset_m, target_y_offset_m,
177 target_z_offset_m, near_m, internal ));
179 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index,
180 false, 0, 0.0, 0.0, 0.0,
181 x_offset_m, y_offset_m, z_offset_m,
182 heading_offset_deg, pitch_offset_deg,
183 roll_offset_deg, fov_deg, aspect_ratio_multiplier,
184 0, 0, 0, near_m, internal ));
195 string viewpath, nodepath, strdata;
198 // reset offsets and fov to configuration defaults
200 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
201 viewpath = "/sim/view";
202 sprintf(stridx, "[%d]", i);
208 nodepath += "/config/x-offset-m";
209 fgSetDouble("/sim/current-view/x-offset-m",fgGetDouble(nodepath.c_str()));
212 nodepath += "/config/y-offset-m";
213 fgSetDouble("/sim/current-view/y-offset-m",fgGetDouble(nodepath.c_str()));
216 nodepath += "/config/z-offset-m";
217 fgSetDouble("/sim/current-view/z-offset-m",fgGetDouble(nodepath.c_str()));
220 nodepath += "/config/pitch-offset-deg";
221 fgSetDouble("/sim/current-view/pitch-offset-deg",
222 fgGetDouble(nodepath.c_str()));
225 nodepath += "/config/heading-offset-deg";
226 fgSetDouble("/sim/current-view/heading-offset-deg",
227 fgGetDouble(nodepath.c_str()));
230 nodepath += "/config/roll-offset-deg";
231 fgSetDouble("/sim/current-view/roll-offset-deg",
232 fgGetDouble(nodepath.c_str()));
235 nodepath += "/config/default-field-of-view-deg";
236 fov_deg = fgGetDouble(nodepath.c_str());
237 if (fov_deg < 10.0) {
240 fgSetDouble("/sim/current-view/field-of-view",fov_deg);
242 // target offsets for lookat mode only...
244 nodepath += "/config/target-x-offset-m";
245 fgSetDouble("/sim/current-view/target-x-offset-deg",
246 fgGetDouble(nodepath.c_str()));
249 nodepath += "/config/target-y-offset-m";
250 fgSetDouble("/sim/current-view/target-y-offset-deg",
251 fgGetDouble(nodepath.c_str()));
254 nodepath += "/config/target-z-offset-m";
255 fgSetDouble("/sim/current-view/target-z-offset-deg",
256 fgGetDouble(nodepath.c_str()));
264 typedef double (FGViewMgr::*double_getter)() const;
269 // these are bound to the current view properties
270 fgTie("/sim/current-view/heading-offset-deg", this,
271 &FGViewMgr::getViewHeadingOffset_deg,
272 &FGViewMgr::setViewHeadingOffset_deg);
273 fgSetArchivable("/sim/current-view/heading-offset-deg");
274 fgTie("/sim/current-view/goal-heading-offset-deg", this,
275 &FGViewMgr::getViewGoalHeadingOffset_deg,
276 &FGViewMgr::setViewGoalHeadingOffset_deg);
277 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
278 fgTie("/sim/current-view/pitch-offset-deg", this,
279 &FGViewMgr::getViewPitchOffset_deg,
280 &FGViewMgr::setViewPitchOffset_deg);
281 fgSetArchivable("/sim/current-view/pitch-offset-deg");
282 fgTie("/sim/current-view/goal-pitch-offset-deg", this,
283 &FGViewMgr::getGoalViewPitchOffset_deg,
284 &FGViewMgr::setGoalViewPitchOffset_deg);
285 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
286 fgTie("/sim/current-view/roll-offset-deg", this,
287 &FGViewMgr::getViewRollOffset_deg,
288 &FGViewMgr::setViewRollOffset_deg);
289 fgSetArchivable("/sim/current-view/roll-offset-deg");
290 fgTie("/sim/current-view/goal-roll-offset-deg", this,
291 &FGViewMgr::getGoalViewRollOffset_deg,
292 &FGViewMgr::setGoalViewRollOffset_deg);
293 fgSetArchivable("/sim/current-view/goal-roll-offset-deg");
295 fgTie("/sim/current-view/view-number", this,
296 &FGViewMgr::getView, &FGViewMgr::setView);
297 fgSetArchivable("/sim/current-view/view-number", FALSE);
299 fgTie("/sim/current-view/axes/long", this,
300 (double_getter)0, &FGViewMgr::setViewAxisLong);
301 fgSetArchivable("/sim/current-view/axes/long");
303 fgTie("/sim/current-view/axes/lat", this,
304 (double_getter)0, &FGViewMgr::setViewAxisLat);
305 fgSetArchivable("/sim/current-view/axes/lat");
307 fgTie("/sim/current-view/field-of-view", this,
308 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
309 fgSetArchivable("/sim/current-view/field-of-view");
311 fgTie("/sim/current-view/aspect-ratio-multiplier", this,
312 &FGViewMgr::getARM_deg, &FGViewMgr::setARM_deg);
313 fgSetArchivable("/sim/current-view/field-of-view");
315 fgTie("/sim/current-view/ground-level-nearplane-m", this,
316 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
317 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
325 // need to redo these bindings to the new locations (move to viewer?)
326 fgUntie("/sim/current-view/heading-offset-deg");
327 fgUntie("/sim/current-view/goal-heading-offset-deg");
328 fgUntie("/sim/current-view/pitch-offset-deg");
329 fgUntie("/sim/current-view/goal-pitch-offset-deg");
330 fgUntie("/sim/current-view/field-of-view");
331 fgUntie("/sim/current-view/aspect-ratio-multiplier");
332 fgUntie("/sim/current-view/view-number");
333 fgUntie("/sim/current-view/axes/long");
334 fgUntie("/sim/current-view/axes/lat");
335 fgUntie("/sim/current-view/ground-level-nearplane-m");
339 FGViewMgr::update (double dt)
342 string viewpath, nodepath;
343 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
345 FGViewer * view = get_current_view();
351 viewpath = "/sim/view";
352 sprintf(stridx, "[%d]", i);
355 FGViewer *loop_view = (FGViewer *)get_view( i );
357 // Set up view location and orientation
360 nodepath += "/config/from-model";
361 if (!fgGetBool(nodepath.c_str())) {
363 nodepath += "/config/eye-lon-deg-path";
364 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
366 nodepath += "/config/eye-lat-deg-path";
367 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
369 nodepath += "/config/eye-alt-ft-path";
370 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
372 nodepath += "/config/eye-roll-deg-path";
373 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
375 nodepath += "/config/eye-pitch-deg-path";
376 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
378 nodepath += "/config/eye-heading-deg-path";
379 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
380 loop_view->setPosition(lon_deg, lat_deg, alt_ft);
381 loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
383 // force recalc in viewer
384 loop_view->set_dirty();
387 // if lookat (type 1) then get target data...
388 if (loop_view->getType() == FG_LOOKAT) {
390 nodepath += "/config/from-model";
391 if (!fgGetBool(nodepath.c_str())) {
393 nodepath += "/config/target-lon-deg-path";
394 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
396 nodepath += "/config/target-lat-deg-path";
397 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
399 nodepath += "/config/target-alt-ft-path";
400 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
402 nodepath += "/config/target-roll-deg-path";
403 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
405 nodepath += "/config/target-pitch-deg-path";
406 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
408 nodepath += "/config/target-heading-deg-path";
409 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
411 loop_view->setTargetPosition(lon_deg, lat_deg, alt_ft);
412 loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
414 loop_view->set_dirty();
418 setViewXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
419 setViewYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
420 setViewZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
422 setViewTargetXOffset_m(fgGetDouble("/sim/current-view/target-x-offset-m"));
423 setViewTargetYOffset_m(fgGetDouble("/sim/current-view/target-y-offset-m"));
424 setViewTargetZOffset_m(fgGetDouble("/sim/current-view/target-z-offset-m"));
426 // Update the current view
432 FGViewMgr::copyToCurrent()
435 string viewpath, nodepath;
438 viewpath = "/sim/view";
439 sprintf(stridx, "[%d]", i);
442 // copy certain view config data for default values
444 nodepath += "/config/default-heading-offset-deg";
445 fgSetDouble("/sim/current-view/config/heading-offset-deg",
446 fgGetDouble(nodepath.c_str()));
449 nodepath += "/config/pitch-offset-deg";
450 fgSetDouble("/sim/current-view/config/pitch-offset-deg",
451 fgGetDouble(nodepath.c_str()));
454 nodepath += "/config/roll-offset-deg";
455 fgSetDouble("/sim/current-view/config/roll-offset-deg",
456 fgGetDouble(nodepath.c_str()));
459 nodepath += "/config/default-field-of-view-deg";
460 fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
461 fgGetDouble(nodepath.c_str()));
464 nodepath += "/config/from-model";
465 fgSetBool("/sim/current-view/config/from-model",
466 fgGetBool(nodepath.c_str()));
470 fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
471 fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
472 fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
473 fgSetDouble("/sim/current-view/goal-heading-offset-deg",
474 get_current_view()->getGoalHeadingOffset_deg());
475 fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
476 get_current_view()->getGoalPitchOffset_deg());
477 fgSetDouble("/sim/current-view/goal-roll-offset-deg",
478 get_current_view()->getRollOffset_deg());
479 fgSetDouble("/sim/current-view/heading-offset-deg",
480 get_current_view()->getHeadingOffset_deg());
481 fgSetDouble("/sim/current-view/pitch-offset-deg",
482 get_current_view()->getPitchOffset_deg());
483 fgSetDouble("/sim/current-view/roll-offset-deg",
484 get_current_view()->getRollOffset_deg());
485 fgSetDouble("/sim/current-view/target-x-offset-m",
486 get_current_view()->getTargetXOffset_m());
487 fgSetDouble("/sim/current-view/target-y-offset-m",
488 get_current_view()->getTargetYOffset_m());
489 fgSetDouble("/sim/current-view/target-z-offset-m",
490 get_current_view()->getTargetZOffset_m());
492 fgSetBool("/sim/current-view/internal",
493 get_current_view()->getInternal());
499 FGViewMgr::getViewHeadingOffset_deg () const
501 const FGViewer * view = get_current_view();
502 return (view == 0 ? 0 : view->getHeadingOffset_deg());
506 FGViewMgr::setViewHeadingOffset_deg (double offset)
508 FGViewer * view = get_current_view();
510 view->setGoalHeadingOffset_deg(offset);
511 view->setHeadingOffset_deg(offset);
516 FGViewMgr::getViewGoalHeadingOffset_deg () const
518 const FGViewer * view = get_current_view();
519 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
523 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
525 FGViewer * view = get_current_view();
527 view->setGoalHeadingOffset_deg(offset);
531 FGViewMgr::getViewPitchOffset_deg () const
533 const FGViewer * view = get_current_view();
534 return (view == 0 ? 0 : view->getPitchOffset_deg());
538 FGViewMgr::setViewPitchOffset_deg (double tilt)
540 FGViewer * view = get_current_view();
542 view->setGoalPitchOffset_deg(tilt);
543 view->setPitchOffset_deg(tilt);
548 FGViewMgr::getGoalViewPitchOffset_deg () const
550 const FGViewer * view = get_current_view();
551 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
555 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
557 FGViewer * view = get_current_view();
559 view->setGoalPitchOffset_deg(tilt);
563 FGViewMgr::getViewRollOffset_deg () const
565 const FGViewer * view = get_current_view();
566 return (view == 0 ? 0 : view->getRollOffset_deg());
570 FGViewMgr::setViewRollOffset_deg (double tilt)
572 FGViewer * view = get_current_view();
574 view->setGoalRollOffset_deg(tilt);
575 view->setRollOffset_deg(tilt);
580 FGViewMgr::getGoalViewRollOffset_deg () const
582 const FGViewer * view = get_current_view();
583 return (view == 0 ? 0 : view->getGoalRollOffset_deg());
587 FGViewMgr::setGoalViewRollOffset_deg (double tilt)
589 FGViewer * view = get_current_view();
591 view->setGoalRollOffset_deg(tilt);
595 FGViewMgr::getViewXOffset_m () const
597 const FGViewer * view = get_current_view();
599 return ((FGViewer *)view)->getXOffset_m();
606 FGViewMgr::setViewXOffset_m (double x)
608 FGViewer * view = get_current_view();
610 view->setXOffset_m(x);
615 FGViewMgr::getViewYOffset_m () const
617 const FGViewer * view = get_current_view();
619 return ((FGViewer *)view)->getYOffset_m();
626 FGViewMgr::setViewYOffset_m (double y)
628 FGViewer * view = get_current_view();
630 view->setYOffset_m(y);
635 FGViewMgr::getViewZOffset_m () const
637 const FGViewer * view = get_current_view();
639 return ((FGViewer *)view)->getZOffset_m();
646 FGViewMgr::setViewZOffset_m (double z)
648 FGViewer * view = get_current_view();
650 view->setZOffset_m(z);
655 FGViewMgr::getViewTargetXOffset_m () const
657 const FGViewer * view = get_current_view();
659 return ((FGViewer *)view)->getTargetXOffset_m();
666 FGViewMgr::setViewTargetXOffset_m (double x)
668 FGViewer * view = get_current_view();
670 view->setTargetXOffset_m(x);
675 FGViewMgr::getViewTargetYOffset_m () const
677 const FGViewer * view = get_current_view();
679 return ((FGViewer *)view)->getTargetYOffset_m();
686 FGViewMgr::setViewTargetYOffset_m (double y)
688 FGViewer * view = get_current_view();
690 view->setTargetYOffset_m(y);
695 FGViewMgr::getViewTargetZOffset_m () const
697 const FGViewer * view = get_current_view();
699 return ((FGViewer *)view)->getTargetZOffset_m();
706 FGViewMgr::setViewTargetZOffset_m (double z)
708 FGViewer * view = get_current_view();
710 view->setTargetZOffset_m(z);
715 FGViewMgr::getView () const
721 FGViewMgr::setView (int newview )
723 // if newview number too low wrap to last view...
725 newview = (int)views.size() -1;
727 // if newview number to high wrap to zero...
728 if ( newview > ((int)views.size() -1) ) {
736 // Copy the fdm's position into the SGLocation which is shared with
738 globals->get_aircraft_model()->update(0);
745 FGViewMgr::getFOV_deg () const
747 const FGViewer * view = get_current_view();
748 return (view == 0 ? 0 : view->get_fov());
752 FGViewMgr::setFOV_deg (double fov)
754 FGViewer * view = get_current_view();
760 FGViewMgr::getARM_deg () const
762 const FGViewer * view = get_current_view();
763 return (view == 0 ? 0 : view->get_aspect_ratio_multiplier());
767 FGViewMgr::setARM_deg (double aspect_ratio_multiplier)
769 FGViewer * view = get_current_view();
771 view->set_aspect_ratio_multiplier(aspect_ratio_multiplier);
775 FGViewMgr::getNear_m () const
777 const FGViewer * view = get_current_view();
778 return (view == 0 ? 0.5f : view->getNear_m());
782 FGViewMgr::setNear_m (double near_m)
784 FGViewer * view = get_current_view();
786 view->setNear_m(near_m);
790 FGViewMgr::setViewAxisLong (double axis)
796 FGViewMgr::setViewAxisLat (double axis)
802 FGViewMgr::do_axes ()
804 // Take no action when hat is centered
805 if ( ( axis_long < 0.01 ) &&
806 ( axis_long > -0.01 ) &&
807 ( axis_lat < 0.01 ) &&
812 double viewDir = 999;
814 /* Do all the quick and easy cases */
815 if (axis_long < 0) { // Longitudinal axis forward
816 if (axis_lat == axis_long)
817 viewDir = fgGetDouble("/sim/view/config/front-left-direction-deg");
818 else if (axis_lat == - axis_long)
819 viewDir = fgGetDouble("/sim/view/config/front-right-direction-deg");
820 else if (axis_lat == 0)
821 viewDir = fgGetDouble("/sim/view/config/front-direction-deg");
822 } else if (axis_long > 0) { // Longitudinal axis backward
823 if (axis_lat == - axis_long)
824 viewDir = fgGetDouble("/sim/view/config/back-left-direction-deg");
825 else if (axis_lat == axis_long)
826 viewDir = fgGetDouble("/sim/view/config/back-right-direction-deg");
827 else if (axis_lat == 0)
828 viewDir = fgGetDouble("/sim/view/config/back-direction-deg");
829 } else if (axis_long == 0) { // Longitudinal axis neutral
831 viewDir = fgGetDouble("/sim/view/config/left-direction-deg");
832 else if (axis_lat > 0)
833 viewDir = fgGetDouble("/sim/view/config/right-direction-deg");
834 else return; /* And assertion failure maybe? */
837 // Do all the difficult cases
839 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
840 if ( viewDir < -1 ) viewDir += 360;
842 get_current_view()->setGoalHeadingOffset_deg(viewDir);