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 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
63 viewpath = "/sim/view";
64 sprintf(stridx, "[%d]", i);
67 // find out what type of view this is...
70 strdata = fgGetString(nodepath.c_str());
72 // find out if this is an internal view (e.g. in cockpit, low near plane)
73 internal = false; // default
75 nodepath += "/internal";
76 internal = fgGetBool(nodepath.c_str());
79 // this is assumed to be an aircraft model...we will need to read
80 // model-from-type as well.
81 // find out if this is a model we are looking from...
83 nodepath += "/config/from-model";
84 from_model = fgGetBool(nodepath.c_str());
86 // get model index (which model)
89 nodepath += "/config/from-model-idx";
90 from_model_index = fgGetInt(nodepath.c_str());
93 if ( strcmp("lookat",strdata.c_str()) == 0 ) {
94 // find out if this is a model we are looking at...
96 nodepath += "/config/at-model";
97 at_model = fgGetBool(nodepath.c_str());
99 // get model index (which model)
102 nodepath += "/config/at-model-idx";
103 at_model_index = fgGetInt(nodepath.c_str());
106 nodepath += "/config/at-model-roll-damping";
107 damp_roll = fgGetDouble(nodepath.c_str(), 0.0);
109 nodepath += "/config/at-model-pitch-damping";
110 damp_pitch = fgGetDouble(nodepath.c_str(), 0.0);
112 nodepath += "/config/at-model-heading-damping";
113 damp_heading = fgGetDouble(nodepath.c_str(), 0.0);
118 nodepath += "/config/x-offset-m";
119 x_offset_m = fgGetDouble(nodepath.c_str());
121 nodepath += "/config/y-offset-m";
122 y_offset_m = fgGetDouble(nodepath.c_str());
124 nodepath += "/config/z-offset-m";
125 z_offset_m = fgGetDouble(nodepath.c_str());
127 nodepath += "/config/pitch-offset-deg";
128 pitch_offset_deg = fgGetDouble(nodepath.c_str());
129 fgSetDouble(nodepath.c_str(),pitch_offset_deg);
131 nodepath += "/config/heading-offset-deg";
132 heading_offset_deg = fgGetDouble(nodepath.c_str());
133 fgSetDouble(nodepath.c_str(),heading_offset_deg);
135 nodepath += "/config/roll-offset-deg";
136 roll_offset_deg = fgGetDouble(nodepath.c_str());
137 fgSetDouble(nodepath.c_str(),roll_offset_deg);
139 nodepath += "/config/default-field-of-view-deg";
140 fov_deg = fgGetDouble(nodepath.c_str());
142 // target offsets for lookat mode only...
144 nodepath += "/config/target-x-offset-m";
145 target_x_offset_m = fgGetDouble(nodepath.c_str());
147 nodepath += "/config/target-y-offset-m";
148 target_y_offset_m = fgGetDouble(nodepath.c_str());
150 nodepath += "/config/target-z-offset-m";
151 target_z_offset_m = fgGetDouble(nodepath.c_str());
154 nodepath += "/config/ground-level-nearplane-m";
155 near_m = fgGetDouble(nodepath.c_str());
157 // supporting two types now "lookat" = 1 and "lookfrom" = 0
158 if ( strcmp("lookat",strdata.c_str()) == 0 )
159 add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
160 at_model, at_model_index,
161 damp_roll, damp_pitch, damp_heading,
162 x_offset_m, y_offset_m,z_offset_m,
163 heading_offset_deg, pitch_offset_deg,
164 roll_offset_deg, fov_deg,
165 target_x_offset_m, target_y_offset_m,
166 target_z_offset_m, near_m, internal ));
168 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index,
169 false, 0, 0.0, 0.0, 0.0,
170 x_offset_m, y_offset_m, z_offset_m,
171 heading_offset_deg, pitch_offset_deg,
172 roll_offset_deg, fov_deg, 0, 0, 0, near_m,
184 string viewpath, nodepath, strdata;
187 // reset offsets and fov to configuration defaults
189 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
190 viewpath = "/sim/view";
191 sprintf(stridx, "[%d]", i);
197 nodepath += "/config/x-offset-m";
198 fgSetDouble("/sim/current-view/x-offset-m",fgGetDouble(nodepath.c_str()));
201 nodepath += "/config/y-offset-m";
202 fgSetDouble("/sim/current-view/y-offset-m",fgGetDouble(nodepath.c_str()));
205 nodepath += "/config/z-offset-m";
206 fgSetDouble("/sim/current-view/z-offset-m",fgGetDouble(nodepath.c_str()));
209 nodepath += "/config/pitch-offset-deg";
210 fgSetDouble("/sim/current-view/pitch-offset-deg",
211 fgGetDouble(nodepath.c_str()));
214 nodepath += "/config/heading-offset-deg";
215 fgSetDouble("/sim/current-view/heading-offset-deg",
216 fgGetDouble(nodepath.c_str()));
219 nodepath += "/config/roll-offset-deg";
220 fgSetDouble("/sim/current-view/roll-offset-deg",
221 fgGetDouble(nodepath.c_str()));
224 nodepath += "/config/default-field-of-view-deg";
225 fov_deg = fgGetDouble(nodepath.c_str());
226 if (fov_deg < 10.0) {
229 fgSetDouble("/sim/current-view/field-of-view",fov_deg);
231 // target offsets for lookat mode only...
233 nodepath += "/config/target-x-offset-m";
234 fgSetDouble("/sim/current-view/target-x-offset-deg",
235 fgGetDouble(nodepath.c_str()));
238 nodepath += "/config/target-y-offset-m";
239 fgSetDouble("/sim/current-view/target-y-offset-deg",
240 fgGetDouble(nodepath.c_str()));
243 nodepath += "/config/target-z-offset-m";
244 fgSetDouble("/sim/current-view/target-z-offset-deg",
245 fgGetDouble(nodepath.c_str()));
253 typedef double (FGViewMgr::*double_getter)() const;
258 // these are bound to the current view properties
259 fgTie("/sim/current-view/heading-offset-deg", this,
260 &FGViewMgr::getViewHeadingOffset_deg,
261 &FGViewMgr::setViewHeadingOffset_deg);
262 fgSetArchivable("/sim/current-view/heading-offset-deg");
263 fgTie("/sim/current-view/goal-heading-offset-deg", this,
264 &FGViewMgr::getViewGoalHeadingOffset_deg,
265 &FGViewMgr::setViewGoalHeadingOffset_deg);
266 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
267 fgTie("/sim/current-view/pitch-offset-deg", this,
268 &FGViewMgr::getViewPitchOffset_deg,
269 &FGViewMgr::setViewPitchOffset_deg);
270 fgSetArchivable("/sim/current-view/pitch-offset-deg");
271 fgTie("/sim/current-view/goal-pitch-offset-deg", this,
272 &FGViewMgr::getGoalViewPitchOffset_deg,
273 &FGViewMgr::setGoalViewPitchOffset_deg);
274 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
275 fgTie("/sim/current-view/roll-offset-deg", this,
276 &FGViewMgr::getViewRollOffset_deg,
277 &FGViewMgr::setViewRollOffset_deg);
278 fgSetArchivable("/sim/current-view/roll-offset-deg");
279 fgTie("/sim/current-view/goal-roll-offset-deg", this,
280 &FGViewMgr::getGoalViewRollOffset_deg,
281 &FGViewMgr::setGoalViewRollOffset_deg);
282 fgSetArchivable("/sim/current-view/goal-roll-offset-deg");
284 fgTie("/sim/current-view/view-number", this,
285 &FGViewMgr::getView, &FGViewMgr::setView);
286 fgSetArchivable("/sim/current-view/view-number", FALSE);
288 fgTie("/sim/current-view/axes/long", this,
289 (double_getter)0, &FGViewMgr::setViewAxisLong);
290 fgSetArchivable("/sim/current-view/axes/long");
292 fgTie("/sim/current-view/axes/lat", this,
293 (double_getter)0, &FGViewMgr::setViewAxisLat);
294 fgSetArchivable("/sim/current-view/axes/lat");
296 fgTie("/sim/current-view/field-of-view", this,
297 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
298 fgSetArchivable("/sim/current-view/field-of-view");
300 fgTie("/sim/current-view/ground-level-nearplane-m", this,
301 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
302 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
310 // need to redo these bindings to the new locations (move to viewer?)
311 fgUntie("/sim/current-view/heading-offset-deg");
312 fgUntie("/sim/current-view/goal-heading-offset-deg");
313 fgUntie("/sim/current-view/pitch-offset-deg");
314 fgUntie("/sim/current-view/goal-pitch-offset-deg");
315 fgUntie("/sim/field-of-view");
316 fgUntie("/sim/current-view/view-number");
317 fgUntie("/sim/current-view/axes/long");
318 fgUntie("/sim/current-view/axes/lat");
319 fgUntie("/sim/current-view/ground-level-nearplane-m");
323 FGViewMgr::update (double dt)
326 string viewpath, nodepath;
327 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
329 FGViewer * view = get_current_view();
335 viewpath = "/sim/view";
336 sprintf(stridx, "[%d]", i);
339 FGViewer *loop_view = (FGViewer *)get_view( i );
341 // Set up view location and orientation
344 nodepath += "/config/from-model";
345 if (!fgGetBool(nodepath.c_str())) {
347 nodepath += "/config/eye-lon-deg-path";
348 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
350 nodepath += "/config/eye-lat-deg-path";
351 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
353 nodepath += "/config/eye-alt-ft-path";
354 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
356 nodepath += "/config/eye-roll-deg-path";
357 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
359 nodepath += "/config/eye-pitch-deg-path";
360 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
362 nodepath += "/config/eye-heading-deg-path";
363 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
364 loop_view->setPosition(lon_deg, lat_deg, alt_ft);
365 loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
367 // force recalc in viewer
368 loop_view->set_dirty();
371 // if lookat (type 1) then get target data...
372 if (loop_view->getType() == FG_LOOKAT) {
374 nodepath += "/config/from-model";
375 if (!fgGetBool(nodepath.c_str())) {
377 nodepath += "/config/target-lon-deg-path";
378 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
380 nodepath += "/config/target-lat-deg-path";
381 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
383 nodepath += "/config/target-alt-ft-path";
384 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
386 nodepath += "/config/target-roll-deg-path";
387 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
389 nodepath += "/config/target-pitch-deg-path";
390 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
392 nodepath += "/config/target-heading-deg-path";
393 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
395 loop_view->setTargetPosition(lon_deg, lat_deg, alt_ft);
396 loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
398 loop_view->set_dirty();
402 setViewXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
403 setViewYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
404 setViewZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
406 setViewTargetXOffset_m(fgGetDouble("/sim/current-view/target-x-offset-m"));
407 setViewTargetYOffset_m(fgGetDouble("/sim/current-view/target-y-offset-m"));
408 setViewTargetZOffset_m(fgGetDouble("/sim/current-view/target-z-offset-m"));
410 // Update the current view
416 FGViewMgr::copyToCurrent()
419 string viewpath, nodepath;
422 viewpath = "/sim/view";
423 sprintf(stridx, "[%d]", i);
426 // copy certain view config data for default values
428 nodepath += "/config/default-heading-offset-deg";
429 fgSetDouble("/sim/current-view/config/heading-offset-deg",
430 fgGetDouble(nodepath.c_str()));
433 nodepath += "/config/pitch-offset-deg";
434 fgSetDouble("/sim/current-view/config/pitch-offset-deg",
435 fgGetDouble(nodepath.c_str()));
438 nodepath += "/config/roll-offset-deg";
439 fgSetDouble("/sim/current-view/config/roll-offset-deg",
440 fgGetDouble(nodepath.c_str()));
443 nodepath += "/config/default-field-of-view-deg";
444 fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
445 fgGetDouble(nodepath.c_str()));
448 nodepath += "/config/from-model";
449 fgSetBool("/sim/current-view/config/from-model",
450 fgGetBool(nodepath.c_str()));
454 fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
455 fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
456 fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
457 fgSetDouble("/sim/current-view/goal-heading-offset-deg",
458 get_current_view()->getGoalHeadingOffset_deg());
459 fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
460 get_current_view()->getGoalPitchOffset_deg());
461 fgSetDouble("/sim/current-view/goal-roll-offset-deg",
462 get_current_view()->getRollOffset_deg());
463 fgSetDouble("/sim/current-view/heading-offset-deg",
464 get_current_view()->getHeadingOffset_deg());
465 fgSetDouble("/sim/current-view/pitch-offset-deg",
466 get_current_view()->getPitchOffset_deg());
467 fgSetDouble("/sim/current-view/roll-offset-deg",
468 get_current_view()->getRollOffset_deg());
469 fgSetDouble("/sim/current-view/target-x-offset-m",
470 get_current_view()->getTargetXOffset_m());
471 fgSetDouble("/sim/current-view/target-y-offset-m",
472 get_current_view()->getTargetYOffset_m());
473 fgSetDouble("/sim/current-view/target-z-offset-m",
474 get_current_view()->getTargetZOffset_m());
476 fgSetBool("/sim/current-view/internal",
477 get_current_view()->getInternal());
483 FGViewMgr::getViewHeadingOffset_deg () const
485 const FGViewer * view = get_current_view();
486 return (view == 0 ? 0 : view->getHeadingOffset_deg());
490 FGViewMgr::setViewHeadingOffset_deg (double offset)
492 FGViewer * view = get_current_view();
494 view->setGoalHeadingOffset_deg(offset);
495 view->setHeadingOffset_deg(offset);
500 FGViewMgr::getViewGoalHeadingOffset_deg () const
502 const FGViewer * view = get_current_view();
503 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
507 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
509 FGViewer * view = get_current_view();
511 view->setGoalHeadingOffset_deg(offset);
515 FGViewMgr::getViewPitchOffset_deg () const
517 const FGViewer * view = get_current_view();
518 return (view == 0 ? 0 : view->getPitchOffset_deg());
522 FGViewMgr::setViewPitchOffset_deg (double tilt)
524 FGViewer * view = get_current_view();
526 view->setGoalPitchOffset_deg(tilt);
527 view->setPitchOffset_deg(tilt);
532 FGViewMgr::getGoalViewPitchOffset_deg () const
534 const FGViewer * view = get_current_view();
535 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
539 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
541 FGViewer * view = get_current_view();
543 view->setGoalPitchOffset_deg(tilt);
547 FGViewMgr::getViewRollOffset_deg () const
549 const FGViewer * view = get_current_view();
550 return (view == 0 ? 0 : view->getRollOffset_deg());
554 FGViewMgr::setViewRollOffset_deg (double tilt)
556 FGViewer * view = get_current_view();
558 view->setGoalRollOffset_deg(tilt);
559 view->setRollOffset_deg(tilt);
564 FGViewMgr::getGoalViewRollOffset_deg () const
566 const FGViewer * view = get_current_view();
567 return (view == 0 ? 0 : view->getGoalRollOffset_deg());
571 FGViewMgr::setGoalViewRollOffset_deg (double tilt)
573 FGViewer * view = get_current_view();
575 view->setGoalRollOffset_deg(tilt);
579 FGViewMgr::getViewXOffset_m () const
581 const FGViewer * view = get_current_view();
583 return ((FGViewer *)view)->getXOffset_m();
590 FGViewMgr::setViewXOffset_m (double x)
592 FGViewer * view = get_current_view();
594 view->setXOffset_m(x);
599 FGViewMgr::getViewYOffset_m () const
601 const FGViewer * view = get_current_view();
603 return ((FGViewer *)view)->getYOffset_m();
610 FGViewMgr::setViewYOffset_m (double y)
612 FGViewer * view = get_current_view();
614 view->setYOffset_m(y);
619 FGViewMgr::getViewZOffset_m () const
621 const FGViewer * view = get_current_view();
623 return ((FGViewer *)view)->getZOffset_m();
630 FGViewMgr::setViewZOffset_m (double z)
632 FGViewer * view = get_current_view();
634 view->setZOffset_m(z);
639 FGViewMgr::getViewTargetXOffset_m () const
641 const FGViewer * view = get_current_view();
643 return ((FGViewer *)view)->getTargetXOffset_m();
650 FGViewMgr::setViewTargetXOffset_m (double x)
652 FGViewer * view = get_current_view();
654 view->setTargetXOffset_m(x);
659 FGViewMgr::getViewTargetYOffset_m () const
661 const FGViewer * view = get_current_view();
663 return ((FGViewer *)view)->getTargetYOffset_m();
670 FGViewMgr::setViewTargetYOffset_m (double y)
672 FGViewer * view = get_current_view();
674 view->setTargetYOffset_m(y);
679 FGViewMgr::getViewTargetZOffset_m () const
681 const FGViewer * view = get_current_view();
683 return ((FGViewer *)view)->getTargetZOffset_m();
690 FGViewMgr::setViewTargetZOffset_m (double z)
692 FGViewer * view = get_current_view();
694 view->setTargetZOffset_m(z);
699 FGViewMgr::getView () const
705 FGViewMgr::setView (int newview )
707 // if newview number too low wrap to last view...
709 newview = (int)views.size() -1;
711 // if newview number to high wrap to zero...
712 if ( newview > ((int)views.size() -1) ) {
723 FGViewMgr::getFOV_deg () const
725 const FGViewer * view = get_current_view();
726 return (view == 0 ? 0 : view->get_fov());
730 FGViewMgr::setFOV_deg (double fov)
732 FGViewer * view = get_current_view();
738 FGViewMgr::getNear_m () const
740 const FGViewer * view = get_current_view();
741 return (view == 0 ? 0.5f : view->getNear_m());
745 FGViewMgr::setNear_m (double near_m)
747 FGViewer * view = get_current_view();
749 view->setNear_m(near_m);
753 FGViewMgr::setViewAxisLong (double axis)
759 FGViewMgr::setViewAxisLat (double axis)
765 FGViewMgr::do_axes ()
767 // Take no action when hat is centered
768 if ( ( axis_long < 0.01 ) &&
769 ( axis_long > -0.01 ) &&
770 ( axis_lat < 0.01 ) &&
775 double viewDir = 999;
777 /* Do all the quick and easy cases */
778 if (axis_long < 0) { // Longitudinal axis forward
779 if (axis_lat == axis_long)
780 viewDir = fgGetDouble("/sim/view/config/front-left-direction-deg");
781 else if (axis_lat == - axis_long)
782 viewDir = fgGetDouble("/sim/view/config/front-right-direction-deg");
783 else if (axis_lat == 0)
784 viewDir = fgGetDouble("/sim/view/config/front-direction-deg");
785 } else if (axis_long > 0) { // Longitudinal axis backward
786 if (axis_lat == - axis_long)
787 viewDir = fgGetDouble("/sim/view/config/back-left-direction-deg");
788 else if (axis_lat == axis_long)
789 viewDir = fgGetDouble("/sim/view/config/back-right-direction-deg");
790 else if (axis_lat == 0)
791 viewDir = fgGetDouble("/sim/view/config/back-direction-deg");
792 } else if (axis_long == 0) { // Longitudinal axis neutral
794 viewDir = fgGetDouble("/sim/view/config/left-direction-deg");
795 else if (axis_lat > 0)
796 viewDir = fgGetDouble("/sim/view/config/right-direction-deg");
797 else return; /* And assertion failure maybe? */
800 // Do all the difficult cases
802 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
803 if ( viewDir < -1 ) viewDir += 360;
805 get_current_view()->setGoalHeadingOffset_deg(viewDir);