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 (double 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
263 FGViewMgr::copyToCurrent()
265 fgSetDouble("/sim/current-view/x-offset-m", getPilotXOffset_m());
266 fgSetDouble("/sim/current-view/y-offset-m", getPilotYOffset_m());
267 fgSetDouble("/sim/current-view/z-offset-m", getPilotZOffset_m());
272 FGViewMgr::getViewOffset_deg () const
274 const FGViewer * view = get_current_view();
275 return (view == 0 ? 0 : view->getHeadingOffset_deg());
279 FGViewMgr::setViewOffset_deg (double offset)
281 FGViewer * view = get_current_view();
283 view->setGoalHeadingOffset_deg(offset);
284 view->setHeadingOffset_deg(offset);
289 FGViewMgr::getGoalViewOffset_deg () const
291 const FGViewer * view = get_current_view();
292 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
296 FGViewMgr::setGoalViewOffset_deg (double offset)
298 FGViewer * view = get_current_view();
300 view->setGoalHeadingOffset_deg(offset);
304 FGViewMgr::getViewTilt_deg () const
306 const FGViewer * view = get_current_view();
307 return (view == 0 ? 0 : view->getPitchOffset_deg());
311 FGViewMgr::setViewTilt_deg (double tilt)
313 FGViewer * view = get_current_view();
315 view->setGoalPitchOffset_deg(tilt);
316 view->setPitchOffset_deg(tilt);
321 FGViewMgr::getGoalViewTilt_deg () const
323 const FGViewer * view = get_current_view();
324 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
328 FGViewMgr::setGoalViewTilt_deg (double tilt)
330 FGViewer * view = get_current_view();
332 view->setGoalPitchOffset_deg(tilt);
336 FGViewMgr::getPilotXOffset_m () const
338 const FGViewer * view = get_current_view();
340 return ((FGViewer *)view)->getXOffset_m();
347 FGViewMgr::setPilotXOffset_m (double x)
349 FGViewer * view = get_current_view();
351 view->setXOffset_m(x);
356 FGViewMgr::getPilotYOffset_m () const
358 const FGViewer * view = get_current_view();
360 return ((FGViewer *)view)->getYOffset_m();
367 FGViewMgr::setPilotYOffset_m (double y)
369 FGViewer * view = get_current_view();
371 view->setYOffset_m(y);
376 FGViewMgr::getPilotZOffset_m () const
378 const FGViewer * view = get_current_view();
380 return ((FGViewer *)view)->getZOffset_m();
387 FGViewMgr::setPilotZOffset_m (double z)
389 FGViewer * view = get_current_view();
391 view->setZOffset_m(z);
396 FGViewMgr::getFOV_deg () const
398 const FGViewer * view = get_current_view();
399 return (view == 0 ? 0 : view->get_fov());
403 FGViewMgr::setFOV_deg (double fov)
405 FGViewer * view = get_current_view();
411 FGViewMgr::getNear_m () const
413 const FGViewer * view = get_current_view();
414 return (view == 0 ? 0.5f : view->getNear_m());
418 FGViewMgr::setNear_m (double near_m)
420 FGViewer * view = get_current_view();
422 view->setNear_m(near_m);
426 FGViewMgr::setViewAxisLong (double axis)
432 FGViewMgr::setViewAxisLat (double axis)
438 FGViewMgr::do_axes ()
440 // Take no action when hat is centered
441 if ( ( axis_long < 0.01 ) &&
442 ( axis_long > -0.01 ) &&
443 ( axis_lat < 0.01 ) &&
448 double viewDir = 999;
450 /* Do all the quick and easy cases */
451 if (axis_long < 0) { // Longitudinal axis forward
452 if (axis_lat == axis_long)
454 else if (axis_lat == - axis_long)
456 else if (axis_lat == 0)
458 } else if (axis_long > 0) { // Longitudinal axis backward
459 if (axis_lat == - axis_long)
461 else if (axis_lat == axis_long)
463 else if (axis_lat == 0)
465 } else if (axis_long == 0) { // Longitudinal axis neutral
468 else if (axis_lat > 0)
470 else return; /* And assertion failure maybe? */
473 // Do all the difficult cases
475 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
476 if ( viewDir < -1 ) viewDir += 360;
478 get_current_view()->setGoalHeadingOffset_deg(viewDir);