1 // viewer.cxx -- class for managing a viewer in the flightgear world.
3 // Written by Curtis Olson, started August 1997.
4 // overhaul started October 2000.
6 // Copyright (C) 1997 - 2000 Curtis L. Olson - curt@flightgear.org
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.
25 #include <simgear/compiler.h>
31 #include <plib/ssg.h> // plib include
33 #include <simgear/constants.h>
34 #include <simgear/debug/logstream.hxx>
35 #include <simgear/math/point3d.hxx>
36 #include <simgear/math/polar3d.hxx>
37 #include <simgear/math/vector.hxx>
39 #include <Aircraft/aircraft.hxx>
40 #include <Cockpit/panel.hxx>
41 #include <Scenery/scenery.hxx>
43 #include "globals.hxx"
48 FGViewer::FGViewer( void )
53 #define USE_FAST_VIEWROT
54 #ifdef USE_FAST_VIEWROT
55 // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
56 // This takes advantage of the fact that VIEWo and VIEW_OFFSET
57 // only have entries in the upper 3x3 block
58 // and that LARC_TO_SSG is just a shift of rows NHV
59 inline static void fgMakeViewRot( sgMat4 dst, const sgMat4 m1, const sgMat4 m2 )
61 for ( int j = 0 ; j < 3 ; j++ ) {
62 dst[2][j] = m2[0][0] * m1[0][j] +
66 dst[0][j] = m2[1][0] * m1[0][j] +
70 dst[1][j] = m2[2][0] * m1[0][j] +
85 // Initialize a view structure
86 void FGViewer::init( void ) {
89 FG_LOG( FG_VIEW, FG_INFO, "Initializing View parameters" );
91 view_offset = goal_view_offset =
92 globals->get_options()->get_default_view_offset();
93 sgSetVec3( pilot_offset, 0.0, 0.0, 0.0 );
95 globals->get_options()->set_win_ratio( globals->get_options()->get_xsize() /
96 globals->get_options()->get_ysize()
99 #ifndef USE_FAST_VIEWROT
100 // This never changes -- NHV
101 LARC_TO_SSG[0][0] = 0.0;
102 LARC_TO_SSG[0][1] = 1.0;
103 LARC_TO_SSG[0][2] = -0.0;
104 LARC_TO_SSG[0][3] = 0.0;
106 LARC_TO_SSG[1][0] = 0.0;
107 LARC_TO_SSG[1][1] = 0.0;
108 LARC_TO_SSG[1][2] = 1.0;
109 LARC_TO_SSG[1][3] = 0.0;
111 LARC_TO_SSG[2][0] = 1.0;
112 LARC_TO_SSG[2][1] = -0.0;
113 LARC_TO_SSG[2][2] = 0.0;
114 LARC_TO_SSG[2][3] = 0.0;
116 LARC_TO_SSG[3][0] = 0.0;
117 LARC_TO_SSG[3][1] = 0.0;
118 LARC_TO_SSG[3][2] = 0.0;
119 LARC_TO_SSG[3][3] = 1.0;
120 #endif // USE_FAST_VIEWROT
124 #define USE_FAST_LOCAL
125 #ifdef USE_FAST_LOCAL
126 inline static void fgMakeLOCAL( sgMat4 dst, const double Theta,
127 const double Phi, const double Psi)
129 SGfloat cosTheta = (SGfloat) cos(Theta);
130 SGfloat sinTheta = (SGfloat) sin(Theta);
131 SGfloat cosPhi = (SGfloat) cos(Phi);
132 SGfloat sinPhi = (SGfloat) sin(Phi);
133 SGfloat sinPsi = (SGfloat) sin(Psi) ;
134 SGfloat cosPsi = (SGfloat) cos(Psi) ;
136 dst[0][0] = cosPhi * cosTheta;
137 dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
138 dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
141 dst[1][0] = -sinPhi * cosTheta;
142 dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
143 dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
144 dst[1][3] = SG_ZERO ;
146 dst[2][0] = sinTheta;
147 dst[2][1] = cosTheta * -sinPsi;
148 dst[2][2] = cosTheta * cosPsi;
159 // convert sgMat4 to MAT3 and print
160 static void print_sgMat4( sgMat4 &in) {
162 for ( i = 0; i < 4; i++ ) {
163 for ( j = 0; j < 4; j++ ) {
164 printf("%10.4f ", in[i][j]);
171 // Update the view parameters
172 void FGViewer::update() {
174 sgVec3 minus_z, forward;
177 // calculate the cartesion coords of the current lat/lon/0 elev
178 Point3D p = Point3D( geod_view_pos[0],
182 tmp = sgPolarToCart3d(p) - scenery.center;
183 sgSetVec3( zero_elev, tmp[0], tmp[1], tmp[2] );
185 // calculate view position in current FG view coordinate system
186 // p.lon & p.lat are already defined earlier, p.radius was set to
187 // the sea level radius, so now we add in our altitude.
188 if ( geod_view_pos[2] > (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
189 p.setz( p.radius() + geod_view_pos[2] );
191 p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
194 tmp = sgPolarToCart3d(p);
195 sgdSetVec3( abs_view_pos, tmp[0], tmp[1], tmp[2] );
198 sgdSetVec3( sc, scenery.center.x(), scenery.center.y(), scenery.center.z());
200 sgdSubVec3( vp, abs_view_pos, sc );
201 sgSetVec3( view_pos, vp );
202 // view_pos = abs_view_pos - scenery.center;
204 FG_LOG( FG_VIEW, FG_DEBUG, "sea level radius = " << sea_level_radius );
205 FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
206 FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = "
207 << abs_view_pos[0] << ","
208 << abs_view_pos[1] << ","
209 << abs_view_pos[2] );
210 FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = "
211 << view_pos[0] << "," << view_pos[1] << "," << view_pos[2] );
213 // code to calculate LOCAL matrix calculated from Phi, Theta, and
214 // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
217 #ifdef USE_FAST_LOCAL
219 fgMakeLOCAL( LOCAL, rph[1], rph[0], -rph[2] );
221 #else // USE_TEXT_BOOK_METHOD
224 sgSetVec3( rollvec, 0.0, 0.0, 1.0 );
226 sgMakeRotMat4( PHI, rph[0] * RAD_TO_DEG, rollvec );
229 sgSetVec3( pitchvec, 0.0, 1.0, 0.0 );
230 sgMat4 THETA; // pitch
231 sgMakeRotMat4( THETA, rph[1] * RAD_TO_DEG, pitchvec );
235 // sgMultMat4( ROT, PHI, THETA );
236 sgCopyMat4( ROT, PHI );
237 sgPostMultMat4( ROT, THETA );
240 sgSetVec3( yawvec, 1.0, 0.0, 0.0 );
241 sgMat4 PSI; // heading
242 sgMakeRotMat4( PSI, -rph[2] * RAD_TO_DEG, yawvec );
245 // sgMultMat4( LOCAL, ROT, PSI );
246 sgCopyMat4( LOCAL, ROT );
247 sgPostMultMat4( LOCAL, PSI );
249 #endif // USE_FAST_LOCAL
251 // cout << "LOCAL matrix" << endl;
252 // print_sgMat4( LOCAL );
255 geod_view_pos[0] * RAD_TO_DEG,
257 -geod_view_pos[1] * RAD_TO_DEG );
259 sgSetVec3( local_up, UP[0][0], UP[0][1], UP[0][2] );
260 // sgXformVec3( local_up, UP );
261 // cout << "Local Up = " << local_up[0] << "," << local_up[1] << ","
262 // << local_up[2] << endl;
264 // Alternative method to Derive local up vector based on
265 // *geodetic* coordinates
266 // alt_up = sgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
267 // printf( " Alt Up = (%.4f, %.4f, %.4f)\n",
268 // alt_up.x, alt_up.y, alt_up.z);
270 // VIEWo = LOCAL * UP
271 // sgMultMat4( VIEWo, LOCAL, UP );
272 sgCopyMat4( VIEWo, LOCAL );
273 sgPostMultMat4( VIEWo, UP );
274 // cout << "VIEWo matrix" << endl;
275 // print_sgMat4( VIEWo );
277 // generate the sg view up and forward vectors
278 sgSetVec3( view_up, VIEWo[0][0], VIEWo[0][1], VIEWo[0][2] );
279 // cout << "view = " << view[0] << ","
280 // << view[1] << "," << view[2] << endl;
281 sgSetVec3( forward, VIEWo[2][0], VIEWo[2][1], VIEWo[2][2] );
282 // cout << "forward = " << forward[0] << ","
283 // << forward[1] << "," << forward[2] << endl;
285 // generate the pilot offset vector in world coordinates
286 sgVec3 pilot_offset_world;
287 sgSetVec3( pilot_offset_world,
288 pilot_offset[2], pilot_offset[1], -pilot_offset[0] );
289 sgXformVec3( pilot_offset_world, pilot_offset_world, VIEWo );
291 // generate the view offset matrix
292 sgMakeRotMat4( VIEW_OFFSET, view_offset * RAD_TO_DEG, view_up );
293 // cout << "VIEW_OFFSET matrix" << endl;
294 // print_sgMat4( VIEW_OFFSET );
295 sgXformVec3( view_forward, forward, VIEW_OFFSET );
296 // cout << "view_forward = " << view_forward[0] << ","
297 // << view_forward[1] << "," << view_forward[2] << endl;
299 // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
300 #ifdef USE_FAST_VIEWROT
301 fgMakeViewRot( VIEW_ROT, VIEW_OFFSET, VIEWo );
303 // sgMultMat4( VIEW_ROT, VIEW_OFFSET, VIEWo );
304 // sgPreMultMat4( VIEW_ROT, LARC_TO_SSG );
305 sgCopyMat4( VIEW_ROT, VIEWo );
306 sgPostMultMat4( VIEW_ROT, VIEW_OFFSET );
307 sgPreMultMat4( VIEW_ROT, LARC_TO_SSG );
309 // cout << "VIEW_ROT matrix" << endl;
310 // print_sgMat4( VIEW_ROT );
313 sgAddVec3( trans_vec, view_pos, pilot_offset_world );
315 // VIEW = VIEW_ROT * TRANS
316 sgCopyMat4( VIEW, VIEW_ROT );
317 sgPostMultMat4ByTransMat4( VIEW, trans_vec );
319 //!!!!!!!!!!!!!!!!!!!
320 // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
321 // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
322 // this in gui.cxx for now just testing
323 extern float quat_mat[4][4];
324 sgPreMultMat4( VIEW, quat_mat);
325 // !!!!!!!!!! testing
327 // Given a vector pointing straight down (-Z), map into onto the
328 // local plane representing "horizontal". This should give us the
329 // local direction for moving "south".
330 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
332 sgmap_vec_onto_cur_surface_plane(local_up, view_pos, minus_z,
334 sgNormalizeVec3(surface_south);
335 // cout << "Surface direction directly south " << surface_south[0] << ","
336 // << surface_south[1] << "," << surface_south[2] << endl;
338 // now calculate the surface east vector
339 #define USE_FAST_SURFACE_EAST
340 #ifdef USE_FAST_SURFACE_EAST
342 sgNegateVec3(local_down, local_up);
343 sgVectorProductVec3(surface_east, surface_south, local_down);
347 sgMakeRotMat4( TMP, FG_PI_2 * RAD_TO_DEG, local_up );
349 sgMakeRotMat4( TMP, FG_PI_2 * RAD_TO_DEG, view_up );
350 #endif // USE_LOCAL_UP
351 // cout << "sgMat4 TMP" << endl;
352 // print_sgMat4( TMP );
353 sgXformVec3(surface_east, surface_south, TMP);
354 #endif // USE_FAST_SURFACE_EAST
355 // cout << "Surface direction directly east " << surface_east[0] << ","
356 // << surface_east[1] << "," << surface_east[2] << endl;
357 // cout << "Should be close to zero = "
358 // << sgScalarProductVec3(surface_south, surface_east) << endl;
364 void FGViewer::CurrentNormalInLocalPlane(sgVec3 dst, sgVec3 src) {
366 sgSetVec3(tmp, src[0], src[1], src[2] );
368 sgTransposeNegateMat4 ( TMP, UP ) ;
369 sgXformVec3(tmp, tmp, TMP);
370 sgSetVec3(dst, tmp[2], tmp[1], tmp[0] );
375 FGViewer::~FGViewer( void ) {