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
33 #include <simgear/compiler.h>
35 #include <Model/acmodel.hxx>
37 #include "viewmgr.hxx"
38 #include "fg_props.hxx"
42 FGViewMgr::FGViewMgr( void ) :
51 FGViewMgr::~FGViewMgr( void ) {
58 string viewpath, nodepath, strdata;
59 bool from_model = false;
60 bool at_model = false;
61 int from_model_index = 0;
62 int at_model_index = 0;
64 double damp_roll = 0.0, damp_pitch = 0.0, damp_heading = 0.0;
65 double x_offset_m, y_offset_m, z_offset_m, fov_deg;
66 double heading_offset_deg, pitch_offset_deg, roll_offset_deg;
67 double target_x_offset_m, target_y_offset_m, target_z_offset_m;
71 double aspect_ratio_multiplier
72 = fgGetDouble("/sim/current-view/aspect-ratio-multiplier");
74 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
75 viewpath = "/sim/view";
76 sprintf(stridx, "[%d]", i);
79 // find out what type of view this is...
82 strdata = fgGetString(nodepath.c_str());
84 // find out if this is an internal view (e.g. in cockpit, low near plane)
85 internal = false; // default
87 nodepath += "/internal";
88 internal = fgGetBool(nodepath.c_str());
91 // this is assumed to be an aircraft model...we will need to read
92 // model-from-type as well.
93 // find out if this is a model we are looking from...
95 nodepath += "/config/from-model";
96 from_model = fgGetBool(nodepath.c_str());
98 // get model index (which model)
101 nodepath += "/config/from-model-idx";
102 from_model_index = fgGetInt(nodepath.c_str());
105 if ( strcmp("lookat",strdata.c_str()) == 0 ) {
106 // find out if this is a model we are looking at...
108 nodepath += "/config/at-model";
109 at_model = fgGetBool(nodepath.c_str());
111 // get model index (which model)
114 nodepath += "/config/at-model-idx";
115 at_model_index = fgGetInt(nodepath.c_str());
118 nodepath += "/config/at-model-roll-damping";
119 damp_roll = fgGetDouble(nodepath.c_str(), 0.0);
121 nodepath += "/config/at-model-pitch-damping";
122 damp_pitch = fgGetDouble(nodepath.c_str(), 0.0);
124 nodepath += "/config/at-model-heading-damping";
125 damp_heading = fgGetDouble(nodepath.c_str(), 0.0);
130 nodepath += "/config/x-offset-m";
131 x_offset_m = fgGetDouble(nodepath.c_str());
133 nodepath += "/config/y-offset-m";
134 y_offset_m = fgGetDouble(nodepath.c_str());
136 nodepath += "/config/z-offset-m";
137 z_offset_m = fgGetDouble(nodepath.c_str());
139 nodepath += "/config/pitch-offset-deg";
140 pitch_offset_deg = fgGetDouble(nodepath.c_str());
141 fgSetDouble(nodepath.c_str(),pitch_offset_deg);
143 nodepath += "/config/heading-offset-deg";
144 heading_offset_deg = fgGetDouble(nodepath.c_str());
145 fgSetDouble(nodepath.c_str(),heading_offset_deg);
147 nodepath += "/config/roll-offset-deg";
148 roll_offset_deg = fgGetDouble(nodepath.c_str());
149 fgSetDouble(nodepath.c_str(),roll_offset_deg);
151 nodepath += "/config/default-field-of-view-deg";
152 fov_deg = fgGetDouble(nodepath.c_str());
154 // target offsets for lookat mode only...
156 nodepath += "/config/target-x-offset-m";
157 target_x_offset_m = fgGetDouble(nodepath.c_str());
159 nodepath += "/config/target-y-offset-m";
160 target_y_offset_m = fgGetDouble(nodepath.c_str());
162 nodepath += "/config/target-z-offset-m";
163 target_z_offset_m = fgGetDouble(nodepath.c_str());
166 nodepath += "/config/ground-level-nearplane-m";
167 near_m = fgGetDouble(nodepath.c_str());
169 // supporting two types now "lookat" = 1 and "lookfrom" = 0
170 if ( strcmp("lookat",strdata.c_str()) == 0 )
171 add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
172 at_model, at_model_index,
173 damp_roll, damp_pitch, damp_heading,
174 x_offset_m, y_offset_m,z_offset_m,
175 heading_offset_deg, pitch_offset_deg,
176 roll_offset_deg, fov_deg, aspect_ratio_multiplier,
177 target_x_offset_m, target_y_offset_m,
178 target_z_offset_m, near_m, internal ));
180 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index,
181 false, 0, 0.0, 0.0, 0.0,
182 x_offset_m, y_offset_m, z_offset_m,
183 heading_offset_deg, pitch_offset_deg,
184 roll_offset_deg, fov_deg, aspect_ratio_multiplier,
185 0, 0, 0, near_m, internal ));
196 string viewpath, nodepath, strdata;
199 // reset offsets and fov to configuration defaults
201 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
202 viewpath = "/sim/view";
203 sprintf(stridx, "[%d]", i);
209 nodepath += "/config/x-offset-m";
210 fgSetDouble("/sim/current-view/x-offset-m",fgGetDouble(nodepath.c_str()));
213 nodepath += "/config/y-offset-m";
214 fgSetDouble("/sim/current-view/y-offset-m",fgGetDouble(nodepath.c_str()));
217 nodepath += "/config/z-offset-m";
218 fgSetDouble("/sim/current-view/z-offset-m",fgGetDouble(nodepath.c_str()));
221 nodepath += "/config/pitch-offset-deg";
222 fgSetDouble("/sim/current-view/pitch-offset-deg",
223 fgGetDouble(nodepath.c_str()));
226 nodepath += "/config/heading-offset-deg";
227 fgSetDouble("/sim/current-view/heading-offset-deg",
228 fgGetDouble(nodepath.c_str()));
231 nodepath += "/config/roll-offset-deg";
232 fgSetDouble("/sim/current-view/roll-offset-deg",
233 fgGetDouble(nodepath.c_str()));
236 nodepath += "/config/default-field-of-view-deg";
237 fov_deg = fgGetDouble(nodepath.c_str());
238 if (fov_deg < 10.0) {
241 fgSetDouble("/sim/current-view/field-of-view",fov_deg);
243 // target offsets for lookat mode only...
245 nodepath += "/config/target-x-offset-m";
246 fgSetDouble("/sim/current-view/target-x-offset-deg",
247 fgGetDouble(nodepath.c_str()));
250 nodepath += "/config/target-y-offset-m";
251 fgSetDouble("/sim/current-view/target-y-offset-deg",
252 fgGetDouble(nodepath.c_str()));
255 nodepath += "/config/target-z-offset-m";
256 fgSetDouble("/sim/current-view/target-z-offset-deg",
257 fgGetDouble(nodepath.c_str()));
265 typedef double (FGViewMgr::*double_getter)() const;
270 // these are bound to the current view properties
271 fgTie("/sim/current-view/heading-offset-deg", this,
272 &FGViewMgr::getViewHeadingOffset_deg,
273 &FGViewMgr::setViewHeadingOffset_deg);
274 fgSetArchivable("/sim/current-view/heading-offset-deg");
275 fgTie("/sim/current-view/goal-heading-offset-deg", this,
276 &FGViewMgr::getViewGoalHeadingOffset_deg,
277 &FGViewMgr::setViewGoalHeadingOffset_deg);
278 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
279 fgTie("/sim/current-view/pitch-offset-deg", this,
280 &FGViewMgr::getViewPitchOffset_deg,
281 &FGViewMgr::setViewPitchOffset_deg);
282 fgSetArchivable("/sim/current-view/pitch-offset-deg");
283 fgTie("/sim/current-view/goal-pitch-offset-deg", this,
284 &FGViewMgr::getGoalViewPitchOffset_deg,
285 &FGViewMgr::setGoalViewPitchOffset_deg);
286 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
287 fgTie("/sim/current-view/roll-offset-deg", this,
288 &FGViewMgr::getViewRollOffset_deg,
289 &FGViewMgr::setViewRollOffset_deg);
290 fgSetArchivable("/sim/current-view/roll-offset-deg");
291 fgTie("/sim/current-view/goal-roll-offset-deg", this,
292 &FGViewMgr::getGoalViewRollOffset_deg,
293 &FGViewMgr::setGoalViewRollOffset_deg);
294 fgSetArchivable("/sim/current-view/goal-roll-offset-deg");
296 fgTie("/sim/current-view/view-number", this,
297 &FGViewMgr::getView, &FGViewMgr::setView);
298 fgSetArchivable("/sim/current-view/view-number", FALSE);
300 fgTie("/sim/current-view/axes/long", this,
301 (double_getter)0, &FGViewMgr::setViewAxisLong);
302 fgSetArchivable("/sim/current-view/axes/long");
304 fgTie("/sim/current-view/axes/lat", this,
305 (double_getter)0, &FGViewMgr::setViewAxisLat);
306 fgSetArchivable("/sim/current-view/axes/lat");
308 fgTie("/sim/current-view/field-of-view", this,
309 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
310 fgSetArchivable("/sim/current-view/field-of-view");
312 fgTie("/sim/current-view/aspect-ratio-multiplier", this,
313 &FGViewMgr::getARM_deg, &FGViewMgr::setARM_deg);
314 fgSetArchivable("/sim/current-view/field-of-view");
316 fgTie("/sim/current-view/ground-level-nearplane-m", this,
317 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
318 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
326 // need to redo these bindings to the new locations (move to viewer?)
327 fgUntie("/sim/current-view/heading-offset-deg");
328 fgUntie("/sim/current-view/goal-heading-offset-deg");
329 fgUntie("/sim/current-view/pitch-offset-deg");
330 fgUntie("/sim/current-view/goal-pitch-offset-deg");
331 fgUntie("/sim/current-view/field-of-view");
332 fgUntie("/sim/current-view/aspect-ratio-multiplier");
333 fgUntie("/sim/current-view/view-number");
334 fgUntie("/sim/current-view/axes/long");
335 fgUntie("/sim/current-view/axes/lat");
336 fgUntie("/sim/current-view/ground-level-nearplane-m");
340 FGViewMgr::update (double dt)
343 string viewpath, nodepath;
344 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
346 FGViewer * view = get_current_view();
352 viewpath = "/sim/view";
353 sprintf(stridx, "[%d]", i);
356 FGViewer *loop_view = (FGViewer *)get_view( i );
358 // Set up view location and orientation
361 nodepath += "/config/from-model";
362 if (!fgGetBool(nodepath.c_str())) {
364 nodepath += "/config/eye-lon-deg-path";
365 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
367 nodepath += "/config/eye-lat-deg-path";
368 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
370 nodepath += "/config/eye-alt-ft-path";
371 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
373 nodepath += "/config/eye-roll-deg-path";
374 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
376 nodepath += "/config/eye-pitch-deg-path";
377 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
379 nodepath += "/config/eye-heading-deg-path";
380 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
381 loop_view->setPosition(lon_deg, lat_deg, alt_ft);
382 loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
384 // force recalc in viewer
385 loop_view->set_dirty();
388 // if lookat (type 1) then get target data...
389 if (loop_view->getType() == FG_LOOKAT) {
391 nodepath += "/config/from-model";
392 if (!fgGetBool(nodepath.c_str())) {
394 nodepath += "/config/target-lon-deg-path";
395 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
397 nodepath += "/config/target-lat-deg-path";
398 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
400 nodepath += "/config/target-alt-ft-path";
401 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
403 nodepath += "/config/target-roll-deg-path";
404 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
406 nodepath += "/config/target-pitch-deg-path";
407 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
409 nodepath += "/config/target-heading-deg-path";
410 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
412 loop_view->setTargetPosition(lon_deg, lat_deg, alt_ft);
413 loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
415 loop_view->set_dirty();
419 setViewXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
420 setViewYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
421 setViewZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
423 setViewTargetXOffset_m(fgGetDouble("/sim/current-view/target-x-offset-m"));
424 setViewTargetYOffset_m(fgGetDouble("/sim/current-view/target-y-offset-m"));
425 setViewTargetZOffset_m(fgGetDouble("/sim/current-view/target-z-offset-m"));
427 // Update the current view
433 FGViewMgr::copyToCurrent()
436 string viewpath, nodepath;
439 viewpath = "/sim/view";
440 sprintf(stridx, "[%d]", i);
443 // copy certain view config data for default values
445 nodepath += "/config/default-heading-offset-deg";
446 fgSetDouble("/sim/current-view/config/heading-offset-deg",
447 fgGetDouble(nodepath.c_str()));
450 nodepath += "/config/pitch-offset-deg";
451 fgSetDouble("/sim/current-view/config/pitch-offset-deg",
452 fgGetDouble(nodepath.c_str()));
455 nodepath += "/config/roll-offset-deg";
456 fgSetDouble("/sim/current-view/config/roll-offset-deg",
457 fgGetDouble(nodepath.c_str()));
460 nodepath += "/config/default-field-of-view-deg";
461 fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
462 fgGetDouble(nodepath.c_str()));
465 nodepath += "/config/from-model";
466 fgSetBool("/sim/current-view/config/from-model",
467 fgGetBool(nodepath.c_str()));
471 fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
472 fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
473 fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
474 fgSetDouble("/sim/current-view/goal-heading-offset-deg",
475 get_current_view()->getGoalHeadingOffset_deg());
476 fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
477 get_current_view()->getGoalPitchOffset_deg());
478 fgSetDouble("/sim/current-view/goal-roll-offset-deg",
479 get_current_view()->getRollOffset_deg());
480 fgSetDouble("/sim/current-view/heading-offset-deg",
481 get_current_view()->getHeadingOffset_deg());
482 fgSetDouble("/sim/current-view/pitch-offset-deg",
483 get_current_view()->getPitchOffset_deg());
484 fgSetDouble("/sim/current-view/roll-offset-deg",
485 get_current_view()->getRollOffset_deg());
486 fgSetDouble("/sim/current-view/target-x-offset-m",
487 get_current_view()->getTargetXOffset_m());
488 fgSetDouble("/sim/current-view/target-y-offset-m",
489 get_current_view()->getTargetYOffset_m());
490 fgSetDouble("/sim/current-view/target-z-offset-m",
491 get_current_view()->getTargetZOffset_m());
493 fgSetBool("/sim/current-view/internal",
494 get_current_view()->getInternal());
500 FGViewMgr::getViewHeadingOffset_deg () const
502 const FGViewer * view = get_current_view();
503 return (view == 0 ? 0 : view->getHeadingOffset_deg());
507 FGViewMgr::setViewHeadingOffset_deg (double offset)
509 FGViewer * view = get_current_view();
511 view->setGoalHeadingOffset_deg(offset);
512 view->setHeadingOffset_deg(offset);
517 FGViewMgr::getViewGoalHeadingOffset_deg () const
519 const FGViewer * view = get_current_view();
520 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
524 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
526 FGViewer * view = get_current_view();
528 view->setGoalHeadingOffset_deg(offset);
532 FGViewMgr::getViewPitchOffset_deg () const
534 const FGViewer * view = get_current_view();
535 return (view == 0 ? 0 : view->getPitchOffset_deg());
539 FGViewMgr::setViewPitchOffset_deg (double tilt)
541 FGViewer * view = get_current_view();
543 view->setGoalPitchOffset_deg(tilt);
544 view->setPitchOffset_deg(tilt);
549 FGViewMgr::getGoalViewPitchOffset_deg () const
551 const FGViewer * view = get_current_view();
552 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
556 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
558 FGViewer * view = get_current_view();
560 view->setGoalPitchOffset_deg(tilt);
564 FGViewMgr::getViewRollOffset_deg () const
566 const FGViewer * view = get_current_view();
567 return (view == 0 ? 0 : view->getRollOffset_deg());
571 FGViewMgr::setViewRollOffset_deg (double tilt)
573 FGViewer * view = get_current_view();
575 view->setGoalRollOffset_deg(tilt);
576 view->setRollOffset_deg(tilt);
581 FGViewMgr::getGoalViewRollOffset_deg () const
583 const FGViewer * view = get_current_view();
584 return (view == 0 ? 0 : view->getGoalRollOffset_deg());
588 FGViewMgr::setGoalViewRollOffset_deg (double tilt)
590 FGViewer * view = get_current_view();
592 view->setGoalRollOffset_deg(tilt);
596 FGViewMgr::getViewXOffset_m () const
598 const FGViewer * view = get_current_view();
600 return ((FGViewer *)view)->getXOffset_m();
607 FGViewMgr::setViewXOffset_m (double x)
609 FGViewer * view = get_current_view();
611 view->setXOffset_m(x);
616 FGViewMgr::getViewYOffset_m () const
618 const FGViewer * view = get_current_view();
620 return ((FGViewer *)view)->getYOffset_m();
627 FGViewMgr::setViewYOffset_m (double y)
629 FGViewer * view = get_current_view();
631 view->setYOffset_m(y);
636 FGViewMgr::getViewZOffset_m () const
638 const FGViewer * view = get_current_view();
640 return ((FGViewer *)view)->getZOffset_m();
647 FGViewMgr::setViewZOffset_m (double z)
649 FGViewer * view = get_current_view();
651 view->setZOffset_m(z);
656 FGViewMgr::getViewTargetXOffset_m () const
658 const FGViewer * view = get_current_view();
660 return ((FGViewer *)view)->getTargetXOffset_m();
667 FGViewMgr::setViewTargetXOffset_m (double x)
669 FGViewer * view = get_current_view();
671 view->setTargetXOffset_m(x);
676 FGViewMgr::getViewTargetYOffset_m () const
678 const FGViewer * view = get_current_view();
680 return ((FGViewer *)view)->getTargetYOffset_m();
687 FGViewMgr::setViewTargetYOffset_m (double y)
689 FGViewer * view = get_current_view();
691 view->setTargetYOffset_m(y);
696 FGViewMgr::getViewTargetZOffset_m () const
698 const FGViewer * view = get_current_view();
700 return ((FGViewer *)view)->getTargetZOffset_m();
707 FGViewMgr::setViewTargetZOffset_m (double z)
709 FGViewer * view = get_current_view();
711 view->setTargetZOffset_m(z);
716 FGViewMgr::getView () const
722 FGViewMgr::setView (int newview )
724 // if newview number too low wrap to last view...
726 newview = (int)views.size() -1;
728 // if newview number to high wrap to zero...
729 if ( newview > ((int)views.size() -1) ) {
737 // Copy the fdm's position into the SGLocation which is shared with
739 globals->get_aircraft_model()->update(0);
746 FGViewMgr::getFOV_deg () const
748 const FGViewer * view = get_current_view();
749 return (view == 0 ? 0 : view->get_fov());
753 FGViewMgr::setFOV_deg (double fov)
755 FGViewer * view = get_current_view();
761 FGViewMgr::getARM_deg () const
763 const FGViewer * view = get_current_view();
764 return (view == 0 ? 0 : view->get_aspect_ratio_multiplier());
768 FGViewMgr::setARM_deg (double aspect_ratio_multiplier)
770 FGViewer * view = get_current_view();
772 view->set_aspect_ratio_multiplier(aspect_ratio_multiplier);
776 FGViewMgr::getNear_m () const
778 const FGViewer * view = get_current_view();
779 return (view == 0 ? 0.5f : view->getNear_m());
783 FGViewMgr::setNear_m (double near_m)
785 FGViewer * view = get_current_view();
787 view->setNear_m(near_m);
791 FGViewMgr::setViewAxisLong (double axis)
797 FGViewMgr::setViewAxisLat (double axis)
803 FGViewMgr::do_axes ()
805 // Take no action when hat is centered
806 if ( ( axis_long < 0.01 ) &&
807 ( axis_long > -0.01 ) &&
808 ( axis_lat < 0.01 ) &&
813 double viewDir = 999;
815 /* Do all the quick and easy cases */
816 if (axis_long < 0) { // Longitudinal axis forward
817 if (axis_lat == axis_long)
818 viewDir = fgGetDouble("/sim/view/config/front-left-direction-deg");
819 else if (axis_lat == - axis_long)
820 viewDir = fgGetDouble("/sim/view/config/front-right-direction-deg");
821 else if (axis_lat == 0)
822 viewDir = fgGetDouble("/sim/view/config/front-direction-deg");
823 } else if (axis_long > 0) { // Longitudinal axis backward
824 if (axis_lat == - axis_long)
825 viewDir = fgGetDouble("/sim/view/config/back-left-direction-deg");
826 else if (axis_lat == axis_long)
827 viewDir = fgGetDouble("/sim/view/config/back-right-direction-deg");
828 else if (axis_lat == 0)
829 viewDir = fgGetDouble("/sim/view/config/back-direction-deg");
830 } else if (axis_long == 0) { // Longitudinal axis neutral
832 viewDir = fgGetDouble("/sim/view/config/left-direction-deg");
833 else if (axis_lat > 0)
834 viewDir = fgGetDouble("/sim/view/config/right-direction-deg");
835 else return; /* And assertion failure maybe? */
838 // Do all the difficult cases
840 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
841 if ( viewDir < -1 ) viewDir += 360;
843 get_current_view()->setGoalHeadingOffset_deg(viewDir);