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 - curt@flightgear.org
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;
54 double damp_alt, damp_roll, damp_pitch, damp_heading;
55 double x_offset_m, y_offset_m, z_offset_m, fov_deg;
56 double heading_offset_deg, pitch_offset_deg, roll_offset_deg;
57 double target_x_offset_m, target_y_offset_m, target_z_offset_m;
61 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
62 viewpath = "/sim/view";
63 sprintf(stridx, "[%d]", i);
66 // find out what type of view this is...
69 strdata = fgGetString(nodepath.c_str());
71 // find out if this is an internal view (e.g. in cockpit, low near plane)
72 internal = false; // default
74 nodepath += "/internal";
75 internal = fgGetBool(nodepath.c_str());
78 // this is assumed to be an aircraft model...we will need to read
79 // model-from-type as well.
80 // find out if this is a model we are looking from...
82 nodepath += "/config/from-model";
83 from_model = fgGetBool(nodepath.c_str());
85 // get model index (which model)
88 nodepath += "/config/from-model-idx";
89 from_model_index = fgGetInt(nodepath.c_str());
92 if ( strcmp("lookat",strdata.c_str()) == 0 ) {
93 // find out if this is a model we are looking at...
95 nodepath += "/config/at-model";
96 at_model = fgGetBool(nodepath.c_str());
98 // get model index (which model)
101 nodepath += "/config/at-model-idx";
102 at_model_index = fgGetInt(nodepath.c_str());
105 nodepath += "/config/at-model-roll-damping";
106 damp_roll = fgGetDouble(nodepath.c_str(), 0.0);
108 nodepath += "/config/at-model-pitch-damping";
109 damp_pitch = fgGetDouble(nodepath.c_str(), 0.0);
111 nodepath += "/config/at-model-heading-damping";
112 damp_heading = fgGetDouble(nodepath.c_str(), 0.0);
117 nodepath += "/config/x-offset-m";
118 x_offset_m = fgGetDouble(nodepath.c_str());
120 nodepath += "/config/y-offset-m";
121 y_offset_m = fgGetDouble(nodepath.c_str());
123 nodepath += "/config/z-offset-m";
124 z_offset_m = fgGetDouble(nodepath.c_str());
126 nodepath += "/config/pitch-offset-deg";
127 pitch_offset_deg = fgGetDouble(nodepath.c_str());
128 fgSetDouble(nodepath.c_str(),pitch_offset_deg);
130 nodepath += "/config/heading-offset-deg";
131 heading_offset_deg = fgGetDouble(nodepath.c_str());
132 fgSetDouble(nodepath.c_str(),heading_offset_deg);
134 nodepath += "/config/roll-offset-deg";
135 roll_offset_deg = fgGetDouble(nodepath.c_str());
136 fgSetDouble(nodepath.c_str(),roll_offset_deg);
138 nodepath += "/config/default-field-of-view-deg";
139 fov_deg = fgGetDouble(nodepath.c_str());
141 // target offsets for lookat mode only...
143 nodepath += "/config/target-x-offset-m";
144 target_x_offset_m = fgGetDouble(nodepath.c_str());
146 nodepath += "/config/target-y-offset-m";
147 target_y_offset_m = fgGetDouble(nodepath.c_str());
149 nodepath += "/config/target-z-offset-m";
150 target_z_offset_m = fgGetDouble(nodepath.c_str());
153 nodepath += "/config/ground-level-nearplane-m";
154 near_m = fgGetDouble(nodepath.c_str());
156 // supporting two types now "lookat" = 1 and "lookfrom" = 0
157 if ( strcmp("lookat",strdata.c_str()) == 0 )
158 add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
159 at_model, at_model_index,
160 damp_roll, damp_pitch, damp_heading,
161 x_offset_m, y_offset_m,z_offset_m,
162 heading_offset_deg, pitch_offset_deg,
163 roll_offset_deg, fov_deg,
164 target_x_offset_m, target_y_offset_m,
165 target_z_offset_m, near_m, internal ));
167 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index,
168 false, 0, 0.0, 0.0, 0.0,
169 x_offset_m, y_offset_m, z_offset_m,
170 heading_offset_deg, pitch_offset_deg,
171 roll_offset_deg, fov_deg, 0, 0, 0, near_m,
183 string viewpath, nodepath, strdata;
186 // reset offsets and fov to configuration defaults
188 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
189 viewpath = "/sim/view";
190 sprintf(stridx, "[%d]", i);
196 nodepath += "/config/x-offset-m";
197 fgSetDouble("/sim/current-view/x-offset-m",fgGetDouble(nodepath.c_str()));
200 nodepath += "/config/y-offset-m";
201 fgSetDouble("/sim/current-view/y-offset-m",fgGetDouble(nodepath.c_str()));
204 nodepath += "/config/z-offset-m";
205 fgSetDouble("/sim/current-view/z-offset-m",fgGetDouble(nodepath.c_str()));
208 nodepath += "/config/pitch-offset-deg";
209 fgSetDouble("/sim/current-view/pitch-offset-deg",
210 fgGetDouble(nodepath.c_str()));
213 nodepath += "/config/heading-offset-deg";
214 fgSetDouble("/sim/current-view/heading-offset-deg",
215 fgGetDouble(nodepath.c_str()));
218 nodepath += "/config/roll-offset-deg";
219 fgSetDouble("/sim/current-view/roll-offset-deg",
220 fgGetDouble(nodepath.c_str()));
223 nodepath += "/config/default-field-of-view-deg";
224 fov_deg = fgGetDouble(nodepath.c_str());
225 if (fov_deg < 10.0) {
228 fgSetDouble("/sim/current-view/field-of-view",fov_deg);
230 // target offsets for lookat mode only...
232 nodepath += "/config/target-x-offset-m";
233 fgSetDouble("/sim/current-view/target-x-offset-deg",
234 fgGetDouble(nodepath.c_str()));
237 nodepath += "/config/target-y-offset-m";
238 fgSetDouble("/sim/current-view/target-y-offset-deg",
239 fgGetDouble(nodepath.c_str()));
242 nodepath += "/config/target-z-offset-m";
243 fgSetDouble("/sim/current-view/target-z-offset-deg",
244 fgGetDouble(nodepath.c_str()));
252 typedef double (FGViewMgr::*double_getter)() const;
257 // these are bound to the current view properties
258 fgTie("/sim/current-view/heading-offset-deg", this,
259 &FGViewMgr::getViewHeadingOffset_deg,
260 &FGViewMgr::setViewHeadingOffset_deg);
261 fgSetArchivable("/sim/current-view/heading-offset-deg");
262 fgTie("/sim/current-view/goal-heading-offset-deg", this,
263 &FGViewMgr::getViewGoalHeadingOffset_deg,
264 &FGViewMgr::setViewGoalHeadingOffset_deg);
265 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
266 fgTie("/sim/current-view/pitch-offset-deg", this,
267 &FGViewMgr::getViewPitchOffset_deg,
268 &FGViewMgr::setViewPitchOffset_deg);
269 fgSetArchivable("/sim/current-view/pitch-offset-deg");
270 fgTie("/sim/current-view/goal-pitch-offset-deg", this,
271 &FGViewMgr::getGoalViewPitchOffset_deg,
272 &FGViewMgr::setGoalViewPitchOffset_deg);
273 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
275 fgTie("/sim/current-view/view-number", this,
276 &FGViewMgr::getView, &FGViewMgr::setView);
277 fgSetArchivable("/sim/current-view/view-number", FALSE);
279 fgTie("/sim/current-view/axes/long", this,
280 (double_getter)0, &FGViewMgr::setViewAxisLong);
281 fgSetArchivable("/sim/current-view/axes/long");
283 fgTie("/sim/current-view/axes/lat", this,
284 (double_getter)0, &FGViewMgr::setViewAxisLat);
285 fgSetArchivable("/sim/current-view/axes/lat");
287 fgTie("/sim/current-view/field-of-view", this,
288 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
289 fgSetArchivable("/sim/current-view/field-of-view");
291 fgTie("/sim/current-view/ground-level-nearplane-m", this,
292 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
293 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
301 // need to redo these bindings to the new locations (move to viewer?)
302 fgUntie("/sim/current-view/heading-offset-deg");
303 fgUntie("/sim/current-view/goal-heading-offset-deg");
304 fgUntie("/sim/current-view/pitch-offset-deg");
305 fgUntie("/sim/current-view/goal-pitch-offset-deg");
306 fgUntie("/sim/field-of-view");
307 fgUntie("/sim/current-view/view-number");
308 fgUntie("/sim/current-view/axes/long");
309 fgUntie("/sim/current-view/axes/lat");
310 fgUntie("/sim/current-view/ground-level-nearplane-m");
314 FGViewMgr::update (double dt)
317 string viewpath, nodepath;
318 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
320 FGViewer * view = get_current_view();
326 viewpath = "/sim/view";
327 sprintf(stridx, "[%d]", i);
330 FGViewer *loop_view = (FGViewer *)get_view( i );
332 // Set up view location and orientation
335 nodepath += "/config/from-model";
336 if (!fgGetBool(nodepath.c_str())) {
338 nodepath += "/config/eye-lon-deg-path";
339 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
341 nodepath += "/config/eye-lat-deg-path";
342 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
344 nodepath += "/config/eye-alt-ft-path";
345 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
347 nodepath += "/config/eye-roll-deg-path";
348 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
350 nodepath += "/config/eye-pitch-deg-path";
351 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
353 nodepath += "/config/eye-heading-deg-path";
354 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
355 loop_view->setPosition(lon_deg, lat_deg, alt_ft);
356 loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
358 // force recalc in viewer
359 loop_view->set_dirty();
362 // if lookat (type 1) then get target data...
363 if (loop_view->getType() == FG_LOOKAT) {
365 nodepath += "/config/from-model";
366 if (!fgGetBool(nodepath.c_str())) {
368 nodepath += "/config/target-lon-deg-path";
369 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
371 nodepath += "/config/target-lat-deg-path";
372 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
374 nodepath += "/config/target-alt-ft-path";
375 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
377 nodepath += "/config/target-roll-deg-path";
378 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
380 nodepath += "/config/target-pitch-deg-path";
381 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
383 nodepath += "/config/target-heading-deg-path";
384 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
386 loop_view->setTargetPosition(lon_deg, lat_deg, alt_ft);
387 loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
389 loop_view->set_dirty();
393 setViewXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
394 setViewYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
395 setViewZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
397 setViewTargetXOffset_m(fgGetDouble("/sim/current-view/target-x-offset-m"));
398 setViewTargetYOffset_m(fgGetDouble("/sim/current-view/target-y-offset-m"));
399 setViewTargetZOffset_m(fgGetDouble("/sim/current-view/target-z-offset-m"));
401 // Update the current view
407 FGViewMgr::copyToCurrent()
410 string viewpath, nodepath;
413 viewpath = "/sim/view";
414 sprintf(stridx, "[%d]", i);
417 // copy certain view config data for default values
419 nodepath += "/config/default-heading-offset-deg";
420 fgSetDouble("/sim/current-view/config/heading-offset-deg",
421 fgGetDouble(nodepath.c_str()));
424 nodepath += "/config/pitch-offset-deg";
425 fgSetDouble("/sim/current-view/config/pitch-offset-deg",
426 fgGetDouble(nodepath.c_str()));
429 nodepath += "/config/roll-offset-deg";
430 fgSetDouble("/sim/current-view/config/roll-offset-deg",
431 fgGetDouble(nodepath.c_str()));
434 nodepath += "/config/default-field-of-view-deg";
435 fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
436 fgGetDouble(nodepath.c_str()));
439 fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
440 fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
441 fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
442 fgSetDouble("/sim/current-view/goal-heading-offset-deg",
443 get_current_view()->getGoalHeadingOffset_deg());
444 fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
445 get_current_view()->getGoalPitchOffset_deg());
446 fgSetDouble("/sim/current-view/goal-roll-offset-deg",
447 get_current_view()->getRollOffset_deg());
448 fgSetDouble("/sim/current-view/heading-offset-deg",
449 get_current_view()->getHeadingOffset_deg());
450 fgSetDouble("/sim/current-view/pitch-offset-deg",
451 get_current_view()->getPitchOffset_deg());
452 fgSetDouble("/sim/current-view/roll-offset-deg",
453 get_current_view()->getRollOffset_deg());
454 fgSetDouble("/sim/current-view/target-x-offset-m",
455 get_current_view()->getTargetXOffset_m());
456 fgSetDouble("/sim/current-view/target-y-offset-m",
457 get_current_view()->getTargetYOffset_m());
458 fgSetDouble("/sim/current-view/target-z-offset-m",
459 get_current_view()->getTargetZOffset_m());
464 FGViewMgr::getViewHeadingOffset_deg () const
466 const FGViewer * view = get_current_view();
467 return (view == 0 ? 0 : view->getHeadingOffset_deg());
471 FGViewMgr::setViewHeadingOffset_deg (double offset)
473 FGViewer * view = get_current_view();
475 view->setGoalHeadingOffset_deg(offset);
476 view->setHeadingOffset_deg(offset);
481 FGViewMgr::getViewGoalHeadingOffset_deg () const
483 const FGViewer * view = get_current_view();
484 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
488 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
490 FGViewer * view = get_current_view();
492 view->setGoalHeadingOffset_deg(offset);
496 FGViewMgr::getViewPitchOffset_deg () const
498 const FGViewer * view = get_current_view();
499 return (view == 0 ? 0 : view->getPitchOffset_deg());
503 FGViewMgr::setViewPitchOffset_deg (double tilt)
505 FGViewer * view = get_current_view();
507 view->setGoalPitchOffset_deg(tilt);
508 view->setPitchOffset_deg(tilt);
513 FGViewMgr::getGoalViewPitchOffset_deg () const
515 const FGViewer * view = get_current_view();
516 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
520 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
522 FGViewer * view = get_current_view();
524 view->setGoalPitchOffset_deg(tilt);
528 FGViewMgr::getViewXOffset_m () const
530 const FGViewer * view = get_current_view();
532 return ((FGViewer *)view)->getXOffset_m();
539 FGViewMgr::setViewXOffset_m (double x)
541 FGViewer * view = get_current_view();
543 view->setXOffset_m(x);
548 FGViewMgr::getViewYOffset_m () const
550 const FGViewer * view = get_current_view();
552 return ((FGViewer *)view)->getYOffset_m();
559 FGViewMgr::setViewYOffset_m (double y)
561 FGViewer * view = get_current_view();
563 view->setYOffset_m(y);
568 FGViewMgr::getViewZOffset_m () const
570 const FGViewer * view = get_current_view();
572 return ((FGViewer *)view)->getZOffset_m();
579 FGViewMgr::setViewZOffset_m (double z)
581 FGViewer * view = get_current_view();
583 view->setZOffset_m(z);
588 FGViewMgr::getViewTargetXOffset_m () const
590 const FGViewer * view = get_current_view();
592 return ((FGViewer *)view)->getTargetXOffset_m();
599 FGViewMgr::setViewTargetXOffset_m (double x)
601 FGViewer * view = get_current_view();
603 view->setTargetXOffset_m(x);
608 FGViewMgr::getViewTargetYOffset_m () const
610 const FGViewer * view = get_current_view();
612 return ((FGViewer *)view)->getTargetYOffset_m();
619 FGViewMgr::setViewTargetYOffset_m (double y)
621 FGViewer * view = get_current_view();
623 view->setTargetYOffset_m(y);
628 FGViewMgr::getViewTargetZOffset_m () const
630 const FGViewer * view = get_current_view();
632 return ((FGViewer *)view)->getTargetZOffset_m();
639 FGViewMgr::setViewTargetZOffset_m (double z)
641 FGViewer * view = get_current_view();
643 view->setTargetZOffset_m(z);
648 FGViewMgr::getView () const
654 FGViewMgr::setView (int newview )
656 // if newview number too low wrap to last view...
658 newview = (int)views.size() -1;
660 // if newview number to high wrap to zero...
661 if ( newview > ((int)views.size() -1) ) {
672 FGViewMgr::getFOV_deg () const
674 const FGViewer * view = get_current_view();
675 return (view == 0 ? 0 : view->get_fov());
679 FGViewMgr::setFOV_deg (double fov)
681 FGViewer * view = get_current_view();
687 FGViewMgr::getNear_m () const
689 const FGViewer * view = get_current_view();
690 return (view == 0 ? 0.5f : view->getNear_m());
694 FGViewMgr::setNear_m (double near_m)
696 FGViewer * view = get_current_view();
698 view->setNear_m(near_m);
702 FGViewMgr::setViewAxisLong (double axis)
708 FGViewMgr::setViewAxisLat (double axis)
714 FGViewMgr::do_axes ()
716 // Take no action when hat is centered
717 if ( ( axis_long < 0.01 ) &&
718 ( axis_long > -0.01 ) &&
719 ( axis_lat < 0.01 ) &&
724 double viewDir = 999;
726 /* Do all the quick and easy cases */
727 if (axis_long < 0) { // Longitudinal axis forward
728 if (axis_lat == axis_long)
729 viewDir = fgGetDouble("/sim/view/config/front-left-direction-deg");
730 else if (axis_lat == - axis_long)
731 viewDir = fgGetDouble("/sim/view/config/front-right-direction-deg");
732 else if (axis_lat == 0)
733 viewDir = fgGetDouble("/sim/view/config/front-direction-deg");
734 } else if (axis_long > 0) { // Longitudinal axis backward
735 if (axis_lat == - axis_long)
736 viewDir = fgGetDouble("/sim/view/config/back-left-direction-deg");
737 else if (axis_lat == axis_long)
738 viewDir = fgGetDouble("/sim/view/config/back-right-direction-deg");
739 else if (axis_lat == 0)
740 viewDir = fgGetDouble("/sim/view/config/back-direction-deg");
741 } else if (axis_long == 0) { // Longitudinal axis neutral
743 viewDir = fgGetDouble("/sim/view/config/left-direction-deg");
744 else if (axis_lat > 0)
745 viewDir = fgGetDouble("/sim/view/config/right-direction-deg");
746 else return; /* And assertion failure maybe? */
749 // Do all the difficult cases
751 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
752 if ( viewDir < -1 ) viewDir += 360;
754 get_current_view()->setGoalHeadingOffset_deg(viewDir);