]> git.mxchange.org Git - flightgear.git/blob - Main/views.cxx
Updated to compile in audio support if OSS available.
[flightgear.git] / Main / views.cxx
1 // views.cxx -- data structures and routines for managing and view
2 //               parameters.
3 //
4 // Written by Curtis Olson, started August 1997.
5 //
6 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23 // (Log is kept at end of this file)
24
25
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #include <Debug/fg_debug.h>
31 #include <Flight/flight.h>
32 #include <Include/fg_constants.h>
33 #include <Math/mat3.h>
34 #include <Math/polar3d.h>
35 #include <Math/vector.h>
36 #include <Scenery/scenery.hxx>
37 #include <Time/fg_time.hxx>
38
39 #include "options.hxx"
40 #include "views.hxx"
41
42
43 // This is a record containing current view parameters
44 fgVIEW current_view;
45
46
47 // Constructor
48 fgVIEW::fgVIEW( void ) {
49 }
50
51
52 // Initialize a view structure
53 void fgVIEW::Init( void ) {
54     fgPrintf( FG_VIEW, FG_INFO, "Initializing View parameters\n");
55
56     view_offset = 0.0;
57     goal_view_offset = 0.0;
58
59     winWidth = 640;  // FG_DEFAULT_WIN_WIDTH
60     winHeight = 480; // FG_DEFAULT_WIN_HEIGHT
61     win_ratio = (double) winWidth / (double) winHeight;
62     update_fov = TRUE;
63 }
64
65
66 // Update the field of view parameters
67 void fgVIEW::UpdateFOV( fgOPTIONS *o ) {
68     double theta_x, theta_y;
69         
70     // printf("win_ratio = %.2f\n", win_ratio);
71     // calculate sin() and cos() of fov / 2 in X direction;
72     theta_x = (o->fov * win_ratio * DEG_TO_RAD) / 2.0;
73     // printf("theta_x = %.2f\n", theta_x);
74     sin_fov_x = sin(theta_x);
75     cos_fov_x = cos(theta_x);
76     slope_x =  - cos_fov_x / sin_fov_x;
77     // printf("slope_x = %.2f\n", slope_x);
78
79     // calculate sin() and cos() of fov / 2 in Y direction;
80     theta_y = (o->fov * DEG_TO_RAD) / 2.0;
81     // printf("theta_y = %.2f\n", theta_y);
82     sin_fov_y = sin(theta_y);
83     cos_fov_y = cos(theta_y);
84     slope_y = cos_fov_y / sin_fov_y;
85     // printf("slope_y = %.2f\n", slope_y);
86 }
87
88
89 // Update the view parameters
90 void fgVIEW::Update( fgFLIGHT *f ) {
91     fgOPTIONS *o;
92     fgPolarPoint3d p;
93     MAT3vec vec, forward, v0, minus_z;
94     MAT3mat R, TMP, UP, LOCAL, VIEW;
95     double ntmp;
96
97     o = &current_options;
98
99     if(update_fov == TRUE) {
100         // printf("Updating fov\n");
101         UpdateFOV(o);
102         update_fov = FALSE;
103     }
104                 
105     scenery.center.x = scenery.next_center.x;
106     scenery.center.y = scenery.next_center.y;
107     scenery.center.z = scenery.next_center.z;
108
109     // calculate the cartesion coords of the current lat/lon/0 elev
110     p.lon = FG_Longitude;
111     p.lat = FG_Lat_geocentric;
112     p.radius = FG_Sea_level_radius * FEET_TO_METER;
113
114     cur_zero_elev = fgPolarToCart3d(p);
115
116     cur_zero_elev.x -= scenery.center.x;
117     cur_zero_elev.y -= scenery.center.y;
118     cur_zero_elev.z -= scenery.center.z;
119
120     // calculate view position in current FG view coordinate system
121     // p.lon & p.lat are already defined earlier
122     p.radius = FG_Radius_to_vehicle * FEET_TO_METER + 1.0;
123
124     abs_view_pos = fgPolarToCart3d(p);
125
126     view_pos.x = abs_view_pos.x - scenery.center.x;
127     view_pos.y = abs_view_pos.y - scenery.center.y;
128     view_pos.z = abs_view_pos.z - scenery.center.z;
129
130     fgPrintf( FG_VIEW, FG_DEBUG, "Absolute view pos = %.4f, %.4f, %.4f\n", 
131            abs_view_pos.x, abs_view_pos.y, abs_view_pos.z);
132     fgPrintf( FG_VIEW, FG_DEBUG, "Relative view pos = %.4f, %.4f, %.4f\n", 
133            view_pos.x, view_pos.y, view_pos.z);
134
135     // Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw)
136     // from FG_T_local_to_body[3][3]
137
138     // Question: Why is the LaRCsim matrix arranged so differently
139     // than the one we need???
140     LOCAL[0][0] = FG_T_local_to_body_33;
141     LOCAL[0][1] = -FG_T_local_to_body_32;
142     LOCAL[0][2] = -FG_T_local_to_body_31;
143     LOCAL[0][3] = 0.0;
144     LOCAL[1][0] = -FG_T_local_to_body_23;
145     LOCAL[1][1] = FG_T_local_to_body_22;
146     LOCAL[1][2] = FG_T_local_to_body_21;
147     LOCAL[1][3] = 0.0;
148     LOCAL[2][0] = -FG_T_local_to_body_13;
149     LOCAL[2][1] = FG_T_local_to_body_12;
150     LOCAL[2][2] = FG_T_local_to_body_11;
151     LOCAL[2][3] = 0.0;
152     LOCAL[3][0] = LOCAL[3][1] = LOCAL[3][2] = LOCAL[3][3] = 0.0;
153     LOCAL[3][3] = 1.0;
154     // printf("LaRCsim LOCAL matrix\n");
155     // MAT3print(LOCAL, stdout);
156
157 #ifdef OLD_LOCAL_TO_BODY_CODE
158         // old code to calculate LOCAL matrix calculated from Phi,
159         // Theta, and Psi (roll, pitch, yaw)
160
161         MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
162         MAT3rotate(R, vec, FG_Phi);
163         /* printf("Roll matrix\n"); */
164         /* MAT3print(R, stdout); */
165
166         MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
167         /* MAT3mult_vec(vec, vec, R); */
168         MAT3rotate(TMP, vec, FG_Theta);
169         /* printf("Pitch matrix\n"); */
170         /* MAT3print(TMP, stdout); */
171         MAT3mult(R, R, TMP);
172
173         MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
174         /* MAT3mult_vec(vec, vec, R); */
175         /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
176         MAT3rotate(TMP, vec, -FG_Psi);
177         /* printf("Yaw matrix\n");
178            MAT3print(TMP, stdout); */
179         MAT3mult(LOCAL, R, TMP);
180         // printf("FG derived LOCAL matrix\n");
181         // MAT3print(LOCAL, stdout);
182 #endif // OLD_LOCAL_TO_BODY_CODE
183
184     // Derive the local UP transformation matrix based on *geodetic*
185     // coordinates
186     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
187     MAT3rotate(R, vec, FG_Longitude);     // R = rotate about Z axis
188     // printf("Longitude matrix\n");
189     // MAT3print(R, stdout);
190
191     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
192     MAT3mult_vec(vec, vec, R);
193     MAT3rotate(TMP, vec, -FG_Latitude);  // TMP = rotate about X axis
194     // printf("Latitude matrix\n");
195     // MAT3print(TMP, stdout);
196
197     MAT3mult(UP, R, TMP);
198     // printf("Local up matrix\n");
199     // MAT3print(UP, stdout);
200
201     MAT3_SET_VEC(local_up, 1.0, 0.0, 0.0);
202     MAT3mult_vec(local_up, local_up, UP);
203
204     // printf( "Local Up = (%.4f, %.4f, %.4f)\n",
205     //         local_up[0], local_up[1], local_up[2]);
206     
207     // Alternative method to Derive local up vector based on
208     // *geodetic* coordinates
209     // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
210     // printf( "    Alt Up = (%.4f, %.4f, %.4f)\n", 
211     //         alt_up.x, alt_up.y, alt_up.z);
212
213     // Calculate the VIEW matrix
214     MAT3mult(VIEW, LOCAL, UP);
215     // printf("VIEW matrix\n");
216     // MAT3print(VIEW, stdout);
217
218     // generate the current up, forward, and fwrd-view vectors
219     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
220     MAT3mult_vec(view_up, vec, VIEW);
221
222     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
223     MAT3mult_vec(forward, vec, VIEW);
224     // printf( "Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1], 
225     //         forward[2]);
226
227     MAT3rotate(TMP, view_up, view_offset);
228     MAT3mult_vec(view_forward, forward, TMP);
229
230     // make a vector to the current view position
231     MAT3_SET_VEC(v0, view_pos.x, view_pos.y, view_pos.z);
232
233     // Given a vector pointing straight down (-Z), map into onto the
234     // local plane representing "horizontal".  This should give us the
235     // local direction for moving "south".
236     MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
237     map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
238     MAT3_NORMALIZE_VEC(surface_south, ntmp);
239     // printf( "Surface direction directly south %.2f %.2f %.2f\n",
240     //         surface_south[0], surface_south[1], surface_south[2]);
241
242     // now calculate the surface east vector
243     MAT3rotate(TMP, view_up, FG_PI_2);
244     MAT3mult_vec(surface_east, surface_south, TMP);
245     // printf( "Surface direction directly east %.2f %.2f %.2f\n",
246     //         surface_east[0], surface_east[1], surface_east[2]);
247     // printf( "Should be close to zero = %.2f\n", 
248     //         MAT3_DOT_PRODUCT(surface_south, surface_east));
249 }
250
251
252 // Update the "World to Eye" transformation matrix
253 // This is most useful for view frustum culling
254 void fgVIEW::UpdateWorldToEye( fgFLIGHT *f ) {
255     MAT3mat R_Phi, R_Theta, R_Psi, R_Lat, R_Lon, T_view;
256     MAT3mat TMP;
257     MAT3hvec vec;
258
259     // if we have a view offset use slow way for now
260     if(fabs(view_offset)>FG_EPSILON){ 
261         // Roll Matrix
262         MAT3_SET_HVEC(vec, 0.0, 0.0, -1.0, 1.0);
263         MAT3rotate(R_Phi, vec, FG_Phi);
264         // printf("Roll matrix (Phi)\n");
265         // MAT3print(R_Phi, stdout);
266
267         // Pitch Matrix
268         MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
269         MAT3rotate(R_Theta, vec, FG_Theta);
270         // printf("\nPitch matrix (Theta)\n");
271         // MAT3print(R_Theta, stdout);
272
273         // Yaw Matrix
274         MAT3_SET_HVEC(vec, 0.0, -1.0, 0.0, 1.0);
275         MAT3rotate(R_Psi, vec, FG_Psi + FG_PI - view_offset );
276         // printf("\nYaw matrix (Psi)\n");
277         // MAT3print(R_Psi, stdout);
278
279         // aircraft roll/pitch/yaw
280         MAT3mult(TMP, R_Phi, R_Theta);
281         MAT3mult(AIRCRAFT, TMP, R_Psi);
282
283     } else { // JUST USE LOCAL_TO_BODY  NHV 5/25/98
284         // hey this is even different then LOCAL[][] above ??
285          
286         AIRCRAFT[0][0] = -FG_T_local_to_body_22;
287         AIRCRAFT[0][1] = -FG_T_local_to_body_23;
288         AIRCRAFT[0][2] = FG_T_local_to_body_21;
289         AIRCRAFT[0][3] = 0.0;
290         AIRCRAFT[1][0] = FG_T_local_to_body_32;
291         AIRCRAFT[1][1] = FG_T_local_to_body_33;
292         AIRCRAFT[1][2] = -FG_T_local_to_body_31;
293         AIRCRAFT[1][3] = 0.0;
294         AIRCRAFT[2][0] = FG_T_local_to_body_12;
295         AIRCRAFT[2][1] = FG_T_local_to_body_13;
296         AIRCRAFT[2][2] = -FG_T_local_to_body_11;
297         AIRCRAFT[2][3] = 0.0;
298         AIRCRAFT[3][0] = AIRCRAFT[3][1] = AIRCRAFT[3][2] = AIRCRAFT[3][3] = 0.0;
299         AIRCRAFT[3][3] = 1.0;
300
301         // ??? SOMETHING LIKE THIS SHOULD WORK    NHV
302         // Rotate about LOCAL_UP  (AIRCRAFT[2][])
303         // MAT3_SET_HVEC(vec, AIRCRAFT[2][0], AIRCRAFT[2][1],
304         //                        AIRCRAFT[2][2], AIRCRAFT[2][3]);
305         // MAT3rotate(TMP, vec, FG_PI - view_offset );
306         // MAT3mult(AIRCRAFT, AIRCRAFT, TMP);
307     }
308     // printf("\naircraft roll pitch yaw\n");
309     // MAT3print(AIRCRAFT, stdout);
310
311     // View position in scenery centered coordinates
312     MAT3_SET_HVEC(vec, view_pos.x, view_pos.y, view_pos.z, 1.0);
313     MAT3translate(T_view, vec);
314     // printf("\nTranslation matrix\n");
315     // MAT3print(T_view, stdout);
316
317     // Latitude
318     MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
319     // R_Lat = rotate about X axis
320     MAT3rotate(R_Lat, vec, FG_Latitude);
321     // printf("\nLatitude matrix\n");
322     // MAT3print(R_Lat, stdout);
323
324     // Longitude
325     MAT3_SET_HVEC(vec, 0.0, 0.0, 1.0, 1.0);
326     // R_Lon = rotate about Z axis
327     MAT3rotate(R_Lon, vec, FG_Longitude - FG_PI_2 );
328     // printf("\nLongitude matrix\n");
329     // MAT3print(R_Lon, stdout);
330
331 #ifdef THIS_IS_OLD_CODE
332     // View position in scenery centered coordinates
333     MAT3_SET_HVEC(vec, view_pos.x, view_pos.y, view_pos.z, 1.0);
334     MAT3translate(T_view, vec);
335     // printf("\nTranslation matrix\n");
336     // MAT3print(T_view, stdout);
337
338     // aircraft roll/pitch/yaw
339     MAT3mult(TMP, R_Phi, R_Theta);
340     MAT3mult(AIRCRAFT, TMP, R_Psi);
341     // printf("\naircraft roll pitch yaw\n");
342     // MAT3print(AIRCRAFT, stdout);
343 #endif THIS_IS_OLD_CODE
344
345     // lon/lat
346     MAT3mult(WORLD, R_Lat, R_Lon);
347     // printf("\nworld\n");
348     // MAT3print(WORLD, stdout);
349
350     MAT3mult(EYE_TO_WORLD, AIRCRAFT, WORLD);
351     MAT3mult(EYE_TO_WORLD, EYE_TO_WORLD, T_view);
352     // printf("\nEye to world\n");
353     // MAT3print(EYE_TO_WORLD, stdout);
354
355     MAT3invert(WORLD_TO_EYE, EYE_TO_WORLD);
356     // printf("\nWorld to eye\n");
357     // MAT3print(WORLD_TO_EYE, stdout);
358
359     // printf( "\nview_pos = %.2f %.2f %.2f\n", 
360     //         view_pos.x, view_pos.y, view_pos.z );
361
362     // MAT3_SET_HVEC(eye, 0.0, 0.0, 0.0, 1.0);
363     // MAT3mult_vec(vec, eye, EYE_TO_WORLD);
364     // printf("\neye -> world = %.2f %.2f %.2f\n", vec[0], vec[1], vec[2]);
365
366     // MAT3_SET_HVEC(vec1, view_pos.x, view_pos.y, view_pos.z, 1.0);
367     // MAT3mult_vec(vec, vec1, WORLD_TO_EYE);
368     // printf( "\nabs_view_pos -> eye = %.2f %.2f %.2f\n", 
369     //         vec[0], vec[1], vec[2]);
370 }
371
372
373 // Destructor
374 fgVIEW::~fgVIEW( void ) {
375 }
376
377
378 // $Log$
379 // Revision 1.12  1998/06/03 00:47:15  curt
380 // Updated to compile in audio support if OSS available.
381 // Updated for new version of Steve's audio library.
382 // STL includes don't use .h
383 // Small view optimizations.
384 //
385 // Revision 1.11  1998/05/27 02:24:05  curt
386 // View optimizations by Norman Vine.
387 //
388 // Revision 1.10  1998/05/17 16:59:03  curt
389 // First pass at view frustum culling now operational.
390 //
391 // Revision 1.9  1998/05/16 13:08:37  curt
392 // C++ - ified views.[ch]xx
393 // Shuffled some additional view parameters into the fgVIEW class.
394 // Changed tile-radius to tile-diameter because it is a much better
395 //   name.
396 // Added a WORLD_TO_EYE transformation to views.cxx.  This allows us
397 //  to transform world space to eye space for view frustum culling.
398 //
399 // Revision 1.8  1998/05/02 01:51:01  curt
400 // Updated polartocart conversion routine.
401 //
402 // Revision 1.7  1998/04/30 12:34:20  curt
403 // Added command line rendering options:
404 //   enable/disable fog/haze
405 //   specify smooth/flat shading
406 //   disable sky blending and just use a solid color
407 //   enable wireframe drawing mode
408 //
409 // Revision 1.6  1998/04/28 01:20:23  curt
410 // Type-ified fgTIME and fgVIEW.
411 // Added a command line option to disable textures.
412 //
413 // Revision 1.5  1998/04/26 05:10:04  curt
414 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
415 //
416 // Revision 1.4  1998/04/25 22:04:53  curt
417 // Use already calculated LaRCsim values to create the roll/pitch/yaw
418 // transformation matrix (we call it LOCAL)
419 //
420 // Revision 1.3  1998/04/25 20:24:02  curt
421 // Cleaned up initialization sequence to eliminate interdependencies
422 // between sun position, lighting, and view position.  This creates a
423 // valid single pass initialization path.
424 //
425 // Revision 1.2  1998/04/24 00:49:22  curt
426 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
427 // Trying out some different option parsing code.
428 // Some code reorganization.
429 //
430 // Revision 1.1  1998/04/22 13:25:45  curt
431 // C++ - ifing the code.
432 // Starting a bit of reorganization of lighting code.
433 //
434 // Revision 1.16  1998/04/18 04:11:29  curt
435 // Moved fg_debug to it's own library, added zlib support.
436 //
437 // Revision 1.15  1998/02/20 00:16:24  curt
438 // Thursday's tweaks.
439 //
440 // Revision 1.14  1998/02/09 15:07:50  curt
441 // Minor tweaks.
442 //
443 // Revision 1.13  1998/02/07 15:29:45  curt
444 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
445 // <chotchkiss@namg.us.anritsu.com>
446 //
447 // Revision 1.12  1998/01/29 00:50:28  curt
448 // Added a view record field for absolute x, y, z position.
449 //
450 // Revision 1.11  1998/01/27 00:47:58  curt
451 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
452 // system and commandline/config file processing code.
453 //
454 // Revision 1.10  1998/01/19 19:27:09  curt
455 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
456 // This should simplify things tremendously.
457 //
458 // Revision 1.9  1998/01/13 00:23:09  curt
459 // Initial changes to support loading and management of scenery tiles.  Note,
460 // there's still a fair amount of work left to be done.
461 //
462 // Revision 1.8  1997/12/30 22:22:33  curt
463 // Further integration of event manager.
464 //
465 // Revision 1.7  1997/12/30 20:47:45  curt
466 // Integrated new event manager with subsystem initializations.
467 //
468 // Revision 1.6  1997/12/22 04:14:32  curt
469 // Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
470 //
471 // Revision 1.5  1997/12/18 04:07:02  curt
472 // Worked on properly translating and positioning the sky dome.
473 //
474 // Revision 1.4  1997/12/17 23:13:36  curt
475 // Began working on rendering a sky.
476 //
477 // Revision 1.3  1997/12/15 23:54:50  curt
478 // Add xgl wrappers for debugging.
479 // Generate terrain normals on the fly.
480 //
481 // Revision 1.2  1997/12/10 22:37:48  curt
482 // Prepended "fg" on the name of all global structures that didn't have it yet.
483 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
484 //
485 // Revision 1.1  1997/08/27 21:31:17  curt
486 // Initial revision.
487 //