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 ) {
74 // Initialize a view structure
75 void FGView::Init( void ) {
76 FG_LOG( FG_VIEW, FG_INFO, "Initializing View parameters" );
78 view_mode = FG_VIEW_FIRST_PERSON;
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 )
135 void FGView::cycle_view_mode() {
136 if ( view_mode == FG_VIEW_FIRST_PERSON ) {
137 view_mode = FG_VIEW_FOLLOW;
138 } else if ( view_mode == FG_VIEW_FOLLOW ) {
139 view_mode = FG_VIEW_FIRST_PERSON;
144 // Update the view volume, position, and orientation
145 void FGView::UpdateViewParams( void ) {
146 FGInterface *f = current_aircraft.fdm_state;
150 if ((current_options.get_panel_status() != panel_hist) && (current_options.get_panel_status()))
152 FGPanel::OurPanel->ReInit( 0, 0, 1024, 768);
155 if ( ! current_options.get_panel_status() ) {
156 // xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
158 // xglViewport(0, (GLint)((winHeight)*0.5768), (GLint)(winWidth),
159 // (GLint)((winHeight)*0.4232) );
162 panel_hist = current_options.get_panel_status();
166 void getRotMatrix(double* out, MAT3vec vec, double radians)
168 /* This function contributed by Erich Boleyn (erich@uruk.org) */
169 /* This function used from the Mesa OpenGL code (matrix.c) */
171 double vx, vy, vz, xy, yz, zx, xs, ys, zs, one_c; //, xx, yy, zz
177 // mag = getMagnitude();
183 #define M(row,col) out[row*4 + col]
186 * Arbitrary axis rotation matrix.
188 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
189 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
190 * (which is about the X-axis), and the two composite transforms
191 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
192 * from the arbitrary axis to the X-axis then back. They are
193 * all elementary rotations.
195 * Rz' is a rotation about the Z-axis, to bring the axis vector
196 * into the x-z plane. Then Ry' is applied, rotating about the
197 * Y-axis to bring the axis vector parallel with the X-axis. The
198 * rotation about the X-axis is then performed. Ry and Rz are
199 * simply the respective inverse transforms to bring the arbitrary
200 * axis back to it's original orientation. The first transforms
201 * Rz' and Ry' are considered inverses, since the data from the
202 * arbitrary axis gives you info on how to get to it, not how
203 * to get away from it, and an inverse must be applied.
205 * The basic calculation used is to recognize that the arbitrary
206 * axis vector (x, y, z), since it is of unit length, actually
207 * represents the sines and cosines of the angles to rotate the
208 * X-axis to the same orientation, with theta being the angle about
209 * Z and phi the angle about Y (in the order described above)
212 * cos ( theta ) = x / sqrt ( 1 - z^2 )
213 * sin ( theta ) = y / sqrt ( 1 - z^2 )
215 * cos ( phi ) = sqrt ( 1 - z^2 )
218 * Note that cos ( phi ) can further be inserted to the above
221 * cos ( theta ) = x / cos ( phi )
222 * sin ( theta ) = y / cos ( phi )
224 * ...etc. Because of those relations and the standard trigonometric
225 * relations, it is pssible to reduce the transforms down to what
226 * is used below. It may be that any primary axis chosen will give the
227 * same results (modulo a sign convention) using thie method.
229 * Particularly nice is to notice that all divisions that might
230 * have caused trouble when parallel to certain planes or
231 * axis go away with care paid to reducing the expressions.
232 * After checking, it does perform correctly under all cases, since
233 * in all the cases of division where the denominator would have
234 * been zero, the numerator would have been zero as well, giving
235 * the expected result.
249 M(0,0) = (one_c * vx * vx) + c;
251 yz = vy * vz * one_c;
255 M(1,1) = (one_c * vy * vy) + c;
257 zx = vz * vx * one_c;
261 M(2,2) = (one_c * vz *vz) + c;
263 xy = vx * vy * one_c;
267 // M(0,0) = (one_c * xx) + c;
268 // M(1,0) = (one_c * xy) - zs;
269 // M(2,0) = (one_c * zx) + ys;
271 // M(0,1) = (one_c * xy) + zs;
272 // M(1,1) = (one_c * yy) + c;
273 // M(2,1) = (one_c * yz) - xs;
275 // M(0,2) = (one_c * zx) - ys;
276 // M(1,2) = (one_c * yz) + xs;
277 // M(2,2) = (one_c * zz) + c;
283 // Update the view parameters
284 void FGView::UpdateViewMath( FGInterface *f ) {
286 MAT3vec vec, forward, v0, minus_z;
287 MAT3mat R, TMP, UP, LOCAL, VIEW;
291 // printf("Updating fov\n");
292 UpdateFOV( current_options );
296 scenery.center = scenery.next_center;
298 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
299 // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x,
300 // scenery.center.y, scenery.center.z);
302 // calculate the cartesion coords of the current lat/lon/0 elev
303 p = Point3D( f->get_Longitude(),
304 f->get_Lat_geocentric(),
305 f->get_Sea_level_radius() * FEET_TO_METER );
307 cur_zero_elev = fgPolarToCart3d(p) - scenery.center;
309 // calculate view position in current FG view coordinate system
310 // p.lon & p.lat are already defined earlier, p.radius was set to
311 // the sea level radius, so now we add in our altitude.
312 if ( f->get_Altitude() * FEET_TO_METER >
313 (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
314 p.setz( p.radius() + f->get_Altitude() * FEET_TO_METER );
316 p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
319 abs_view_pos = fgPolarToCart3d(p);
321 #else // FG_VIEW_INLINE_OPTIMIZATIONS
323 double tmp_radius = f->get_Sea_level_radius() * FEET_TO_METER;
324 double tmp = f->get_cos_lat_geocentric() * tmp_radius;
326 cur_zero_elev.setx(f->get_cos_longitude()*tmp - scenery.center.x());
327 cur_zero_elev.sety(f->get_sin_longitude()*tmp - scenery.center.y());
328 cur_zero_elev.setz(f->get_sin_lat_geocentric()*tmp_radius - scenery.center.z());
330 // calculate view position in current FG view coordinate system
331 // p.lon & p.lat are already defined earlier, p.radius was set to
332 // the sea level radius, so now we add in our altitude.
333 if ( f->get_Altitude() * FEET_TO_METER >
334 (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
335 tmp_radius += f->get_Altitude() * FEET_TO_METER;
337 tmp_radius += scenery.cur_elev + 0.5 * METER_TO_FEET ;
339 tmp = f->get_cos_lat_geocentric() * tmp_radius;
340 abs_view_pos.setx(f->get_cos_longitude()*tmp);
341 abs_view_pos.sety(f->get_sin_longitude()*tmp);
342 abs_view_pos.setz(f->get_sin_lat_geocentric()*tmp_radius);
344 #endif // FG_VIEW_INLINE_OPTIMIZATIONS
346 view_pos = abs_view_pos - scenery.center;
348 FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
349 FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
350 FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = " << view_pos );
352 // Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw)
353 // from FG_T_local_to_body[3][3]
355 if ( use_larcsim_local_to_body ) {
357 // Question: Why is the LaRCsim matrix arranged so differently
358 // than the one we need???
360 // Answer (I think): The LaRCsim matrix is generated in a
361 // different reference frame than we've set up for our world
363 LOCAL[0][0] = f->get_T_local_to_body_33();
364 LOCAL[0][1] = -f->get_T_local_to_body_32();
365 LOCAL[0][2] = -f->get_T_local_to_body_31();
367 LOCAL[1][0] = -f->get_T_local_to_body_23();
368 LOCAL[1][1] = f->get_T_local_to_body_22();
369 LOCAL[1][2] = f->get_T_local_to_body_21();
371 LOCAL[2][0] = -f->get_T_local_to_body_13();
372 LOCAL[2][1] = f->get_T_local_to_body_12();
373 LOCAL[2][2] = f->get_T_local_to_body_11();
375 LOCAL[3][0] = LOCAL[3][1] = LOCAL[3][2] = LOCAL[3][3] = 0.0;
378 // printf("LaRCsim LOCAL matrix\n");
379 // MAT3print(LOCAL, stdout);
383 // calculate the transformation matrix to go from LaRCsim to ssg
385 sgSetVec3( vec1, 0.0, 1.0, 0.0 );
387 sgMakeRotMat4( mat1, 90, vec1 );
390 sgSetVec3( vec2, 1.0, 0.0, 0.0 );
392 sgMakeRotMat4( mat2, 90, vec2 );
394 sgMultMat4( sgLARC_TO_SSG, mat1, mat2 );
397 cout << "LaRCsim to SSG:" << endl;
401 for ( i = 0; i < 4; i++ ) {
402 for ( j = 0; j < 4; j++ ) {
403 print[i][j] = sgLARC_TO_SSG[i][j];
406 MAT3print( print, stdout);
409 // code to calculate LOCAL matrix calculated from Phi, Theta, and
410 // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
413 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
414 MAT3rotate(R, vec, f->get_Phi());
415 // cout << "Roll matrix" << endl;
416 // MAT3print(R, stdout);
419 sgSetVec3( sgrollvec, 0.0, 0.0, 1.0 );
420 sgMat4 sgPHI; // roll
421 sgMakeRotMat4( sgPHI, f->get_Phi() * RAD_TO_DEG, sgrollvec );
424 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
425 MAT3rotate(TMP, vec, f->get_Theta());
426 // cout << "Pitch matrix" << endl;;
427 // MAT3print(TMP, stdout);
429 // cout << "tmp rotation matrix, R:" << endl;;
430 // MAT3print(R, stdout);
433 sgSetVec3( sgpitchvec, 0.0, 1.0, 0.0 );
434 sgMat4 sgTHETA; // pitch
435 sgMakeRotMat4( sgTHETA, f->get_Theta() * RAD_TO_DEG,
439 sgMultMat4( sgROT, sgPHI, sgTHETA );
442 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
443 MAT3rotate(TMP, vec, -f->get_Psi());
444 // cout << "Yaw matrix" << endl;
445 // MAT3print(TMP, stdout);
446 MAT3mult(LOCAL, R, TMP);
447 // cout << "LOCAL matrix:" << endl;
448 // MAT3print(LOCAL, stdout);
451 sgSetVec3( sgyawvec, 1.0, 0.0, 0.0 );
452 sgMat4 sgPSI; // pitch
453 sgMakeRotMat4( sgPSI, -f->get_Psi() * RAD_TO_DEG, sgyawvec );
455 sgMultMat4( sgLOCAL, sgROT, sgPSI );
461 for ( i = 0; i < 4; i++ ) {
462 for ( j = 0; j < 4; j++ ) {
463 print[i][j] = sgLOCAL[i][j];
466 MAT3print( print, stdout);
468 } // if ( use_larcsim_local_to_body )
470 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
472 // Derive the local UP transformation matrix based on *geodetic*
474 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
475 MAT3rotate(R, vec, f->get_Longitude()); // R = rotate about Z axis
476 // printf("Longitude matrix\n");
477 // MAT3print(R, stdout);
479 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
480 MAT3mult_vec(vec, vec, R);
481 MAT3rotate(TMP, vec, -f->get_Latitude()); // TMP = rotate about X axis
482 // printf("Latitude matrix\n");
483 // MAT3print(TMP, stdout);
485 MAT3mult(UP, R, TMP);
486 // cout << "Local up matrix" << endl;;
487 // MAT3print(UP, stdout);
490 f->get_Longitude() * RAD_TO_DEG,
492 -f->get_Latitude() * RAD_TO_DEG );
494 cout << "FG derived UP matrix using sg routines" << endl;
498 for ( i = 0; i < 4; i++ ) {
499 for ( j = 0; j < 4; j++ ) {
500 print[i][j] = sgUP[i][j];
503 MAT3print( print, stdout);
506 MAT3_SET_VEC(local_up, 1.0, 0.0, 0.0);
507 MAT3mult_vec(local_up, local_up, UP);
509 // printf( "Local Up = (%.4f, %.4f, %.4f)\n",
510 // local_up[0], local_up[1], local_up[2]);
512 // Alternative method to Derive local up vector based on
513 // *geodetic* coordinates
514 // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
515 // printf( " Alt Up = (%.4f, %.4f, %.4f)\n",
516 // alt_up.x, alt_up.y, alt_up.z);
518 // Calculate the VIEW matrix
519 MAT3mult(VIEW, LOCAL, UP);
520 // cout << "VIEW matrix" << endl;;
521 // MAT3print(VIEW, stdout);
523 sgMat4 sgTMP, sgTMP2;
524 sgMultMat4( sgTMP, sgLOCAL, sgUP );
526 // generate the sg view up vector
528 sgSetVec3( vec1, 1.0, 0.0, 0.0 );
529 sgXformVec3( sgview_up, vec1, sgTMP );
531 // generate the view offset matrix
532 sgMakeRotMat4( sgVIEW_OFFSET, view_offset * RAD_TO_DEG, sgview_up );
535 cout << "sg VIEW_OFFSET matrix" << endl;
539 for ( i = 0; i < 4; i++ ) {
540 for ( j = 0; j < 4; j++ ) {
541 print[i][j] = sgVIEW_OFFSET[i][j];
544 MAT3print( print, stdout);
547 sgMultMat4( sgTMP2, sgTMP, sgVIEW_OFFSET );
548 sgMultMat4( sgVIEW_ROT, sgLARC_TO_SSG, sgTMP2 );
550 sgMakeTransMat4( sgTRANS, view_pos.x(), view_pos.y(), view_pos.z() );
552 sgMultMat4( sgVIEW, sgVIEW_ROT, sgTRANS );
555 sgCopyMat4( tmp.m, sgVIEW );
556 follow.push_back( tmp );
558 // generate the current up, forward, and fwrd-view vectors
559 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
560 MAT3mult_vec(view_up, vec, VIEW);
563 cout << "FG derived VIEW matrix using sg routines" << endl;
567 for ( i = 0; i < 4; i++ ) {
568 for ( j = 0; j < 4; j++ ) {
569 print[i][j] = sgVIEW[i][j];
572 MAT3print( print, stdout);
575 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
576 MAT3mult_vec(forward, vec, VIEW);
577 // printf( "Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1],
580 MAT3rotate(TMP, view_up, view_offset);
581 MAT3mult_vec(view_forward, forward, TMP);
583 // make a vector to the current view position
584 MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
586 // Given a vector pointing straight down (-Z), map into onto the
587 // local plane representing "horizontal". This should give us the
588 // local direction for moving "south".
589 MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
590 map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
591 MAT3_NORMALIZE_VEC(surface_south, ntmp);
592 // printf( "Surface direction directly south %.2f %.2f %.2f\n",
593 // surface_south[0], surface_south[1], surface_south[2]);
595 // now calculate the surface east vector
596 MAT3rotate(TMP, view_up, FG_PI_2);
597 MAT3mult_vec(surface_east, surface_south, TMP);
598 // printf( "Surface direction directly east %.2f %.2f %.2f\n",
599 // surface_east[0], surface_east[1], surface_east[2]);
600 // printf( "Should be close to zero = %.2f\n",
601 // MAT3_DOT_PRODUCT(surface_south, surface_east));
603 #else // FG_VIEW_INLINE_OPTIMIZATIONS
605 // // Build spherical to cartesian transform matrix directly
606 double cos_lat = f->get_cos_latitude(); // cos(-f->get_Latitude());
607 double sin_lat = -f->get_sin_latitude(); // sin(-f->get_Latitude());
608 double cos_lon = f->get_cos_longitude(); //cos(f->get_Longitude());
609 double sin_lon = f->get_sin_longitude(); //sin(f->get_Longitude());
611 double *mat = (double *)UP;
613 mat[0] = cos_lat*cos_lon;
614 mat[1] = cos_lat*sin_lon;
621 mat[8] = sin_lat*cos_lon;
622 mat[9] = sin_lat*sin_lon;
624 mat[11] = mat[12] = mat[13] = mat[14] = 0.0;
627 MAT3mult(VIEW, LOCAL, UP);
629 // THESE COULD JUST BE POINTERS !!!
630 MAT3_SET_VEC(local_up, mat[0], mat[1], mat[2]);
631 MAT3_SET_VEC(view_up, VIEW[0][0], VIEW[0][1], VIEW[0][2]);
632 MAT3_SET_VEC(forward, VIEW[2][0], VIEW[2][1], VIEW[2][2]);
634 getRotMatrix((double *)TMP, view_up, view_offset);
635 MAT3mult_vec(view_forward, forward, TMP);
637 // make a vector to the current view position
638 MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
640 // Given a vector pointing straight down (-Z), map into onto the
641 // local plane representing "horizontal". This should give us the
642 // local direction for moving "south".
643 MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
644 map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
646 MAT3_NORMALIZE_VEC(surface_south, ntmp);
647 // printf( "Surface direction directly south %.6f %.6f %.6f\n",
648 // surface_south[0], surface_south[1], surface_south[2]);
650 // now calculate the surface east vector
651 getRotMatrix((double *)TMP, view_up, FG_PI_2);
652 MAT3mult_vec(surface_east, surface_south, TMP);
653 // printf( "Surface direction directly east %.6f %.6f %.6f\n",
654 // surface_east[0], surface_east[1], surface_east[2]);
655 // printf( "Should be close to zero = %.6f\n",
656 // MAT3_DOT_PRODUCT(surface_south, surface_east));
657 #endif // !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
662 FGView::~FGView( void ) {