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 x_offset_m, y_offset_m, z_offset_m, fov_deg;
55 double heading_offset_deg, pitch_offset_deg, roll_offset_deg;
56 double target_x_offset_m, target_y_offset_m, target_z_offset_m;
59 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
60 viewpath = "/sim/view";
61 sprintf(stridx, "[%d]", i);
64 // find out what type of view this is...
67 strdata = fgGetString(nodepath.c_str());
70 // this is assumed to be an aircraft model...we will need to read
71 // model-from-type as well.
72 // find out if this is a model we are looking from...
74 nodepath += "/config/from-model";
75 from_model = fgGetBool(nodepath.c_str());
77 // get model index (which model)
80 nodepath += "/config/from-model-idx";
81 from_model_index = fgGetInt(nodepath.c_str());
84 if ( strcmp("lookat",strdata.c_str()) == 0 ) {
85 // find out if this is a model we are looking at...
87 nodepath += "/config/at-model";
88 at_model = fgGetBool(nodepath.c_str());
90 // get model index (which model)
93 nodepath += "/config/at-model-idx";
94 at_model_index = fgGetInt(nodepath.c_str());
99 nodepath += "/config/x-offset-m";
100 x_offset_m = fgGetDouble(nodepath.c_str());
102 nodepath += "/config/y-offset-m";
103 y_offset_m = fgGetDouble(nodepath.c_str());
105 nodepath += "/config/z-offset-m";
106 z_offset_m = fgGetDouble(nodepath.c_str());
108 nodepath += "/config/pitch-offset-deg";
109 pitch_offset_deg = fgGetDouble(nodepath.c_str());
110 fgSetDouble(nodepath.c_str(),pitch_offset_deg);
112 nodepath += "/config/heading-offset-deg";
113 heading_offset_deg = fgGetDouble(nodepath.c_str());
114 fgSetDouble(nodepath.c_str(),heading_offset_deg);
116 nodepath += "/config/roll-offset-deg";
117 roll_offset_deg = fgGetDouble(nodepath.c_str());
118 fgSetDouble(nodepath.c_str(),roll_offset_deg);
120 nodepath += "/config/default-field-of-view-deg";
121 fov_deg = fgGetDouble(nodepath.c_str());
123 // target offsets for lookat mode only...
125 nodepath += "/config/target-x-offset-m";
126 target_x_offset_m = fgGetDouble(nodepath.c_str());
128 nodepath += "/config/target-y-offset-m";
129 target_y_offset_m = fgGetDouble(nodepath.c_str());
131 nodepath += "/config/target-z-offset-m";
132 target_z_offset_m = fgGetDouble(nodepath.c_str());
135 nodepath += "/config/ground-level-nearplane-m";
136 near_m = fgGetDouble(nodepath.c_str());
138 // supporting two types now "lookat" = 1 and "lookfrom" = 0
139 if ( strcmp("lookat",strdata.c_str()) == 0 )
140 add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
141 at_model, at_model_index, x_offset_m,
142 y_offset_m,z_offset_m,
143 heading_offset_deg, pitch_offset_deg,
144 roll_offset_deg, fov_deg,
145 target_x_offset_m, target_y_offset_m,
146 target_z_offset_m, near_m ));
148 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index, false,
149 0, x_offset_m, y_offset_m, z_offset_m,
150 heading_offset_deg, pitch_offset_deg,
151 roll_offset_deg, fov_deg, 0, 0, 0, near_m ));
162 string viewpath, nodepath, strdata;
165 // reset offsets and fov to configuration defaults
167 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
168 viewpath = "/sim/view";
169 sprintf(stridx, "[%d]", i);
175 nodepath += "/config/x-offset-m";
176 fgSetDouble("/sim/current-view/x-offset-m",fgGetDouble(nodepath.c_str()));
179 nodepath += "/config/y-offset-m";
180 fgSetDouble("/sim/current-view/y-offset-m",fgGetDouble(nodepath.c_str()));
183 nodepath += "/config/z-offset-m";
184 fgSetDouble("/sim/current-view/z-offset-m",fgGetDouble(nodepath.c_str()));
187 nodepath += "/config/pitch-offset-deg";
188 fgSetDouble("/sim/current-view/pitch-offset-deg",
189 fgGetDouble(nodepath.c_str()));
192 nodepath += "/config/heading-offset-deg";
193 fgSetDouble("/sim/current-view/heading-offset-deg",
194 fgGetDouble(nodepath.c_str()));
197 nodepath += "/config/roll-offset-deg";
198 fgSetDouble("/sim/current-view/roll-offset-deg",
199 fgGetDouble(nodepath.c_str()));
202 nodepath += "/config/default-field-of-view-deg";
203 fov_deg = fgGetDouble(nodepath.c_str());
204 if (fov_deg < 10.0) {
207 fgSetDouble("/sim/current-view/field-of-view",fov_deg);
209 // target offsets for lookat mode only...
211 nodepath += "/config/target-x-offset-m";
212 fgSetDouble("/sim/current-view/target-x-offset-deg",
213 fgGetDouble(nodepath.c_str()));
216 nodepath += "/config/target-y-offset-m";
217 fgSetDouble("/sim/current-view/target-y-offset-deg",
218 fgGetDouble(nodepath.c_str()));
221 nodepath += "/config/target-z-offset-m";
222 fgSetDouble("/sim/current-view/target-z-offset-deg",
223 fgGetDouble(nodepath.c_str()));
231 typedef double (FGViewMgr::*double_getter)() const;
236 // these are bound to the current view properties
237 fgTie("/sim/current-view/heading-offset-deg", this,
238 &FGViewMgr::getViewHeadingOffset_deg,
239 &FGViewMgr::setViewHeadingOffset_deg);
240 fgSetArchivable("/sim/current-view/heading-offset-deg");
241 fgTie("/sim/current-view/goal-heading-offset-deg", this,
242 &FGViewMgr::getViewGoalHeadingOffset_deg,
243 &FGViewMgr::setViewGoalHeadingOffset_deg);
244 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
245 fgTie("/sim/current-view/pitch-offset-deg", this,
246 &FGViewMgr::getViewPitchOffset_deg,
247 &FGViewMgr::setViewPitchOffset_deg);
248 fgSetArchivable("/sim/current-view/pitch-offset-deg");
249 fgTie("/sim/current-view/goal-pitch-offset-deg", this,
250 &FGViewMgr::getGoalViewPitchOffset_deg,
251 &FGViewMgr::setGoalViewPitchOffset_deg);
252 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
254 fgTie("/sim/current-view/view-number", this,
255 &FGViewMgr::getView, &FGViewMgr::setView);
256 fgSetArchivable("/sim/current-view/view-number", FALSE);
258 fgTie("/sim/current-view/axes/long", this,
259 (double_getter)0, &FGViewMgr::setViewAxisLong);
260 fgSetArchivable("/sim/current-view/axes/long");
262 fgTie("/sim/current-view/axes/lat", this,
263 (double_getter)0, &FGViewMgr::setViewAxisLat);
264 fgSetArchivable("/sim/current-view/axes/lat");
266 fgTie("/sim/current-view/field-of-view", this,
267 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
268 fgSetArchivable("/sim/current-view/field-of-view");
270 fgTie("/sim/current-view/ground-level-nearplane-m", this,
271 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
272 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
280 // need to redo these bindings to the new locations (move to viewer?)
281 fgUntie("/sim/current-view/heading-offset-deg");
282 fgUntie("/sim/current-view/goal-heading-offset-deg");
283 fgUntie("/sim/current-view/pitch-offset-deg");
284 fgUntie("/sim/current-view/goal-pitch-offset-deg");
285 fgUntie("/sim/field-of-view");
286 fgUntie("/sim/current-view/view-number");
287 fgUntie("/sim/current-view/axes/long");
288 fgUntie("/sim/current-view/axes/lat");
289 fgUntie("/sim/current-view/ground-level-nearplane-m");
293 FGViewMgr::update (double dt)
296 string viewpath, nodepath;
297 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
299 FGViewer * view = get_current_view();
305 viewpath = "/sim/view";
306 sprintf(stridx, "[%d]", i);
309 FGViewer *loop_view = (FGViewer *)get_view( i );
311 // Set up view location and orientation
314 nodepath += "/config/from-model";
315 if (!fgGetBool(nodepath.c_str())) {
317 nodepath += "/config/eye-lon-deg-path";
318 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
320 nodepath += "/config/eye-lat-deg-path";
321 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
323 nodepath += "/config/eye-alt-ft-path";
324 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
326 nodepath += "/config/eye-roll-deg-path";
327 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
329 nodepath += "/config/eye-pitch-deg-path";
330 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
332 nodepath += "/config/eye-heading-deg-path";
333 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
334 loop_view->setPosition(lon_deg, lat_deg, alt_ft);
335 loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
337 // force recalc in viewer
338 loop_view->set_dirty();
341 // if lookat (type 1) then get target data...
342 if (loop_view->getType() == FG_LOOKAT) {
344 nodepath += "/config/from-model";
345 if (!fgGetBool(nodepath.c_str())) {
347 nodepath += "/config/target-lon-deg-path";
348 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
350 nodepath += "/config/target-lat-deg-path";
351 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
353 nodepath += "/config/target-alt-ft-path";
354 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
356 nodepath += "/config/target-roll-deg-path";
357 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
359 nodepath += "/config/target-pitch-deg-path";
360 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
362 nodepath += "/config/target-heading-deg-path";
363 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
365 loop_view ->setTargetPosition(lon_deg, lat_deg, alt_ft);
366 loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
368 loop_view->set_dirty();
372 setViewXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
373 setViewYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
374 setViewZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
376 setViewTargetXOffset_m(fgGetDouble("/sim/current-view/target-x-offset-m"));
377 setViewTargetYOffset_m(fgGetDouble("/sim/current-view/target-y-offset-m"));
378 setViewTargetZOffset_m(fgGetDouble("/sim/current-view/target-z-offset-m"));
380 // Update the current view
386 FGViewMgr::copyToCurrent()
389 string viewpath, nodepath;
392 viewpath = "/sim/view";
393 sprintf(stridx, "[%d]", i);
396 // copy certain view config data for default values
398 nodepath += "/config/default-heading-offset-deg";
399 fgSetDouble("/sim/current-view/config/heading-offset-deg",
400 fgGetDouble(nodepath.c_str()));
403 nodepath += "/config/pitch-offset-deg";
404 fgSetDouble("/sim/current-view/config/pitch-offset-deg",
405 fgGetDouble(nodepath.c_str()));
408 nodepath += "/config/roll-offset-deg";
409 fgSetDouble("/sim/current-view/config/roll-offset-deg",
410 fgGetDouble(nodepath.c_str()));
413 nodepath += "/config/default-field-of-view-deg";
414 fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
415 fgGetDouble(nodepath.c_str()));
418 fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
419 fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
420 fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
421 fgSetDouble("/sim/current-view/goal-heading-offset-deg",
422 get_current_view()->getGoalHeadingOffset_deg());
423 fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
424 get_current_view()->getGoalPitchOffset_deg());
425 fgSetDouble("/sim/current-view/goal-roll-offset-deg",
426 get_current_view()->getRollOffset_deg());
427 fgSetDouble("/sim/current-view/heading-offset-deg",
428 get_current_view()->getHeadingOffset_deg());
429 fgSetDouble("/sim/current-view/pitch-offset-deg",
430 get_current_view()->getPitchOffset_deg());
431 fgSetDouble("/sim/current-view/roll-offset-deg",
432 get_current_view()->getRollOffset_deg());
433 fgSetDouble("/sim/current-view/target-x-offset-m",
434 get_current_view()->getTargetXOffset_m());
435 fgSetDouble("/sim/current-view/target-y-offset-m",
436 get_current_view()->getTargetYOffset_m());
437 fgSetDouble("/sim/current-view/target-z-offset-m",
438 get_current_view()->getTargetZOffset_m());
443 FGViewMgr::getViewHeadingOffset_deg () const
445 const FGViewer * view = get_current_view();
446 return (view == 0 ? 0 : view->getHeadingOffset_deg());
450 FGViewMgr::setViewHeadingOffset_deg (double offset)
452 FGViewer * view = get_current_view();
454 view->setGoalHeadingOffset_deg(offset);
455 view->setHeadingOffset_deg(offset);
460 FGViewMgr::getViewGoalHeadingOffset_deg () const
462 const FGViewer * view = get_current_view();
463 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
467 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
469 FGViewer * view = get_current_view();
471 view->setGoalHeadingOffset_deg(offset);
475 FGViewMgr::getViewPitchOffset_deg () const
477 const FGViewer * view = get_current_view();
478 return (view == 0 ? 0 : view->getPitchOffset_deg());
482 FGViewMgr::setViewPitchOffset_deg (double tilt)
484 FGViewer * view = get_current_view();
486 view->setGoalPitchOffset_deg(tilt);
487 view->setPitchOffset_deg(tilt);
492 FGViewMgr::getGoalViewPitchOffset_deg () const
494 const FGViewer * view = get_current_view();
495 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
499 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
501 FGViewer * view = get_current_view();
503 view->setGoalPitchOffset_deg(tilt);
507 FGViewMgr::getViewXOffset_m () const
509 const FGViewer * view = get_current_view();
511 return ((FGViewer *)view)->getXOffset_m();
518 FGViewMgr::setViewXOffset_m (double x)
520 FGViewer * view = get_current_view();
522 view->setXOffset_m(x);
527 FGViewMgr::getViewYOffset_m () const
529 const FGViewer * view = get_current_view();
531 return ((FGViewer *)view)->getYOffset_m();
538 FGViewMgr::setViewYOffset_m (double y)
540 FGViewer * view = get_current_view();
542 view->setYOffset_m(y);
547 FGViewMgr::getViewZOffset_m () const
549 const FGViewer * view = get_current_view();
551 return ((FGViewer *)view)->getZOffset_m();
558 FGViewMgr::setViewZOffset_m (double z)
560 FGViewer * view = get_current_view();
562 view->setZOffset_m(z);
567 FGViewMgr::getViewTargetXOffset_m () const
569 const FGViewer * view = get_current_view();
571 return ((FGViewer *)view)->getTargetXOffset_m();
578 FGViewMgr::setViewTargetXOffset_m (double x)
580 FGViewer * view = get_current_view();
582 view->setTargetXOffset_m(x);
587 FGViewMgr::getViewTargetYOffset_m () const
589 const FGViewer * view = get_current_view();
591 return ((FGViewer *)view)->getTargetYOffset_m();
598 FGViewMgr::setViewTargetYOffset_m (double y)
600 FGViewer * view = get_current_view();
602 view->setTargetYOffset_m(y);
607 FGViewMgr::getViewTargetZOffset_m () const
609 const FGViewer * view = get_current_view();
611 return ((FGViewer *)view)->getTargetZOffset_m();
618 FGViewMgr::setViewTargetZOffset_m (double z)
620 FGViewer * view = get_current_view();
622 view->setTargetZOffset_m(z);
627 FGViewMgr::getView () const
633 FGViewMgr::setView (int newview )
635 if ( newview < 0 || newview > (int)views.size() ) {
646 FGViewMgr::getFOV_deg () const
648 const FGViewer * view = get_current_view();
649 return (view == 0 ? 0 : view->get_fov());
653 FGViewMgr::setFOV_deg (double fov)
655 FGViewer * view = get_current_view();
661 FGViewMgr::getNear_m () const
663 const FGViewer * view = get_current_view();
664 return (view == 0 ? 0.5f : view->getNear_m());
668 FGViewMgr::setNear_m (double near_m)
670 FGViewer * view = get_current_view();
672 view->setNear_m(near_m);
676 FGViewMgr::setViewAxisLong (double axis)
682 FGViewMgr::setViewAxisLat (double axis)
688 FGViewMgr::do_axes ()
690 // Take no action when hat is centered
691 if ( ( axis_long < 0.01 ) &&
692 ( axis_long > -0.01 ) &&
693 ( axis_lat < 0.01 ) &&
698 double viewDir = 999;
700 /* Do all the quick and easy cases */
701 if (axis_long < 0) { // Longitudinal axis forward
702 if (axis_lat == axis_long)
703 viewDir = fgGetDouble("/sim/view/config/front-left-direction-deg");
704 else if (axis_lat == - axis_long)
705 viewDir = fgGetDouble("/sim/view/config/front-right-direction-deg");
706 else if (axis_lat == 0)
707 viewDir = fgGetDouble("/sim/view/config/front-direction-deg");
708 } else if (axis_long > 0) { // Longitudinal axis backward
709 if (axis_lat == - axis_long)
710 viewDir = fgGetDouble("/sim/view/config/back-left-direction-deg");
711 else if (axis_lat == axis_long)
712 viewDir = fgGetDouble("/sim/view/config/back-right-direction-deg");
713 else if (axis_lat == 0)
714 viewDir = fgGetDouble("/sim/view/config/back-direction-deg");
715 } else if (axis_long == 0) { // Longitudinal axis neutral
717 viewDir = fgGetDouble("/sim/view/config/left-direction-deg");
718 else if (axis_lat > 0)
719 viewDir = fgGetDouble("/sim/view/config/right-direction-deg");
720 else return; /* And assertion failure maybe? */
723 // Do all the difficult cases
725 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
726 if ( viewDir < -1 ) viewDir += 360;
728 get_current_view()->setGoalHeadingOffset_deg(viewDir);