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
40 #define FG_FOV_MIN 0.1
41 #define FG_FOV_MAX 179.9
44 // Define a structure containing view information
55 enum fgScalingType { // nominal Field Of View actually applies to ...
56 FG_SCALING_WIDTH, // window width
57 FG_SCALING_MAX, // max(width, height)
58 // FG_SCALING_G_MEAN, // geometric_mean(width, height)
59 // FG_SCALING_INDEPENDENT // whole screen
64 // flag forcing a recalc of derived view parameters
70 fgScalingType scalingType;
72 // the nominal field of view (angle, in degrees)
75 // ratio of window width and height; height = width * aspect_ratio
78 // the current view offset angle from forward (rotated about the
81 bool reverse_view_offset;
83 // the goal view offset angle (used for smooth view changes)
84 double goal_view_offset;
86 // the view tilt angles
88 double goal_view_tilt;
90 // geodetic view position
91 sgdVec3 geod_view_pos;
93 // absolute view position in earth coordinates
96 // view position in opengl world coordinates (this is the
97 // abs_view_pos translated to scenery.center)
100 // radius to sea level from center of the earth (m)
101 double sea_level_radius;
103 // cartesion coordinates of current lon/lat if at sea level
104 // translated to scenery.center
107 // height ASL of the terrain for our current view position
108 // (future?) double ground_elev;
110 // pilot offset from center of gravity. The X axis is positive
111 // out the tail, Y is out the right wing, and Z is positive up.
112 // Distances in meters of course.
115 // surface vector heading south
116 sgVec3 surface_south;
118 // surface vector heading east (used to unambiguously align sky
122 // world up vector (normal to the plane tangent to the earth's
123 // surface at the spot we are directly above
126 // sg versions of our friendly matrices
127 sgMat4 VIEW, VIEW_ROT, UP;
129 inline void set_dirty() { dirty = true; }
130 inline void set_clean() { dirty = false; }
132 // Update the view volume, position, and orientation
133 virtual void update() = 0;
141 virtual ~FGViewer( void );
143 virtual void init ();
144 virtual void bind ();
145 virtual void unbind ();
146 virtual void update (int dt);
148 //////////////////////////////////////////////////////////////////////
150 //////////////////////////////////////////////////////////////////////
152 inline void set_fov( double fov_deg ) {
156 inline void set_aspect_ratio( double r ) {
159 inline void set_view_offset( double a ) {
163 inline void inc_view_offset( double amt ) {
167 inline void set_goal_view_offset( double a) {
169 goal_view_offset = a;
170 while ( goal_view_offset < 0 ) {
171 goal_view_offset += 360.0;
173 while ( goal_view_offset > 360.0 ) {
174 goal_view_offset -= 360.0;
177 inline void set_reverse_view_offset( bool val ) {
178 reverse_view_offset = val;
180 inline void set_view_tilt( double a ) {
184 inline void inc_view_tilt( double amt ) {
188 inline void set_goal_view_tilt( double a) {
191 while ( goal_view_tilt < 0 ) {
192 goal_view_tilt += 360.0;
194 while ( goal_view_tilt > 360.0 ) {
195 goal_view_tilt -= 360.0;
198 inline void set_geod_view_pos( double lon, double lat, double alt ) {
199 // data should be in radians and meters asl
201 // cout << "set_geod_view_pos = " << lon << ", " << lat << ", " << alt
203 sgdSetVec3( geod_view_pos, lon, lat, alt );
205 inline void set_pilot_offset( float x, float y, float z ) {
207 sgSetVec3( pilot_offset, x, y, z );
209 inline void set_sea_level_radius( double r ) {
210 // data should be in meters from the center of the earth
212 sea_level_radius = r;
215 //////////////////////////////////////////////////////////////////////
216 // accessor functions
217 //////////////////////////////////////////////////////////////////////
218 inline int get_type() const { return _type ; }
219 inline int is_a( int t ) const { return get_type() == t ; }
220 inline bool is_dirty() const { return dirty; }
221 inline double get_fov() const { return fov; }
222 inline double get_aspect_ratio() const { return aspect_ratio; }
223 inline double get_view_offset() const { return view_offset; }
224 inline bool get_reverse_view_offset() const { return reverse_view_offset; }
225 inline double get_goal_view_offset() const { return goal_view_offset; }
226 inline double get_view_tilt() const { return view_tilt; }
227 inline double get_goal_view_tilt() const { return goal_view_tilt; }
228 inline double *get_geod_view_pos() { return geod_view_pos; }
229 inline float *get_pilot_offset() { return pilot_offset; }
230 inline double get_sea_level_radius() const { return sea_level_radius; }
231 // Get horizontal field of view angle, in degrees.
233 // Get vertical field of view angle, in degrees.
236 //////////////////////////////////////////////////////////////////////
237 // derived values accessor functions
238 //////////////////////////////////////////////////////////////////////
239 inline double *get_abs_view_pos() {
240 if ( dirty ) { update(); }
243 inline float *get_view_pos() {
244 if ( dirty ) { update(); }
247 inline float *get_zero_elev() {
248 if ( dirty ) { update(); }
252 // inline double get_ground_elev() {
253 // if ( dirty ) { update(); }
254 // return ground_elev;
256 inline float *get_surface_south() {
257 if ( dirty ) { update(); }
258 return surface_south;
260 inline float *get_surface_east() {
261 if ( dirty ) { update(); }
264 inline float *get_world_up() {
265 if ( dirty ) { update(); }
268 inline const sgVec4 *get_VIEW() {
269 if ( dirty ) { update(); }
272 inline const sgVec4 *get_VIEW_ROT() {
273 if ( dirty ) { update(); }
276 inline const sgVec4 *get_UP() {
277 if ( dirty ) { update(); }
283 #endif // _VIEWER_HXX