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;
145 // Basically, this is a modified version of the Mesa gluLookAt()
146 // function that's been modified slightly so we can capture the
147 // result before sending it off to OpenGL land.
148 void FGView::LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
149 GLdouble centerx, GLdouble centery, GLdouble centerz,
150 GLdouble upx, GLdouble upy, GLdouble upz ) {
152 GLdouble x[3], y[3], z[3];
155 m = current_view.MODEL_VIEW;
157 /* Make rotation matrix */
160 z[0] = eyex - centerx;
161 z[1] = eyey - centery;
162 z[2] = eyez - centerz;
163 mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
164 if (mag) { /* mpichler, 19950515 */
175 /* X vector = Y cross Z */
176 x[0] = y[1]*z[2] - y[2]*z[1];
177 x[1] = -y[0]*z[2] + y[2]*z[0];
178 x[2] = y[0]*z[1] - y[1]*z[0];
180 /* Recompute Y = Z cross X */
181 y[0] = z[1]*x[2] - z[2]*x[1];
182 y[1] = -z[0]*x[2] + z[2]*x[0];
183 y[2] = z[0]*x[1] - z[1]*x[0];
185 /* mpichler, 19950515 */
186 /* cross product gives area of parallelogram, which is < 1.0 for
187 * non-perpendicular unit-length vectors; so normalize x, y here
190 mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
197 mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
204 #define M(row,col) m[col*4+row]
205 M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = 0.0;
206 M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = 0.0;
207 M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = 0.0;
208 // the following is part of the original gluLookAt(), but we are
209 // commenting it out because we know we are going to be doing a
210 // translation below which will set these values anyways
211 // M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0;
214 // Translate Eye to Origin
215 // replaces: glTranslated( -eyex, -eyey, -eyez );
217 // this has been slightly modified from the original glTranslate()
218 // code because we know that coming into this m[12] = m[13] =
219 // m[14] = 0.0, and m[15] = 1.0;
220 m[12] = m[0] * -eyex + m[4] * -eyey + m[8] * -eyez /* + m[12] */;
221 m[13] = m[1] * -eyex + m[5] * -eyey + m[9] * -eyez /* + m[13] */;
222 m[14] = m[2] * -eyex + m[6] * -eyey + m[10] * -eyez /* + m[14] */;
223 m[15] = 1.0 /* m[3] * -eyex + m[7] * -eyey + m[11] * -eyez + m[15] */;
225 // xglMultMatrixd( m );
231 // Update the view volume, position, and orientation
232 void FGView::UpdateViewParams( void ) {
233 FGInterface *f = current_aircraft.fdm_state;
236 // UpdateWorldToEye(f);
238 if ((current_options.get_panel_status() != panel_hist) && (current_options.get_panel_status()))
240 FGPanel::OurPanel->ReInit( 0, 0, 1024, 768);
243 if ( ! current_options.get_panel_status() ) {
244 xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
246 xglViewport(0, (GLint)((winHeight)*0.5768), (GLint)(winWidth),
247 (GLint)((winHeight)*0.4232) );
250 // Tell GL we are about to modify the projection parameters
251 xglMatrixMode(GL_PROJECTION);
253 if ( f->get_Altitude() * FEET_TO_METER - scenery.cur_elev > 10.0 ) {
254 // ssgSetNearFar( 10.0, 100000.0 );
255 gluPerspective(current_options.get_fov(), win_ratio, 10.0, 100000.0);
257 // ssgSetNearFar( 0.5, 100000.0 );
258 gluPerspective(current_options.get_fov(), win_ratio, 0.5, 100000.0);
259 // printf("Near ground, minimizing near clip plane\n");
263 xglMatrixMode(GL_MODELVIEW);
266 // set up our view volume (default)
267 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
268 LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
269 view_pos.x() + view_forward[0],
270 view_pos.y() + view_forward[1],
271 view_pos.z() + view_forward[2],
272 view_up[0], view_up[1], view_up[2]);
274 // look almost straight up (testing and eclipse watching)
275 /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
276 view_pos.x() + view_up[0] + .001,
277 view_pos.y() + view_up[1] + .001,
278 view_pos.z() + view_up[2] + .001,
279 view_up[0], view_up[1], view_up[2]); */
281 // lock view horizontally towards sun (testing)
282 /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
283 view_pos.x() + surface_to_sun[0],
284 view_pos.y() + surface_to_sun[1],
285 view_pos.z() + surface_to_sun[2],
286 view_up[0], view_up[1], view_up[2]); */
288 // lock view horizontally towards south (testing)
289 /* LookAt(view_pos.x(), view_pos.y(), view_pos.z(),
290 view_pos.x() + surface_south[0],
291 view_pos.y() + surface_south[1],
292 view_pos.z() + surface_south[2],
293 view_up[0], view_up[1], view_up[2]); */
295 #else // defined(FG_VIEW_INLINE_OPTIMIZATIONS)
296 //void FGView::LookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
297 // GLdouble centerx, GLdouble centery, GLdouble centerz,
298 // GLdouble upx, GLdouble upy, GLdouble upz )
301 GLdouble x[3], y[3], z[3];
304 m = current_view.MODEL_VIEW;
306 /* Make rotation matrix */
309 z[0] = -view_forward[0]; //eyex - centerx;
310 z[1] = -view_forward[1]; //eyey - centery;
311 z[2] = -view_forward[2]; //eyez - centerz;
313 // In our case this is a unit vector NHV
315 // mag = sqrt( z[0]*z[0] + z[1]*z[1] + z[2]*z[2] );
316 // if (mag) { /* mpichler, 19950515 */
318 // printf("mag(%f) ", mag);
325 y[0] = view_up[0]; //upx;
326 y[1] = view_up[1]; //upy;
327 y[2] = view_up[2]; //upz;
329 /* X vector = Y cross Z */
330 x[0] = y[1]*z[2] - y[2]*z[1];
331 x[1] = -y[0]*z[2] + y[2]*z[0];
332 x[2] = y[0]*z[1] - y[1]*z[0];
334 // printf(" %f %f %f ", y[0], y[1], y[2]);
336 /* Recompute Y = Z cross X */
337 // y[0] = z[1]*x[2] - z[2]*x[1];
338 // y[1] = -z[0]*x[2] + z[2]*x[0];
339 // y[2] = z[0]*x[1] - z[1]*x[0];
341 // printf(" %f %f %f\n", y[0], y[1], y[2]);
343 // In our case these are unit vectors NHV
345 /* mpichler, 19950515 */
346 /* cross product gives area of parallelogram, which is < 1.0 for
347 * non-perpendicular unit-length vectors; so normalize x, y here
350 // mag = sqrt( x[0]*x[0] + x[1]*x[1] + x[2]*x[2] );
353 // printf("mag2(%f) ", mag);
359 // mag = sqrt( y[0]*y[0] + y[1]*y[1] + y[2]*y[2] );
362 // printf("mag3(%f)\n", mag);
368 #define M(row,col) m[col*4+row]
369 M(0,0) = x[0]; M(0,1) = x[1]; M(0,2) = x[2]; M(0,3) = 0.0;
370 M(1,0) = y[0]; M(1,1) = y[1]; M(1,2) = y[2]; M(1,3) = 0.0;
371 M(2,0) = z[0]; M(2,1) = z[1]; M(2,2) = z[2]; M(2,3) = 0.0;
372 // the following is part of the original gluLookAt(), but we are
373 // commenting it out because we know we are going to be doing a
374 // translation below which will set these values anyways
375 // M(3,0) = 0.0; M(3,1) = 0.0; M(3,2) = 0.0; M(3,3) = 1.0;
378 // Translate Eye to Origin
379 // replaces: glTranslated( -eyex, -eyey, -eyez );
381 // this has been slightly modified from the original glTranslate()
382 // code because we know that coming into this m[12] = m[13] =
383 // m[14] = 0.0, and m[15] = 1.0;
384 m[12] = m[0] * -view_pos.x() + m[4] * -view_pos.y() + m[8] * -view_pos.z() /* + m[12] */;
385 m[13] = m[1] * -view_pos.x() + m[5] * -view_pos.y() + m[9] * -view_pos.z() /* + m[13] */;
386 m[14] = m[2] * -view_pos.x() + m[6] * -view_pos.y() + m[10] * -view_pos.z() /* + m[14] */;
387 m[15] = 1.0 /* m[3] * -view_pos.x() + m[7] * -view_pos.y() + m[11] * -view_pos.z() + m[15] */;
389 // xglMultMatrixd( m );
392 #endif // FG_VIEW_INLINE_OPTIMIZATIONS
394 panel_hist = current_options.get_panel_status();
398 void getRotMatrix(double* out, MAT3vec vec, double radians)
400 /* This function contributed by Erich Boleyn (erich@uruk.org) */
401 /* This function used from the Mesa OpenGL code (matrix.c) */
403 double vx, vy, vz, xy, yz, zx, xs, ys, zs, one_c; //, xx, yy, zz
409 // mag = getMagnitude();
415 #define M(row,col) out[row*4 + col]
418 * Arbitrary axis rotation matrix.
420 * This is composed of 5 matrices, Rz, Ry, T, Ry', Rz', multiplied
421 * like so: Rz * Ry * T * Ry' * Rz'. T is the final rotation
422 * (which is about the X-axis), and the two composite transforms
423 * Ry' * Rz' and Rz * Ry are (respectively) the rotations necessary
424 * from the arbitrary axis to the X-axis then back. They are
425 * all elementary rotations.
427 * Rz' is a rotation about the Z-axis, to bring the axis vector
428 * into the x-z plane. Then Ry' is applied, rotating about the
429 * Y-axis to bring the axis vector parallel with the X-axis. The
430 * rotation about the X-axis is then performed. Ry and Rz are
431 * simply the respective inverse transforms to bring the arbitrary
432 * axis back to it's original orientation. The first transforms
433 * Rz' and Ry' are considered inverses, since the data from the
434 * arbitrary axis gives you info on how to get to it, not how
435 * to get away from it, and an inverse must be applied.
437 * The basic calculation used is to recognize that the arbitrary
438 * axis vector (x, y, z), since it is of unit length, actually
439 * represents the sines and cosines of the angles to rotate the
440 * X-axis to the same orientation, with theta being the angle about
441 * Z and phi the angle about Y (in the order described above)
444 * cos ( theta ) = x / sqrt ( 1 - z^2 )
445 * sin ( theta ) = y / sqrt ( 1 - z^2 )
447 * cos ( phi ) = sqrt ( 1 - z^2 )
450 * Note that cos ( phi ) can further be inserted to the above
453 * cos ( theta ) = x / cos ( phi )
454 * sin ( theta ) = y / cos ( phi )
456 * ...etc. Because of those relations and the standard trigonometric
457 * relations, it is pssible to reduce the transforms down to what
458 * is used below. It may be that any primary axis chosen will give the
459 * same results (modulo a sign convention) using thie method.
461 * Particularly nice is to notice that all divisions that might
462 * have caused trouble when parallel to certain planes or
463 * axis go away with care paid to reducing the expressions.
464 * After checking, it does perform correctly under all cases, since
465 * in all the cases of division where the denominator would have
466 * been zero, the numerator would have been zero as well, giving
467 * the expected result.
481 M(0,0) = (one_c * vx * vx) + c;
483 yz = vy * vz * one_c;
487 M(1,1) = (one_c * vy * vy) + c;
489 zx = vz * vx * one_c;
493 M(2,2) = (one_c * vz *vz) + c;
495 xy = vx * vy * one_c;
499 // M(0,0) = (one_c * xx) + c;
500 // M(1,0) = (one_c * xy) - zs;
501 // M(2,0) = (one_c * zx) + ys;
503 // M(0,1) = (one_c * xy) + zs;
504 // M(1,1) = (one_c * yy) + c;
505 // M(2,1) = (one_c * yz) - xs;
507 // M(0,2) = (one_c * zx) - ys;
508 // M(1,2) = (one_c * yz) + xs;
509 // M(2,2) = (one_c * zz) + c;
515 // Update the view parameters
516 void FGView::UpdateViewMath( FGInterface *f ) {
518 MAT3vec vec, forward, v0, minus_z;
519 MAT3mat R, TMP, UP, LOCAL, VIEW;
523 // printf("Updating fov\n");
524 UpdateFOV( current_options );
528 scenery.center = scenery.next_center;
530 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
531 // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x,
532 // scenery.center.y, scenery.center.z);
534 // calculate the cartesion coords of the current lat/lon/0 elev
535 p = Point3D( f->get_Longitude(),
536 f->get_Lat_geocentric(),
537 f->get_Sea_level_radius() * FEET_TO_METER );
539 cur_zero_elev = fgPolarToCart3d(p) - scenery.center;
541 // calculate view position in current FG view coordinate system
542 // p.lon & p.lat are already defined earlier, p.radius was set to
543 // the sea level radius, so now we add in our altitude.
544 if ( f->get_Altitude() * FEET_TO_METER >
545 (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
546 p.setz( p.radius() + f->get_Altitude() * FEET_TO_METER );
548 p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
551 abs_view_pos = fgPolarToCart3d(p);
553 #else // FG_VIEW_INLINE_OPTIMIZATIONS
555 double tmp_radius = f->get_Sea_level_radius() * FEET_TO_METER;
556 double tmp = f->get_cos_lat_geocentric() * tmp_radius;
558 cur_zero_elev.setx(f->get_cos_longitude()*tmp - scenery.center.x());
559 cur_zero_elev.sety(f->get_sin_longitude()*tmp - scenery.center.y());
560 cur_zero_elev.setz(f->get_sin_lat_geocentric()*tmp_radius - scenery.center.z());
562 // calculate view position in current FG view coordinate system
563 // p.lon & p.lat are already defined earlier, p.radius was set to
564 // the sea level radius, so now we add in our altitude.
565 if ( f->get_Altitude() * FEET_TO_METER >
566 (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
567 tmp_radius += f->get_Altitude() * FEET_TO_METER;
569 tmp_radius += scenery.cur_elev + 0.5 * METER_TO_FEET ;
571 tmp = f->get_cos_lat_geocentric() * tmp_radius;
572 abs_view_pos.setx(f->get_cos_longitude()*tmp);
573 abs_view_pos.sety(f->get_sin_longitude()*tmp);
574 abs_view_pos.setz(f->get_sin_lat_geocentric()*tmp_radius);
576 #endif // FG_VIEW_INLINE_OPTIMIZATIONS
578 view_pos = abs_view_pos - scenery.center;
580 FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
581 FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
582 FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = " << view_pos );
584 // Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw)
585 // from FG_T_local_to_body[3][3]
587 if ( use_larcsim_local_to_body ) {
589 // Question: Why is the LaRCsim matrix arranged so differently
590 // than the one we need???
592 // Answer (I think): The LaRCsim matrix is generated in a
593 // different reference frame than we've set up for our world
595 LOCAL[0][0] = f->get_T_local_to_body_33();
596 LOCAL[0][1] = -f->get_T_local_to_body_32();
597 LOCAL[0][2] = -f->get_T_local_to_body_31();
599 LOCAL[1][0] = -f->get_T_local_to_body_23();
600 LOCAL[1][1] = f->get_T_local_to_body_22();
601 LOCAL[1][2] = f->get_T_local_to_body_21();
603 LOCAL[2][0] = -f->get_T_local_to_body_13();
604 LOCAL[2][1] = f->get_T_local_to_body_12();
605 LOCAL[2][2] = f->get_T_local_to_body_11();
607 LOCAL[3][0] = LOCAL[3][1] = LOCAL[3][2] = LOCAL[3][3] = 0.0;
610 // printf("LaRCsim LOCAL matrix\n");
611 // MAT3print(LOCAL, stdout);
615 // calculate the transformation matrix to go from LaRCsim to ssg
617 sgSetVec3( vec1, 0.0, 1.0, 0.0 );
619 sgMakeRotMat4( mat1, 90, vec1 );
622 sgSetVec3( vec2, 1.0, 0.0, 0.0 );
624 sgMakeRotMat4( mat2, 90, vec2 );
626 sgMultMat4( sgLARC_TO_SSG, mat1, mat2 );
629 cout << "LaRCsim to SSG:" << endl;
633 for ( i = 0; i < 4; i++ ) {
634 for ( j = 0; j < 4; j++ ) {
635 print[i][j] = sgLARC_TO_SSG[i][j];
638 MAT3print( print, stdout);
641 // code to calculate LOCAL matrix calculated from Phi, Theta, and
642 // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
645 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
646 MAT3rotate(R, vec, f->get_Phi());
647 // cout << "Roll matrix" << endl;
648 // MAT3print(R, stdout);
651 sgSetVec3( sgrollvec, 0.0, 0.0, 1.0 );
652 sgMat4 sgPHI; // roll
653 sgMakeRotMat4( sgPHI, f->get_Phi() * RAD_TO_DEG, sgrollvec );
656 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
657 MAT3rotate(TMP, vec, f->get_Theta());
658 // cout << "Pitch matrix" << endl;;
659 // MAT3print(TMP, stdout);
661 // cout << "tmp rotation matrix, R:" << endl;;
662 // MAT3print(R, stdout);
665 sgSetVec3( sgpitchvec, 0.0, 1.0, 0.0 );
666 sgMat4 sgTHETA; // pitch
667 sgMakeRotMat4( sgTHETA, f->get_Theta() * RAD_TO_DEG,
671 sgMultMat4( sgROT, sgPHI, sgTHETA );
674 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
675 MAT3rotate(TMP, vec, -f->get_Psi());
676 // cout << "Yaw matrix" << endl;
677 // MAT3print(TMP, stdout);
678 MAT3mult(LOCAL, R, TMP);
679 // cout << "LOCAL matrix:" << endl;
680 // MAT3print(LOCAL, stdout);
683 sgSetVec3( sgyawvec, 1.0, 0.0, 0.0 );
684 sgMat4 sgPSI; // pitch
685 sgMakeRotMat4( sgPSI, -f->get_Psi() * RAD_TO_DEG, sgyawvec );
687 sgMultMat4( sgLOCAL, sgROT, sgPSI );
693 for ( i = 0; i < 4; i++ ) {
694 for ( j = 0; j < 4; j++ ) {
695 print[i][j] = sgLOCAL[i][j];
698 MAT3print( print, stdout);
700 } // if ( use_larcsim_local_to_body )
702 #if !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
704 // Derive the local UP transformation matrix based on *geodetic*
706 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
707 MAT3rotate(R, vec, f->get_Longitude()); // R = rotate about Z axis
708 // printf("Longitude matrix\n");
709 // MAT3print(R, stdout);
711 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
712 MAT3mult_vec(vec, vec, R);
713 MAT3rotate(TMP, vec, -f->get_Latitude()); // TMP = rotate about X axis
714 // printf("Latitude matrix\n");
715 // MAT3print(TMP, stdout);
717 MAT3mult(UP, R, TMP);
718 // cout << "Local up matrix" << endl;;
719 // MAT3print(UP, stdout);
722 f->get_Longitude() * RAD_TO_DEG,
724 -f->get_Latitude() * RAD_TO_DEG );
726 cout << "FG derived UP matrix using sg routines" << endl;
730 for ( i = 0; i < 4; i++ ) {
731 for ( j = 0; j < 4; j++ ) {
732 print[i][j] = sgUP[i][j];
735 MAT3print( print, stdout);
738 MAT3_SET_VEC(local_up, 1.0, 0.0, 0.0);
739 MAT3mult_vec(local_up, local_up, UP);
741 // printf( "Local Up = (%.4f, %.4f, %.4f)\n",
742 // local_up[0], local_up[1], local_up[2]);
744 // Alternative method to Derive local up vector based on
745 // *geodetic* coordinates
746 // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
747 // printf( " Alt Up = (%.4f, %.4f, %.4f)\n",
748 // alt_up.x, alt_up.y, alt_up.z);
750 // Calculate the VIEW matrix
751 MAT3mult(VIEW, LOCAL, UP);
752 // cout << "VIEW matrix" << endl;;
753 // MAT3print(VIEW, stdout);
756 sgMultMat4( sgTMP, sgLOCAL, sgUP );
757 sgMultMat4( sgVIEW_ROT, sgLARC_TO_SSG, sgTMP );
759 sgMakeTransMat4( sgTRANS, view_pos.x(), view_pos.y(), view_pos.z() );
761 sgMultMat4( sgVIEW, sgVIEW_ROT, sgTRANS );
764 sgCopyMat4( tmp.m, sgVIEW );
765 follow.push_back( tmp );
768 cout << "FG derived VIEW matrix using sg routines" << endl;
772 for ( i = 0; i < 4; i++ ) {
773 for ( j = 0; j < 4; j++ ) {
774 print[i][j] = sgVIEW[i][j];
777 MAT3print( print, stdout);
781 // generate the current up, forward, and fwrd-view vectors
782 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
783 MAT3mult_vec(view_up, vec, VIEW);
785 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
786 MAT3mult_vec(forward, vec, VIEW);
787 // printf( "Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1],
790 MAT3rotate(TMP, view_up, view_offset);
791 MAT3mult_vec(view_forward, forward, TMP);
793 // make a vector to the current view position
794 MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
796 // Given a vector pointing straight down (-Z), map into onto the
797 // local plane representing "horizontal". This should give us the
798 // local direction for moving "south".
799 MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
800 map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
801 MAT3_NORMALIZE_VEC(surface_south, ntmp);
802 // printf( "Surface direction directly south %.2f %.2f %.2f\n",
803 // surface_south[0], surface_south[1], surface_south[2]);
805 // now calculate the surface east vector
806 MAT3rotate(TMP, view_up, FG_PI_2);
807 MAT3mult_vec(surface_east, surface_south, TMP);
808 // printf( "Surface direction directly east %.2f %.2f %.2f\n",
809 // surface_east[0], surface_east[1], surface_east[2]);
810 // printf( "Should be close to zero = %.2f\n",
811 // MAT3_DOT_PRODUCT(surface_south, surface_east));
813 #else // FG_VIEW_INLINE_OPTIMIZATIONS
815 // // Build spherical to cartesian transform matrix directly
816 double cos_lat = f->get_cos_latitude(); // cos(-f->get_Latitude());
817 double sin_lat = -f->get_sin_latitude(); // sin(-f->get_Latitude());
818 double cos_lon = f->get_cos_longitude(); //cos(f->get_Longitude());
819 double sin_lon = f->get_sin_longitude(); //sin(f->get_Longitude());
821 double *mat = (double *)UP;
823 mat[0] = cos_lat*cos_lon;
824 mat[1] = cos_lat*sin_lon;
831 mat[8] = sin_lat*cos_lon;
832 mat[9] = sin_lat*sin_lon;
834 mat[11] = mat[12] = mat[13] = mat[14] = 0.0;
837 MAT3mult(VIEW, LOCAL, UP);
839 // THESE COULD JUST BE POINTERS !!!
840 MAT3_SET_VEC(local_up, mat[0], mat[1], mat[2]);
841 MAT3_SET_VEC(view_up, VIEW[0][0], VIEW[0][1], VIEW[0][2]);
842 MAT3_SET_VEC(forward, VIEW[2][0], VIEW[2][1], VIEW[2][2]);
844 getRotMatrix((double *)TMP, view_up, view_offset);
845 MAT3mult_vec(view_forward, forward, TMP);
847 // make a vector to the current view position
848 MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
850 // Given a vector pointing straight down (-Z), map into onto the
851 // local plane representing "horizontal". This should give us the
852 // local direction for moving "south".
853 MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
854 map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
856 MAT3_NORMALIZE_VEC(surface_south, ntmp);
857 // printf( "Surface direction directly south %.6f %.6f %.6f\n",
858 // surface_south[0], surface_south[1], surface_south[2]);
860 // now calculate the surface east vector
861 getRotMatrix((double *)TMP, view_up, FG_PI_2);
862 MAT3mult_vec(surface_east, surface_south, TMP);
863 // printf( "Surface direction directly east %.6f %.6f %.6f\n",
864 // surface_east[0], surface_east[1], surface_east[2]);
865 // printf( "Should be close to zero = %.6f\n",
866 // MAT3_DOT_PRODUCT(surface_south, surface_east));
867 #endif // !defined(FG_VIEW_INLINE_OPTIMIZATIONS)
872 FGView::~FGView( void ) {