]> git.mxchange.org Git - flightgear.git/blob - src/Main/viewer.cxx
Fixed a bug with view-offset specification from the command line. There
[flightgear.git] / src / Main / viewer.cxx
1 // viewer.cxx -- class for managing a viewer in the flightgear world.
2 //
3 // Written by Curtis Olson, started August 1997.
4 //                          overhaul started October 2000.
5 //
6 // Copyright (C) 1997 - 2000  Curtis L. Olson  - curt@flightgear.org
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23
24
25 #include <simgear/compiler.h>
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/constants.h>
33
34 #include "viewer.hxx"
35
36
37 // Constructor
38 FGViewer::FGViewer( void ):
39     scalingType(FG_SCALING_MAX),
40     fov(55.0),
41     view_offset(0.0),
42     goal_view_offset(0.0),
43     view_tilt(0.0),
44     goal_view_tilt(0.0)
45 {
46     sgSetVec3( pilot_offset, 0.0, 0.0, 0.0 );
47     sgdZeroVec3(geod_view_pos);
48     sgdZeroVec3(abs_view_pos);
49     sea_level_radius = SG_EQUATORIAL_RADIUS_M; 
50     //a reasonable guess for init, so that the math doesn't blow up
51 }
52
53
54 // Destructor
55 FGViewer::~FGViewer( void ) {
56 }
57
58 void
59 FGViewer::init ()
60 {
61 }
62
63 void
64 FGViewer::bind ()
65 {
66 }
67
68 void
69 FGViewer::unbind ()
70 {
71 }
72
73 double
74 FGViewer::get_h_fov()
75 {
76     switch (scalingType) {
77     case FG_SCALING_WIDTH:  // h_fov == fov
78         return fov;
79     case FG_SCALING_MAX:
80         if (aspect_ratio < 1.0) {
81             // h_fov == fov
82             return fov;
83         } else {
84             // v_fov == fov
85             return atan(tan(fov/2 * SG_DEGREES_TO_RADIANS) / aspect_ratio) *
86                 SG_RADIANS_TO_DEGREES * 2;
87         }
88     default:
89         assert(false);
90     }
91 }
92
93 double
94 FGViewer::get_v_fov()
95 {
96     switch (scalingType) {
97     case FG_SCALING_WIDTH:  // h_fov == fov
98         return atan(tan(fov/2 * SG_DEGREES_TO_RADIANS) * aspect_ratio) *
99             SG_RADIANS_TO_DEGREES * 2;
100     case FG_SCALING_MAX:
101         if (aspect_ratio < 1.0) {
102             // h_fov == fov
103             return atan(tan(fov/2 * SG_DEGREES_TO_RADIANS) * aspect_ratio) *
104                 SG_RADIANS_TO_DEGREES * 2;
105         } else {
106             // v_fov == fov
107             return fov;
108         }
109     default:
110         assert(false);
111     }
112 }
113
114 void
115 FGViewer::update (int dt)
116 {
117   int i;
118   for ( i = 0; i < dt; i++ ) {
119     if ( fabs(get_goal_view_offset() - get_view_offset()) < 0.05 ) {
120       set_view_offset( get_goal_view_offset() );
121       break;
122     } else {
123       // move current_view.view_offset towards
124       // current_view.goal_view_offset
125       if ( get_goal_view_offset() > get_view_offset() )
126         {
127           if ( get_goal_view_offset() - get_view_offset() < SGD_PI ){
128             inc_view_offset( 0.01 );
129           } else {
130             inc_view_offset( -0.01 );
131           }
132         } else {
133           if ( get_view_offset() - get_goal_view_offset() < SGD_PI ){
134             inc_view_offset( -0.01 );
135           } else {
136             inc_view_offset( 0.01 );
137           }
138         }
139       if ( get_view_offset() > SGD_2PI ) {
140         inc_view_offset( -SGD_2PI );
141       } else if ( get_view_offset() < 0 ) {
142         inc_view_offset( SGD_2PI );
143       }
144     }
145   }
146
147   for ( i = 0; i < dt; i++ ) {
148     if ( fabs(get_goal_view_tilt() - get_view_tilt()) < 0.05 ) {
149       set_view_tilt( get_goal_view_tilt() );
150       break;
151     } else {
152       // move current_view.view_tilt towards
153       // current_view.goal_view_tilt
154       if ( get_goal_view_tilt() > get_view_tilt() )
155         {
156           if ( get_goal_view_tilt() - get_view_tilt() < SGD_PI ){
157             inc_view_tilt( 0.01 );
158           } else {
159             inc_view_tilt( -0.01 );
160           }
161         } else {
162           if ( get_view_tilt() - get_goal_view_tilt() < SGD_PI ){
163             inc_view_tilt( -0.01 );
164           } else {
165             inc_view_tilt( 0.01 );
166           }
167         }
168       if ( get_view_tilt() > SGD_2PI ) {
169         inc_view_tilt( -SGD_2PI );
170       } else if ( get_view_tilt() < 0 ) {
171         inc_view_tilt( SGD_2PI );
172       }
173     }
174   }
175 }