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;
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-alt-damping";
100 damp_alt = fgGetDouble(nodepath.c_str(), 0.0);
103 nodepath += "/config/at-model-roll-damping";
104 damp_roll = fgGetDouble(nodepath.c_str(), 0.0);
107 nodepath += "/config/at-model-pitch-damping";
108 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_alt, 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 ));
167 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index,
168 false, 0, 0.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 ));
182 string viewpath, nodepath, strdata;
185 // reset offsets and fov to configuration defaults
187 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
188 viewpath = "/sim/view";
189 sprintf(stridx, "[%d]", i);
195 nodepath += "/config/x-offset-m";
196 fgSetDouble("/sim/current-view/x-offset-m",fgGetDouble(nodepath.c_str()));
199 nodepath += "/config/y-offset-m";
200 fgSetDouble("/sim/current-view/y-offset-m",fgGetDouble(nodepath.c_str()));
203 nodepath += "/config/z-offset-m";
204 fgSetDouble("/sim/current-view/z-offset-m",fgGetDouble(nodepath.c_str()));
207 nodepath += "/config/pitch-offset-deg";
208 fgSetDouble("/sim/current-view/pitch-offset-deg",
209 fgGetDouble(nodepath.c_str()));
212 nodepath += "/config/heading-offset-deg";
213 fgSetDouble("/sim/current-view/heading-offset-deg",
214 fgGetDouble(nodepath.c_str()));
217 nodepath += "/config/roll-offset-deg";
218 fgSetDouble("/sim/current-view/roll-offset-deg",
219 fgGetDouble(nodepath.c_str()));
222 nodepath += "/config/default-field-of-view-deg";
223 fov_deg = fgGetDouble(nodepath.c_str());
224 if (fov_deg < 10.0) {
227 fgSetDouble("/sim/current-view/field-of-view",fov_deg);
229 // target offsets for lookat mode only...
231 nodepath += "/config/target-x-offset-m";
232 fgSetDouble("/sim/current-view/target-x-offset-deg",
233 fgGetDouble(nodepath.c_str()));
236 nodepath += "/config/target-y-offset-m";
237 fgSetDouble("/sim/current-view/target-y-offset-deg",
238 fgGetDouble(nodepath.c_str()));
241 nodepath += "/config/target-z-offset-m";
242 fgSetDouble("/sim/current-view/target-z-offset-deg",
243 fgGetDouble(nodepath.c_str()));
251 typedef double (FGViewMgr::*double_getter)() const;
256 // these are bound to the current view properties
257 fgTie("/sim/current-view/heading-offset-deg", this,
258 &FGViewMgr::getViewHeadingOffset_deg,
259 &FGViewMgr::setViewHeadingOffset_deg);
260 fgSetArchivable("/sim/current-view/heading-offset-deg");
261 fgTie("/sim/current-view/goal-heading-offset-deg", this,
262 &FGViewMgr::getViewGoalHeadingOffset_deg,
263 &FGViewMgr::setViewGoalHeadingOffset_deg);
264 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
265 fgTie("/sim/current-view/pitch-offset-deg", this,
266 &FGViewMgr::getViewPitchOffset_deg,
267 &FGViewMgr::setViewPitchOffset_deg);
268 fgSetArchivable("/sim/current-view/pitch-offset-deg");
269 fgTie("/sim/current-view/goal-pitch-offset-deg", this,
270 &FGViewMgr::getGoalViewPitchOffset_deg,
271 &FGViewMgr::setGoalViewPitchOffset_deg);
272 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
274 fgTie("/sim/current-view/view-number", this,
275 &FGViewMgr::getView, &FGViewMgr::setView);
276 fgSetArchivable("/sim/current-view/view-number", FALSE);
278 fgTie("/sim/current-view/axes/long", this,
279 (double_getter)0, &FGViewMgr::setViewAxisLong);
280 fgSetArchivable("/sim/current-view/axes/long");
282 fgTie("/sim/current-view/axes/lat", this,
283 (double_getter)0, &FGViewMgr::setViewAxisLat);
284 fgSetArchivable("/sim/current-view/axes/lat");
286 fgTie("/sim/current-view/field-of-view", this,
287 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
288 fgSetArchivable("/sim/current-view/field-of-view");
290 fgTie("/sim/current-view/ground-level-nearplane-m", this,
291 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
292 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
300 // need to redo these bindings to the new locations (move to viewer?)
301 fgUntie("/sim/current-view/heading-offset-deg");
302 fgUntie("/sim/current-view/goal-heading-offset-deg");
303 fgUntie("/sim/current-view/pitch-offset-deg");
304 fgUntie("/sim/current-view/goal-pitch-offset-deg");
305 fgUntie("/sim/field-of-view");
306 fgUntie("/sim/current-view/view-number");
307 fgUntie("/sim/current-view/axes/long");
308 fgUntie("/sim/current-view/axes/lat");
309 fgUntie("/sim/current-view/ground-level-nearplane-m");
313 FGViewMgr::update (double dt)
316 string viewpath, nodepath;
317 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
319 FGViewer * view = get_current_view();
325 viewpath = "/sim/view";
326 sprintf(stridx, "[%d]", i);
329 FGViewer *loop_view = (FGViewer *)get_view( i );
331 // Set up view location and orientation
334 nodepath += "/config/from-model";
335 if (!fgGetBool(nodepath.c_str())) {
337 nodepath += "/config/eye-lon-deg-path";
338 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
340 nodepath += "/config/eye-lat-deg-path";
341 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
343 nodepath += "/config/eye-alt-ft-path";
344 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
346 nodepath += "/config/eye-roll-deg-path";
347 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
349 nodepath += "/config/eye-pitch-deg-path";
350 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
352 nodepath += "/config/eye-heading-deg-path";
353 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
354 loop_view->setPosition(lon_deg, lat_deg, alt_ft);
355 loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
357 // force recalc in viewer
358 loop_view->set_dirty();
361 // if lookat (type 1) then get target data...
362 if (loop_view->getType() == FG_LOOKAT) {
364 nodepath += "/config/from-model";
365 if (!fgGetBool(nodepath.c_str())) {
367 nodepath += "/config/target-lon-deg-path";
368 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
370 nodepath += "/config/target-lat-deg-path";
371 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
373 nodepath += "/config/target-alt-ft-path";
374 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
376 nodepath += "/config/target-roll-deg-path";
377 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
379 nodepath += "/config/target-pitch-deg-path";
380 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
382 nodepath += "/config/target-heading-deg-path";
383 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
385 loop_view->setTargetPosition(lon_deg, lat_deg, alt_ft);
386 loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
388 loop_view->set_dirty();
392 setViewXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
393 setViewYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
394 setViewZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
396 setViewTargetXOffset_m(fgGetDouble("/sim/current-view/target-x-offset-m"));
397 setViewTargetYOffset_m(fgGetDouble("/sim/current-view/target-y-offset-m"));
398 setViewTargetZOffset_m(fgGetDouble("/sim/current-view/target-z-offset-m"));
400 // Update the current view
406 FGViewMgr::copyToCurrent()
409 string viewpath, nodepath;
412 viewpath = "/sim/view";
413 sprintf(stridx, "[%d]", i);
416 // copy certain view config data for default values
418 nodepath += "/config/default-heading-offset-deg";
419 fgSetDouble("/sim/current-view/config/heading-offset-deg",
420 fgGetDouble(nodepath.c_str()));
423 nodepath += "/config/pitch-offset-deg";
424 fgSetDouble("/sim/current-view/config/pitch-offset-deg",
425 fgGetDouble(nodepath.c_str()));
428 nodepath += "/config/roll-offset-deg";
429 fgSetDouble("/sim/current-view/config/roll-offset-deg",
430 fgGetDouble(nodepath.c_str()));
433 nodepath += "/config/default-field-of-view-deg";
434 fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
435 fgGetDouble(nodepath.c_str()));
438 fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
439 fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
440 fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
441 fgSetDouble("/sim/current-view/goal-heading-offset-deg",
442 get_current_view()->getGoalHeadingOffset_deg());
443 fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
444 get_current_view()->getGoalPitchOffset_deg());
445 fgSetDouble("/sim/current-view/goal-roll-offset-deg",
446 get_current_view()->getRollOffset_deg());
447 fgSetDouble("/sim/current-view/heading-offset-deg",
448 get_current_view()->getHeadingOffset_deg());
449 fgSetDouble("/sim/current-view/pitch-offset-deg",
450 get_current_view()->getPitchOffset_deg());
451 fgSetDouble("/sim/current-view/roll-offset-deg",
452 get_current_view()->getRollOffset_deg());
453 fgSetDouble("/sim/current-view/target-x-offset-m",
454 get_current_view()->getTargetXOffset_m());
455 fgSetDouble("/sim/current-view/target-y-offset-m",
456 get_current_view()->getTargetYOffset_m());
457 fgSetDouble("/sim/current-view/target-z-offset-m",
458 get_current_view()->getTargetZOffset_m());
463 FGViewMgr::getViewHeadingOffset_deg () const
465 const FGViewer * view = get_current_view();
466 return (view == 0 ? 0 : view->getHeadingOffset_deg());
470 FGViewMgr::setViewHeadingOffset_deg (double offset)
472 FGViewer * view = get_current_view();
474 view->setGoalHeadingOffset_deg(offset);
475 view->setHeadingOffset_deg(offset);
480 FGViewMgr::getViewGoalHeadingOffset_deg () const
482 const FGViewer * view = get_current_view();
483 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
487 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
489 FGViewer * view = get_current_view();
491 view->setGoalHeadingOffset_deg(offset);
495 FGViewMgr::getViewPitchOffset_deg () const
497 const FGViewer * view = get_current_view();
498 return (view == 0 ? 0 : view->getPitchOffset_deg());
502 FGViewMgr::setViewPitchOffset_deg (double tilt)
504 FGViewer * view = get_current_view();
506 view->setGoalPitchOffset_deg(tilt);
507 view->setPitchOffset_deg(tilt);
512 FGViewMgr::getGoalViewPitchOffset_deg () const
514 const FGViewer * view = get_current_view();
515 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
519 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
521 FGViewer * view = get_current_view();
523 view->setGoalPitchOffset_deg(tilt);
527 FGViewMgr::getViewXOffset_m () const
529 const FGViewer * view = get_current_view();
531 return ((FGViewer *)view)->getXOffset_m();
538 FGViewMgr::setViewXOffset_m (double x)
540 FGViewer * view = get_current_view();
542 view->setXOffset_m(x);
547 FGViewMgr::getViewYOffset_m () const
549 const FGViewer * view = get_current_view();
551 return ((FGViewer *)view)->getYOffset_m();
558 FGViewMgr::setViewYOffset_m (double y)
560 FGViewer * view = get_current_view();
562 view->setYOffset_m(y);
567 FGViewMgr::getViewZOffset_m () const
569 const FGViewer * view = get_current_view();
571 return ((FGViewer *)view)->getZOffset_m();
578 FGViewMgr::setViewZOffset_m (double z)
580 FGViewer * view = get_current_view();
582 view->setZOffset_m(z);
587 FGViewMgr::getViewTargetXOffset_m () const
589 const FGViewer * view = get_current_view();
591 return ((FGViewer *)view)->getTargetXOffset_m();
598 FGViewMgr::setViewTargetXOffset_m (double x)
600 FGViewer * view = get_current_view();
602 view->setTargetXOffset_m(x);
607 FGViewMgr::getViewTargetYOffset_m () const
609 const FGViewer * view = get_current_view();
611 return ((FGViewer *)view)->getTargetYOffset_m();
618 FGViewMgr::setViewTargetYOffset_m (double y)
620 FGViewer * view = get_current_view();
622 view->setTargetYOffset_m(y);
627 FGViewMgr::getViewTargetZOffset_m () const
629 const FGViewer * view = get_current_view();
631 return ((FGViewer *)view)->getTargetZOffset_m();
638 FGViewMgr::setViewTargetZOffset_m (double z)
640 FGViewer * view = get_current_view();
642 view->setTargetZOffset_m(z);
647 FGViewMgr::getView () const
653 FGViewMgr::setView (int newview )
655 // if newview number too low wrap to last view...
657 newview = (int)views.size() -1;
659 // if newview number to high wrap to zero...
660 if ( newview > ((int)views.size() -1) ) {
671 FGViewMgr::getFOV_deg () const
673 const FGViewer * view = get_current_view();
674 return (view == 0 ? 0 : view->get_fov());
678 FGViewMgr::setFOV_deg (double fov)
680 FGViewer * view = get_current_view();
686 FGViewMgr::getNear_m () const
688 const FGViewer * view = get_current_view();
689 return (view == 0 ? 0.5f : view->getNear_m());
693 FGViewMgr::setNear_m (double near_m)
695 FGViewer * view = get_current_view();
697 view->setNear_m(near_m);
701 FGViewMgr::setViewAxisLong (double axis)
707 FGViewMgr::setViewAxisLat (double axis)
713 FGViewMgr::do_axes ()
715 // Take no action when hat is centered
716 if ( ( axis_long < 0.01 ) &&
717 ( axis_long > -0.01 ) &&
718 ( axis_lat < 0.01 ) &&
723 double viewDir = 999;
725 /* Do all the quick and easy cases */
726 if (axis_long < 0) { // Longitudinal axis forward
727 if (axis_lat == axis_long)
728 viewDir = fgGetDouble("/sim/view/config/front-left-direction-deg");
729 else if (axis_lat == - axis_long)
730 viewDir = fgGetDouble("/sim/view/config/front-right-direction-deg");
731 else if (axis_lat == 0)
732 viewDir = fgGetDouble("/sim/view/config/front-direction-deg");
733 } else if (axis_long > 0) { // Longitudinal axis backward
734 if (axis_lat == - axis_long)
735 viewDir = fgGetDouble("/sim/view/config/back-left-direction-deg");
736 else if (axis_lat == axis_long)
737 viewDir = fgGetDouble("/sim/view/config/back-right-direction-deg");
738 else if (axis_lat == 0)
739 viewDir = fgGetDouble("/sim/view/config/back-direction-deg");
740 } else if (axis_long == 0) { // Longitudinal axis neutral
742 viewDir = fgGetDouble("/sim/view/config/left-direction-deg");
743 else if (axis_lat > 0)
744 viewDir = fgGetDouble("/sim/view/config/right-direction-deg");
745 else return; /* And assertion failure maybe? */
748 // Do all the difficult cases
750 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
751 if ( viewDir < -1 ) viewDir += 360;
753 get_current_view()->setGoalHeadingOffset_deg(viewDir);