1 // viewer.hxx -- class for managing a viewer in the flightgear world.
3 // Written by Curtis Olson, started August 1997.
4 // overhaul started October 2000.
6 // Copyright (C) 1997 - 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.
30 # error This library requires C++
33 #include <simgear/compiler.h>
35 #include <plib/sg.h> // plib include
38 #define FG_FOV_MIN 0.1
39 #define FG_FOV_MAX 179.9
42 // Define a structure containing view information
55 // flag forcing a recalc of derived view parameters
62 // the field of view in the x (width) direction
65 // ratio of x and y fov's; fov(y) = fov(x) * fov_ratio
68 // ratio of window width and height; height = width * aspect_ratio
71 // the current view offset angle from forward (rotated about the
74 bool reverse_view_offset;
76 // the goal view offset angle (used for smooth view changes)
77 double goal_view_offset;
79 // geodetic view position
80 sgdVec3 geod_view_pos;
82 // absolute view position in earth coordinates
85 // view position in opengl world coordinates (this is the
86 // abs_view_pos translated to scenery.center)
89 // radius to sea level from center of the earth (m)
90 double sea_level_radius;
92 // cartesion coordinates of current lon/lat if at sea level
93 // translated to scenery.center
96 // height ASL of the terrain for our current view position
97 // (future?) double ground_elev;
99 // pilot offset from center of gravity. The X axis is positive
100 // out the tail, Y is out the right wing, and Z is positive up.
101 // Distances in meters of course.
104 // surface vector heading south
105 sgVec3 surface_south;
107 // surface vector heading east (used to unambiguously align sky
111 // world up vector (normal to the plane tangent to the earth's
112 // surface at the spot we are directly above
115 // sg versions of our friendly matrices
116 sgMat4 VIEW, VIEW_ROT, UP;
118 inline void set_dirty() { dirty = true; }
119 inline void set_clean() { dirty = false; }
121 // Update the view volume, position, and orientation
122 virtual void update() = 0;
130 virtual ~FGViewer( void );
132 //////////////////////////////////////////////////////////////////////
134 //////////////////////////////////////////////////////////////////////
135 inline void set_fov( double amount ) { fov = amount; }
136 // Don't provide set_fov_ratio explicitely. Use set_aspect_ratio
138 inline void set_aspect_ratio( double r ) {
140 fov_ratio = atan(tan(fov/2 * SG_DEGREES_TO_RADIANS) * aspect_ratio) *
141 SG_RADIANS_TO_DEGREES / (fov/2);
143 inline void set_view_offset( double a ) {
147 inline void inc_view_offset( double amt ) {
151 inline void set_goal_view_offset( double a) {
153 goal_view_offset = a;
154 while ( goal_view_offset < 0 ) {
155 goal_view_offset += 360.0;
157 while ( goal_view_offset > 360.0 ) {
158 goal_view_offset -= 360.0;
161 inline void set_reverse_view_offset( bool val ) {
162 reverse_view_offset = val;
164 inline void set_geod_view_pos( double lon, double lat, double alt ) {
165 // data should be in radians and meters asl
167 // cout << "set_geod_view_pos = " << lon << ", " << lat << ", " << alt
169 sgdSetVec3( geod_view_pos, lon, lat, alt );
171 inline void set_pilot_offset( float x, float y, float z ) {
173 sgSetVec3( pilot_offset, x, y, z );
175 inline void set_sea_level_radius( double r ) {
176 // data should be in meters from the center of the earth
178 sea_level_radius = r;
181 //////////////////////////////////////////////////////////////////////
182 // accessor functions
183 //////////////////////////////////////////////////////////////////////
184 inline int get_type() const { return _type ; }
185 inline int is_a( int t ) const { return get_type() == t ; }
186 inline bool is_dirty() const { return dirty; }
187 inline double get_fov() const { return fov; }
188 inline double get_aspect_ratio() const { return aspect_ratio; }
189 inline double get_fov_ratio() const { return fov_ratio; }
190 inline double get_view_offset() const { return view_offset; }
191 inline bool get_reverse_view_offset() const { return reverse_view_offset; }
192 inline double get_goal_view_offset() const { return goal_view_offset; }
193 inline double *get_geod_view_pos() { return geod_view_pos; }
194 inline float *get_pilot_offset() { return pilot_offset; }
195 inline double get_sea_level_radius() const { return sea_level_radius; }
197 //////////////////////////////////////////////////////////////////////
198 // derived values accessor functions
199 //////////////////////////////////////////////////////////////////////
200 inline double *get_abs_view_pos() {
201 if ( dirty ) { update(); }
204 inline float *get_view_pos() {
205 if ( dirty ) { update(); }
208 inline float *get_zero_elev() {
209 if ( dirty ) { update(); }
213 // inline double get_ground_elev() {
214 // if ( dirty ) { update(); }
215 // return ground_elev;
217 inline float *get_surface_south() {
218 if ( dirty ) { update(); }
219 return surface_south;
221 inline float *get_surface_east() {
222 if ( dirty ) { update(); }
225 inline float *get_world_up() {
226 if ( dirty ) { update(); }
229 inline const sgVec4 *get_VIEW() {
230 if ( dirty ) { update(); }
233 inline const sgVec4 *get_VIEW_ROT() {
234 if ( dirty ) { update(); }
237 inline const sgVec4 *get_UP() {
238 if ( dirty ) { update(); }
244 #endif // _VIEWER_HXX