1 // views.hxx -- data structures and routines for managing and view parameters.
3 // Written by Curtis Olson, started August 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
29 # error This library requires C++
32 #include <Include/compiler.h>
36 #include <sg.h> // plib include
38 #include <FDM/flight.hxx>
39 #include <Math/mat3.h>
40 #include <Math/point3d.hxx>
41 #include <Time/fg_time.hxx>
42 #include <Time/light.hxx>
44 #include "options.hxx"
54 typedef list < FGMat4Wrapper > sgMat4_list;
55 typedef sgMat4_list::iterator sgMat4_list_iterator;
56 typedef sgMat4_list::const_iterator const_sgMat4_list_iterator;
59 // used in views.cxx and tilemgr.cxx
60 #define USE_FAST_FOV_CLIP
63 // Define a structure containing view information
70 FG_VIEW_FIRST_PERSON = 0,
74 // the current offset from forward for viewing
77 // the goal view offset for viewing (used for smooth view changes)
78 double goal_view_offset;
80 // flag forcing update of fov related stuff
83 // fov of view is specified in the y direction, win_ratio is used to
84 // calculate the fov in the X direction = width/height
87 // width & height of window
88 int winWidth, winHeight;
90 // sin and cos of (fov / 2) in Y axis
91 double sin_fov_y, cos_fov_y;
92 double sinlon, coslon;
94 // slope of view frustum edge in eye space Y axis
97 // sin and cos of (fov / 2) in X axis
98 double sin_fov_x, cos_fov_x;
100 // slope of view frustum edge in eye space X axis
103 #if defined( USE_FAST_FOV_CLIP )
104 double fov_x_clip, fov_y_clip;
105 #endif // USE_FAST_FOV_CLIP
107 // View frustum cull ratio (% of tiles culled ... used for
108 // reporting purposes)
111 // Number of triangles rendered;
115 // absolute view position
116 Point3D abs_view_pos;
118 // view position translated to scenery.center
121 // cartesion coordinates of current lon/lat if at sea level
122 // translated to scenery.center*/
123 Point3D cur_zero_elev;
125 // vector in cartesian coordinates from current position to the
126 // postion on the earth's surface the sun is directly over
129 // surface direction to go to head towards sun
130 MAT3vec surface_to_sun;
132 // vector in cartesian coordinates from current position to the
133 // postion on the earth's surface the moon is directly over
136 // surface direction to go to head towards moon
137 MAT3vec surface_to_moon;
139 // surface vector heading south
140 MAT3vec surface_south;
142 // surface vector heading east (used to unambiguously align sky
144 MAT3vec surface_east;
146 // local up vector (normal to the plane tangent to the earth's
147 // surface at the spot we are directly above
150 // up vector for the view (usually point straight up through the
151 // top of the aircraft
154 // the vector pointing straight out the nose of the aircraft
155 MAT3vec view_forward;
157 // Transformation matrix for eye coordinates to aircraft coordinates
160 // Transformation matrix for the view direction offset relative to
161 // the AIRCRAFT matrix
164 // Transformation matrix for aircraft coordinates to world
168 // Combined transformation from eye coordinates to world coordinates
169 MAT3mat EYE_TO_WORLD;
171 // Inverse of EYE_TO_WORLD which is a transformation from world
172 // coordinates to eye coordinates
173 MAT3mat WORLD_TO_EYE;
175 // Current model view matrix;
176 GLfloat MODEL_VIEW[16];
179 fgViewMode view_mode;
181 // sg versions of our friendly matrices
182 sgMat4 sgLOCAL, sgUP, sgVIEW_ROT, sgTRANS, sgVIEW, sgLARC_TO_SSG;
184 // queue of view matrices so we can have a follow view
195 // Initialize a view class
198 // Basically, this is a modified version of the Mesa gluLookAt()
199 // function that's been modified slightly so we can capture the
200 // result (and use it later) otherwise this all gets calculated in
201 // OpenGL land and we don't have access to the results.
202 void LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
203 GLdouble centerx, GLdouble centery, GLdouble centerz,
204 GLdouble upx, GLdouble upy, GLdouble upz );
206 // Update the view volume, position, and orientation
207 void UpdateViewParams( void );
209 // Flag to request that UpdateFOV() be called next time
210 // UpdateViewMath() is run.
211 inline void force_update_fov_math() { update_fov = true; }
213 // Update the view parameters
214 void UpdateViewMath( FGInterface *f );
216 // Update the "World to Eye" transformation matrix
217 void UpdateWorldToEye( FGInterface *f );
219 // Update the field of view coefficients
220 void UpdateFOV( const fgOPTIONS& o );
223 void cycle_view_mode();
225 // accessor functions
226 inline double get_view_offset() const { return view_offset; }
227 inline void set_view_offset( double a ) { view_offset = a; }
228 inline void inc_view_offset( double amt ) { view_offset += amt; }
229 inline double get_goal_view_offset() const { return goal_view_offset; }
230 inline void set_goal_view_offset( double a) { goal_view_offset = a; }
231 inline double get_win_ratio() const { return win_ratio; }
232 inline void set_win_ratio( double r ) { win_ratio = r; }
233 inline int get_winWidth() const { return winWidth; }
234 inline void set_winWidth( int w ) { winWidth = w; }
235 inline int get_winHeight() const { return winHeight; }
236 inline void set_winHeight( int h ) { winHeight = h; }
237 inline double get_slope_y() const { return slope_y; }
238 inline double get_slope_x() const { return slope_x; }
239 #if defined( USE_FAST_FOV_CLIP )
240 inline double get_fov_x_clip() const { return fov_x_clip; }
241 inline double get_fov_y_clip() const { return fov_y_clip; }
242 #endif // USE_FAST_FOV_CLIP
243 inline double get_vfc_ratio() const { return vfc_ratio; }
244 inline void set_vfc_ratio(double r) { vfc_ratio = r; }
245 inline int get_tris_rendered() const { return tris_rendered; }
246 inline void set_tris_rendered( int tris) { tris_rendered = tris; }
247 inline int get_tris_culled() const { return tris_culled; }
248 inline void set_tris_culled( int tris) { tris_culled = tris; }
249 inline Point3D get_abs_view_pos() const { return abs_view_pos; }
250 inline Point3D get_view_pos() const { return view_pos; }
251 inline Point3D get_cur_zero_elev() const { return cur_zero_elev; }
252 inline double *get_to_sun() { return to_sun; }
253 inline void set_to_sun( double x, double y, double z) {
258 inline double *get_surface_to_sun() { return surface_to_sun; }
259 inline void set_surface_to_sun( double x, double y, double z) {
260 surface_to_sun[0] = x;
261 surface_to_sun[1] = y;
262 surface_to_sun[2] = z;
264 inline double *get_to_moon() { return to_moon; }
265 inline void set_to_moon( double x, double y, double z) {
270 inline double *get_surface_to_moon() { return surface_to_moon; }
271 inline void set_surface_to_moon( double x, double y, double z) {
272 surface_to_moon[0] = x;
273 surface_to_moon[1] = y;
274 surface_to_moon[2] = z;
276 inline double *get_surface_south() { return surface_south; }
277 inline double *get_surface_east() { return surface_east; }
278 inline double *get_local_up() { return local_up; }
279 inline double *get_view_forward() { return view_forward; }
280 inline const MAT3mat *get_WORLD_TO_EYE() const { return &WORLD_TO_EYE; }
281 inline GLfloat *get_MODEL_VIEW() { return MODEL_VIEW; }
285 extern FGView current_view;