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 <GUI/sgVec3Slider.hxx> // FIXME: this should NOT be needed
30 #include "viewmgr.hxx"
31 #include "fg_props.hxx"
34 string viewpath, nodepath, strdata;
37 FGViewMgr::FGViewMgr( void ) :
46 FGViewMgr::~FGViewMgr( void ) {
53 bool from_model = false;
54 bool at_model = false;
55 int from_model_index = 0;
56 int at_model_index = 0;
57 double x_offset_m, y_offset_m, 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());
100 nodepath += "/config/x-offset-m";
101 x_offset_m = fgGetDouble(nodepath.c_str());
103 nodepath += "/config/y-offset-m";
104 y_offset_m = fgGetDouble(nodepath.c_str());
106 nodepath += "/config/z-offset-m";
107 z_offset_m = fgGetDouble(nodepath.c_str());
110 nodepath += "/config/ground-level-nearplane-m";
111 near_m = fgGetDouble(nodepath.c_str());
113 // supporting two types now "lookat" = 1 and "lookfrom" = 0
114 if ( strcmp("lookat",strdata.c_str()) == 0 )
115 add_view(new FGViewer ( FG_LOOKAT, from_model, from_model_index,
116 at_model, at_model_index, x_offset_m, y_offset_m,
117 z_offset_m, near_m ));
119 add_view(new FGViewer ( FG_LOOKFROM, from_model, from_model_index, false,
120 0, x_offset_m, y_offset_m, z_offset_m, near_m ));
128 typedef double (FGViewMgr::*double_getter)() const;
133 // these are bound to the current view properties
134 fgTie("/sim/current-view/heading-offset-deg", this,
135 &FGViewMgr::getViewOffset_deg, &FGViewMgr::setViewOffset_deg);
136 fgSetArchivable("/sim/current-view/heading-offset-deg");
137 fgTie("/sim/current-view/goal-heading-offset-deg", this,
138 &FGViewMgr::getGoalViewOffset_deg, &FGViewMgr::setGoalViewOffset_deg);
139 fgSetArchivable("/sim/current-view/goal-heading-offset-deg");
140 fgTie("/sim/current-view/pitch-offset-deg", this,
141 &FGViewMgr::getViewTilt_deg, &FGViewMgr::setViewTilt_deg);
142 fgSetArchivable("/sim/current-view/pitch-offset-deg");
143 fgTie("/sim/current-view/goal-pitch-offset-deg", this,
144 &FGViewMgr::getGoalViewTilt_deg, &FGViewMgr::setGoalViewTilt_deg);
145 fgSetArchivable("/sim/current-view/goal-pitch-offset-deg");
147 fgTie("/sim/current-view/axes/long", this,
148 (double_getter)0, &FGViewMgr::setViewAxisLong);
149 fgTie("/sim/current-view/axes/lat", this,
150 (double_getter)0, &FGViewMgr::setViewAxisLat);
152 fgTie("/sim/current-view/field-of-view", this,
153 &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
154 fgSetArchivable("/sim/current-view/field-of-view");
156 fgTie("/sim/current-view/ground-level-nearplane-m", this,
157 &FGViewMgr::getNear_m, &FGViewMgr::setNear_m);
158 fgSetArchivable("/sim/current-view/ground-level-nearplane-m");
166 // need to redo these bindings to the new locations (move to viewer?)
167 fgUntie("/sim/current-view/heading-offset-deg");
168 fgUntie("/sim/current-view/goal-heading-offset-deg");
169 fgUntie("/sim/current-view/pitch-offset-deg");
170 fgUntie("/sim/current-view/goal-pitch-offset-deg");
171 fgUntie("/sim/field-of-view");
172 fgUntie("/sim/current-view/axes/long");
173 fgUntie("/sim/current-view/axes/lat");
177 FGViewMgr::update (int dt)
180 double lon_deg, lat_deg, alt_ft, roll_deg, pitch_deg, heading_deg;
182 FGViewer * view = get_current_view();
188 viewpath = "/sim/view";
189 sprintf(stridx, "[%d]", i);
193 FGViewer *loop_view = (FGViewer *)get_view( i );
195 // Set up view location and orientation
198 nodepath += "/config/from-model";
199 if (!fgGetBool(nodepath.c_str())) {
201 nodepath += "/config/eye-lon-deg-path";
202 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
204 nodepath += "/config/eye-lat-deg-path";
205 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
207 nodepath += "/config/eye-alt-ft-path";
208 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
210 nodepath += "/config/eye-roll-deg-path";
211 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
213 nodepath += "/config/eye-pitch-deg-path";
214 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
216 nodepath += "/config/eye-heading-deg-path";
217 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
218 loop_view->setPosition(lon_deg, lat_deg, alt_ft);
219 loop_view->setOrientation(roll_deg, pitch_deg, heading_deg);
221 // force recalc in viewer
222 loop_view->set_dirty();
225 // if lookat (type 1) then get target data...
226 if (loop_view->getType() == 1) {
228 nodepath += "/config/from-model";
229 if (!fgGetBool(nodepath.c_str())) {
231 nodepath += "/config/target-lon-deg-path";
232 lon_deg = fgGetDouble(fgGetString(nodepath.c_str()));
234 nodepath += "/config/target-lat-deg-path";
235 lat_deg = fgGetDouble(fgGetString(nodepath.c_str()));
237 nodepath += "/config/target-alt-ft-path";
238 alt_ft = fgGetDouble(fgGetString(nodepath.c_str()));
240 nodepath += "/config/target-roll-deg-path";
241 roll_deg = fgGetDouble(fgGetString(nodepath.c_str()));
243 nodepath += "/config/target-pitch-deg-path";
244 pitch_deg = fgGetDouble(fgGetString(nodepath.c_str()));
246 nodepath += "/config/target-heading-deg-path";
247 heading_deg = fgGetDouble(fgGetString(nodepath.c_str()));
249 loop_view ->setTargetPosition(lon_deg, lat_deg, alt_ft);
250 loop_view->setTargetOrientation(roll_deg, pitch_deg, heading_deg);
252 loop_view->set_dirty();
255 setPilotXOffset_m(fgGetDouble("/sim/current-view/x-offset-m"));
256 setPilotYOffset_m(fgGetDouble("/sim/current-view/y-offset-m"));
257 setPilotZOffset_m(fgGetDouble("/sim/current-view/z-offset-m"));
259 // Update the current view
266 FGViewMgr::copyToCurrent()
268 fgSetDouble("/sim/current-view/x-offset-m", getPilotXOffset_m());
269 fgSetDouble("/sim/current-view/y-offset-m", getPilotYOffset_m());
270 fgSetDouble("/sim/current-view/z-offset-m", getPilotZOffset_m());
275 FGViewMgr::getViewOffset_deg () const
277 const FGViewer * view = get_current_view();
278 return (view == 0 ? 0 : view->getHeadingOffset_deg());
282 FGViewMgr::setViewOffset_deg (double offset)
284 FGViewer * view = get_current_view();
286 view->setGoalHeadingOffset_deg(offset);
287 view->setHeadingOffset_deg(offset);
292 FGViewMgr::getGoalViewOffset_deg () const
294 const FGViewer * view = get_current_view();
295 return (view == 0 ? 0 : view->getGoalHeadingOffset_deg());
299 FGViewMgr::setGoalViewOffset_deg (double offset)
301 FGViewer * view = get_current_view();
303 view->setGoalHeadingOffset_deg(offset);
307 FGViewMgr::getViewTilt_deg () const
309 const FGViewer * view = get_current_view();
310 return (view == 0 ? 0 : view->getPitchOffset_deg());
314 FGViewMgr::setViewTilt_deg (double tilt)
316 FGViewer * view = get_current_view();
318 view->setGoalPitchOffset_deg(tilt);
319 view->setPitchOffset_deg(tilt);
324 FGViewMgr::getGoalViewTilt_deg () const
326 const FGViewer * view = get_current_view();
327 return (view == 0 ? 0 : view->getGoalPitchOffset_deg());
331 FGViewMgr::setGoalViewTilt_deg (double tilt)
333 FGViewer * view = get_current_view();
335 view->setGoalPitchOffset_deg(tilt);
339 FGViewMgr::getPilotXOffset_m () const
341 const FGViewer * view = get_current_view();
343 return ((FGViewer *)view)->getXOffset_m();
350 FGViewMgr::setPilotXOffset_m (double x)
352 FGViewer * view = get_current_view();
354 view->setXOffset_m(x);
359 FGViewMgr::getPilotYOffset_m () const
361 const FGViewer * view = get_current_view();
363 return ((FGViewer *)view)->getYOffset_m();
370 FGViewMgr::setPilotYOffset_m (double y)
372 FGViewer * view = get_current_view();
374 view->setYOffset_m(y);
379 FGViewMgr::getPilotZOffset_m () const
381 const FGViewer * view = get_current_view();
383 return ((FGViewer *)view)->getZOffset_m();
390 FGViewMgr::setPilotZOffset_m (double z)
392 FGViewer * view = get_current_view();
394 view->setZOffset_m(z);
399 FGViewMgr::getFOV_deg () const
401 const FGViewer * view = get_current_view();
402 return (view == 0 ? 0 : view->get_fov());
406 FGViewMgr::setFOV_deg (double fov)
408 FGViewer * view = get_current_view();
414 FGViewMgr::getNear_m () const
416 const FGViewer * view = get_current_view();
417 return (view == 0 ? 0.5f : view->getNear_m());
421 FGViewMgr::setNear_m (double near_m)
423 FGViewer * view = get_current_view();
425 view->setNear_m(near_m);
429 FGViewMgr::setViewAxisLong (double axis)
435 FGViewMgr::setViewAxisLat (double axis)
441 FGViewMgr::do_axes ()
443 // Take no action when hat is centered
444 if ( ( axis_long < 0.01 ) &&
445 ( axis_long > -0.01 ) &&
446 ( axis_lat < 0.01 ) &&
451 double viewDir = 999;
453 /* Do all the quick and easy cases */
454 if (axis_long < 0) { // Longitudinal axis forward
455 if (axis_lat == axis_long)
457 else if (axis_lat == - axis_long)
459 else if (axis_lat == 0)
461 } else if (axis_long > 0) { // Longitudinal axis backward
462 if (axis_lat == - axis_long)
464 else if (axis_lat == axis_long)
466 else if (axis_lat == 0)
468 } else if (axis_long == 0) { // Longitudinal axis neutral
471 else if (axis_lat > 0)
473 else return; /* And assertion failure maybe? */
476 // Do all the difficult cases
478 viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
479 if ( viewDir < -1 ) viewDir += 360;
481 get_current_view()->setGoalHeadingOffset_deg(viewDir);