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 at_model_damping;
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;
60 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
61 viewpath = "/sim/view";
62 sprintf(stridx, "[%d]", i);
65 // find out what type of view this is...
68 strdata = fgGetString(nodepath.c_str());
71 // this is assumed to be an aircraft model...we will need to read
72 // model-from-type as well.
73 // find out if this is a model we are looking from...
75 nodepath += "/config/from-model";
76 from_model = fgGetBool(nodepath.c_str());
78 // get model index (which model)
81 nodepath += "/config/from-model-idx";
82 from_model_index = fgGetInt(nodepath.c_str());
85 if ( strcmp("lookat",strdata.c_str()) == 0 ) {
86 // find out if this is a model we are looking at...
88 nodepath += "/config/at-model";
89 at_model = fgGetBool(nodepath.c_str());
91 // get model index (which model)
94 nodepath += "/config/at-model-idx";
95 at_model_index = fgGetInt(nodepath.c_str());
97 // view damping (0.0: no damping; <0.0: all but alt; >0.0: damp all)
99 nodepath += "/config/at-model-damping";
100 at_model_damping = fgGetDouble(nodepath.c_str(), 0.0);
105 nodepath += "/config/x-offset-m";
106 x_offset_m = fgGetDouble(nodepath.c_str());
108 nodepath += "/config/y-offset-m";
109 y_offset_m = fgGetDouble(nodepath.c_str());
111 nodepath += "/config/z-offset-m";
112 z_offset_m = fgGetDouble(nodepath.c_str());
114 nodepath += "/config/pitch-offset-deg";
115 pitch_offset_deg = fgGetDouble(nodepath.c_str());
116 fgSetDouble(nodepath.c_str(),pitch_offset_deg);
118 nodepath += "/config/heading-offset-deg";
119 heading_offset_deg = fgGetDouble(nodepath.c_str());
120 fgSetDouble(nodepath.c_str(),heading_offset_deg);
122 nodepath += "/config/roll-offset-deg";
123 roll_offset_deg = fgGetDouble(nodepath.c_str());
124 fgSetDouble(nodepath.c_str(),roll_offset_deg);
126 nodepath += "/config/default-field-of-view-deg";
127 fov_deg = fgGetDouble(nodepath.c_str());
129 // target offsets for lookat mode only...
131 nodepath += "/config/target-x-offset-m";
132 target_x_offset_m = fgGetDouble(nodepath.c_str());
134 nodepath += "/config/target-y-offset-m";
135 target_y_offset_m = fgGetDouble(nodepath.c_str());
137 nodepath += "/config/target-z-offset-m";
138 target_z_offset_m = fgGetDouble(nodepath.c_str());
141 nodepath += "/config/ground-level-nearplane-m";
142 near_m = fgGetDouble(nodepath.c_str());
144 // supporting two types now "lookat" = 1 and "lookfrom" = 0
145 if ( strcmp("lookat",strdata.c_str()) == 0 )
146 add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
147 at_model, at_model_index, at_model_damping,
148 x_offset_m, y_offset_m,z_offset_m,
149 heading_offset_deg, pitch_offset_deg,
150 roll_offset_deg, fov_deg,
151 target_x_offset_m, target_y_offset_m,
152 target_z_offset_m, near_m ));
154 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index, false,
155 0, 0.0, x_offset_m, y_offset_m, z_offset_m,
156 heading_offset_deg, pitch_offset_deg,
157 roll_offset_deg, fov_deg, 0, 0, 0, near_m ));
168 string viewpath, nodepath, strdata;
171 // reset offsets and fov to configuration defaults
173 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
174 viewpath = "/sim/view";
175 sprintf(stridx, "[%d]", i);
181 nodepath += "/config/x-offset-m";
182 fgSetDouble("/sim/current-view/x-offset-m",fgGetDouble(nodepath.c_str()));
185 nodepath += "/config/y-offset-m";
186 fgSetDouble("/sim/current-view/y-offset-m",fgGetDouble(nodepath.c_str()));
189 nodepath += "/config/z-offset-m";
190 fgSetDouble("/sim/current-view/z-offset-m",fgGetDouble(nodepath.c_str()));
193 nodepath += "/config/pitch-offset-deg";
194 fgSetDouble("/sim/current-view/pitch-offset-deg",
195 fgGetDouble(nodepath.c_str()));
198 nodepath += "/config/heading-offset-deg";
199 fgSetDouble("/sim/current-view/heading-offset-deg",
200 fgGetDouble(nodepath.c_str()));
203 nodepath += "/config/roll-offset-deg";
204 fgSetDouble("/sim/current-view/roll-offset-deg",
205 fgGetDouble(nodepath.c_str()));
208 nodepath += "/config/default-field-of-view-deg";
209 fov_deg = fgGetDouble(nodepath.c_str());
210 if (fov_deg < 10.0) {
213 fgSetDouble("/sim/current-view/field-of-view",fov_deg);
215 // target offsets for lookat mode only...
217 nodepath += "/config/target-x-offset-m";
218 fgSetDouble("/sim/current-view/target-x-offset-deg",
219 fgGetDouble(nodepath.c_str()));
222 nodepath += "/config/target-y-offset-m";
223 fgSetDouble("/sim/current-view/target-y-offset-deg",
224 fgGetDouble(nodepath.c_str()));
227 nodepath += "/config/target-z-offset-m";
228 fgSetDouble("/sim/current-view/target-z-offset-deg",
229 fgGetDouble(nodepath.c_str()));
237 typedef double (FGViewMgr::*double_getter)() const;
242 // these are bound to the current view properties
243 fgTie("/sim/current-view/heading-offset-deg", this,
244 &FGViewMgr::getViewHeadingOffset_deg,
245 &FGViewMgr::setViewHeadingOffset_deg);
246 fgSetArchivable("/sim/current-view/heading-offset-deg");
247 fgTie("/sim/current-view/goal-heading-offset-deg", this,
248 &FGViewMgr::getViewGoalHeadingOffset_deg,
249 &FGViewMgr::setViewGoalHeadingOffset_deg);
250 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
251 fgTie("/sim/current-view/pitch-offset-deg", this,
252 &FGViewMgr::getViewPitchOffset_deg,
253 &FGViewMgr::setViewPitchOffset_deg);
254 fgSetArchivable("/sim/current-view/pitch-offset-deg");
255 fgTie("/sim/current-view/goal-pitch-offset-deg", this,
256 &FGViewMgr::getGoalViewPitchOffset_deg,
257 &FGViewMgr::setGoalViewPitchOffset_deg);
258 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
260 fgTie("/sim/current-view/view-number", this,
261 &FGViewMgr::getView, &FGViewMgr::setView);
262 fgSetArchivable("/sim/current-view/view-number", FALSE);
264 fgTie("/sim/current-view/axes/long", this,
265 (double_getter)0, &FGViewMgr::setViewAxisLong);
266 fgSetArchivable("/sim/current-view/axes/long");
268 fgTie("/sim/current-view/axes/lat", this,
269 (double_getter)0, &FGViewMgr::setViewAxisLat);
270 fgSetArchivable("/sim/current-view/axes/lat");
272 fgTie("/sim/current-view/field-of-view", this,
273 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
274 fgSetArchivable("/sim/current-view/field-of-view");
276 fgTie("/sim/current-view/ground-level-nearplane-m", this,
277 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
278 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
286 // need to redo these bindings to the new locations (move to viewer?)
287 fgUntie("/sim/current-view/heading-offset-deg");
288 fgUntie("/sim/current-view/goal-heading-offset-deg");
289 fgUntie("/sim/current-view/pitch-offset-deg");
290 fgUntie("/sim/current-view/goal-pitch-offset-deg");
291 fgUntie("/sim/field-of-view");
292 fgUntie("/sim/current-view/view-number");
293 fgUntie("/sim/current-view/axes/long");
294 fgUntie("/sim/current-view/axes/lat");
295 fgUntie("/sim/current-view/ground-level-nearplane-m");
299 FGViewMgr::update (double dt)
302 string viewpath, nodepath;
303 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
305 FGViewer * view = get_current_view();
311 viewpath = "/sim/view";
312 sprintf(stridx, "[%d]", i);
315 FGViewer *loop_view = (FGViewer *)get_view( i );
317 // Set up view location and orientation
320 nodepath += "/config/from-model";
321 if (!fgGetBool(nodepath.c_str())) {
323 nodepath += "/config/eye-lon-deg-path";
324 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
326 nodepath += "/config/eye-lat-deg-path";
327 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
329 nodepath += "/config/eye-alt-ft-path";
330 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
332 nodepath += "/config/eye-roll-deg-path";
333 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
335 nodepath += "/config/eye-pitch-deg-path";
336 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
338 nodepath += "/config/eye-heading-deg-path";
339 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
340 loop_view->setPosition(lon_deg, lat_deg, alt_ft);
341 loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
343 // force recalc in viewer
344 loop_view->set_dirty();
347 // if lookat (type 1) then get target data...
348 if (loop_view->getType() == FG_LOOKAT) {
350 nodepath += "/config/from-model";
351 if (!fgGetBool(nodepath.c_str())) {
353 nodepath += "/config/target-lon-deg-path";
354 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
356 nodepath += "/config/target-lat-deg-path";
357 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
359 nodepath += "/config/target-alt-ft-path";
360 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
362 nodepath += "/config/target-roll-deg-path";
363 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
365 nodepath += "/config/target-pitch-deg-path";
366 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
368 nodepath += "/config/target-heading-deg-path";
369 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
371 loop_view ->setTargetPosition(lon_deg, lat_deg, alt_ft);
372 loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
374 loop_view->set_dirty();
378 setViewXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
379 setViewYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
380 setViewZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
382 setViewTargetXOffset_m(fgGetDouble("/sim/current-view/target-x-offset-m"));
383 setViewTargetYOffset_m(fgGetDouble("/sim/current-view/target-y-offset-m"));
384 setViewTargetZOffset_m(fgGetDouble("/sim/current-view/target-z-offset-m"));
386 // Update the current view
392 FGViewMgr::copyToCurrent()
395 string viewpath, nodepath;
398 viewpath = "/sim/view";
399 sprintf(stridx, "[%d]", i);
402 // copy certain view config data for default values
404 nodepath += "/config/default-heading-offset-deg";
405 fgSetDouble("/sim/current-view/config/heading-offset-deg",
406 fgGetDouble(nodepath.c_str()));
409 nodepath += "/config/pitch-offset-deg";
410 fgSetDouble("/sim/current-view/config/pitch-offset-deg",
411 fgGetDouble(nodepath.c_str()));
414 nodepath += "/config/roll-offset-deg";
415 fgSetDouble("/sim/current-view/config/roll-offset-deg",
416 fgGetDouble(nodepath.c_str()));
419 nodepath += "/config/default-field-of-view-deg";
420 fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
421 fgGetDouble(nodepath.c_str()));
424 fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
425 fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
426 fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
427 fgSetDouble("/sim/current-view/goal-heading-offset-deg",
428 get_current_view()->getGoalHeadingOffset_deg());
429 fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
430 get_current_view()->getGoalPitchOffset_deg());
431 fgSetDouble("/sim/current-view/goal-roll-offset-deg",
432 get_current_view()->getRollOffset_deg());
433 fgSetDouble("/sim/current-view/heading-offset-deg",
434 get_current_view()->getHeadingOffset_deg());
435 fgSetDouble("/sim/current-view/pitch-offset-deg",
436 get_current_view()->getPitchOffset_deg());
437 fgSetDouble("/sim/current-view/roll-offset-deg",
438 get_current_view()->getRollOffset_deg());
439 fgSetDouble("/sim/current-view/target-x-offset-m",
440 get_current_view()->getTargetXOffset_m());
441 fgSetDouble("/sim/current-view/target-y-offset-m",
442 get_current_view()->getTargetYOffset_m());
443 fgSetDouble("/sim/current-view/target-z-offset-m",
444 get_current_view()->getTargetZOffset_m());
449 FGViewMgr::getViewHeadingOffset_deg () const
451 const FGViewer * view = get_current_view();
452 return (view == 0 ? 0 : view->getHeadingOffset_deg());
456 FGViewMgr::setViewHeadingOffset_deg (double offset)
458 FGViewer * view = get_current_view();
460 view->setGoalHeadingOffset_deg(offset);
461 view->setHeadingOffset_deg(offset);
466 FGViewMgr::getViewGoalHeadingOffset_deg () const
468 const FGViewer * view = get_current_view();
469 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
473 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
475 FGViewer * view = get_current_view();
477 view->setGoalHeadingOffset_deg(offset);
481 FGViewMgr::getViewPitchOffset_deg () const
483 const FGViewer * view = get_current_view();
484 return (view == 0 ? 0 : view->getPitchOffset_deg());
488 FGViewMgr::setViewPitchOffset_deg (double tilt)
490 FGViewer * view = get_current_view();
492 view->setGoalPitchOffset_deg(tilt);
493 view->setPitchOffset_deg(tilt);
498 FGViewMgr::getGoalViewPitchOffset_deg () const
500 const FGViewer * view = get_current_view();
501 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
505 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
507 FGViewer * view = get_current_view();
509 view->setGoalPitchOffset_deg(tilt);
513 FGViewMgr::getViewXOffset_m () const
515 const FGViewer * view = get_current_view();
517 return ((FGViewer *)view)->getXOffset_m();
524 FGViewMgr::setViewXOffset_m (double x)
526 FGViewer * view = get_current_view();
528 view->setXOffset_m(x);
533 FGViewMgr::getViewYOffset_m () const
535 const FGViewer * view = get_current_view();
537 return ((FGViewer *)view)->getYOffset_m();
544 FGViewMgr::setViewYOffset_m (double y)
546 FGViewer * view = get_current_view();
548 view->setYOffset_m(y);
553 FGViewMgr::getViewZOffset_m () const
555 const FGViewer * view = get_current_view();
557 return ((FGViewer *)view)->getZOffset_m();
564 FGViewMgr::setViewZOffset_m (double z)
566 FGViewer * view = get_current_view();
568 view->setZOffset_m(z);
573 FGViewMgr::getViewTargetXOffset_m () const
575 const FGViewer * view = get_current_view();
577 return ((FGViewer *)view)->getTargetXOffset_m();
584 FGViewMgr::setViewTargetXOffset_m (double x)
586 FGViewer * view = get_current_view();
588 view->setTargetXOffset_m(x);
593 FGViewMgr::getViewTargetYOffset_m () const
595 const FGViewer * view = get_current_view();
597 return ((FGViewer *)view)->getTargetYOffset_m();
604 FGViewMgr::setViewTargetYOffset_m (double y)
606 FGViewer * view = get_current_view();
608 view->setTargetYOffset_m(y);
613 FGViewMgr::getViewTargetZOffset_m () const
615 const FGViewer * view = get_current_view();
617 return ((FGViewer *)view)->getTargetZOffset_m();
624 FGViewMgr::setViewTargetZOffset_m (double z)
626 FGViewer * view = get_current_view();
628 view->setTargetZOffset_m(z);
633 FGViewMgr::getView () const
639 FGViewMgr::setView (int newview )
641 // if newview number too low wrap to last view...
643 newview = (int)views.size() -1;
645 // if newview number to high wrap to zero...
646 if ( newview > ((int)views.size() -1) ) {
657 FGViewMgr::getFOV_deg () const
659 const FGViewer * view = get_current_view();
660 return (view == 0 ? 0 : view->get_fov());
664 FGViewMgr::setFOV_deg (double fov)
666 FGViewer * view = get_current_view();
672 FGViewMgr::getNear_m () const
674 const FGViewer * view = get_current_view();
675 return (view == 0 ? 0.5f : view->getNear_m());
679 FGViewMgr::setNear_m (double near_m)
681 FGViewer * view = get_current_view();
683 view->setNear_m(near_m);
687 FGViewMgr::setViewAxisLong (double axis)
693 FGViewMgr::setViewAxisLat (double axis)
699 FGViewMgr::do_axes ()
701 // Take no action when hat is centered
702 if ( ( axis_long < 0.01 ) &&
703 ( axis_long > -0.01 ) &&
704 ( axis_lat < 0.01 ) &&
709 double viewDir = 999;
711 /* Do all the quick and easy cases */
712 if (axis_long < 0) { // Longitudinal axis forward
713 if (axis_lat == axis_long)
714 viewDir = fgGetDouble("/sim/view/config/front-left-direction-deg");
715 else if (axis_lat == - axis_long)
716 viewDir = fgGetDouble("/sim/view/config/front-right-direction-deg");
717 else if (axis_lat == 0)
718 viewDir = fgGetDouble("/sim/view/config/front-direction-deg");
719 } else if (axis_long > 0) { // Longitudinal axis backward
720 if (axis_lat == - axis_long)
721 viewDir = fgGetDouble("/sim/view/config/back-left-direction-deg");
722 else if (axis_lat == axis_long)
723 viewDir = fgGetDouble("/sim/view/config/back-right-direction-deg");
724 else if (axis_lat == 0)
725 viewDir = fgGetDouble("/sim/view/config/back-direction-deg");
726 } else if (axis_long == 0) { // Longitudinal axis neutral
728 viewDir = fgGetDouble("/sim/view/config/left-direction-deg");
729 else if (axis_lat > 0)
730 viewDir = fgGetDouble("/sim/view/config/right-direction-deg");
731 else return; /* And assertion failure maybe? */
734 // Do all the difficult cases
736 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
737 if ( viewDir < -1 ) viewDir += 360;
739 get_current_view()->setGoalHeadingOffset_deg(viewDir);