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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <string.h> // strcmp
32 #include <simgear/compiler.h>
34 #include <Model/acmodel.hxx>
36 #include "viewmgr.hxx"
40 FGViewMgr::FGViewMgr( void ) :
43 config_list(fgGetNode("/sim", true)->getChildren("view")),
49 FGViewMgr::~FGViewMgr( void ) {
55 double aspect_ratio_multiplier
56 = fgGetDouble("/sim/current-view/aspect-ratio-multiplier");
58 for (unsigned int i = 0; i < config_list.size(); i++) {
59 SGPropertyNode *n = config_list[i];
61 // find out if this is an internal view (e.g. in cockpit, low near plane)
62 bool internal = n->getBoolValue("internal", false);
65 // this is assumed to be an aircraft model...we will need to read
66 // model-from-type as well.
68 // find out if this is a model we are looking from...
69 bool from_model = n->getBoolValue("config/from-model");
70 int from_model_index = n->getIntValue("config/from-model-idx");
72 double x_offset_m = n->getDoubleValue("config/x-offset-m");
73 double y_offset_m = n->getDoubleValue("config/y-offset-m");
74 double z_offset_m = n->getDoubleValue("config/z-offset-m");
76 double heading_offset_deg = n->getDoubleValue("config/heading-offset-deg");
77 n->setDoubleValue("config/heading-offset-deg", heading_offset_deg);
78 double pitch_offset_deg = n->getDoubleValue("config/pitch-offset-deg");
79 n->setDoubleValue("config/pitch-offset-deg", pitch_offset_deg);
80 double roll_offset_deg = n->getDoubleValue("config/roll-offset-deg");
81 n->setDoubleValue("config/roll-offset-deg", roll_offset_deg);
83 double fov_deg = n->getDoubleValue("config/default-field-of-view-deg");
84 double near_m = n->getDoubleValue("config/ground-level-nearplane-m");
86 // supporting two types "lookat" = 1 and "lookfrom" = 0
87 const char *type = n->getStringValue("type");
88 if (!strcmp(type, "lookat")) {
90 bool at_model = n->getBoolValue("config/at-model");
91 int at_model_index = n->getIntValue("config/at-model-idx");
93 double damp_roll = n->getDoubleValue("config/at-model-roll-damping");
94 double damp_pitch = n->getDoubleValue("config/at-model-pitch-damping");
95 double damp_heading = n->getDoubleValue("config/at-model-heading-damping");
97 double target_x_offset_m = n->getDoubleValue("config/target-x-offset-m");
98 double target_y_offset_m = n->getDoubleValue("config/target-y-offset-m");
99 double target_z_offset_m = n->getDoubleValue("config/target-z-offset-m");
101 add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
102 at_model, at_model_index,
103 damp_roll, damp_pitch, damp_heading,
104 x_offset_m, y_offset_m,z_offset_m,
105 heading_offset_deg, pitch_offset_deg,
106 roll_offset_deg, fov_deg, aspect_ratio_multiplier,
107 target_x_offset_m, target_y_offset_m,
108 target_z_offset_m, near_m, internal ));
110 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index,
111 false, 0, 0.0, 0.0, 0.0,
112 x_offset_m, y_offset_m, z_offset_m,
113 heading_offset_deg, pitch_offset_deg,
114 roll_offset_deg, fov_deg, aspect_ratio_multiplier,
115 0, 0, 0, near_m, internal ));
125 // reset offsets and fov to configuration defaults
126 for (unsigned int i = 0; i < config_list.size(); i++) {
127 SGPropertyNode *n = config_list[i];
130 fgSetDouble("/sim/current-view/x-offset-m",
131 n->getDoubleValue("config/x-offset-m"));
132 fgSetDouble("/sim/current-view/y-offset-m",
133 n->getDoubleValue("config/y-offset-m"));
134 fgSetDouble("/sim/current-view/z-offset-m",
135 n->getDoubleValue("config/z-offset-m"));
136 fgSetDouble("/sim/current-view/pitch-offset-deg",
137 n->getDoubleValue("config/pitch-offset-deg"));
138 fgSetDouble("/sim/current-view/heading-offset-deg",
139 n->getDoubleValue("config/heading-offset-deg"));
140 fgSetDouble("/sim/current-view/roll-offset-deg",
141 n->getDoubleValue("config/roll-offset-deg"));
143 double fov_deg = n->getDoubleValue("config/default-field-of-view-deg");
146 fgSetDouble("/sim/current-view/field-of-view", fov_deg);
148 // target offsets for lookat mode only...
149 fgSetDouble("/sim/current-view/target-x-offset-deg",
150 n->getDoubleValue("config/target-x-offset-m"));
151 fgSetDouble("/sim/current-view/target-y-offset-deg",
152 n->getDoubleValue("config/target-y-offset-m"));
153 fgSetDouble("/sim/current-view/target-z-offset-deg",
154 n->getDoubleValue("config/target-z-offset-m"));
159 typedef double (FGViewMgr::*double_getter)() const;
164 // these are bound to the current view properties
165 fgTie("/sim/current-view/heading-offset-deg", this,
166 &FGViewMgr::getViewHeadingOffset_deg,
167 &FGViewMgr::setViewHeadingOffset_deg);
168 fgSetArchivable("/sim/current-view/heading-offset-deg");
169 fgTie("/sim/current-view/goal-heading-offset-deg", this,
170 &FGViewMgr::getViewGoalHeadingOffset_deg,
171 &FGViewMgr::setViewGoalHeadingOffset_deg);
172 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
173 fgTie("/sim/current-view/pitch-offset-deg", this,
174 &FGViewMgr::getViewPitchOffset_deg,
175 &FGViewMgr::setViewPitchOffset_deg);
176 fgSetArchivable("/sim/current-view/pitch-offset-deg");
177 fgTie("/sim/current-view/goal-pitch-offset-deg", this,
178 &FGViewMgr::getGoalViewPitchOffset_deg,
179 &FGViewMgr::setGoalViewPitchOffset_deg);
180 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
181 fgTie("/sim/current-view/roll-offset-deg", this,
182 &FGViewMgr::getViewRollOffset_deg,
183 &FGViewMgr::setViewRollOffset_deg);
184 fgSetArchivable("/sim/current-view/roll-offset-deg");
185 fgTie("/sim/current-view/goal-roll-offset-deg", this,
186 &FGViewMgr::getGoalViewRollOffset_deg,
187 &FGViewMgr::setGoalViewRollOffset_deg);
188 fgSetArchivable("/sim/current-view/goal-roll-offset-deg");
190 fgTie("/sim/current-view/view-number", this,
191 &FGViewMgr::getView, &FGViewMgr::setView);
192 fgSetArchivable("/sim/current-view/view-number", FALSE);
194 fgTie("/sim/current-view/axes/long", this,
195 (double_getter)0, &FGViewMgr::setViewAxisLong);
196 fgSetArchivable("/sim/current-view/axes/long");
198 fgTie("/sim/current-view/axes/lat", this,
199 (double_getter)0, &FGViewMgr::setViewAxisLat);
200 fgSetArchivable("/sim/current-view/axes/lat");
202 fgTie("/sim/current-view/field-of-view", this,
203 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
204 fgSetArchivable("/sim/current-view/field-of-view");
206 fgTie("/sim/current-view/aspect-ratio-multiplier", this,
207 &FGViewMgr::getARM_deg, &FGViewMgr::setARM_deg);
208 fgSetArchivable("/sim/current-view/field-of-view");
210 fgTie("/sim/current-view/ground-level-nearplane-m", this,
211 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
212 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
220 // need to redo these bindings to the new locations (move to viewer?)
221 fgUntie("/sim/current-view/heading-offset-deg");
222 fgUntie("/sim/current-view/goal-heading-offset-deg");
223 fgUntie("/sim/current-view/pitch-offset-deg");
224 fgUntie("/sim/current-view/goal-pitch-offset-deg");
225 fgUntie("/sim/current-view/field-of-view");
226 fgUntie("/sim/current-view/aspect-ratio-multiplier");
227 fgUntie("/sim/current-view/view-number");
228 fgUntie("/sim/current-view/axes/long");
229 fgUntie("/sim/current-view/axes/lat");
230 fgUntie("/sim/current-view/ground-level-nearplane-m");
234 FGViewMgr::update (double dt)
236 FGViewer * view = get_current_view();
240 FGViewer *loop_view = (FGViewer *)get_view(current);
241 SGPropertyNode *n = config_list[current];
242 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
244 // Set up view location and orientation
246 if (!n->getBoolValue("config/from-model")) {
247 lon_deg = fgGetDouble(n->getStringValue("config/eye-lon-deg-path"));
248 lat_deg = fgGetDouble(n->getStringValue("config/eye-lat-deg-path"));
249 alt_ft = fgGetDouble(n->getStringValue("config/eye-alt-ft-path"));
250 roll_deg = fgGetDouble(n->getStringValue("config/eye-roll-deg-path"));
251 pitch_deg = fgGetDouble(n->getStringValue("config/eye-pitch-deg-path"));
252 heading_deg = fgGetDouble(n->getStringValue("config/eye-heading-deg-path"));
254 loop_view->setPosition(lon_deg, lat_deg, alt_ft);
255 loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
257 // force recalc in viewer
258 loop_view->set_dirty();
261 // if lookat (type 1) then get target data...
262 if (loop_view->getType() == FG_LOOKAT) {
263 if (!n->getBoolValue("config/from-model")) {
264 lon_deg = fgGetDouble(n->getStringValue("config/target-lon-deg-path"));
265 lat_deg = fgGetDouble(n->getStringValue("config/target-lat-deg-path"));
266 alt_ft = fgGetDouble(n->getStringValue("config/target-alt-ft-path"));
267 roll_deg = fgGetDouble(n->getStringValue("config/target-roll-deg-path"));
268 pitch_deg = fgGetDouble(n->getStringValue("config/target-pitch-deg-path"));
269 heading_deg = fgGetDouble(n->getStringValue("config/target-heading-deg-path"));
271 loop_view->setTargetPosition(lon_deg, lat_deg, alt_ft);
272 loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
274 loop_view->set_dirty();
278 setViewXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
279 setViewYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
280 setViewZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
282 setViewTargetXOffset_m(fgGetDouble("/sim/current-view/target-x-offset-m"));
283 setViewTargetYOffset_m(fgGetDouble("/sim/current-view/target-y-offset-m"));
284 setViewTargetZOffset_m(fgGetDouble("/sim/current-view/target-z-offset-m"));
286 // Update the current view
292 FGViewMgr::copyToCurrent()
294 SGPropertyNode *n = config_list[current];
296 // copy certain view config data for default values
297 fgSetDouble("/sim/current-view/config/heading-offset-deg",
298 n->getDoubleValue("config/default-heading-offset-deg"));
299 fgSetDouble("/sim/current-view/config/pitch-offset-deg",
300 n->getDoubleValue("config/pitch-offset-deg"));
301 fgSetDouble("/sim/current-view/config/roll-offset-deg",
302 n->getDoubleValue("config/roll-offset-deg"));
303 fgSetDouble("/sim/current-view/config/default-field-of-view-deg",
304 n->getDoubleValue("config/default-field-of-view-deg"));
305 fgSetBool("/sim/current-view/config/from-model",
306 n->getBoolValue("config/from-model"));
309 fgSetDouble("/sim/current-view/x-offset-m", getViewXOffset_m());
310 fgSetDouble("/sim/current-view/y-offset-m", getViewYOffset_m());
311 fgSetDouble("/sim/current-view/z-offset-m", getViewZOffset_m());
313 fgSetDouble("/sim/current-view/goal-heading-offset-deg",
314 get_current_view()->getGoalHeadingOffset_deg());
315 fgSetDouble("/sim/current-view/goal-pitch-offset-deg",
316 get_current_view()->getGoalPitchOffset_deg());
317 fgSetDouble("/sim/current-view/goal-roll-offset-deg",
318 get_current_view()->getRollOffset_deg());
319 fgSetDouble("/sim/current-view/heading-offset-deg",
320 get_current_view()->getHeadingOffset_deg());
321 fgSetDouble("/sim/current-view/pitch-offset-deg",
322 get_current_view()->getPitchOffset_deg());
323 fgSetDouble("/sim/current-view/roll-offset-deg",
324 get_current_view()->getRollOffset_deg());
325 fgSetDouble("/sim/current-view/target-x-offset-m",
326 get_current_view()->getTargetXOffset_m());
327 fgSetDouble("/sim/current-view/target-y-offset-m",
328 get_current_view()->getTargetYOffset_m());
329 fgSetDouble("/sim/current-view/target-z-offset-m",
330 get_current_view()->getTargetZOffset_m());
331 fgSetBool("/sim/current-view/internal",
332 get_current_view()->getInternal());
337 FGViewMgr::getViewHeadingOffset_deg () const
339 const FGViewer * view = get_current_view();
340 return (view == 0 ? 0 : view->getHeadingOffset_deg());
344 FGViewMgr::setViewHeadingOffset_deg (double offset)
346 FGViewer * view = get_current_view();
348 view->setGoalHeadingOffset_deg(offset);
349 view->setHeadingOffset_deg(offset);
354 FGViewMgr::getViewGoalHeadingOffset_deg () const
356 const FGViewer * view = get_current_view();
357 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
361 FGViewMgr::setViewGoalHeadingOffset_deg (double offset)
363 FGViewer * view = get_current_view();
365 view->setGoalHeadingOffset_deg(offset);
369 FGViewMgr::getViewPitchOffset_deg () const
371 const FGViewer * view = get_current_view();
372 return (view == 0 ? 0 : view->getPitchOffset_deg());
376 FGViewMgr::setViewPitchOffset_deg (double tilt)
378 FGViewer * view = get_current_view();
380 view->setGoalPitchOffset_deg(tilt);
381 view->setPitchOffset_deg(tilt);
386 FGViewMgr::getGoalViewPitchOffset_deg () const
388 const FGViewer * view = get_current_view();
389 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
393 FGViewMgr::setGoalViewPitchOffset_deg (double tilt)
395 FGViewer * view = get_current_view();
397 view->setGoalPitchOffset_deg(tilt);
401 FGViewMgr::getViewRollOffset_deg () const
403 const FGViewer * view = get_current_view();
404 return (view == 0 ? 0 : view->getRollOffset_deg());
408 FGViewMgr::setViewRollOffset_deg (double tilt)
410 FGViewer * view = get_current_view();
412 view->setGoalRollOffset_deg(tilt);
413 view->setRollOffset_deg(tilt);
418 FGViewMgr::getGoalViewRollOffset_deg () const
420 const FGViewer * view = get_current_view();
421 return (view == 0 ? 0 : view->getGoalRollOffset_deg());
425 FGViewMgr::setGoalViewRollOffset_deg (double tilt)
427 FGViewer * view = get_current_view();
429 view->setGoalRollOffset_deg(tilt);
433 FGViewMgr::getViewXOffset_m () const
435 const FGViewer * view = get_current_view();
437 return ((FGViewer *)view)->getXOffset_m();
444 FGViewMgr::setViewXOffset_m (double x)
446 FGViewer * view = get_current_view();
448 view->setXOffset_m(x);
453 FGViewMgr::getViewYOffset_m () const
455 const FGViewer * view = get_current_view();
457 return ((FGViewer *)view)->getYOffset_m();
464 FGViewMgr::setViewYOffset_m (double y)
466 FGViewer * view = get_current_view();
468 view->setYOffset_m(y);
473 FGViewMgr::getViewZOffset_m () const
475 const FGViewer * view = get_current_view();
477 return ((FGViewer *)view)->getZOffset_m();
484 FGViewMgr::setViewZOffset_m (double z)
486 FGViewer * view = get_current_view();
488 view->setZOffset_m(z);
493 FGViewMgr::getViewTargetXOffset_m () const
495 const FGViewer * view = get_current_view();
497 return ((FGViewer *)view)->getTargetXOffset_m();
504 FGViewMgr::setViewTargetXOffset_m (double x)
506 FGViewer * view = get_current_view();
508 view->setTargetXOffset_m(x);
513 FGViewMgr::getViewTargetYOffset_m () const
515 const FGViewer * view = get_current_view();
517 return ((FGViewer *)view)->getTargetYOffset_m();
524 FGViewMgr::setViewTargetYOffset_m (double y)
526 FGViewer * view = get_current_view();
528 view->setTargetYOffset_m(y);
533 FGViewMgr::getViewTargetZOffset_m () const
535 const FGViewer * view = get_current_view();
537 return ((FGViewer *)view)->getTargetZOffset_m();
544 FGViewMgr::setViewTargetZOffset_m (double z)
546 FGViewer * view = get_current_view();
548 view->setTargetZOffset_m(z);
553 FGViewMgr::getView () const
559 FGViewMgr::setView (int newview)
561 // negative numbers -> set view with node index -newview
563 for (int i = 0; i < (int)config_list.size(); i++) {
564 int index = -config_list[i]->getIndex();
565 if (index == newview)
572 // if newview number too low wrap to last view...
574 newview = (int)views.size() -1;
576 // if newview number to high wrap to zero...
577 if (newview > ((int)views.size() -1))
585 // Copy the fdm's position into the SGLocation which is shared with
587 globals->get_aircraft_model()->update(0);
594 FGViewMgr::getFOV_deg () const
596 const FGViewer * view = get_current_view();
597 return (view == 0 ? 0 : view->get_fov());
601 FGViewMgr::setFOV_deg (double fov)
603 FGViewer * view = get_current_view();
609 FGViewMgr::getARM_deg () const
611 const FGViewer * view = get_current_view();
612 return (view == 0 ? 0 : view->get_aspect_ratio_multiplier());
616 FGViewMgr::setARM_deg (double aspect_ratio_multiplier)
618 FGViewer * view = get_current_view();
620 view->set_aspect_ratio_multiplier(aspect_ratio_multiplier);
624 FGViewMgr::getNear_m () const
626 const FGViewer * view = get_current_view();
627 return (view == 0 ? 0.5f : view->getNear_m());
631 FGViewMgr::setNear_m (double near_m)
633 FGViewer * view = get_current_view();
635 view->setNear_m(near_m);
639 FGViewMgr::setViewAxisLong (double axis)
645 FGViewMgr::setViewAxisLat (double axis)
651 FGViewMgr::do_axes ()
653 // Take no action when hat is centered
654 if ( ( axis_long < 0.01 ) &&
655 ( axis_long > -0.01 ) &&
656 ( axis_lat < 0.01 ) &&
661 double viewDir = 999;
663 /* Do all the quick and easy cases */
664 if (axis_long < 0) { // Longitudinal axis forward
665 if (axis_lat == axis_long)
666 viewDir = fgGetDouble("/sim/view/config/front-left-direction-deg");
667 else if (axis_lat == - axis_long)
668 viewDir = fgGetDouble("/sim/view/config/front-right-direction-deg");
669 else if (axis_lat == 0)
670 viewDir = fgGetDouble("/sim/view/config/front-direction-deg");
671 } else if (axis_long > 0) { // Longitudinal axis backward
672 if (axis_lat == - axis_long)
673 viewDir = fgGetDouble("/sim/view/config/back-left-direction-deg");
674 else if (axis_lat == axis_long)
675 viewDir = fgGetDouble("/sim/view/config/back-right-direction-deg");
676 else if (axis_lat == 0)
677 viewDir = fgGetDouble("/sim/view/config/back-direction-deg");
678 } else if (axis_long == 0) { // Longitudinal axis neutral
680 viewDir = fgGetDouble("/sim/view/config/left-direction-deg");
681 else if (axis_lat > 0)
682 viewDir = fgGetDouble("/sim/view/config/right-direction-deg");
683 else return; /* And assertion failure maybe? */
686 // Do all the difficult cases
688 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
689 if ( viewDir < -1 ) viewDir += 360;
691 get_current_view()->setGoalHeadingOffset_deg(viewDir);