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.
22 // (Log is kept at end of this file)
30 # error This library requires C++
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>
40 #include "options.hxx"
43 // used in views.cxx and tilemgr.cxx
44 #define USE_FAST_FOV_CLIP
47 // Define a structure containing view information
52 // the current offset from forward for viewing
55 // the goal view offset for viewing (used for smooth view changes)
56 double goal_view_offset;
58 // flag forcing update of fov related stuff
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
65 // width & height of window
66 int winWidth, winHeight;
68 // sin and cos of (fov / 2) in Y axis
69 double sin_fov_y, cos_fov_y;
70 double sinlon, coslon;
72 // slope of view frustum edge in eye space Y axis
75 // sin and cos of (fov / 2) in X axis
76 double sin_fov_x, cos_fov_x;
78 // slope of view frustum edge in eye space X axis
81 #if defined( USE_FAST_FOV_CLIP )
82 double fov_x_clip, fov_y_clip;
83 #endif // USE_FAST_FOV_CLIP
85 // View frustum cull ratio (% of tiles culled ... used for
86 // reporting purposes)
89 // Number of triangles rendered;
93 // absolute view position
96 // view position translated to scenery.center
99 // cartesion coordinates of current lon/lat if at sea level
100 // translated to scenery.center*/
101 Point3D cur_zero_elev;
103 // vector in cartesian coordinates from current position to the
104 // postion on the earth's surface the sun is directly over
107 // surface direction to go to head towards sun
108 MAT3vec surface_to_sun;
110 // vector in cartesian coordinates from current position to the
111 // postion on the earth's surface the moon is directly over
114 // surface direction to go to head towards moon
115 MAT3vec surface_to_moon;
117 // surface vector heading south
118 MAT3vec surface_south;
120 // surface vector heading east (used to unambiguously align sky
122 MAT3vec surface_east;
124 // local up vector (normal to the plane tangent to the earth's
125 // surface at the spot we are directly above
128 // up vector for the view (usually point straight up through the
129 // top of the aircraft
132 // the vector pointing straight out the nose of the aircraft
133 MAT3vec view_forward;
135 // Transformation matrix for eye coordinates to aircraft coordinates
138 // Transformation matrix for the view direction offset relative to
139 // the AIRCRAFT matrix
142 // Transformation matrix for aircraft coordinates to world
146 // Combined transformation from eye coordinates to world coordinates
147 MAT3mat EYE_TO_WORLD;
149 // Inverse of EYE_TO_WORLD which is a transformation from world
150 // coordinates to eye coordinates
151 MAT3mat WORLD_TO_EYE;
153 // Current model view matrix;
154 GLdouble MODEL_VIEW[16];
164 // Initialize a view class
167 // Basically, this is a modified version of the Mesa gluLookAt()
168 // function that's been modified slightly so we can capture the
169 // result (and use it later) otherwise this all gets calculated in
170 // OpenGL land and we don't have access to the results.
171 void LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
172 GLdouble centerx, GLdouble centery, GLdouble centerz,
173 GLdouble upx, GLdouble upy, GLdouble upz );
175 // Update the view volume, position, and orientation
176 void UpdateViewParams( void );
178 // Flag to request that UpdateFOV() be called next time
179 // UpdateViewMath() is run.
180 inline void force_update_fov_math() { update_fov = true; }
182 // Update the view parameters
183 void UpdateViewMath( FGInterface *f );
185 // Update the "World to Eye" transformation matrix
186 void UpdateWorldToEye( FGInterface *f );
188 // Update the field of view coefficients
189 void UpdateFOV( const fgOPTIONS& o );
191 // accessor functions
192 inline double get_view_offset() const { return view_offset; }
193 inline void set_view_offset( double a ) { view_offset = a; }
194 inline void inc_view_offset( double amt ) { view_offset += amt; }
195 inline double get_goal_view_offset() const { return goal_view_offset; }
196 inline void set_goal_view_offset( double a) { goal_view_offset = a; }
197 inline double get_win_ratio() const { return win_ratio; }
198 inline void set_win_ratio( double r ) { win_ratio = r; }
199 inline int get_winWidth() const { return winWidth; }
200 inline void set_winWidth( int w ) { winWidth = w; }
201 inline int get_winHeight() const { return winHeight; }
202 inline void set_winHeight( int h ) { winHeight = h; }
203 inline double get_slope_y() const { return slope_y; }
204 inline double get_slope_x() const { return slope_x; }
205 #if defined( USE_FAST_FOV_CLIP )
206 inline double get_fov_x_clip() const { return fov_x_clip; }
207 inline double get_fov_y_clip() const { return fov_y_clip; }
208 #endif // USE_FAST_FOV_CLIP
209 inline double get_vfc_ratio() const { return vfc_ratio; }
210 inline void set_vfc_ratio(double r) { vfc_ratio = r; }
211 inline int get_tris_rendered() const { return tris_rendered; }
212 inline void set_tris_rendered( int tris) { tris_rendered = tris; }
213 inline int get_tris_culled() const { return tris_culled; }
214 inline void set_tris_culled( int tris) { tris_culled = tris; }
215 inline Point3D get_abs_view_pos() const { return abs_view_pos; }
216 inline Point3D get_view_pos() const { return view_pos; }
217 inline Point3D get_cur_zero_elev() const { return cur_zero_elev; }
218 inline double *get_to_sun() { return to_sun; }
219 inline void set_to_sun( double x, double y, double z) {
224 inline double *get_surface_to_sun() { return surface_to_sun; }
225 inline void set_surface_to_sun( double x, double y, double z) {
226 surface_to_sun[0] = x;
227 surface_to_sun[1] = y;
228 surface_to_sun[2] = z;
230 inline double *get_to_moon() { return to_moon; }
231 inline void set_to_moon( double x, double y, double z) {
236 inline double *get_surface_to_moon() { return surface_to_moon; }
237 inline void set_surface_to_moon( double x, double y, double z) {
238 surface_to_moon[0] = x;
239 surface_to_moon[1] = y;
240 surface_to_moon[2] = z;
242 inline double *get_surface_south() { return surface_south; }
243 inline double *get_surface_east() { return surface_east; }
244 inline double *get_local_up() { return local_up; }
245 inline const MAT3mat *get_WORLD_TO_EYE() const { return &WORLD_TO_EYE; }
246 inline GLdouble *get_MODEL_VIEW() { return MODEL_VIEW; }
250 extern FGView current_view;
257 // Revision 1.1 1999/04/05 21:32:47 curt
260 // Revision 1.23 1999/04/03 04:21:06 curt
261 // Integration of Steve's plib conglomeration.
262 // Optimizations (tm) by Norman Vine.
264 // Revision 1.22 1999/03/22 02:08:15 curt
265 // Changes contributed by Durk Talsma:
267 // Here's a few changes I made to fg-0.58 this weekend. Included are the
268 // following features:
269 // - Sun and moon have a halo
270 // - The moon has a light vector, moon_angle, etc. etc. so that we can have
271 // some moonlight during the night.
272 // - Lot's of small changes tweakes, including some stuff Norman Vine sent
275 // Revision 1.21 1999/02/05 21:29:15 curt
276 // Modifications to incorporate Jon S. Berndts flight model code.
278 // Revision 1.20 1999/02/02 20:13:38 curt
279 // MSVC++ portability changes by Bernie Bright:
281 // Lib/Serial/serial.[ch]xx: Initial Windows support - incomplete.
282 // Simulator/Astro/stars.cxx: typo? included <stdio> instead of <cstdio>
283 // Simulator/Cockpit/hud.cxx: Added Standard headers
284 // Simulator/Cockpit/panel.cxx: Redefinition of default parameter
285 // Simulator/Flight/flight.cxx: Replaced cout with FG_LOG. Deleted <stdio.h>
286 // Simulator/Main/fg_init.cxx:
287 // Simulator/Main/GLUTmain.cxx:
288 // Simulator/Main/options.hxx: Shuffled <fg_serial.hxx> dependency
289 // Simulator/Objects/material.hxx:
290 // Simulator/Time/timestamp.hxx: VC++ friend kludge
291 // Simulator/Scenery/tile.[ch]xx: Fixed using std::X declarations
292 // Simulator/Main/views.hxx: Added a constant
294 // Revision 1.19 1999/02/01 21:33:36 curt
295 // Renamed FlightGear/Simulator/Flight to FlightGear/Simulator/FDM since
296 // Jon accepted my offer to do this and thought it was a good idea.
298 // Revision 1.18 1998/12/11 20:26:30 curt
299 // Fixed view frustum culling accuracy bug so we can look out the sides and
300 // back without tri-stripes dropping out.
302 // Revision 1.17 1998/12/09 18:50:29 curt
303 // Converted "class fgVIEW" to "class FGView" and updated to make data
304 // members private and make required accessor functions.
306 // Revision 1.16 1998/12/05 15:54:25 curt
307 // Renamed class fgFLIGHT to class FGState as per request by JSB.
309 // Revision 1.15 1998/10/16 23:27:56 curt
312 // Revision 1.14 1998/10/16 00:54:04 curt
313 // Converted to Point3D class.
315 // Revision 1.13 1998/09/08 15:04:36 curt
316 // Optimizations by Norman Vine.
318 // Revision 1.12 1998/08/24 20:11:15 curt
319 // Added i/I to toggle full vs. minimal HUD.
320 // Added a --hud-tris vs --hud-culled option.
321 // Moved options accessor funtions to options.hxx.
323 // Revision 1.11 1998/08/20 20:32:35 curt
324 // Reshuffled some of the code in and around views.[ch]xx
326 // Revision 1.10 1998/07/08 14:45:09 curt
327 // polar3d.h renamed to polar3d.hxx
328 // vector.h renamed to vector.hxx
329 // updated audio support so it waits to create audio classes (and tie up
330 // /dev/dsp) until the mpg123 player is finished.
332 // Revision 1.9 1998/07/04 00:52:27 curt
333 // Add my own version of gluLookAt() (which is nearly identical to the
334 // Mesa/glu version.) But, by calculating the Model View matrix our selves
335 // we can save this matrix without having to read it back in from the video
336 // card. This hopefully allows us to save a few cpu cycles when rendering
337 // out the fragments because we can just use glLoadMatrixd() with the
338 // precalculated matrix for each tile rather than doing a push(), translate(),
339 // pop() for every fragment.
341 // Panel status defaults to off for now until it gets a bit more developed.
343 // Extract OpenGL driver info on initialization.
345 // Revision 1.8 1998/05/27 02:24:06 curt
346 // View optimizations by Norman Vine.
348 // Revision 1.7 1998/05/17 16:59:04 curt
349 // First pass at view frustum culling now operational.
351 // Revision 1.6 1998/05/16 13:08:37 curt
352 // C++ - ified views.[ch]xx
353 // Shuffled some additional view parameters into the fgVIEW class.
354 // Changed tile-radius to tile-diameter because it is a much better
356 // Added a WORLD_TO_EYE transformation to views.cxx. This allows us
357 // to transform world space to eye space for view frustum culling.
359 // Revision 1.5 1998/05/02 01:51:02 curt
360 // Updated polartocart conversion routine.
362 // Revision 1.4 1998/04/28 01:20:24 curt
363 // Type-ified fgTIME and fgVIEW.
364 // Added a command line option to disable textures.
366 // Revision 1.3 1998/04/25 22:06:31 curt
367 // Edited cvs log messages in source files ... bad bad bad!
369 // Revision 1.2 1998/04/24 00:49:22 curt
370 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
371 // Trying out some different option parsing code.
372 // Some code reorganization.
374 // Revision 1.1 1998/04/22 13:25:46 curt
375 // C++ - ifing the code.
376 // Starting a bit of reorganization of lighting code.
378 // Revision 1.11 1998/04/21 17:02:42 curt
379 // Prepairing for C++ integration.
381 // Revision 1.10 1998/02/07 15:29:45 curt
382 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
383 // <chotchkiss@namg.us.anritsu.com>
385 // Revision 1.9 1998/01/29 00:50:29 curt
386 // Added a view record field for absolute x, y, z position.
388 // Revision 1.8 1998/01/27 00:47:58 curt
389 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
390 // system and commandline/config file processing code.
392 // Revision 1.7 1998/01/22 02:59:38 curt
393 // Changed #ifdef FILE_H to #ifdef _FILE_H
395 // Revision 1.6 1998/01/19 19:27:10 curt
396 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
397 // This should simplify things tremendously.
399 // Revision 1.5 1997/12/22 04:14:32 curt
400 // Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
402 // Revision 1.4 1997/12/17 23:13:36 curt
403 // Began working on rendering a sky.
405 // Revision 1.3 1997/12/15 23:54:51 curt
406 // Add xgl wrappers for debugging.
407 // Generate terrain normals on the fly.
409 // Revision 1.2 1997/12/10 22:37:48 curt
410 // Prepended "fg" on the name of all global structures that didn't have it yet.
411 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
413 // Revision 1.1 1997/08/27 21:31:18 curt