]> git.mxchange.org Git - flightgear.git/blob - Main/views.hxx
2808f4f9dda4668211a1002d36a033861ccb9046
[flightgear.git] / Main / views.hxx
1 // views.hxx -- data structures and routines for managing and view parameters.
2 //
3 // Written by Curtis Olson, started August 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
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 // (Log is kept at end of this file)
23
24
25 #ifndef _VIEWS_HXX
26 #define _VIEWS_HXX
27
28
29 #ifndef __cplusplus                                                          
30 # error This library requires C++
31 #endif                                   
32
33
34 #include <FDM/flight.hxx>
35 #include <Math/mat3.h>
36 #include <Math/point3d.hxx>
37 #include <Time/fg_time.hxx>
38 #include <Time/light.hxx>
39
40 #include "options.hxx"
41
42
43 // used in views.cxx and tilemgr.cxx
44 #define USE_FAST_FOV_CLIP 
45
46
47 // Define a structure containing view information
48 class FGView {
49
50 public:
51
52     // the current offset from forward for viewing
53     double view_offset;
54
55     // the goal view offset for viewing (used for smooth view changes)
56     double goal_view_offset;
57
58     // flag forcing update of fov related stuff
59     bool update_fov;
60         
61     // fov of view is specified in the y direction, win_ratio is used to
62     // calculate the fov in the X direction = width/height
63     double win_ratio;
64
65     // width & height of window
66     int winWidth, winHeight;
67
68     // sin and cos of (fov / 2) in Y axis
69     double sin_fov_y, cos_fov_y;
70     double sinlon, coslon;
71
72     // slope of view frustum edge in eye space Y axis
73     double slope_y;
74
75     // sin and cos of (fov / 2) in X axis
76     double sin_fov_x, cos_fov_x;
77
78     // slope of view frustum edge in eye space X axis
79     double slope_x;
80
81 #if defined( USE_FAST_FOV_CLIP )
82     double fov_x_clip, fov_y_clip;
83 #endif // USE_FAST_FOV_CLIP
84
85     // View frustum cull ratio (% of tiles culled ... used for
86     // reporting purposes)
87     double vfc_ratio;
88
89     // Number of triangles rendered;
90     int tris_rendered;
91
92     // absolute view position
93     Point3D abs_view_pos;
94
95     // view position translated to scenery.center
96     Point3D view_pos;
97
98     // cartesion coordinates of current lon/lat if at sea level
99     // translated to scenery.center*/
100     Point3D cur_zero_elev;
101
102     // vector in cartesian coordinates from current position to the
103     // postion on the earth's surface the sun is directly over
104     MAT3vec to_sun;
105     
106     // surface direction to go to head towards sun
107     MAT3vec surface_to_sun;
108
109     // surface vector heading south
110     MAT3vec surface_south;
111
112     // surface vector heading east (used to unambiguously align sky
113     // with sun)
114     MAT3vec surface_east;
115
116     // local up vector (normal to the plane tangent to the earth's
117     // surface at the spot we are directly above
118     MAT3vec local_up;
119
120     // up vector for the view (usually point straight up through the
121     // top of the aircraft
122     MAT3vec view_up;
123
124     // the vector pointing straight out the nose of the aircraft
125     MAT3vec view_forward;
126
127     // Transformation matrix for eye coordinates to aircraft coordinates
128     MAT3mat AIRCRAFT;
129
130     // Transformation matrix for the view direction offset relative to
131     // the AIRCRAFT matrix
132     MAT3mat VIEW_OFFSET;
133
134     // Transformation matrix for aircraft coordinates to world
135     // coordinates
136     MAT3mat WORLD;
137
138     // Combined transformation from eye coordinates to world coordinates
139     MAT3mat EYE_TO_WORLD;
140
141     // Inverse of EYE_TO_WORLD which is a transformation from world
142     // coordinates to eye coordinates
143     MAT3mat WORLD_TO_EYE;
144
145     // Current model view matrix;
146     GLdouble MODEL_VIEW[16];
147
148 public:
149
150     // Constructor
151     FGView( void );
152
153     // Destructor
154     ~FGView( void );
155
156     // Initialize a view class
157     void Init( void );
158
159     // Basically, this is a modified version of the Mesa gluLookAt()
160     // function that's been modified slightly so we can capture the
161     // result (and use it later) otherwise this all gets calculated in
162     // OpenGL land and we don't have access to the results.
163     void LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
164                  GLdouble centerx, GLdouble centery, GLdouble centerz,
165                  GLdouble upx, GLdouble upy, GLdouble upz );
166
167     // Update the view volume, position, and orientation
168     void UpdateViewParams( void );
169
170     // Flag to request that UpdateFOV() be called next time
171     // UpdateViewMath() is run.
172     inline void force_update_fov_math() { update_fov = true; }
173
174     // Update the view parameters
175     void UpdateViewMath( FGState *f );
176
177     // Update the "World to Eye" transformation matrix
178     void UpdateWorldToEye( FGState *f );
179
180     // Update the field of view coefficients
181     void UpdateFOV( const fgOPTIONS& o );
182
183     // accessor functions
184     inline double get_view_offset() const { return view_offset; }
185     inline void set_view_offset( double a ) { view_offset = a; }
186     inline void inc_view_offset( double amt ) { view_offset += amt; }
187     inline double get_goal_view_offset() const { return goal_view_offset; }
188     inline void set_goal_view_offset( double a) { goal_view_offset = a; }
189     inline double get_win_ratio() const { return win_ratio; }
190     inline void set_win_ratio( double r ) { win_ratio = r; }
191     inline int get_winWidth() const { return winWidth; }
192     inline void set_winWidth( int w ) { winWidth = w; }
193     inline int get_winHeight() const { return winHeight; }
194     inline void set_winHeight( int h ) { winHeight = h; }
195     inline double get_slope_y() const { return slope_y; }
196     inline double get_slope_x() const { return slope_x; }
197 #if defined( USE_FAST_FOV_CLIP )
198     inline double get_fov_x_clip() const { return fov_x_clip; }
199     inline double get_fov_y_clip() const { return fov_y_clip; }
200 #endif // USE_FAST_FOV_CLIP
201     inline double get_vfc_ratio() const { return vfc_ratio; }
202     inline void set_vfc_ratio(double r) { vfc_ratio = r; }
203     inline int get_tris_rendered() const { return tris_rendered; }
204     inline void set_tris_rendered( int tris) { tris_rendered = tris; }
205     inline Point3D get_abs_view_pos() const { return abs_view_pos; }
206     inline Point3D get_view_pos() const { return view_pos; }
207     inline Point3D get_cur_zero_elev() const { return cur_zero_elev; }
208     inline double *get_to_sun() { return to_sun; }
209     inline void set_to_sun( double x, double y, double z) {
210         to_sun[0] = x;
211         to_sun[1] = y;
212         to_sun[2] = z;
213     }
214     inline double *get_surface_to_sun() { return surface_to_sun; }
215     inline void set_surface_to_sun( double x, double y, double z) {
216         surface_to_sun[0] = x;
217         surface_to_sun[1] = y;
218         surface_to_sun[2] = z;
219     }
220     inline double *get_surface_south() { return surface_south; }
221     inline double *get_surface_east() { return surface_east; }
222     inline double *get_local_up() { return local_up; }
223     inline const MAT3mat *get_WORLD_TO_EYE() const { return &WORLD_TO_EYE; }
224     inline GLdouble *get_MODEL_VIEW() { return MODEL_VIEW; }
225 };
226
227
228 extern FGView current_view;
229
230
231 #endif // _VIEWS_HXX
232
233
234 // $Log$
235 // Revision 1.20  1999/02/02 20:13:38  curt
236 // MSVC++ portability changes by Bernie Bright:
237 //
238 // Lib/Serial/serial.[ch]xx: Initial Windows support - incomplete.
239 // Simulator/Astro/stars.cxx: typo? included <stdio> instead of <cstdio>
240 // Simulator/Cockpit/hud.cxx: Added Standard headers
241 // Simulator/Cockpit/panel.cxx: Redefinition of default parameter
242 // Simulator/Flight/flight.cxx: Replaced cout with FG_LOG.  Deleted <stdio.h>
243 // Simulator/Main/fg_init.cxx:
244 // Simulator/Main/GLUTmain.cxx:
245 // Simulator/Main/options.hxx: Shuffled <fg_serial.hxx> dependency
246 // Simulator/Objects/material.hxx:
247 // Simulator/Time/timestamp.hxx: VC++ friend kludge
248 // Simulator/Scenery/tile.[ch]xx: Fixed using std::X declarations
249 // Simulator/Main/views.hxx: Added a constant
250 //
251 // Revision 1.19  1999/02/01 21:33:36  curt
252 // Renamed FlightGear/Simulator/Flight to FlightGear/Simulator/FDM since
253 // Jon accepted my offer to do this and thought it was a good idea.
254 //
255 // Revision 1.18  1998/12/11 20:26:30  curt
256 // Fixed view frustum culling accuracy bug so we can look out the sides and
257 // back without tri-stripes dropping out.
258 //
259 // Revision 1.17  1998/12/09 18:50:29  curt
260 // Converted "class fgVIEW" to "class FGView" and updated to make data
261 // members private and make required accessor functions.
262 //
263 // Revision 1.16  1998/12/05 15:54:25  curt
264 // Renamed class fgFLIGHT to class FGState as per request by JSB.
265 //
266 // Revision 1.15  1998/10/16 23:27:56  curt
267 // C++-ifying.
268 //
269 // Revision 1.14  1998/10/16 00:54:04  curt
270 // Converted to Point3D class.
271 //
272 // Revision 1.13  1998/09/08 15:04:36  curt
273 // Optimizations by Norman Vine.
274 //
275 // Revision 1.12  1998/08/24 20:11:15  curt
276 // Added i/I to toggle full vs. minimal HUD.
277 // Added a --hud-tris vs --hud-culled option.
278 // Moved options accessor funtions to options.hxx.
279 //
280 // Revision 1.11  1998/08/20 20:32:35  curt
281 // Reshuffled some of the code in and around views.[ch]xx
282 //
283 // Revision 1.10  1998/07/08 14:45:09  curt
284 // polar3d.h renamed to polar3d.hxx
285 // vector.h renamed to vector.hxx
286 // updated audio support so it waits to create audio classes (and tie up
287 //   /dev/dsp) until the mpg123 player is finished.
288 //
289 // Revision 1.9  1998/07/04 00:52:27  curt
290 // Add my own version of gluLookAt() (which is nearly identical to the
291 // Mesa/glu version.)  But, by calculating the Model View matrix our selves
292 // we can save this matrix without having to read it back in from the video
293 // card.  This hopefully allows us to save a few cpu cycles when rendering
294 // out the fragments because we can just use glLoadMatrixd() with the
295 // precalculated matrix for each tile rather than doing a push(), translate(),
296 // pop() for every fragment.
297 //
298 // Panel status defaults to off for now until it gets a bit more developed.
299 //
300 // Extract OpenGL driver info on initialization.
301 //
302 // Revision 1.8  1998/05/27 02:24:06  curt
303 // View optimizations by Norman Vine.
304 //
305 // Revision 1.7  1998/05/17 16:59:04  curt
306 // First pass at view frustum culling now operational.
307 //
308 // Revision 1.6  1998/05/16 13:08:37  curt
309 // C++ - ified views.[ch]xx
310 // Shuffled some additional view parameters into the fgVIEW class.
311 // Changed tile-radius to tile-diameter because it is a much better
312 //   name.
313 // Added a WORLD_TO_EYE transformation to views.cxx.  This allows us
314 //  to transform world space to eye space for view frustum culling.
315 //
316 // Revision 1.5  1998/05/02 01:51:02  curt
317 // Updated polartocart conversion routine.
318 //
319 // Revision 1.4  1998/04/28 01:20:24  curt
320 // Type-ified fgTIME and fgVIEW.
321 // Added a command line option to disable textures.
322 //
323 // Revision 1.3  1998/04/25 22:06:31  curt
324 // Edited cvs log messages in source files ... bad bad bad!
325 //
326 // Revision 1.2  1998/04/24 00:49:22  curt
327 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
328 // Trying out some different option parsing code.
329 // Some code reorganization.
330 //
331 // Revision 1.1  1998/04/22 13:25:46  curt
332 // C++ - ifing the code.
333 // Starting a bit of reorganization of lighting code.
334 //
335 // Revision 1.11  1998/04/21 17:02:42  curt
336 // Prepairing for C++ integration.
337 //
338 // Revision 1.10  1998/02/07 15:29:45  curt
339 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
340 // <chotchkiss@namg.us.anritsu.com>
341 //
342 // Revision 1.9  1998/01/29 00:50:29  curt
343 // Added a view record field for absolute x, y, z position.
344 //
345 // Revision 1.8  1998/01/27 00:47:58  curt
346 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
347 // system and commandline/config file processing code.
348 //
349 // Revision 1.7  1998/01/22 02:59:38  curt
350 // Changed #ifdef FILE_H to #ifdef _FILE_H
351 //
352 // Revision 1.6  1998/01/19 19:27:10  curt
353 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
354 // This should simplify things tremendously.
355 //
356 // Revision 1.5  1997/12/22 04:14:32  curt
357 // Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
358 //
359 // Revision 1.4  1997/12/17 23:13:36  curt
360 // Began working on rendering a sky.
361 //
362 // Revision 1.3  1997/12/15 23:54:51  curt
363 // Add xgl wrappers for debugging.
364 // Generate terrain normals on the fly.
365 //
366 // Revision 1.2  1997/12/10 22:37:48  curt
367 // Prepended "fg" on the name of all global structures that didn't have it yet.
368 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
369 //
370 // Revision 1.1  1997/08/27 21:31:18  curt
371 // Initial revision.
372 //