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