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"
32 string viewpath, nodepath, strdata;
35 FGViewMgr::FGViewMgr( void ) :
44 FGViewMgr::~FGViewMgr( void ) {
51 bool from_model = false;
52 bool at_model = false;
53 int from_model_index = 0;
54 int at_model_index = 0;
55 double x_offset_m, y_offset_m, z_offset_m;
58 for (int i = 0; i < fgGetInt("/sim/number-views"); i++) {
59 viewpath = "/sim/view";
60 sprintf(stridx, "[%d]", i);
63 // find out what type of view this is...
66 strdata = fgGetString(nodepath.c_str());
69 // this is assumed to be an aircraft model...we will need to read
70 // model-from-type as well.
71 // find out if this is a model we are looking from...
73 nodepath += "/config/from-model";
74 from_model = fgGetBool(nodepath.c_str());
76 // get model index (which model)
79 nodepath += "/config/from-model-idx";
80 from_model_index = fgGetInt(nodepath.c_str());
83 if ( strcmp("lookat",strdata.c_str()) == 0 ) {
84 // find out if this is a model we are looking at...
86 nodepath += "/config/at-model";
87 at_model = fgGetBool(nodepath.c_str());
89 // get model index (which model)
92 nodepath += "/config/at-model-idx";
93 at_model_index = fgGetInt(nodepath.c_str());
98 nodepath += "/config/x-offset-m";
99 x_offset_m = fgGetDouble(nodepath.c_str());
101 nodepath += "/config/y-offset-m";
102 y_offset_m = fgGetDouble(nodepath.c_str());
104 nodepath += "/config/z-offset-m";
105 z_offset_m = fgGetDouble(nodepath.c_str());
108 nodepath += "/config/ground-level-nearplane-m";
109 near_m = fgGetDouble(nodepath.c_str());
111 // supporting two types now "lookat" = 1 and "lookfrom" = 0
112 if ( strcmp("lookat",strdata.c_str()) == 0 )
113 add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
114 at_model, at_model_index, x_offset_m, y_offset_m,
115 z_offset_m, near_m ));
117 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index, false,
118 0, x_offset_m, y_offset_m, z_offset_m, near_m ));
126 typedef double (FGViewMgr::*double_getter)() const;
131 // these are bound to the current view properties
132 fgTie("/sim/current-view/heading-offset-deg", this,
133 &FGViewMgr::getViewOffset_deg, &FGViewMgr::setViewOffset_deg);
134 fgSetArchivable("/sim/current-view/heading-offset-deg");
135 fgTie("/sim/current-view/goal-heading-offset-deg", this,
136 &FGViewMgr::getGoalViewOffset_deg, &FGViewMgr::setGoalViewOffset_deg);
137 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
138 fgTie("/sim/current-view/pitch-offset-deg", this,
139 &FGViewMgr::getViewTilt_deg, &FGViewMgr::setViewTilt_deg);
140 fgSetArchivable("/sim/current-view/pitch-offset-deg");
141 fgTie("/sim/current-view/goal-pitch-offset-deg", this,
142 &FGViewMgr::getGoalViewTilt_deg, &FGViewMgr::setGoalViewTilt_deg);
143 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
145 fgTie("/sim/current-view/axes/long", this,
146 (double_getter)0, &FGViewMgr::setViewAxisLong);
147 fgTie("/sim/current-view/axes/lat", this,
148 (double_getter)0, &FGViewMgr::setViewAxisLat);
150 fgTie("/sim/current-view/field-of-view", this,
151 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
152 fgSetArchivable("/sim/current-view/field-of-view");
154 fgTie("/sim/current-view/ground-level-nearplane-m", this,
155 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
156 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
164 // need to redo these bindings to the new locations (move to viewer?)
165 fgUntie("/sim/current-view/heading-offset-deg");
166 fgUntie("/sim/current-view/goal-heading-offset-deg");
167 fgUntie("/sim/current-view/pitch-offset-deg");
168 fgUntie("/sim/current-view/goal-pitch-offset-deg");
169 fgUntie("/sim/field-of-view");
170 fgUntie("/sim/current-view/axes/long");
171 fgUntie("/sim/current-view/axes/lat");
175 FGViewMgr::update (int dt)
178 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
180 FGViewer * view = get_current_view();
186 viewpath = "/sim/view";
187 sprintf(stridx, "[%d]", i);
191 FGViewer *loop_view = (FGViewer *)get_view( i );
193 // Set up view location and orientation
196 nodepath += "/config/from-model";
197 if (!fgGetBool(nodepath.c_str())) {
199 nodepath += "/config/eye-lon-deg-path";
200 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
202 nodepath += "/config/eye-lat-deg-path";
203 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
205 nodepath += "/config/eye-alt-ft-path";
206 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
208 nodepath += "/config/eye-roll-deg-path";
209 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
211 nodepath += "/config/eye-pitch-deg-path";
212 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
214 nodepath += "/config/eye-heading-deg-path";
215 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
216 loop_view->setPosition(lon_deg, lat_deg, alt_ft);
217 loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
219 // force recalc in viewer
220 loop_view->set_dirty();
223 // if lookat (type 1) then get target data...
224 if (loop_view->getType() == 1) {
226 nodepath += "/config/from-model";
227 if (!fgGetBool(nodepath.c_str())) {
229 nodepath += "/config/target-lon-deg-path";
230 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
232 nodepath += "/config/target-lat-deg-path";
233 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
235 nodepath += "/config/target-alt-ft-path";
236 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
238 nodepath += "/config/target-roll-deg-path";
239 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
241 nodepath += "/config/target-pitch-deg-path";
242 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
244 nodepath += "/config/target-heading-deg-path";
245 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
247 loop_view ->setTargetPosition(lon_deg, lat_deg, alt_ft);
248 loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
250 loop_view->set_dirty();
253 setPilotXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
254 setPilotYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
255 setPilotZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
257 // Update the current view
264 FGViewMgr::copyToCurrent()
266 fgSetDouble("/sim/current-view/x-offset-m", getPilotXOffset_m());
267 fgSetDouble("/sim/current-view/y-offset-m", getPilotYOffset_m());
268 fgSetDouble("/sim/current-view/z-offset-m", getPilotZOffset_m());
273 FGViewMgr::getViewOffset_deg () const
275 const FGViewer * view = get_current_view();
276 return (view == 0 ? 0 : view->getHeadingOffset_deg());
280 FGViewMgr::setViewOffset_deg (double offset)
282 FGViewer * view = get_current_view();
284 view->setGoalHeadingOffset_deg(offset);
285 view->setHeadingOffset_deg(offset);
290 FGViewMgr::getGoalViewOffset_deg () const
292 const FGViewer * view = get_current_view();
293 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
297 FGViewMgr::setGoalViewOffset_deg (double offset)
299 FGViewer * view = get_current_view();
301 view->setGoalHeadingOffset_deg(offset);
305 FGViewMgr::getViewTilt_deg () const
307 const FGViewer * view = get_current_view();
308 return (view == 0 ? 0 : view->getPitchOffset_deg());
312 FGViewMgr::setViewTilt_deg (double tilt)
314 FGViewer * view = get_current_view();
316 view->setGoalPitchOffset_deg(tilt);
317 view->setPitchOffset_deg(tilt);
322 FGViewMgr::getGoalViewTilt_deg () const
324 const FGViewer * view = get_current_view();
325 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
329 FGViewMgr::setGoalViewTilt_deg (double tilt)
331 FGViewer * view = get_current_view();
333 view->setGoalPitchOffset_deg(tilt);
337 FGViewMgr::getPilotXOffset_m () const
339 const FGViewer * view = get_current_view();
341 return ((FGViewer *)view)->getXOffset_m();
348 FGViewMgr::setPilotXOffset_m (double x)
350 FGViewer * view = get_current_view();
352 view->setXOffset_m(x);
357 FGViewMgr::getPilotYOffset_m () const
359 const FGViewer * view = get_current_view();
361 return ((FGViewer *)view)->getYOffset_m();
368 FGViewMgr::setPilotYOffset_m (double y)
370 FGViewer * view = get_current_view();
372 view->setYOffset_m(y);
377 FGViewMgr::getPilotZOffset_m () const
379 const FGViewer * view = get_current_view();
381 return ((FGViewer *)view)->getZOffset_m();
388 FGViewMgr::setPilotZOffset_m (double z)
390 FGViewer * view = get_current_view();
392 view->setZOffset_m(z);
397 FGViewMgr::getFOV_deg () const
399 const FGViewer * view = get_current_view();
400 return (view == 0 ? 0 : view->get_fov());
404 FGViewMgr::setFOV_deg (double fov)
406 FGViewer * view = get_current_view();
412 FGViewMgr::getNear_m () const
414 const FGViewer * view = get_current_view();
415 return (view == 0 ? 0.5f : view->getNear_m());
419 FGViewMgr::setNear_m (double near_m)
421 FGViewer * view = get_current_view();
423 view->setNear_m(near_m);
427 FGViewMgr::setViewAxisLong (double axis)
433 FGViewMgr::setViewAxisLat (double axis)
439 FGViewMgr::do_axes ()
441 // Take no action when hat is centered
442 if ( ( axis_long < 0.01 ) &&
443 ( axis_long > -0.01 ) &&
444 ( axis_lat < 0.01 ) &&
449 double viewDir = 999;
451 /* Do all the quick and easy cases */
452 if (axis_long < 0) { // Longitudinal axis forward
453 if (axis_lat == axis_long)
455 else if (axis_lat == - axis_long)
457 else if (axis_lat == 0)
459 } else if (axis_long > 0) { // Longitudinal axis backward
460 if (axis_lat == - axis_long)
462 else if (axis_lat == axis_long)
464 else if (axis_lat == 0)
466 } else if (axis_long == 0) { // Longitudinal axis neutral
469 else if (axis_lat > 0)
471 else return; /* And assertion failure maybe? */
474 // Do all the difficult cases
476 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
477 if ( viewDir < -1 ) viewDir += 360;
479 get_current_view()->setGoalHeadingOffset_deg(viewDir);