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
29 #include <simgear/compiler.h>
31 #include <Model/acmodel.hxx>
33 #include "viewmgr.hxx"
34 #include "fg_props.hxx"
38 FGViewMgr::FGViewMgr( void ) :
47 FGViewMgr::~FGViewMgr( void ) {
54 string viewpath, nodepath, strdata;
55 bool from_model = false;
56 bool at_model = false;
57 int from_model_index = 0;
58 int at_model_index = 0;
60 double damp_roll = 0.0, damp_pitch = 0.0, damp_heading = 0.0;
61 double x_offset_m, y_offset_m, z_offset_m, fov_deg;
62 double heading_offset_deg, pitch_offset_deg, roll_offset_deg;
63 double target_x_offset_m, target_y_offset_m, target_z_offset_m;
67 double aspect_ratio_multiplier
68 = fgGetDouble("/sim/current-view/aspect-ratio-multiplier");
70 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
71 viewpath = "/sim/view";
72 sprintf(stridx, "[%d]", i);
75 // find out what type of view this is...
78 strdata = fgGetString(nodepath.c_str());
80 // find out if this is an internal view (e.g. in cockpit, low near plane)
81 internal = false; // default
83 nodepath += "/internal";
84 internal = fgGetBool(nodepath.c_str());
87 // this is assumed to be an aircraft model...we will need to read
88 // model-from-type as well.
89 // find out if this is a model we are looking from...
91 nodepath += "/config/from-model";
92 from_model = fgGetBool(nodepath.c_str());
94 // get model index (which model)
97 nodepath += "/config/from-model-idx";
98 from_model_index = fgGetInt(nodepath.c_str());
101 if ( strcmp("lookat",strdata.c_str()) == 0 ) {
102 // find out if this is a model we are looking at...
104 nodepath += "/config/at-model";
105 at_model = fgGetBool(nodepath.c_str());
107 // get model index (which model)
110 nodepath += "/config/at-model-idx";
111 at_model_index = fgGetInt(nodepath.c_str());
114 nodepath += "/config/at-model-roll-damping";
115 damp_roll = fgGetDouble(nodepath.c_str(), 0.0);
117 nodepath += "/config/at-model-pitch-damping";
118 damp_pitch = fgGetDouble(nodepath.c_str(), 0.0);
120 nodepath += "/config/at-model-heading-damping";
121 damp_heading = fgGetDouble(nodepath.c_str(), 0.0);
126 nodepath += "/config/x-offset-m";
127 x_offset_m = fgGetDouble(nodepath.c_str());
129 nodepath += "/config/y-offset-m";
130 y_offset_m = fgGetDouble(nodepath.c_str());
132 nodepath += "/config/z-offset-m";
133 z_offset_m = fgGetDouble(nodepath.c_str());
135 nodepath += "/config/pitch-offset-deg";
136 pitch_offset_deg = fgGetDouble(nodepath.c_str());
137 fgSetDouble(nodepath.c_str(),pitch_offset_deg);
139 nodepath += "/config/heading-offset-deg";
140 heading_offset_deg = fgGetDouble(nodepath.c_str());
141 fgSetDouble(nodepath.c_str(),heading_offset_deg);
143 nodepath += "/config/roll-offset-deg";
144 roll_offset_deg = fgGetDouble(nodepath.c_str());
145 fgSetDouble(nodepath.c_str(),roll_offset_deg);
147 nodepath += "/config/default-field-of-view-deg";
148 fov_deg = fgGetDouble(nodepath.c_str());
150 // target offsets for lookat mode only...
152 nodepath += "/config/target-x-offset-m";
153 target_x_offset_m = fgGetDouble(nodepath.c_str());
155 nodepath += "/config/target-y-offset-m";
156 target_y_offset_m = fgGetDouble(nodepath.c_str());
158 nodepath += "/config/target-z-offset-m";
159 target_z_offset_m = fgGetDouble(nodepath.c_str());
162 nodepath += "/config/ground-level-nearplane-m";
163 near_m = fgGetDouble(nodepath.c_str());
165 // supporting two types now "lookat" = 1 and "lookfrom" = 0
166 if ( strcmp("lookat",strdata.c_str()) == 0 )
167 add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
168 at_model, at_model_index,
169 damp_roll, damp_pitch, damp_heading,
170 x_offset_m, y_offset_m,z_offset_m,
171 heading_offset_deg, pitch_offset_deg,
172 roll_offset_deg, fov_deg, aspect_ratio_multiplier,
173 target_x_offset_m, target_y_offset_m,
174 target_z_offset_m, near_m, internal ));
176 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index,
177 false, 0, 0.0, 0.0, 0.0,
178 x_offset_m, y_offset_m, z_offset_m,
179 heading_offset_deg, pitch_offset_deg,
180 roll_offset_deg, fov_deg, aspect_ratio_multiplier,
181 0, 0, 0, near_m, internal ));
192 string viewpath, nodepath, strdata;
195 // reset offsets and fov to configuration defaults
197 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
198 viewpath = "/sim/view";
199 sprintf(stridx, "[%d]", i);
205 nodepath += "/config/x-offset-m";
206 fgSetDouble("/sim/current-view/x-offset-m",fgGetDouble(nodepath.c_str()));
209 nodepath += "/config/y-offset-m";
210 fgSetDouble("/sim/current-view/y-offset-m",fgGetDouble(nodepath.c_str()));
213 nodepath += "/config/z-offset-m";
214 fgSetDouble("/sim/current-view/z-offset-m",fgGetDouble(nodepath.c_str()));
217 nodepath += "/config/pitch-offset-deg";
218 fgSetDouble("/sim/current-view/pitch-offset-deg",
219 fgGetDouble(nodepath.c_str()));
222 nodepath += "/config/heading-offset-deg";
223 fgSetDouble("/sim/current-view/heading-offset-deg",
224 fgGetDouble(nodepath.c_str()));
227 nodepath += "/config/roll-offset-deg";
228 fgSetDouble("/sim/current-view/roll-offset-deg",
229 fgGetDouble(nodepath.c_str()));
232 nodepath += "/config/default-field-of-view-deg";
233 fov_deg = fgGetDouble(nodepath.c_str());
234 if (fov_deg < 10.0) {
237 fgSetDouble("/sim/current-view/field-of-view",fov_deg);
239 // target offsets for lookat mode only...
241 nodepath += "/config/target-x-offset-m";
242 fgSetDouble("/sim/current-view/target-x-offset-deg",
243 fgGetDouble(nodepath.c_str()));
246 nodepath += "/config/target-y-offset-m";
247 fgSetDouble("/sim/current-view/target-y-offset-deg",
248 fgGetDouble(nodepath.c_str()));
251 nodepath += "/config/target-z-offset-m";
252 fgSetDouble("/sim/current-view/target-z-offset-deg",
253 fgGetDouble(nodepath.c_str()));
261 typedef double (FGViewMgr::*double_getter)() const;
266 // these are bound to the current view properties
267 fgTie("/sim/current-view/heading-offset-deg", this,
268 &FGViewMgr::getViewHeadingOffset_deg,
269 &FGViewMgr::setViewHeadingOffset_deg);
270 fgSetArchivable("/sim/current-view/heading-offset-deg");
271 fgTie("/sim/current-view/goal-heading-offset-deg", this,
272 &FGViewMgr::getViewGoalHeadingOffset_deg,
273 &FGViewMgr::setViewGoalHeadingOffset_deg);
274 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
275 fgTie("/sim/current-view/pitch-offset-deg", this,
276 &FGViewMgr::getViewPitchOffset_deg,
277 &FGViewMgr::setViewPitchOffset_deg);
278 fgSetArchivable("/sim/current-view/pitch-offset-deg");
279 fgTie("/sim/current-view/goal-pitch-offset-deg", this,
280 &FGViewMgr::getGoalViewPitchOffset_deg,
281 &FGViewMgr::setGoalViewPitchOffset_deg);
282 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
283 fgTie("/sim/current-view/roll-offset-deg", this,
284 &FGViewMgr::getViewRollOffset_deg,
285 &FGViewMgr::setViewRollOffset_deg);
286 fgSetArchivable("/sim/current-view/roll-offset-deg");
287 fgTie("/sim/current-view/goal-roll-offset-deg", this,
288 &FGViewMgr::getGoalViewRollOffset_deg,
289 &FGViewMgr::setGoalViewRollOffset_deg);
290 fgSetArchivable("/sim/current-view/goal-roll-offset-deg");
292 fgTie("/sim/current-view/view-number", this,
293 &FGViewMgr::getView, &FGViewMgr::setView);
294 fgSetArchivable("/sim/current-view/view-number", FALSE);
296 fgTie("/sim/current-view/axes/long", this,
297 (double_getter)0, &FGViewMgr::setViewAxisLong);
298 fgSetArchivable("/sim/current-view/axes/long");
300 fgTie("/sim/current-view/axes/lat", this,
301 (double_getter)0, &FGViewMgr::setViewAxisLat);
302 fgSetArchivable("/sim/current-view/axes/lat");
304 fgTie("/sim/current-view/field-of-view", this,
305 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
306 fgSetArchivable("/sim/current-view/field-of-view");
308 fgTie("/sim/current-view/aspect-ratio-multiplier", this,
309 &FGViewMgr::getARM_deg, &FGViewMgr::setARM_deg);
310 fgSetArchivable("/sim/current-view/field-of-view");
312 fgTie("/sim/current-view/ground-level-nearplane-m", this,
313 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
314 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
322 // need to redo these bindings to the new locations (move to viewer?)
323 fgUntie("/sim/current-view/heading-offset-deg");
324 fgUntie("/sim/current-view/goal-heading-offset-deg");
325 fgUntie("/sim/current-view/pitch-offset-deg");
326 fgUntie("/sim/current-view/goal-pitch-offset-deg");
327 fgUntie("/sim/current-view/field-of-view");
328 fgUntie("/sim/current-view/aspect-ratio-multiplier");
329 fgUntie("/sim/current-view/view-number");
330 fgUntie("/sim/current-view/axes/long");
331 fgUntie("/sim/current-view/axes/lat");
332 fgUntie("/sim/current-view/ground-level-nearplane-m");
336 FGViewMgr::update (double dt)
339 string viewpath, nodepath;
340 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
342 FGViewer * view = get_current_view();
348 viewpath = "/sim/view";
349 sprintf(stridx, "[%d]", i);
352 FGViewer *loop_view = (FGViewer *)get_view( i );
354 // Set up view location and orientation
357 nodepath += "/config/from-model";
358 if (!fgGetBool(nodepath.c_str())) {
360 nodepath += "/config/eye-lon-deg-path";
361 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
363 nodepath += "/config/eye-lat-deg-path";
364 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
366 nodepath += "/config/eye-alt-ft-path";
367 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
369 nodepath += "/config/eye-roll-deg-path";
370 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
372 nodepath += "/config/eye-pitch-deg-path";
373 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
375 nodepath += "/config/eye-heading-deg-path";
376 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
377 loop_view->setPosition(lon_deg, lat_deg, alt_ft);
378 loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
380 // force recalc in viewer
381 loop_view->set_dirty();
384 // if lookat (type 1) then get target data...
385 if (loop_view->getType() == FG_LOOKAT) {
387 nodepath += "/config/from-model";
388 if (!fgGetBool(nodepath.c_str())) {
390 nodepath += "/config/target-lon-deg-path";
391 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
393 nodepath += "/config/target-lat-deg-path";
394 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
396 nodepath += "/config/target-alt-ft-path";
397 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
399 nodepath += "/config/target-roll-deg-path";
400 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
402 nodepath += "/config/target-pitch-deg-path";
403 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
405 nodepath += "/config/target-heading-deg-path";
406 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
408 loop_view->setTargetPosition(lon_deg, lat_deg, alt_ft);
409 loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
411 loop_view->set_dirty();
415 setViewXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
416 setViewYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
417 setViewZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
419 setViewTargetXOffset_m(fgGetDouble("/sim/current-view/target-x-offset-m"));
420 setViewTargetYOffset_m(fgGetDouble("/sim/current-view/target-y-offset-m"));
421 setViewTargetZOffset_m(fgGetDouble("/sim/current-view/target-z-offset-m"));
423 // Update the current view
429 FGViewMgr::copyToCurrent()
432 string viewpath, nodepath;
435 viewpath = "/sim/view";
436 sprintf(stridx, "[%d]", i);
439 // copy certain view config data for default values
441 nodepath += "/config/default-heading-offset-deg";
442 fgSetDouble("/sim/current-view/config/heading-offset-deg",
443 fgGetDouble(nodepath.c_str()));
446 nodepath += "/config/pitch-offset-deg";
447 fgSetDouble("/sim/current-view/config/pitch-offset-deg",
448 fgGetDouble(nodepath.c_str()));
451 nodepath += "/config/roll-offset-deg";
452 fgSetDouble("/sim/current-view/config/roll-offset-deg",
453 fgGetDouble(nodepath.c_str()));
456 nodepath += "/config/default-field-of-view-deg";
457 fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
458 fgGetDouble(nodepath.c_str()));
461 nodepath += "/config/from-model";
462 fgSetBool("/sim/current-view/config/from-model",
463 fgGetBool(nodepath.c_str()));
467 fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
468 fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
469 fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
470 fgSetDouble("/sim/current-view/goal-heading-offset-deg",
471 get_current_view()->getGoalHeadingOffset_deg());
472 fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
473 get_current_view()->getGoalPitchOffset_deg());
474 fgSetDouble("/sim/current-view/goal-roll-offset-deg",
475 get_current_view()->getRollOffset_deg());
476 fgSetDouble("/sim/current-view/heading-offset-deg",
477 get_current_view()->getHeadingOffset_deg());
478 fgSetDouble("/sim/current-view/pitch-offset-deg",
479 get_current_view()->getPitchOffset_deg());
480 fgSetDouble("/sim/current-view/roll-offset-deg",
481 get_current_view()->getRollOffset_deg());
482 fgSetDouble("/sim/current-view/target-x-offset-m",
483 get_current_view()->getTargetXOffset_m());
484 fgSetDouble("/sim/current-view/target-y-offset-m",
485 get_current_view()->getTargetYOffset_m());
486 fgSetDouble("/sim/current-view/target-z-offset-m",
487 get_current_view()->getTargetZOffset_m());
489 fgSetBool("/sim/current-view/internal",
490 get_current_view()->getInternal());
496 FGViewMgr::getViewHeadingOffset_deg () const
498 const FGViewer * view = get_current_view();
499 return (view == 0 ? 0 : view->getHeadingOffset_deg());
503 FGViewMgr::setViewHeadingOffset_deg (double offset)
505 FGViewer * view = get_current_view();
507 view->setGoalHeadingOffset_deg(offset);
508 view->setHeadingOffset_deg(offset);
513 FGViewMgr::getViewGoalHeadingOffset_deg () const
515 const FGViewer * view = get_current_view();
516 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
520 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
522 FGViewer * view = get_current_view();
524 view->setGoalHeadingOffset_deg(offset);
528 FGViewMgr::getViewPitchOffset_deg () const
530 const FGViewer * view = get_current_view();
531 return (view == 0 ? 0 : view->getPitchOffset_deg());
535 FGViewMgr::setViewPitchOffset_deg (double tilt)
537 FGViewer * view = get_current_view();
539 view->setGoalPitchOffset_deg(tilt);
540 view->setPitchOffset_deg(tilt);
545 FGViewMgr::getGoalViewPitchOffset_deg () const
547 const FGViewer * view = get_current_view();
548 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
552 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
554 FGViewer * view = get_current_view();
556 view->setGoalPitchOffset_deg(tilt);
560 FGViewMgr::getViewRollOffset_deg () const
562 const FGViewer * view = get_current_view();
563 return (view == 0 ? 0 : view->getRollOffset_deg());
567 FGViewMgr::setViewRollOffset_deg (double tilt)
569 FGViewer * view = get_current_view();
571 view->setGoalRollOffset_deg(tilt);
572 view->setRollOffset_deg(tilt);
577 FGViewMgr::getGoalViewRollOffset_deg () const
579 const FGViewer * view = get_current_view();
580 return (view == 0 ? 0 : view->getGoalRollOffset_deg());
584 FGViewMgr::setGoalViewRollOffset_deg (double tilt)
586 FGViewer * view = get_current_view();
588 view->setGoalRollOffset_deg(tilt);
592 FGViewMgr::getViewXOffset_m () const
594 const FGViewer * view = get_current_view();
596 return ((FGViewer *)view)->getXOffset_m();
603 FGViewMgr::setViewXOffset_m (double x)
605 FGViewer * view = get_current_view();
607 view->setXOffset_m(x);
612 FGViewMgr::getViewYOffset_m () const
614 const FGViewer * view = get_current_view();
616 return ((FGViewer *)view)->getYOffset_m();
623 FGViewMgr::setViewYOffset_m (double y)
625 FGViewer * view = get_current_view();
627 view->setYOffset_m(y);
632 FGViewMgr::getViewZOffset_m () const
634 const FGViewer * view = get_current_view();
636 return ((FGViewer *)view)->getZOffset_m();
643 FGViewMgr::setViewZOffset_m (double z)
645 FGViewer * view = get_current_view();
647 view->setZOffset_m(z);
652 FGViewMgr::getViewTargetXOffset_m () const
654 const FGViewer * view = get_current_view();
656 return ((FGViewer *)view)->getTargetXOffset_m();
663 FGViewMgr::setViewTargetXOffset_m (double x)
665 FGViewer * view = get_current_view();
667 view->setTargetXOffset_m(x);
672 FGViewMgr::getViewTargetYOffset_m () const
674 const FGViewer * view = get_current_view();
676 return ((FGViewer *)view)->getTargetYOffset_m();
683 FGViewMgr::setViewTargetYOffset_m (double y)
685 FGViewer * view = get_current_view();
687 view->setTargetYOffset_m(y);
692 FGViewMgr::getViewTargetZOffset_m () const
694 const FGViewer * view = get_current_view();
696 return ((FGViewer *)view)->getTargetZOffset_m();
703 FGViewMgr::setViewTargetZOffset_m (double z)
705 FGViewer * view = get_current_view();
707 view->setTargetZOffset_m(z);
712 FGViewMgr::getView () const
718 FGViewMgr::setView (int newview )
720 // if newview number too low wrap to last view...
722 newview = (int)views.size() -1;
724 // if newview number to high wrap to zero...
725 if ( newview > ((int)views.size() -1) ) {
733 // Copy the fdm's position into the SGLocation which is shared with
735 globals->get_aircraft_model()->update(0);
742 FGViewMgr::getFOV_deg () const
744 const FGViewer * view = get_current_view();
745 return (view == 0 ? 0 : view->get_fov());
749 FGViewMgr::setFOV_deg (double fov)
751 FGViewer * view = get_current_view();
757 FGViewMgr::getARM_deg () const
759 const FGViewer * view = get_current_view();
760 return (view == 0 ? 0 : view->get_aspect_ratio_multiplier());
764 FGViewMgr::setARM_deg (double aspect_ratio_multiplier)
766 FGViewer * view = get_current_view();
768 view->set_aspect_ratio_multiplier(aspect_ratio_multiplier);
772 FGViewMgr::getNear_m () const
774 const FGViewer * view = get_current_view();
775 return (view == 0 ? 0.5f : view->getNear_m());
779 FGViewMgr::setNear_m (double near_m)
781 FGViewer * view = get_current_view();
783 view->setNear_m(near_m);
787 FGViewMgr::setViewAxisLong (double axis)
793 FGViewMgr::setViewAxisLat (double axis)
799 FGViewMgr::do_axes ()
801 // Take no action when hat is centered
802 if ( ( axis_long < 0.01 ) &&
803 ( axis_long > -0.01 ) &&
804 ( axis_lat < 0.01 ) &&
809 double viewDir = 999;
811 /* Do all the quick and easy cases */
812 if (axis_long < 0) { // Longitudinal axis forward
813 if (axis_lat == axis_long)
814 viewDir = fgGetDouble("/sim/view/config/front-left-direction-deg");
815 else if (axis_lat == - axis_long)
816 viewDir = fgGetDouble("/sim/view/config/front-right-direction-deg");
817 else if (axis_lat == 0)
818 viewDir = fgGetDouble("/sim/view/config/front-direction-deg");
819 } else if (axis_long > 0) { // Longitudinal axis backward
820 if (axis_lat == - axis_long)
821 viewDir = fgGetDouble("/sim/view/config/back-left-direction-deg");
822 else if (axis_lat == axis_long)
823 viewDir = fgGetDouble("/sim/view/config/back-right-direction-deg");
824 else if (axis_lat == 0)
825 viewDir = fgGetDouble("/sim/view/config/back-direction-deg");
826 } else if (axis_long == 0) { // Longitudinal axis neutral
828 viewDir = fgGetDouble("/sim/view/config/left-direction-deg");
829 else if (axis_lat > 0)
830 viewDir = fgGetDouble("/sim/view/config/right-direction-deg");
831 else return; /* And assertion failure maybe? */
834 // Do all the difficult cases
836 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
837 if ( viewDir < -1 ) viewDir += 360;
839 get_current_view()->setGoalHeadingOffset_deg(viewDir);