1 // views.cxx -- data structures and routines for managing and view
4 // Written by Curtis Olson, started August 1997.
6 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
29 #include <ssg.h> // plib include
31 #include <Aircraft/aircraft.hxx>
32 #include <Cockpit/panel.hxx>
33 #include <Debug/logstream.hxx>
34 #include <Include/fg_constants.h>
35 #include <Math/mat3.h>
36 #include <Math/point3d.hxx>
37 #include <Math/polar3d.hxx>
38 #include <Math/vector.hxx>
39 #include <Scenery/scenery.hxx>
40 #include <Time/fg_time.hxx>
42 #include "options.hxx"
46 // Define following to extract various vectors directly
47 // from matrices we have allready computed
48 // rather then performing 'textbook algebra' to rederive them
49 // Norman Vine -- nhv@yahoo.com
50 // #define FG_VIEW_INLINE_OPTIMIZATIONS
52 // temporary (hopefully) hack
53 static int panel_hist = 0;
56 // specify code paths ... these are done as variable rather than
57 // #define's because down the road we may want to choose between them
58 // on the fly for different flight models ... this way magic carpet
59 // and external modes wouldn't need to recreate the LaRCsim matrices
62 static const bool use_larcsim_local_to_body = false;
65 // This is a record containing current view parameters
70 FGView::FGView( void ) {
75 // Initialize a view structure
76 void FGView::Init( void ) {
77 FG_LOG( FG_VIEW, FG_INFO, "Initializing View parameters" );
80 goal_view_offset = 0.0;
82 winWidth = current_options.get_xsize();
83 winHeight = current_options.get_ysize();
85 if ( ! current_options.get_panel_status() ) {
86 current_view.set_win_ratio( (GLfloat) winWidth / (GLfloat) winHeight );
88 current_view.set_win_ratio( (GLfloat) winWidth /
89 ((GLfloat) (winHeight)*0.4232) );
92 force_update_fov_math();
96 // Update the field of view coefficients
97 void FGView::UpdateFOV( const fgOPTIONS& o ) {
98 ssgSetFOV( o.get_fov(), 0.0 );
100 double fov, theta_x, theta_y;
104 // printf("win_ratio = %.2f\n", win_ratio);
105 // calculate sin() and cos() of fov / 2 in X direction;
106 theta_x = (fov * win_ratio * DEG_TO_RAD) / 2.0;
107 // printf("theta_x = %.2f\n", theta_x);
108 sin_fov_x = sin(theta_x);
109 cos_fov_x = cos(theta_x);
110 slope_x = -cos_fov_x / sin_fov_x;
111 // printf("slope_x = %.2f\n", slope_x);
113 // fov_x_clip and fov_y_clip convoluted algebraic simplification
114 // see code executed in tilemgr.cxx when USE_FAST_FOV_CLIP not
115 // defined Norman Vine -- nhv@yahoo.com
116 #if defined( USE_FAST_FOV_CLIP )
117 fov_x_clip = slope_x*cos_fov_x - sin_fov_x;
118 #endif // defined( USE_FAST_FOV_CLIP )
120 // calculate sin() and cos() of fov / 2 in Y direction;
121 theta_y = (fov * DEG_TO_RAD) / 2.0;
122 // printf("theta_y = %.2f\n", theta_y);
123 sin_fov_y = sin(theta_y);
124 cos_fov_y = cos(theta_y);
125 slope_y = cos_fov_y / sin_fov_y;
126 // printf("slope_y = %.2f\n", slope_y);
128 #if defined( USE_FAST_FOV_CLIP )
129 fov_y_clip = -(slope_y*cos_fov_y + sin_fov_y);
130 #endif // defined( USE_FAST_FOV_CLIP )
134 // Basically, this is a modified version of the Mesa gluLookAt()
135 // function that's been modified slightly so we can capture the
136 // result before sending it off to OpenGL land.
137 void FGView::LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
138 GLdouble centerx, GLdouble centery, GLdouble centerz,
139 GLdouble upx, GLdouble upy, GLdouble upz ) {
141 GLdouble x[3], y[3], z[3];
144 m = current_view.MODEL_VIEW;
146 /* Make rotation matrix */
149 z[0] = eyex - centerx;
150 z[1] = eyey - centery;
151 z[2] = eyez - centerz;
152 mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
153 if (mag) { /* mpichler, 19950515 */
164 /* X vector = Y cross Z */
165 x[0] = y[1]*z[2] - y[2]*z[1];
166 x[1] = -y[0]*z[2] + y[2]*z[0];
167 x[2] = y[0]*z[1] - y[1]*z[0];
169 /* Recompute Y = Z cross X */
170 y[0] = z[1]*x[2] - z[2]*x[1];
171 y[1] = -z[0]*x[2] + z[2]*x[0];
172 y[2] = z[0]*x[1] - z[1]*x[0];
174 /* mpichler, 19950515 */
175 /* cross product gives area of parallelogram, which is < 1.0 for
176 * non-perpendicular unit-length vectors; so normalize x, y here
179 mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
186 mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
193 #define M(row,col) m[col*4+row]
194 M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = 0.0;
195 M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = 0.0;
196 M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = 0.0;
197 // the following is part of the original gluLookAt(), but we are
198 // commenting it out because we know we are going to be doing a
199 // translation below which will set these values anyways
200 // M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0;
203 // Translate Eye to Origin
204 // replaces: glTranslated( -eyex, -eyey, -eyez );
206 // this has been slightly modified from the original glTranslate()
207 // code because we know that coming into this m[12] = m[13] =
208 // m[14] = 0.0, and m[15] = 1.0;
209 m[12] = m[0] * -eyex + m[4] * -eyey + m[8] * -eyez /* + m[12] */;
210 m[13] = m[1] * -eyex + m[5] * -eyey + m[9] * -eyez /* + m[13] */;
211 m[14] = m[2] * -eyex + m[6] * -eyey + m[10] * -eyez /* + m[14] */;
212 m[15] = 1.0 /* m[3] * -eyex + m[7] * -eyey + m[11] * -eyez + m[15] */;
214 // xglMultMatrixd( m );
219 // Update the view volume, position, and orientation
220 void FGView::UpdateViewParams( void ) {
221 FGInterface *f = current_aircraft.fdm_state;
226 if ((current_options.get_panel_status() != panel_hist) && (current_options.get_panel_status()))
228 FGPanel::OurPanel->ReInit( 0, 0, 1024, 768);
231 if ( ! current_options.get_panel_status() ) {
232 xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
234 xglViewport(0, (GLint)((winHeight)*0.5768), (GLint)(winWidth),
235 (GLint)((winHeight)*0.4232) );
238 // Tell GL we are about to modify the projection parameters
239 xglMatrixMode(GL_PROJECTION);
241 if ( f->get_Altitude() * FEET_TO_METER - scenery.cur_elev > 10.0 ) {
242 ssgSetNearFar( 10.0, 100000.0 );
243 gluPerspective(current_options.get_fov(), win_ratio, 10.0, 100000.0);
245 ssgSetNearFar( 0.5, 100000.0 );
246 gluPerspective(current_options.get_fov(), win_ratio, 0.5, 100000.0);
247 // printf("Near ground, minimizing near clip plane\n");
251 xglMatrixMode(GL_MODELVIEW);
254 // set up our view volume (default)
255 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
256 LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
257 view_pos.x() + view_forward[0],
258 view_pos.y() + view_forward[1],
259 view_pos.z() + view_forward[2],
260 view_up[0], view_up[1], view_up[2]);
262 // look almost straight up (testing and eclipse watching)
263 /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
264 view_pos.x() + view_up[0] + .001,
265 view_pos.y() + view_up[1] + .001,
266 view_pos.z() + view_up[2] + .001,
267 view_up[0], view_up[1], view_up[2]); */
269 // lock view horizontally towards sun (testing)
270 /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
271 view_pos.x() + surface_to_sun[0],
272 view_pos.y() + surface_to_sun[1],
273 view_pos.z() + surface_to_sun[2],
274 view_up[0], view_up[1], view_up[2]); */
276 // lock view horizontally towards south (testing)
277 /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
278 view_pos.x() + surface_south[0],
279 view_pos.y() + surface_south[1],
280 view_pos.z() + surface_south[2],
281 view_up[0], view_up[1], view_up[2]); */
283 #else // defined(FG_VIEW_INLINE_OPTIMIZATIONS)
284 //void FGView::LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
285 // GLdouble centerx, GLdouble centery, GLdouble centerz,
286 // GLdouble upx, GLdouble upy, GLdouble upz )
289 GLdouble x[3], y[3], z[3];
292 m = current_view.MODEL_VIEW;
294 /* Make rotation matrix */
297 z[0] = -view_forward[0]; //eyex - centerx;
298 z[1] = -view_forward[1]; //eyey - centery;
299 z[2] = -view_forward[2]; //eyez - centerz;
301 // In our case this is a unit vector NHV
303 // mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
304 // if (mag) { /* mpichler, 19950515 */
306 // printf("mag(%f) ", mag);
313 y[0] = view_up[0]; //upx;
314 y[1] = view_up[1]; //upy;
315 y[2] = view_up[2]; //upz;
317 /* X vector = Y cross Z */
318 x[0] = y[1]*z[2] - y[2]*z[1];
319 x[1] = -y[0]*z[2] + y[2]*z[0];
320 x[2] = y[0]*z[1] - y[1]*z[0];
322 // printf(" %f %f %f ", y[0], y[1], y[2]);
324 /* Recompute Y = Z cross X */
325 // y[0] = z[1]*x[2] - z[2]*x[1];
326 // y[1] = -z[0]*x[2] + z[2]*x[0];
327 // y[2] = z[0]*x[1] - z[1]*x[0];
329 // printf(" %f %f %f\n", y[0], y[1], y[2]);
331 // In our case these are unit vectors NHV
333 /* mpichler, 19950515 */
334 /* cross product gives area of parallelogram, which is < 1.0 for
335 * non-perpendicular unit-length vectors; so normalize x, y here
338 // mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
341 // printf("mag2(%f) ", mag);
347 // mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
350 // printf("mag3(%f)\n", mag);
356 #define M(row,col) m[col*4+row]
357 M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = 0.0;
358 M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = 0.0;
359 M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = 0.0;
360 // the following is part of the original gluLookAt(), but we are
361 // commenting it out because we know we are going to be doing a
362 // translation below which will set these values anyways
363 // M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0;
366 // Translate Eye to Origin
367 // replaces: glTranslated( -eyex, -eyey, -eyez );
369 // this has been slightly modified from the original glTranslate()
370 // code because we know that coming into this m[12] = m[13] =
371 // m[14] = 0.0, and m[15] = 1.0;
372 m[12] = m[0] * -view_pos.x() + m[4] * -view_pos.y() + m[8] * -view_pos.z() /* + m[12] */;
373 m[13] = m[1] * -view_pos.x() + m[5] * -view_pos.y() + m[9] * -view_pos.z() /* + m[13] */;
374 m[14] = m[2] * -view_pos.x() + m[6] * -view_pos.y() + m[10] * -view_pos.z() /* + m[14] */;
375 m[15] = 1.0 /* m[3] * -view_pos.x() + m[7] * -view_pos.y() + m[11] * -view_pos.z() + m[15] */;
377 // xglMultMatrixd( m );
380 #endif // FG_VIEW_INLINE_OPTIMIZATIONS
383 panel_hist = current_options.get_panel_status();
387 void getRotMatrix(double* out, MAT3vec vec, double radians)
389 /* This function contributed by Erich Boleyn (erich@uruk.org) */
390 /* This function used from the Mesa OpenGL code (matrix.c) */
392 double vx, vy, vz, xy, yz, zx, xs, ys, zs, one_c; //, xx, yy, zz
398 // mag = getMagnitude();
404 #define M(row,col) out[row*4 + col]
407 * Arbitrary axis rotation matrix.
409 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
410 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
411 * (which is about the X-axis), and the two composite transforms
412 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
413 * from the arbitrary axis to the X-axis then back. They are
414 * all elementary rotations.
416 * Rz' is a rotation about the Z-axis, to bring the axis vector
417 * into the x-z plane. Then Ry' is applied, rotating about the
418 * Y-axis to bring the axis vector parallel with the X-axis. The
419 * rotation about the X-axis is then performed. Ry and Rz are
420 * simply the respective inverse transforms to bring the arbitrary
421 * axis back to it's original orientation. The first transforms
422 * Rz' and Ry' are considered inverses, since the data from the
423 * arbitrary axis gives you info on how to get to it, not how
424 * to get away from it, and an inverse must be applied.
426 * The basic calculation used is to recognize that the arbitrary
427 * axis vector (x, y, z), since it is of unit length, actually
428 * represents the sines and cosines of the angles to rotate the
429 * X-axis to the same orientation, with theta being the angle about
430 * Z and phi the angle about Y (in the order described above)
433 * cos ( theta ) = x / sqrt ( 1 - z^2 )
434 * sin ( theta ) = y / sqrt ( 1 - z^2 )
436 * cos ( phi ) = sqrt ( 1 - z^2 )
439 * Note that cos ( phi ) can further be inserted to the above
442 * cos ( theta ) = x / cos ( phi )
443 * sin ( theta ) = y / cos ( phi )
445 * ...etc. Because of those relations and the standard trigonometric
446 * relations, it is pssible to reduce the transforms down to what
447 * is used below. It may be that any primary axis chosen will give the
448 * same results (modulo a sign convention) using thie method.
450 * Particularly nice is to notice that all divisions that might
451 * have caused trouble when parallel to certain planes or
452 * axis go away with care paid to reducing the expressions.
453 * After checking, it does perform correctly under all cases, since
454 * in all the cases of division where the denominator would have
455 * been zero, the numerator would have been zero as well, giving
456 * the expected result.
470 M(0,0) = (one_c * vx * vx) + c;
472 yz = vy * vz * one_c;
476 M(1,1) = (one_c * vy * vy) + c;
478 zx = vz * vx * one_c;
482 M(2,2) = (one_c * vz *vz) + c;
484 xy = vx * vy * one_c;
488 // M(0,0) = (one_c * xx) + c;
489 // M(1,0) = (one_c * xy) - zs;
490 // M(2,0) = (one_c * zx) + ys;
492 // M(0,1) = (one_c * xy) + zs;
493 // M(1,1) = (one_c * yy) + c;
494 // M(2,1) = (one_c * yz) - xs;
496 // M(0,2) = (one_c * zx) - ys;
497 // M(1,2) = (one_c * yz) + xs;
498 // M(2,2) = (one_c * zz) + c;
504 // Update the view parameters
505 void FGView::UpdateViewMath( FGInterface *f ) {
507 MAT3vec vec, forward, v0, minus_z;
508 MAT3mat R, TMP, UP, LOCAL, VIEW;
511 sgMat4 sgLOCAL, sgUP, sgVIEW;
514 // printf("Updating fov\n");
515 UpdateFOV( current_options );
519 scenery.center = scenery.next_center;
521 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
522 // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x,
523 // scenery.center.y, scenery.center.z);
525 // calculate the cartesion coords of the current lat/lon/0 elev
526 p = Point3D( f->get_Longitude(),
527 f->get_Lat_geocentric(),
528 f->get_Sea_level_radius() * FEET_TO_METER );
530 cur_zero_elev = fgPolarToCart3d(p) - scenery.center;
532 // calculate view position in current FG view coordinate system
533 // p.lon & p.lat are already defined earlier, p.radius was set to
534 // the sea level radius, so now we add in our altitude.
535 if ( f->get_Altitude() * FEET_TO_METER >
536 (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
537 p.setz( p.radius() + f->get_Altitude() * FEET_TO_METER );
539 p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
542 abs_view_pos = fgPolarToCart3d(p);
544 #else // FG_VIEW_INLINE_OPTIMIZATIONS
546 double tmp_radius = f->get_Sea_level_radius() * FEET_TO_METER;
547 double tmp = f->get_cos_lat_geocentric() * tmp_radius;
549 cur_zero_elev.setx(f->get_cos_longitude()*tmp - scenery.center.x());
550 cur_zero_elev.sety(f->get_sin_longitude()*tmp - scenery.center.y());
551 cur_zero_elev.setz(f->get_sin_lat_geocentric()*tmp_radius - scenery.center.z());
553 // calculate view position in current FG view coordinate system
554 // p.lon & p.lat are already defined earlier, p.radius was set to
555 // the sea level radius, so now we add in our altitude.
556 if ( f->get_Altitude() * FEET_TO_METER >
557 (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
558 tmp_radius += f->get_Altitude() * FEET_TO_METER;
560 tmp_radius += scenery.cur_elev + 0.5 * METER_TO_FEET ;
562 tmp = f->get_cos_lat_geocentric() * tmp_radius;
563 abs_view_pos.setx(f->get_cos_longitude()*tmp);
564 abs_view_pos.sety(f->get_sin_longitude()*tmp);
565 abs_view_pos.setz(f->get_sin_lat_geocentric()*tmp_radius);
567 #endif // FG_VIEW_INLINE_OPTIMIZATIONS
569 view_pos = abs_view_pos - scenery.center;
571 FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
572 FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
573 FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = " << view_pos );
575 // Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw)
576 // from FG_T_local_to_body[3][3]
578 if ( use_larcsim_local_to_body ) {
580 // Question: Why is the LaRCsim matrix arranged so differently
581 // than the one we need???
583 // Answer (I think): The LaRCsim matrix is generated in a
584 // different reference frame than we've set up for our world
586 LOCAL[0][0] = f->get_T_local_to_body_33();
587 LOCAL[0][1] = -f->get_T_local_to_body_32();
588 LOCAL[0][2] = -f->get_T_local_to_body_31();
590 LOCAL[1][0] = -f->get_T_local_to_body_23();
591 LOCAL[1][1] = f->get_T_local_to_body_22();
592 LOCAL[1][2] = f->get_T_local_to_body_21();
594 LOCAL[2][0] = -f->get_T_local_to_body_13();
595 LOCAL[2][1] = f->get_T_local_to_body_12();
596 LOCAL[2][2] = f->get_T_local_to_body_11();
598 LOCAL[3][0] = LOCAL[3][1] = LOCAL[3][2] = LOCAL[3][3] = 0.0;
601 // printf("LaRCsim LOCAL matrix\n");
602 // MAT3print(LOCAL, stdout);
606 // code to calculate LOCAL matrix calculated from Phi, Theta, and
607 // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
610 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
611 MAT3rotate(R, vec, f->get_Phi());
612 /* printf("Roll matrix\n"); */
613 /* MAT3print(R, stdout); */
615 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
616 /* MAT3mult_vec(vec, vec, R); */
617 MAT3rotate(TMP, vec, f->get_Theta());
618 /* printf("Pitch matrix\n"); */
619 /* MAT3print(TMP, stdout); */
622 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
623 /* MAT3mult_vec(vec, vec, R); */
624 /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
625 MAT3rotate(TMP, vec, -f->get_Psi());
626 /* printf("Yaw matrix\n");
627 MAT3print(TMP, stdout); */
628 MAT3mult(LOCAL, R, TMP);
629 // cout << "FG derived LOCAL matrix using MAT3 routines" << endl;
630 // MAT3print(LOCAL, stdout);
632 sgMakeRotMat4( sgLOCAL,
633 f->get_Psi() * RAD_TO_DEG,
634 f->get_Phi() * RAD_TO_DEG,
635 f->get_Theta() * RAD_TO_DEG );
637 cout << "FG derived LOCAL matrix using sg routines" << endl;
641 for ( i = 0; i < 4; i++ ) {
642 for ( j = 0; j < 4; j++ ) {
643 print[i][j] = sgLOCAL[i][j];
646 MAT3print( print, stdout);
649 sgMakeIdentMat4( sgIDENT );
650 for ( i = 0; i < 4; i++ ) {
651 for ( j = 0; j < 4; j++ ) {
652 print[i][j] = sgIDENT[i][j];
655 MAT3print( print, stdout);
657 } // if ( use_larcsim_local_to_body )
659 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
661 // Derive the local UP transformation matrix based on *geodetic*
663 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
664 MAT3rotate(R, vec, f->get_Longitude()); // R = rotate about Z axis
665 // printf("Longitude matrix\n");
666 // MAT3print(R, stdout);
668 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
669 MAT3mult_vec(vec, vec, R);
670 MAT3rotate(TMP, vec, -f->get_Latitude()); // TMP = rotate about X axis
671 // printf("Latitude matrix\n");
672 // MAT3print(TMP, stdout);
674 MAT3mult(UP, R, TMP);
675 // cout << "Local up matrix" << endl;;
676 // MAT3print(UP, stdout);
679 f->get_Longitude() * RAD_TO_DEG,
681 -f->get_Latitude() * RAD_TO_DEG );
683 cout << "FG derived UP matrix using sg routines" << endl;
687 for ( i = 0; i < 4; i++ ) {
688 for ( j = 0; j < 4; j++ ) {
689 print[i][j] = sgUP[i][j];
692 MAT3print( print, stdout);
695 MAT3_SET_VEC(local_up, 1.0, 0.0, 0.0);
696 MAT3mult_vec(local_up, local_up, UP);
698 // printf( "Local Up = (%.4f, %.4f, %.4f)\n",
699 // local_up[0], local_up[1], local_up[2]);
701 // Alternative method to Derive local up vector based on
702 // *geodetic* coordinates
703 // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
704 // printf( " Alt Up = (%.4f, %.4f, %.4f)\n",
705 // alt_up.x, alt_up.y, alt_up.z);
707 // Calculate the VIEW matrix
708 MAT3mult(VIEW, LOCAL, UP);
709 cout << "VIEW matrix" << endl;;
710 MAT3print(VIEW, stdout);
712 sgMultMat4( sgVIEW, sgLOCAL, sgUP );
713 cout << "FG derived VIEW matrix using sg routines" << endl;
717 for ( i = 0; i < 4; i++ ) {
718 for ( j = 0; j < 4; j++ ) {
719 print[i][j] = sgVIEW[i][j];
722 MAT3print( print, stdout);
724 // generate the current up, forward, and fwrd-view vectors
725 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
726 MAT3mult_vec(view_up, vec, VIEW);
728 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
729 MAT3mult_vec(forward, vec, VIEW);
730 // printf( "Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1],
733 MAT3rotate(TMP, view_up, view_offset);
734 MAT3mult_vec(view_forward, forward, TMP);
736 // make a vector to the current view position
737 MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
739 // Given a vector pointing straight down (-Z), map into onto the
740 // local plane representing "horizontal". This should give us the
741 // local direction for moving "south".
742 MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
743 map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
744 MAT3_NORMALIZE_VEC(surface_south, ntmp);
745 // printf( "Surface direction directly south %.2f %.2f %.2f\n",
746 // surface_south[0], surface_south[1], surface_south[2]);
748 // now calculate the surface east vector
749 MAT3rotate(TMP, view_up, FG_PI_2);
750 MAT3mult_vec(surface_east, surface_south, TMP);
751 // printf( "Surface direction directly east %.2f %.2f %.2f\n",
752 // surface_east[0], surface_east[1], surface_east[2]);
753 // printf( "Should be close to zero = %.2f\n",
754 // MAT3_DOT_PRODUCT(surface_south, surface_east));
756 #else // FG_VIEW_INLINE_OPTIMIZATIONS
758 // // Build spherical to cartesian transform matrix directly
759 double cos_lat = f->get_cos_latitude(); // cos(-f->get_Latitude());
760 double sin_lat = -f->get_sin_latitude(); // sin(-f->get_Latitude());
761 double cos_lon = f->get_cos_longitude(); //cos(f->get_Longitude());
762 double sin_lon = f->get_sin_longitude(); //sin(f->get_Longitude());
764 double *mat = (double *)UP;
766 mat[0] = cos_lat*cos_lon;
767 mat[1] = cos_lat*sin_lon;
774 mat[8] = sin_lat*cos_lon;
775 mat[9] = sin_lat*sin_lon;
777 mat[11] = mat[12] = mat[13] = mat[14] = 0.0;
780 MAT3mult(VIEW, LOCAL, UP);
782 // THESE COULD JUST BE POINTERS !!!
783 MAT3_SET_VEC(local_up, mat[0], mat[1], mat[2]);
784 MAT3_SET_VEC(view_up, VIEW[0][0], VIEW[0][1], VIEW[0][2]);
785 MAT3_SET_VEC(forward, VIEW[2][0], VIEW[2][1], VIEW[2][2]);
787 getRotMatrix((double *)TMP, view_up, view_offset);
788 MAT3mult_vec(view_forward, forward, TMP);
790 // make a vector to the current view position
791 MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
793 // Given a vector pointing straight down (-Z), map into onto the
794 // local plane representing "horizontal". This should give us the
795 // local direction for moving "south".
796 MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
797 map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
799 MAT3_NORMALIZE_VEC(surface_south, ntmp);
800 // printf( "Surface direction directly south %.6f %.6f %.6f\n",
801 // surface_south[0], surface_south[1], surface_south[2]);
803 // now calculate the surface east vector
804 getRotMatrix((double *)TMP, view_up, FG_PI_2);
805 MAT3mult_vec(surface_east, surface_south, TMP);
806 // printf( "Surface direction directly east %.6f %.6f %.6f\n",
807 // surface_east[0], surface_east[1], surface_east[2]);
808 // printf( "Should be close to zero = %.6f\n",
809 // MAT3_DOT_PRODUCT(surface_south, surface_east));
810 #endif // !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
814 // Update the "World to Eye" transformation matrix
815 // This is most useful for view frustum culling
816 void FGView::UpdateWorldToEye( FGInterface *f ) {
817 MAT3mat R_Phi, R_Theta, R_Psi, R_Lat, R_Lon, T_view;
821 if ( use_larcsim_local_to_body ) {
823 // Question: hey this is even different then LOCAL[][] above??
824 // Answer: yet another coordinate system, this time the
825 // coordinate system in which we do our view frustum culling.
827 AIRCRAFT[0][0] = -f->get_T_local_to_body_22();
828 AIRCRAFT[0][1] = -f->get_T_local_to_body_23();
829 AIRCRAFT[0][2] = f->get_T_local_to_body_21();
830 AIRCRAFT[0][3] = 0.0;
831 AIRCRAFT[1][0] = f->get_T_local_to_body_32();
832 AIRCRAFT[1][1] = f->get_T_local_to_body_33();
833 AIRCRAFT[1][2] = -f->get_T_local_to_body_31();
834 AIRCRAFT[1][3] = 0.0;
835 AIRCRAFT[2][0] = f->get_T_local_to_body_12();
836 AIRCRAFT[2][1] = f->get_T_local_to_body_13();
837 AIRCRAFT[2][2] = -f->get_T_local_to_body_11();
838 AIRCRAFT[2][3] = 0.0;
839 AIRCRAFT[3][0] = AIRCRAFT[3][1] = AIRCRAFT[3][2] = AIRCRAFT[3][3] = 0.0;
840 AIRCRAFT[3][3] = 1.0;
845 MAT3_SET_HVEC(vec, 0.0, 0.0, -1.0, 1.0);
846 MAT3rotate(R_Phi, vec, f->get_Phi());
847 // printf("Roll matrix (Phi)\n");
848 // MAT3print(R_Phi, stdout);
851 MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
852 MAT3rotate(R_Theta, vec, f->get_Theta());
853 // printf("\nPitch matrix (Theta)\n");
854 // MAT3print(R_Theta, stdout);
857 MAT3_SET_HVEC(vec, 0.0, -1.0, 0.0, 1.0);
858 MAT3rotate(R_Psi, vec, f->get_Psi() + FG_PI /* - view_offset */ );
859 // MAT3rotate(R_Psi, vec, f->get_Psi() + FG_PI - view_offset );
860 // printf("\nYaw matrix (Psi)\n");
861 // MAT3print(R_Psi, stdout);
863 // aircraft roll/pitch/yaw
864 MAT3mult(TMP, R_Phi, R_Theta);
865 MAT3mult(AIRCRAFT, TMP, R_Psi);
867 } // if ( use_larcsim_local_to_body )
869 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
871 // printf("AIRCRAFT matrix\n");
872 // MAT3print(AIRCRAFT, stdout);
874 // View rotation matrix relative to current aircraft orientation
875 MAT3_SET_HVEC(vec, 0.0, -1.0, 0.0, 1.0);
876 MAT3mult_vec(vec, vec, AIRCRAFT);
877 // printf("aircraft up vector = %.2f %.2f %.2f\n",
878 // vec[0], vec[1], vec[2]);
879 MAT3rotate(TMP, vec, -view_offset );
880 MAT3mult(VIEW_OFFSET, AIRCRAFT, TMP);
881 // printf("VIEW_OFFSET matrix\n");
882 // MAT3print(VIEW_OFFSET, stdout);
884 // View position in scenery centered coordinates
885 MAT3_SET_HVEC(vec, view_pos.x(), view_pos.y(), view_pos.z(), 1.0);
886 MAT3translate(T_view, vec);
887 // printf("\nTranslation matrix\n");
888 // MAT3print(T_view, stdout);
891 MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
892 // R_Lat = rotate about X axis
893 MAT3rotate(R_Lat, vec, f->get_Latitude());
894 // printf("\nLatitude matrix\n");
895 // MAT3print(R_Lat, stdout);
898 MAT3_SET_HVEC(vec, 0.0, 0.0, 1.0, 1.0);
899 // R_Lon = rotate about Z axis
900 MAT3rotate(R_Lon, vec, f->get_Longitude() - FG_PI_2 );
901 // printf("\nLongitude matrix\n");
902 // MAT3print(R_Lon, stdout);
905 MAT3mult(WORLD, R_Lat, R_Lon);
906 // printf("\nworld\n");
907 // MAT3print(WORLD, stdout);
909 MAT3mult(EYE_TO_WORLD, VIEW_OFFSET, WORLD);
910 MAT3mult(EYE_TO_WORLD, EYE_TO_WORLD, T_view);
911 // printf("\nEye to world\n");
912 // MAT3print(EYE_TO_WORLD, stdout);
914 MAT3invert(WORLD_TO_EYE, EYE_TO_WORLD);
915 // printf("\nWorld to eye\n");
916 // MAT3print(WORLD_TO_EYE, stdout);
918 // printf( "\nview_pos = %.2f %.2f %.2f\n",
919 // view_pos.x, view_pos.y, view_pos.z );
921 // MAT3_SET_HVEC(eye, 0.0, 0.0, 0.0, 1.0);
922 // MAT3mult_vec(vec, eye, EYE_TO_WORLD);
923 // printf("\neye -> world = %.2f %.2f %.2f\n", vec[0], vec[1], vec[2]);
925 // MAT3_SET_HVEC(vec1, view_pos.x, view_pos.y, view_pos.z, 1.0);
926 // MAT3mult_vec(vec, vec1, WORLD_TO_EYE);
927 // printf( "\nabs_view_pos -> eye = %.2f %.2f %.2f\n",
928 // vec[0], vec[1], vec[2]);
929 #else // FG_VIEW_INLINE_OPTIMIZATIONS
931 MAT3_SET_HVEC(vec, -AIRCRAFT[1][0], -AIRCRAFT[1][1], -AIRCRAFT[1][2], -AIRCRAFT[1][3]);
932 getRotMatrix((double *)TMP, vec, -view_offset );
933 MAT3mult(VIEW_OFFSET, AIRCRAFT, TMP);
934 // MAT3print_formatted(VIEW_OFFSET, stdout, "VIEW_OFFSET matrix:\n",
935 // NULL, "%#8.6f ", "\n");
937 // Build spherical to cartesian transform matrix directly
938 double *mat = (double *)WORLD; //T_view; //WORLD;
939 double cos_lat = f->get_cos_latitude(); //cos(f->get_Latitude());
940 double sin_lat = f->get_sin_latitude(); //sin(f->get_Latitude());
941 // using trig identities this:
942 // mat[0] = cos(f->get_Longitude() - FG_PI_2);//cos_lon;
943 // mat[1] = sin(f->get_Longitude() - FG_PI_2);//sin_lon;
945 mat[0] = f->get_sin_longitude(); //cos_lon;
946 mat[1] = -f->get_cos_longitude(); //sin_lon;
947 mat[4] = -cos_lat*mat[1]; //mat[1]=sin_lon;
948 mat[5] = cos_lat*mat[0]; //mat[0]=cos_lon;
950 mat[8] = sin_lat*mat[1]; //mat[1]=sin_lon;
951 mat[9] = -sin_lat*mat[0]; //mat[0]=cos_lon;
954 // BUILD EYE_TO_WORLD = AIRCRAFT * WORLD
955 // and WORLD_TO_EYE = Inverse( EYE_TO_WORLD) concurrently
956 // by Transposing the 3x3 rotation sub-matrix
957 WORLD_TO_EYE[0][0] = EYE_TO_WORLD[0][0] =
958 VIEW_OFFSET[0][0]*mat[0] + VIEW_OFFSET[0][1]*mat[4] + VIEW_OFFSET[0][2]*mat[8];
960 WORLD_TO_EYE[1][0] = EYE_TO_WORLD[0][1] =
961 VIEW_OFFSET[0][0]*mat[1] + VIEW_OFFSET[0][1]*mat[5] + VIEW_OFFSET[0][2]*mat[9];
963 WORLD_TO_EYE[2][0] = EYE_TO_WORLD[0][2] =
964 VIEW_OFFSET[0][1]*mat[6] + VIEW_OFFSET[0][2]*mat[10];
966 WORLD_TO_EYE[0][1] = EYE_TO_WORLD[1][0] =
967 VIEW_OFFSET[1][0]*mat[0] + VIEW_OFFSET[1][1]*mat[4] + VIEW_OFFSET[1][2]*mat[8];
969 WORLD_TO_EYE[1][1] = EYE_TO_WORLD[1][1] =
970 VIEW_OFFSET[1][0]*mat[1] + VIEW_OFFSET[1][1]*mat[5] + VIEW_OFFSET[1][2]*mat[9];
972 WORLD_TO_EYE[2][1] = EYE_TO_WORLD[1][2] =
973 VIEW_OFFSET[1][1]*mat[6] + VIEW_OFFSET[1][2]*mat[10];
975 WORLD_TO_EYE[0][2] = EYE_TO_WORLD[2][0] =
976 VIEW_OFFSET[2][0]*mat[0] + VIEW_OFFSET[2][1]*mat[4] + VIEW_OFFSET[2][2]*mat[8];
978 WORLD_TO_EYE[1][2] = EYE_TO_WORLD[2][1] =
979 VIEW_OFFSET[2][0]*mat[1] + VIEW_OFFSET[2][1]*mat[5] + VIEW_OFFSET[2][2]*mat[9];
981 WORLD_TO_EYE[2][2] = EYE_TO_WORLD[2][2] =
982 VIEW_OFFSET[2][1]*mat[6] + VIEW_OFFSET[2][2]*mat[10];
984 // TRANSLATE TO VIEW POSITION
985 EYE_TO_WORLD[3][0] = view_pos.x();
986 EYE_TO_WORLD[3][1] = view_pos.y();
987 EYE_TO_WORLD[3][2] = view_pos.z();
990 WORLD_TO_EYE[0][3] = WORLD_TO_EYE[1][3] = WORLD_TO_EYE[2][3] =
991 EYE_TO_WORLD[0][3] = EYE_TO_WORLD[1][3] = EYE_TO_WORLD[2][3] = 0.0;
993 // FILL UNITY ENTRIES
994 WORLD_TO_EYE[3][3] = EYE_TO_WORLD[3][3] = 1.0;
996 /* MAKE THE INVERTED TRANSLATIONS */
997 mat = (double *)EYE_TO_WORLD;
998 WORLD_TO_EYE[3][0] = -mat[12]*mat[0]
1002 WORLD_TO_EYE[3][1] = -mat[12]*mat[4]
1006 WORLD_TO_EYE[3][2] = -mat[12]*mat[8]
1010 // MAT3print_formatted(EYE_TO_WORLD, stdout, "EYE_TO_WORLD matrix:\n",
1011 // NULL, "%#8.6f ", "\n");
1013 // MAT3print_formatted(WORLD_TO_EYE, stdout, "WORLD_TO_EYE matrix:\n",
1014 // NULL, "%#8.6f ", "\n");
1016 #endif // defined(FG_VIEW_INLINE_OPTIMIZATIONS)
1021 // Reject non viewable spheres from current View Frustrum by Curt
1022 // Olson curt@me.umn.edu and Norman Vine nhv@yahoo.com with 'gentle
1023 // guidance' from Steve Baker sbaker@link.com
1025 FGView::SphereClip( const Point3D& cp, const double radius )
1037 mat = (double *)(WORLD_TO_EYE);
1039 eye[2] = x*mat[2] + y*mat[6] + z*mat[10] + mat[14];
1041 // Check near and far clip plane
1042 if( ( eye[2] > radius ) ||
1043 ( eye[2] + radius + current_weather.visibility < 0) )
1044 // ( eye[2] + radius + far_plane < 0) )
1049 // check right and left clip plane (from eye perspective)
1050 x1 = radius * fov_x_clip;
1051 eye[0] = (x*mat[0] + y*mat[4] + z*mat[8] + mat[12]) * slope_x;
1052 if( (eye[2] > -(eye[0]+x1)) || (eye[2] > (eye[0]-x1)) ) {
1056 // check bottom and top clip plane (from eye perspective)
1057 y1 = radius * fov_y_clip;
1058 eye[1] = (x*mat[1] + y*mat[5] + z*mat[9] + mat[13]) * slope_y;
1059 if( (eye[2] > -(eye[1]+y1)) || (eye[2] > (eye[1]-y1)) ) {
1069 FGView::~FGView( void ) {