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