]> git.mxchange.org Git - flightgear.git/blob - Main/views.cxx
651312b9c6bedd03eccd691c1e93fc9da8ef418b
[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 <Aircraft/aircraft.hxx>
31 #include <Cockpit/panel.hxx>
32 #include <Debug/logstream.hxx>
33 #include <Include/fg_constants.h>
34 #include <Math/mat3.h>
35 #include <Math/point3d.hxx>
36 #include <Math/polar3d.hxx>
37 #include <Math/vector.hxx>
38 #include <Scenery/scenery.hxx>
39 #include <Time/fg_time.hxx>
40
41 #include "options.hxx"
42 #include "views.hxx"
43
44
45 // temporary (hopefully) hack
46 static int panel_hist = 0;
47
48
49 // specify code paths ... these are done as variable rather than
50 // #define's because down the road we may want to choose between them
51 // on the fly for different flight models ... this way magic carpet
52 // and external modes wouldn't need to recreate the LaRCsim matrices
53 // themselves.
54
55 static const bool use_larcsim_local_to_body = true;
56
57
58 // This is a record containing current view parameters
59 FGView current_view;
60
61
62 // Constructor
63 FGView::FGView( void ) {
64 }
65
66
67 // Initialize a view structure
68 void FGView::Init( void ) {
69     FG_LOG( FG_VIEW, FG_INFO, "Initializing View parameters" );
70
71     view_offset = 0.0;
72     goal_view_offset = 0.0;
73
74     winWidth = current_options.get_xsize();
75     winHeight = current_options.get_ysize();
76     win_ratio = (double) winWidth / (double) winHeight;
77     force_update_fov_math();
78 }
79
80
81 // Update the field of view coefficients
82 void FGView::UpdateFOV( const fgOPTIONS& o ) {
83     double fov, theta_x, theta_y;
84
85     fov = o.get_fov();
86         
87     // printf("win_ratio = %.2f\n", win_ratio);
88     // calculate sin() and cos() of fov / 2 in X direction;
89     theta_x = (fov * win_ratio * DEG_TO_RAD) / 2.0;
90     // printf("theta_x = %.2f\n", theta_x);
91     sin_fov_x = sin(theta_x);
92     cos_fov_x = cos(theta_x);
93     slope_x =  -cos_fov_x / sin_fov_x;
94     // printf("slope_x = %.2f\n", slope_x);
95
96 #if defined( USE_FAST_FOV_CLIP )
97     fov_x_clip = slope_x*cos_fov_x - sin_fov_x;
98 #endif // defined( USE_FAST_FOV_CLIP )
99
100     // calculate sin() and cos() of fov / 2 in Y direction;
101     theta_y = (fov * DEG_TO_RAD) / 2.0;
102     // printf("theta_y = %.2f\n", theta_y);
103     sin_fov_y = sin(theta_y);
104     cos_fov_y = cos(theta_y);
105     slope_y = cos_fov_y / sin_fov_y;
106     // printf("slope_y = %.2f\n", slope_y);
107
108 #if defined( USE_FAST_FOV_CLIP )
109     fov_y_clip = -(slope_y*cos_fov_y + sin_fov_y);      
110 #endif // defined( USE_FAST_FOV_CLIP )
111 }
112
113
114 // Basically, this is a modified version of the Mesa gluLookAt()
115 // function that's been modified slightly so we can capture the
116 // result before sending it off to OpenGL land.
117 void FGView::LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
118                      GLdouble centerx, GLdouble centery, GLdouble centerz,
119                      GLdouble upx, GLdouble upy, GLdouble upz ) {
120     GLdouble *m;
121     GLdouble x[3], y[3], z[3];
122     GLdouble mag;
123
124     m = current_view.MODEL_VIEW;
125
126     /* Make rotation matrix */
127
128     /* Z vector */
129     z[0] = eyex - centerx;
130     z[1] = eyey - centery;
131     z[2] = eyez - centerz;
132     mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
133     if (mag) {  /* mpichler, 19950515 */
134         z[0] /= mag;
135         z[1] /= mag;
136         z[2] /= mag;
137     }
138
139     /* Y vector */
140     y[0] = upx;
141     y[1] = upy;
142     y[2] = upz;
143
144     /* X vector = Y cross Z */
145     x[0] =  y[1]*z[2] - y[2]*z[1];
146     x[1] = -y[0]*z[2] + y[2]*z[0];
147     x[2] =  y[0]*z[1] - y[1]*z[0];
148     
149     /* Recompute Y = Z cross X */
150     y[0] =  z[1]*x[2] - z[2]*x[1];
151     y[1] = -z[0]*x[2] + z[2]*x[0];
152     y[2] =  z[0]*x[1] - z[1]*x[0];
153
154     /* mpichler, 19950515 */
155     /* cross product gives area of parallelogram, which is < 1.0 for
156      * non-perpendicular unit-length vectors; so normalize x, y here
157      */
158
159     mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
160     if (mag) {
161         x[0] /= mag;
162         x[1] /= mag;
163         x[2] /= mag;
164     }
165
166     mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
167     if (mag) {
168         y[0] /= mag;
169         y[1] /= mag;
170         y[2] /= mag;
171     }
172
173 #define M(row,col)  m[col*4+row]
174     M(0,0) = x[0];  M(0,1) = x[1];  M(0,2) = x[2];  M(0,3) = 0.0;
175     M(1,0) = y[0];  M(1,1) = y[1];  M(1,2) = y[2];  M(1,3) = 0.0;
176     M(2,0) = z[0];  M(2,1) = z[1];  M(2,2) = z[2];  M(2,3) = 0.0;
177     // the following is part of the original gluLookAt(), but we are
178     // commenting it out because we know we are going to be doing a
179     // translation below which will set these values anyways
180     // M(3,0) = 0.0;   M(3,1) = 0.0;   M(3,2) = 0.0;   M(3,3) = 1.0;
181 #undef M
182
183     // Translate Eye to Origin
184     // replaces: glTranslated( -eyex, -eyey, -eyez );
185
186     // this has been slightly modified from the original glTranslate()
187     // code because we know that coming into this m[12] = m[13] =
188     // m[14] = 0.0, and m[15] = 1.0;
189     m[12] = m[0] * -eyex + m[4] * -eyey + m[8]  * -eyez /* + m[12] */;
190     m[13] = m[1] * -eyex + m[5] * -eyey + m[9]  * -eyez /* + m[13] */;
191     m[14] = m[2] * -eyex + m[6] * -eyey + m[10] * -eyez /* + m[14] */;
192     m[15] = 1.0 /* m[3] * -eyex + m[7] * -eyey + m[11] * -eyez + m[15] */;
193
194     // xglMultMatrixd( m );
195     xglLoadMatrixd( m );
196 }
197
198
199 // Update the view volume, position, and orientation
200 void FGView::UpdateViewParams( void ) {
201     FGState *f = current_aircraft.fdm_state;
202
203     UpdateViewMath(f);
204     UpdateWorldToEye(f);
205     
206     if ((current_options.get_panel_status() != panel_hist) &&                          (current_options.get_panel_status()))
207     {
208         fgPanelReInit( 0, 0, 1024, 768);
209     }
210
211     if ( ! current_options.get_panel_status() ) {
212         xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
213     } else {
214         xglViewport(0, (GLint)((winHeight)*0.5768), (GLint)(winWidth), 
215                     (GLint)((winHeight)*0.4232) );
216     }
217
218     // Tell GL we are about to modify the projection parameters
219     xglMatrixMode(GL_PROJECTION);
220     xglLoadIdentity();
221     if ( f->get_Altitude() * FEET_TO_METER - scenery.cur_elev > 10.0 ) {
222         gluPerspective(current_options.get_fov(), win_ratio, 10.0, 100000.0);
223     } else {
224         gluPerspective(current_options.get_fov(), win_ratio, 0.5, 100000.0);
225         // printf("Near ground, minimizing near clip plane\n");
226     }
227     // }
228
229     xglMatrixMode(GL_MODELVIEW);
230     xglLoadIdentity();
231     
232     // set up our view volume (default)
233     LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
234            view_pos.x() + view_forward[0], 
235                view_pos.y() + view_forward[1], 
236                view_pos.z() + view_forward[2],
237                view_up[0], view_up[1], view_up[2]);
238
239     // look almost straight up (testing and eclipse watching)
240     /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
241                view_pos.x() + view_up[0] + .001, 
242                view_pos.y() + view_up[1] + .001, 
243                view_pos.z() + view_up[2] + .001,
244                view_up[0], view_up[1], view_up[2]); */
245
246     // lock view horizontally towards sun (testing)
247     /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
248                view_pos.x() + surface_to_sun[0], 
249                view_pos.y() + surface_to_sun[1], 
250                view_pos.z() + surface_to_sun[2],
251                view_up[0], view_up[1], view_up[2]); */
252
253     // lock view horizontally towards south (testing)
254     /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
255                view_pos.x() + surface_south[0], 
256                view_pos.y() + surface_south[1], 
257                view_pos.z() + surface_south[2],
258                view_up[0], view_up[1], view_up[2]); */
259
260     panel_hist = current_options.get_panel_status();
261 }
262
263
264 // Update the view parameters
265 void FGView::UpdateViewMath( FGState *f ) {
266     Point3D p;
267     MAT3vec vec, forward, v0, minus_z;
268     MAT3mat R, TMP, UP, LOCAL, VIEW;
269     double ntmp;
270
271     if ( update_fov ) {
272         // printf("Updating fov\n");
273         UpdateFOV( current_options );
274         update_fov = false;
275     }
276                 
277     scenery.center = scenery.next_center;
278
279     // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x,
280     //        scenery.center.y, scenery.center.z);
281
282     // calculate the cartesion coords of the current lat/lon/0 elev
283     p = Point3D( f->get_Longitude(), 
284                  f->get_Lat_geocentric(), 
285                  f->get_Sea_level_radius() * FEET_TO_METER );
286
287     cur_zero_elev = fgPolarToCart3d(p) - scenery.center;
288
289     // calculate view position in current FG view coordinate system
290     // p.lon & p.lat are already defined earlier, p.radius was set to
291     // the sea level radius, so now we add in our altitude.
292     if ( f->get_Altitude() * FEET_TO_METER > 
293          (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
294         p.setz( p.radius() + f->get_Altitude() * FEET_TO_METER );
295     } else {
296         p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
297     }
298
299     abs_view_pos = fgPolarToCart3d(p);
300     view_pos = abs_view_pos - scenery.center;
301
302     FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = "
303             << abs_view_pos.x() << ", " 
304             << abs_view_pos.y() << ", " 
305             << abs_view_pos.z() );
306     FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = "
307             << view_pos.x() << ", " << view_pos.y() << ", " << view_pos.z() );
308
309     // Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw)
310     // from FG_T_local_to_body[3][3]
311
312     if ( use_larcsim_local_to_body ) {
313
314         // Question: Why is the LaRCsim matrix arranged so differently
315         // than the one we need???
316
317         // Answer (I think): The LaRCsim matrix is generated in a
318         // different reference frame than we've set up for our world
319
320         LOCAL[0][0] = f->get_T_local_to_body_33();
321         LOCAL[0][1] = -f->get_T_local_to_body_32();
322         LOCAL[0][2] = -f->get_T_local_to_body_31();
323         LOCAL[0][3] = 0.0;
324         LOCAL[1][0] = -f->get_T_local_to_body_23();
325         LOCAL[1][1] = f->get_T_local_to_body_22();
326         LOCAL[1][2] = f->get_T_local_to_body_21();
327         LOCAL[1][3] = 0.0;
328         LOCAL[2][0] = -f->get_T_local_to_body_13();
329         LOCAL[2][1] = f->get_T_local_to_body_12();
330         LOCAL[2][2] = f->get_T_local_to_body_11();
331         LOCAL[2][3] = 0.0;
332         LOCAL[3][0] = LOCAL[3][1] = LOCAL[3][2] = LOCAL[3][3] = 0.0;
333         LOCAL[3][3] = 1.0;
334
335         // printf("LaRCsim LOCAL matrix\n");
336         // MAT3print(LOCAL, stdout);
337
338     } else {
339
340         // code to calculate LOCAL matrix calculated from Phi, Theta, and
341         // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
342         // flight model
343
344         MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
345         MAT3rotate(R, vec, f->get_Phi());
346         /* printf("Roll matrix\n"); */
347         /* MAT3print(R, stdout); */
348
349         MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
350         /* MAT3mult_vec(vec, vec, R); */
351         MAT3rotate(TMP, vec, f->get_Theta());
352         /* printf("Pitch matrix\n"); */
353         /* MAT3print(TMP, stdout); */
354         MAT3mult(R, R, TMP);
355
356         MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
357         /* MAT3mult_vec(vec, vec, R); */
358         /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
359         MAT3rotate(TMP, vec, -f->get_Psi());
360         /* printf("Yaw matrix\n");
361            MAT3print(TMP, stdout); */
362         MAT3mult(LOCAL, R, TMP);
363         // printf("FG derived LOCAL matrix\n");
364         // MAT3print(LOCAL, stdout);
365
366     } // if ( use_larcsim_local_to_body ) 
367
368     // Derive the local UP transformation matrix based on *geodetic*
369     // coordinates
370     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
371     MAT3rotate(R, vec, f->get_Longitude());     // R = rotate about Z axis
372     // printf("Longitude matrix\n");
373     // MAT3print(R, stdout);
374
375     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
376     MAT3mult_vec(vec, vec, R);
377     MAT3rotate(TMP, vec, -f->get_Latitude());  // TMP = rotate about X axis
378     // printf("Latitude matrix\n");
379     // MAT3print(TMP, stdout);
380
381     MAT3mult(UP, R, TMP);
382     // printf("Local up matrix\n");
383     // MAT3print(UP, stdout);
384
385     MAT3_SET_VEC(local_up, 1.0, 0.0, 0.0);
386     MAT3mult_vec(local_up, local_up, UP);
387
388     // printf( "Local Up = (%.4f, %.4f, %.4f)\n",
389     //         local_up[0], local_up[1], local_up[2]);
390     
391     // Alternative method to Derive local up vector based on
392     // *geodetic* coordinates
393     // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
394     // printf( "    Alt Up = (%.4f, %.4f, %.4f)\n", 
395     //         alt_up.x, alt_up.y, alt_up.z);
396
397     // Calculate the VIEW matrix
398     MAT3mult(VIEW, LOCAL, UP);
399     // printf("VIEW matrix\n");
400     // MAT3print(VIEW, stdout);
401
402     // generate the current up, forward, and fwrd-view vectors
403     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
404     MAT3mult_vec(view_up, vec, VIEW);
405
406     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
407     MAT3mult_vec(forward, vec, VIEW);
408     // printf( "Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1], 
409     //         forward[2]);
410
411     MAT3rotate(TMP, view_up, view_offset);
412     MAT3mult_vec(view_forward, forward, TMP);
413
414     // make a vector to the current view position
415     MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
416
417     // Given a vector pointing straight down (-Z), map into onto the
418     // local plane representing "horizontal".  This should give us the
419     // local direction for moving "south".
420     MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
421     map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
422     MAT3_NORMALIZE_VEC(surface_south, ntmp);
423     // printf( "Surface direction directly south %.2f %.2f %.2f\n",
424     //         surface_south[0], surface_south[1], surface_south[2]);
425
426     // now calculate the surface east vector
427     MAT3rotate(TMP, view_up, FG_PI_2);
428     MAT3mult_vec(surface_east, surface_south, TMP);
429     // printf( "Surface direction directly east %.2f %.2f %.2f\n",
430     //         surface_east[0], surface_east[1], surface_east[2]);
431     // printf( "Should be close to zero = %.2f\n", 
432     //         MAT3_DOT_PRODUCT(surface_south, surface_east));
433 }
434
435
436 // Update the "World to Eye" transformation matrix
437 // This is most useful for view frustum culling
438 void FGView::UpdateWorldToEye( FGState *f ) {
439     MAT3mat R_Phi, R_Theta, R_Psi, R_Lat, R_Lon, T_view;
440     MAT3mat TMP;
441     MAT3hvec vec;
442
443     if ( use_larcsim_local_to_body ) {
444
445         // Question: hey this is even different then LOCAL[][] above??
446         // Answer: yet another coordinate system, this time the
447         // coordinate system in which we do our view frustum culling.
448
449         AIRCRAFT[0][0] = -f->get_T_local_to_body_22();
450         AIRCRAFT[0][1] = -f->get_T_local_to_body_23();
451         AIRCRAFT[0][2] = f->get_T_local_to_body_21();
452         AIRCRAFT[0][3] = 0.0;
453         AIRCRAFT[1][0] = f->get_T_local_to_body_32();
454         AIRCRAFT[1][1] = f->get_T_local_to_body_33();
455         AIRCRAFT[1][2] = -f->get_T_local_to_body_31();
456         AIRCRAFT[1][3] = 0.0;
457         AIRCRAFT[2][0] = f->get_T_local_to_body_12();
458         AIRCRAFT[2][1] = f->get_T_local_to_body_13();
459         AIRCRAFT[2][2] = -f->get_T_local_to_body_11();
460         AIRCRAFT[2][3] = 0.0;
461         AIRCRAFT[3][0] = AIRCRAFT[3][1] = AIRCRAFT[3][2] = AIRCRAFT[3][3] = 0.0;
462         AIRCRAFT[3][3] = 1.0;
463
464     } else {
465
466         // Roll Matrix
467         MAT3_SET_HVEC(vec, 0.0, 0.0, -1.0, 1.0);
468         MAT3rotate(R_Phi, vec, f->get_Phi());
469         // printf("Roll matrix (Phi)\n");
470         // MAT3print(R_Phi, stdout);
471
472         // Pitch Matrix
473         MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
474         MAT3rotate(R_Theta, vec, f->get_Theta());
475         // printf("\nPitch matrix (Theta)\n");
476         // MAT3print(R_Theta, stdout);
477
478         // Yaw Matrix
479         MAT3_SET_HVEC(vec, 0.0, -1.0, 0.0, 1.0);
480         MAT3rotate(R_Psi, vec, f->get_Psi() + FG_PI /* - view_offset */ );
481         // MAT3rotate(R_Psi, vec, f->get_Psi() + FG_PI - view_offset );
482         // printf("\nYaw matrix (Psi)\n");
483         // MAT3print(R_Psi, stdout);
484
485         // aircraft roll/pitch/yaw
486         MAT3mult(TMP, R_Phi, R_Theta);
487         MAT3mult(AIRCRAFT, TMP, R_Psi);
488
489     } // if ( use_larcsim_local_to_body )
490
491     // printf("AIRCRAFT matrix\n");
492     // MAT3print(AIRCRAFT, stdout);
493
494     // View rotation matrix relative to current aircraft orientation
495     MAT3_SET_HVEC(vec, 0.0, -1.0, 0.0, 1.0);
496     MAT3mult_vec(vec, vec, AIRCRAFT);
497     // printf("aircraft up vector = %.2f %.2f %.2f\n", 
498     //        vec[0], vec[1], vec[2]);
499     MAT3rotate(TMP, vec, -view_offset );
500     MAT3mult(VIEW_OFFSET, AIRCRAFT, TMP);
501     // printf("VIEW_OFFSET matrix\n");
502     // MAT3print(VIEW_OFFSET, stdout);
503
504     // View position in scenery centered coordinates
505     MAT3_SET_HVEC(vec, view_pos.x(), view_pos.y(), view_pos.z(), 1.0);
506     MAT3translate(T_view, vec);
507     // printf("\nTranslation matrix\n");
508     // MAT3print(T_view, stdout);
509
510     // Latitude
511     MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
512     // R_Lat = rotate about X axis
513     MAT3rotate(R_Lat, vec, f->get_Latitude());
514     // printf("\nLatitude matrix\n");
515     // MAT3print(R_Lat, stdout);
516
517     // Longitude
518     MAT3_SET_HVEC(vec, 0.0, 0.0, 1.0, 1.0);
519     // R_Lon = rotate about Z axis
520     MAT3rotate(R_Lon, vec, f->get_Longitude() - FG_PI_2 );
521     // printf("\nLongitude matrix\n");
522     // MAT3print(R_Lon, stdout);
523
524     // lon/lat
525     MAT3mult(WORLD, R_Lat, R_Lon);
526     // printf("\nworld\n");
527     // MAT3print(WORLD, stdout);
528
529     MAT3mult(EYE_TO_WORLD, VIEW_OFFSET, WORLD);
530     MAT3mult(EYE_TO_WORLD, EYE_TO_WORLD, T_view);
531     // printf("\nEye to world\n");
532     // MAT3print(EYE_TO_WORLD, stdout);
533
534     MAT3invert(WORLD_TO_EYE, EYE_TO_WORLD);
535     // printf("\nWorld to eye\n");
536     // MAT3print(WORLD_TO_EYE, stdout);
537
538     // printf( "\nview_pos = %.2f %.2f %.2f\n", 
539     //         view_pos.x, view_pos.y, view_pos.z );
540
541     // MAT3_SET_HVEC(eye, 0.0, 0.0, 0.0, 1.0);
542     // MAT3mult_vec(vec, eye, EYE_TO_WORLD);
543     // printf("\neye -> world = %.2f %.2f %.2f\n", vec[0], vec[1], vec[2]);
544
545     // MAT3_SET_HVEC(vec1, view_pos.x, view_pos.y, view_pos.z, 1.0);
546     // MAT3mult_vec(vec, vec1, WORLD_TO_EYE);
547     // printf( "\nabs_view_pos -> eye = %.2f %.2f %.2f\n", 
548     //         vec[0], vec[1], vec[2]);
549 }
550
551
552 #if 0
553 // Reject non viewable spheres from current View Frustrum by Curt
554 // Olson curt@me.umn.edu and Norman Vine nhv@yahoo.com with 'gentle
555 // guidance' from Steve Baker sbaker@link.com
556 int
557 FGView::SphereClip( const Point3D& cp, const double radius )
558 {
559     double x1, y1;
560
561     MAT3vec eye;        
562     double *mat;
563     double x, y, z;
564
565     x = cp->x;
566     y = cp->y;
567     z = cp->z;
568         
569     mat = (double *)(WORLD_TO_EYE);
570         
571     eye[2] =  x*mat[2] + y*mat[6] + z*mat[10] + mat[14];
572         
573     // Check near and far clip plane
574     if( ( eye[2] > radius ) ||
575         ( eye[2] + radius + current_weather.visibility < 0) )
576         // ( eye[2] + radius + far_plane < 0) )
577     {
578         return 1;
579     }
580         
581     // check right and left clip plane (from eye perspective)
582     x1 = radius * fov_x_clip;
583     eye[0] = (x*mat[0] + y*mat[4] + z*mat[8] + mat[12]) * slope_x;
584     if( (eye[2] > -(eye[0]+x1)) || (eye[2] > (eye[0]-x1)) ) {
585         return(1);
586     }
587         
588     // check bottom and top clip plane (from eye perspective)
589     y1 = radius * fov_y_clip;
590     eye[1] = (x*mat[1] + y*mat[5] + z*mat[9] + mat[13]) * slope_y; 
591     if( (eye[2] > -(eye[1]+y1)) || (eye[2] > (eye[1]-y1)) ) {
592         return 1;
593     }
594
595     return 0;
596 }
597 #endif
598
599
600 // Destructor
601 FGView::~FGView( void ) {
602 }
603
604
605 // $Log$
606 // Revision 1.32  1999/01/07 20:25:12  curt
607 // Updated struct fgGENERAL to class FGGeneral.
608 //
609 // Revision 1.31  1998/12/11 20:26:28  curt
610 // Fixed view frustum culling accuracy bug so we can look out the sides and
611 // back without tri-stripes dropping out.
612 //
613 // Revision 1.30  1998/12/09 18:50:28  curt
614 // Converted "class fgVIEW" to "class FGView" and updated to make data
615 // members private and make required accessor functions.
616 //
617 // Revision 1.29  1998/12/05 15:54:24  curt
618 // Renamed class fgFLIGHT to class FGState as per request by JSB.
619 //
620 // Revision 1.28  1998/12/03 01:17:20  curt
621 // Converted fgFLIGHT to a class.
622 //
623 // Revision 1.27  1998/11/16 14:00:06  curt
624 // Added pow() macro bug work around.
625 // Added support for starting FGFS at various resolutions.
626 // Added some initial serial port support.
627 // Specify default log levels in main().
628 //
629 // Revision 1.26  1998/11/09 23:39:25  curt
630 // Tweaks for the instrument panel.
631 //
632 // Revision 1.25  1998/11/06 21:18:15  curt
633 // Converted to new logstream debugging facility.  This allows release
634 // builds with no messages at all (and no performance impact) by using
635 // the -DFG_NDEBUG flag.
636 //
637 // Revision 1.24  1998/10/18 01:17:19  curt
638 // Point3D tweaks.
639 //
640 // Revision 1.23  1998/10/17 01:34:26  curt
641 // C++ ifying ...
642 //
643 // Revision 1.22  1998/10/16 00:54:03  curt
644 // Converted to Point3D class.
645 //
646 // Revision 1.21  1998/09/17 18:35:33  curt
647 // Added F8 to toggle fog and F9 to toggle texturing.
648 //
649 // Revision 1.20  1998/09/08 15:04:35  curt
650 // Optimizations by Norman Vine.
651 //
652 // Revision 1.19  1998/08/20 20:32:34  curt
653 // Reshuffled some of the code in and around views.[ch]xx
654 //
655 // Revision 1.18  1998/07/24 21:57:02  curt
656 // Set near clip plane to 0.5 meters when close to the ground.  Also, let the view get a bit closer to the ground before hitting the hard limit.
657 //
658 // Revision 1.17  1998/07/24 21:39:12  curt
659 // Debugging output tweaks.
660 // Cast glGetString to (char *) to avoid compiler errors.
661 // Optimizations to fgGluLookAt() by Norman Vine.
662 //
663 // Revision 1.16  1998/07/13 21:01:41  curt
664 // Wrote access functions for current fgOPTIONS.
665 //
666 // Revision 1.15  1998/07/12 03:14:43  curt
667 // Added ground collision detection.
668 // Did some serious horsing around to be able to "hug" the ground properly
669 //   and still be able to take off.
670 // Set the near clip plane to 1.0 meters when less than 10 meters above the
671 //   ground.
672 // Did some serious horsing around getting the initial airplane position to be
673 //   correct based on rendered terrain elevation.
674 // Added a little cheat/hack that will prevent the view position from ever
675 //   dropping below the terrain, even when the flight model doesn't quite
676 //   put you as high as you'd like.
677 //
678 // Revision 1.14  1998/07/08 14:45:08  curt
679 // polar3d.h renamed to polar3d.hxx
680 // vector.h renamed to vector.hxx
681 // updated audio support so it waits to create audio classes (and tie up
682 //   /dev/dsp) until the mpg123 player is finished.
683 //
684 // Revision 1.13  1998/07/04 00:52:27  curt
685 // Add my own version of gluLookAt() (which is nearly identical to the
686 // Mesa/glu version.)  But, by calculating the Model View matrix our selves
687 // we can save this matrix without having to read it back in from the video
688 // card.  This hopefully allows us to save a few cpu cycles when rendering
689 // out the fragments because we can just use glLoadMatrixd() with the
690 // precalculated matrix for each tile rather than doing a push(), translate(),
691 // pop() for every fragment.
692 //
693 // Panel status defaults to off for now until it gets a bit more developed.
694 //
695 // Extract OpenGL driver info on initialization.
696 //
697 // Revision 1.12  1998/06/03 00:47:15  curt
698 // Updated to compile in audio support if OSS available.
699 // Updated for new version of Steve's audio library.
700 // STL includes don't use .h
701 // Small view optimizations.
702 //
703 // Revision 1.11  1998/05/27 02:24:05  curt
704 // View optimizations by Norman Vine.
705 //
706 // Revision 1.10  1998/05/17 16:59:03  curt
707 // First pass at view frustum culling now operational.
708 //
709 // Revision 1.9  1998/05/16 13:08:37  curt
710 // C++ - ified views.[ch]xx
711 // Shuffled some additional view parameters into the fgVIEW class.
712 // Changed tile-radius to tile-diameter because it is a much better
713 //   name.
714 // Added a WORLD_TO_EYE transformation to views.cxx.  This allows us
715 //  to transform world space to eye space for view frustum culling.
716 //
717 // Revision 1.8  1998/05/02 01:51:01  curt
718 // Updated polartocart conversion routine.
719 //
720 // Revision 1.7  1998/04/30 12:34:20  curt
721 // Added command line rendering options:
722 //   enable/disable fog/haze
723 //   specify smooth/flat shading
724 //   disable sky blending and just use a solid color
725 //   enable wireframe drawing mode
726 //
727 // Revision 1.6  1998/04/28 01:20:23  curt
728 // Type-ified fgTIME and fgVIEW.
729 // Added a command line option to disable textures.
730 //
731 // Revision 1.5  1998/04/26 05:10:04  curt
732 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
733 //
734 // Revision 1.4  1998/04/25 22:04:53  curt
735 // Use already calculated LaRCsim values to create the roll/pitch/yaw
736 // transformation matrix (we call it LOCAL)
737 //
738 // Revision 1.3  1998/04/25 20:24:02  curt
739 // Cleaned up initialization sequence to eliminate interdependencies
740 // between sun position, lighting, and view position.  This creates a
741 // valid single pass initialization path.
742 //
743 // Revision 1.2  1998/04/24 00:49:22  curt
744 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
745 // Trying out some different option parsing code.
746 // Some code reorganization.
747 //
748 // Revision 1.1  1998/04/22 13:25:45  curt
749 // C++ - ifing the code.
750 // Starting a bit of reorganization of lighting code.
751 //
752 // Revision 1.16  1998/04/18 04:11:29  curt
753 // Moved fg_debug to it's own library, added zlib support.
754 //
755 // Revision 1.15  1998/02/20 00:16:24  curt
756 // Thursday's tweaks.
757 //
758 // Revision 1.14  1998/02/09 15:07:50  curt
759 // Minor tweaks.
760 //
761 // Revision 1.13  1998/02/07 15:29:45  curt
762 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
763 // <chotchkiss@namg.us.anritsu.com>
764 //
765 // Revision 1.12  1998/01/29 00:50:28  curt
766 // Added a view record field for absolute x, y, z position.
767 //
768 // Revision 1.11  1998/01/27 00:47:58  curt
769 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
770 // system and commandline/config file processing code.
771 //
772 // Revision 1.10  1998/01/19 19:27:09  curt
773 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
774 // This should simplify things tremendously.
775 //
776 // Revision 1.9  1998/01/13 00:23:09  curt
777 // Initial changes to support loading and management of scenery tiles.  Note,
778 // there's still a fair amount of work left to be done.
779 //
780 // Revision 1.8  1997/12/30 22:22:33  curt
781 // Further integration of event manager.
782 //
783 // Revision 1.7  1997/12/30 20:47:45  curt
784 // Integrated new event manager with subsystem initializations.
785 //
786 // Revision 1.6  1997/12/22 04:14:32  curt
787 // Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
788 //
789 // Revision 1.5  1997/12/18 04:07:02  curt
790 // Worked on properly translating and positioning the sky dome.
791 //
792 // Revision 1.4  1997/12/17 23:13:36  curt
793 // Began working on rendering a sky.
794 //
795 // Revision 1.3  1997/12/15 23:54:50  curt
796 // Add xgl wrappers for debugging.
797 // Generate terrain normals on the fly.
798 //
799 // Revision 1.2  1997/12/10 22:37:48  curt
800 // Prepended "fg" on the name of all global structures that didn't have it yet.
801 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
802 //
803 // Revision 1.1  1997/08/27 21:31:17  curt
804 // Initial revision.
805 //