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