]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewmgr.cxx
Somewhere along the line in the recent changes some std::cout were
[flightgear.git] / src / Main / viewmgr.cxx
1 // viewmgr.cxx -- class for managing all the views in the flightgear world.
2 //
3 // Written by Curtis Olson, started October 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 #include <plib/sg.h>
24
25 #include <GUI/sgVec3Slider.hxx> // FIXME: this should NOT be needed
26
27 #include "viewmgr.hxx"
28 #include "fg_props.hxx"
29
30
31 // Constructor
32 FGViewMgr::FGViewMgr( void ) :
33   axis_long(0),
34   axis_lat(0),
35   current(0)
36 {
37 }
38
39
40 // Destructor
41 FGViewMgr::~FGViewMgr( void ) {
42 }
43
44 void
45 FGViewMgr::init ()
46 {
47   add_view(new FGViewerRPH);
48   add_view(new FGViewerLookAt);
49 }
50
51 typedef double (FGViewMgr::*double_getter)() const;
52
53 void
54 FGViewMgr::bind ()
55 {
56   fgTie("/sim/view/offset-deg", this,
57         &FGViewMgr::getViewOffset_deg, &FGViewMgr::setViewOffset_deg);
58   fgSetArchivable("/sim/view/offset-deg");
59   fgTie("/sim/view/goal-offset-deg", this,
60         &FGViewMgr::getGoalViewOffset_deg, &FGViewMgr::setGoalViewOffset_deg);
61   fgSetArchivable("/sim/view/goal-offset-deg");
62   fgTie("/sim/view/tilt-deg", this,
63         &FGViewMgr::getViewTilt_deg, &FGViewMgr::setViewTilt_deg);
64   fgSetArchivable("/sim/view/tilt-deg");
65   fgTie("/sim/view/goal-tilt-deg", this,
66         &FGViewMgr::getGoalViewTilt_deg, &FGViewMgr::setGoalViewTilt_deg);
67   fgSetArchivable("/sim/view/goal-tilt-deg");
68   fgTie("/sim/view/pilot/x-offset-m", this,
69         &FGViewMgr::getPilotXOffset_m, &FGViewMgr::setPilotXOffset_m);
70   fgSetArchivable("/sim/view/pilot/x-offset-m");
71   fgTie("/sim/view/pilot/y-offset-m", this,
72         &FGViewMgr::getPilotYOffset_m, &FGViewMgr::setPilotYOffset_m);
73   fgSetArchivable("/sim/view/pilot/y-offset-m");
74   fgTie("/sim/view/pilot/z-offset-m", this,
75         &FGViewMgr::getPilotZOffset_m, &FGViewMgr::setPilotZOffset_m);
76   fgSetArchivable("/sim/view/pilot/z-offset-m");
77   fgTie("/sim/field-of-view", this,
78         &FGViewMgr::getFOV_deg, &FGViewMgr::setFOV_deg);
79   fgSetArchivable("/sim/field-of-view");
80   fgTie("/sim/view/axes/long", this,
81         (double_getter)0, &FGViewMgr::setViewAxisLong);
82   fgTie("/sim/view/axes/lat", this,
83         (double_getter)0, &FGViewMgr::setViewAxisLat);
84 }
85
86 void
87 FGViewMgr::unbind ()
88 {
89   fgUntie("/sim/view/offset-deg");
90   fgUntie("/sim/view/goal-offset-deg");
91   fgUntie("/sim/view/tilt-deg");
92   fgUntie("/sim/view/goal-tilt-deg");
93   fgUntie("/sim/view/pilot/x-offset-m");
94   fgUntie("/sim/view/pilot/y-offset-m");
95   fgUntie("/sim/view/pilot/z-offset-m");
96   fgUntie("/sim/field-of-view");
97   fgUntie("/sim/view/axes/long");
98   fgUntie("/sim/view/axes/lat");
99 }
100
101 void
102 FGViewMgr::update (int dt)
103 {
104   FGViewer * view = get_current_view();
105   if (view == 0)
106     return;
107
108                                 // Grab some values we'll need.
109   double lon_rad = fgGetDouble("/position/longitude-deg")
110     * SGD_DEGREES_TO_RADIANS;
111   double lat_rad = fgGetDouble("/position/latitude-deg")
112     * SGD_DEGREES_TO_RADIANS;
113   double alt_m = fgGetDouble("/position/altitude-ft")
114     * SG_FEET_TO_METER;
115   double roll_rad = fgGetDouble("/orientation/roll-deg")
116     * SGD_DEGREES_TO_RADIANS;
117   double pitch_rad = fgGetDouble("/orientation/pitch-deg")
118     * SGD_DEGREES_TO_RADIANS;
119   double heading_rad = fgGetDouble("/orientation/heading-deg")
120     * SGD_DEGREES_TO_RADIANS;
121
122                                 // Set up the pilot view
123   FGViewerRPH *pilot_view = (FGViewerRPH *)get_view( 0 );
124   pilot_view ->set_geod_view_pos(lon_rad, lat_rad, alt_m);
125   pilot_view->set_rph(roll_rad, pitch_rad, heading_rad);
126   if (fgGetString("/sim/flight-model") == "ada") {
127     //+ve x is aft, +ve z is up (see viewer.hxx)
128     pilot_view->set_pilot_offset( -5.0, 0.0, 1.0 ); 
129   }
130
131                                 // Set up the chase view
132
133                                 // FIXME: the matrix math belongs in
134                                 // the viewer, not here.
135   FGViewerLookAt *chase_view = (FGViewerLookAt *)get_view( 1 );
136
137   sgVec3 po;            // chase view pilot_offset
138   sgVec3 wup;           // chase view world up
139   sgSetVec3( po, 0.0, 0.0, 100.0 );
140   sgCopyVec3( wup, pilot_view->get_world_up() );
141   sgMat4 CXFM;          // chase view + pilot offset xform
142   sgMakeRotMat4( CXFM,
143                  chase_view->get_view_offset() * SGD_RADIANS_TO_DEGREES -
144                  heading_rad * SGD_RADIANS_TO_DEGREES,
145                  wup );
146   sgVec3 npo;           // new pilot offset after rotation
147   sgVec3 *pPO = PilotOffsetGet();
148   sgXformVec3( po, *pPO, pilot_view->get_UP() );
149   sgXformVec3( npo, po, CXFM );
150
151   chase_view->set_geod_view_pos(lon_rad, lat_rad, alt_m);
152   chase_view->set_pilot_offset( npo[0], npo[1], npo[2] );
153   chase_view->set_view_forward( pilot_view->get_view_pos() ); 
154   chase_view->set_view_up( wup );
155
156                                 // Update the current view
157   do_axes();
158   view->update(dt);
159 }
160
161 double
162 FGViewMgr::getViewOffset_deg () const
163 {
164   const FGViewer * view = get_current_view();
165   return (view == 0 ? 0 : view->get_view_offset() * SGD_RADIANS_TO_DEGREES);
166 }
167
168 void
169 FGViewMgr::setViewOffset_deg (double offset)
170 {
171   FGViewer * view = get_current_view();
172   if (view != 0)
173     view->set_view_offset(offset * SGD_DEGREES_TO_RADIANS);
174 }
175
176 double
177 FGViewMgr::getGoalViewOffset_deg () const
178 {
179   const FGViewer * view = get_current_view();
180   return (view == 0 ? 0 : view->get_goal_view_offset() * SGD_RADIANS_TO_DEGREES);
181 }
182
183 void
184 FGViewMgr::setGoalViewOffset_deg (double offset)
185 {
186   FGViewer * view = get_current_view();
187   if (view != 0)
188     view->set_goal_view_offset(offset * SGD_DEGREES_TO_RADIANS);
189 }
190
191 double
192 FGViewMgr::getViewTilt_deg () const
193 {
194   const FGViewer * view = get_current_view();
195   return (view == 0 ? 0 : view->get_view_tilt() * SGD_RADIANS_TO_DEGREES);
196 }
197
198 void
199 FGViewMgr::setViewTilt_deg (double tilt)
200 {
201   FGViewer * view = get_current_view();
202   if (view != 0)
203     view->set_view_tilt(tilt * SGD_DEGREES_TO_RADIANS);
204 }
205
206 double
207 FGViewMgr::getGoalViewTilt_deg () const
208 {
209   const FGViewer * view = get_current_view();
210   return (view == 0 ? 0 : view->get_goal_view_tilt() * SGD_RADIANS_TO_DEGREES);
211 }
212
213 void
214 FGViewMgr::setGoalViewTilt_deg (double tilt)
215 {
216   FGViewer * view = get_current_view();
217   if (view != 0)
218     view->set_goal_view_tilt(tilt * SGD_DEGREES_TO_RADIANS);
219 }
220
221 double
222 FGViewMgr::getPilotXOffset_m () const
223 {
224                                 // FIXME: hard-coded pilot view position
225   const FGViewer * pilot_view = get_view(0);
226   if (pilot_view != 0) {
227     float * offset = ((FGViewer *)pilot_view)->get_pilot_offset();
228     return offset[0];
229   } else {
230     return 0;
231   }
232 }
233
234 void
235 FGViewMgr::setPilotXOffset_m (double x)
236 {
237                                 // FIXME: hard-coded pilot view position
238   FGViewer * pilot_view = get_view(0);
239   if (pilot_view != 0) {
240     float * offset = pilot_view->get_pilot_offset();
241     pilot_view->set_pilot_offset(x, offset[1], offset[2]);
242   }
243 }
244
245 double
246 FGViewMgr::getPilotYOffset_m () const
247 {
248                                 // FIXME: hard-coded pilot view position
249   const FGViewer * pilot_view = get_view(0);
250   if (pilot_view != 0) {
251     float * offset = ((FGViewer *)pilot_view)->get_pilot_offset();
252     return offset[1];
253   } else {
254     return 0;
255   }
256 }
257
258 void
259 FGViewMgr::setPilotYOffset_m (double y)
260 {
261                                 // FIXME: hard-coded pilot view position
262   FGViewer * pilot_view = get_view(0);
263   if (pilot_view != 0) {
264     float * offset = pilot_view->get_pilot_offset();
265     pilot_view->set_pilot_offset(offset[0], y, offset[2]);
266   }
267 }
268
269 double
270 FGViewMgr::getPilotZOffset_m () const
271 {
272                                 // FIXME: hard-coded pilot view position
273   const FGViewer * pilot_view = get_view(0);
274   if (pilot_view != 0) {
275     float * offset = ((FGViewer *)pilot_view)->get_pilot_offset();
276     return offset[2];
277   } else {
278     return 0;
279   }
280 }
281
282 void
283 FGViewMgr::setPilotZOffset_m (double z)
284 {
285                                 // FIXME: hard-coded pilot view position
286   FGViewer * pilot_view = get_view(0);
287   if (pilot_view != 0) {
288     float * offset = pilot_view->get_pilot_offset();
289     pilot_view->set_pilot_offset(offset[0], offset[1], z);
290   }
291 }
292
293 double
294 FGViewMgr::getFOV_deg () const
295 {
296   const FGViewer * view = get_current_view();
297   return (view == 0 ? 0 : view->get_fov());
298 }
299
300 void
301 FGViewMgr::setFOV_deg (double fov)
302 {
303   FGViewer * view = get_current_view();
304   if (view != 0)
305     view->set_fov(fov);
306 }
307
308 void
309 FGViewMgr::setViewAxisLong (double axis)
310 {
311   axis_long = axis;
312 }
313
314 void
315 FGViewMgr::setViewAxisLat (double axis)
316 {
317   axis_lat = axis;
318 }
319
320 void
321 FGViewMgr::do_axes ()
322 {
323                                 // Take no action when hat is centered
324   if ( ( axis_long <  0.01 ) &&
325        ( axis_long > -0.01 ) &&
326        ( axis_lat  <  0.01 ) &&
327        ( axis_lat  > -0.01 )
328      )
329     return;
330
331   double viewDir = 999;
332
333   /* Do all the quick and easy cases */
334   if (axis_long < 0) {          // Longitudinal axis forward
335     if (axis_lat == axis_long)
336       viewDir = 45;
337     else if (axis_lat == - axis_long)
338       viewDir = 315;
339     else if (axis_lat == 0)
340       viewDir = 0;
341   } else if (axis_long > 0) {   // Longitudinal axis backward
342     if (axis_lat == - axis_long)
343       viewDir = 135;
344     else if (axis_lat == axis_long)
345       viewDir = 225;
346     else if (axis_lat == 0)
347       viewDir = 180;
348   } else if (axis_long == 0) {  // Longitudinal axis neutral
349     if (axis_lat < 0)
350       viewDir = 90;
351     else if (axis_lat > 0)
352       viewDir = 270;
353     else return; /* And assertion failure maybe? */
354   }
355
356                                 // Do all the difficult cases
357   if ( viewDir > 900 )
358     viewDir = SGD_RADIANS_TO_DEGREES * atan2 ( -axis_lat, -axis_long );
359   if ( viewDir < -1 ) viewDir += 360;
360
361   get_current_view()->set_goal_view_offset(viewDir*SGD_DEGREES_TO_RADIANS);
362 }