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 ) {
51 #define USE_FAST_VIEWROT
52 #ifdef USE_FAST_VIEWROT
53 // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
54 // This takes advantage of the fact that VIEWo and VIEW_OFFSET
55 // only have entries in the upper 3x3 block
56 // and that LARC_TO_SSG is just a shift of rows NHV
57 inline static void fgMakeViewRot( sgMat4 dst, const sgMat4 m1, const sgMat4 m2 )
59 for ( int j = 0 ; j < 3 ; j++ ) {
60 dst[2][j] = m2[0][0] * m1[0][j] +
64 dst[0][j] = m2[1][0] * m1[0][j] +
68 dst[1][j] = m2[2][0] * m1[0][j] +
82 // Initialize a view structure
83 void FGViewer::Init( void ) {
84 FG_LOG( FG_VIEW, FG_INFO, "Initializing View parameters" );
86 view_offset = goal_view_offset = globals->get_options()->get_default_view_offset();
87 sgSetVec3( pilot_offset, 0.0, 0.0, 0.0 );
89 set_win_ratio( globals->get_options()->get_xsize() /
90 globals->get_options()->get_ysize() );
92 #ifndef USE_FAST_VIEWROT
93 // This never changes -- NHV
94 LARC_TO_SSG[0][0] = 0.0;
95 LARC_TO_SSG[0][1] = 1.0;
96 LARC_TO_SSG[0][2] = -0.0;
97 LARC_TO_SSG[0][3] = 0.0;
99 LARC_TO_SSG[1][0] = 0.0;
100 LARC_TO_SSG[1][1] = 0.0;
101 LARC_TO_SSG[1][2] = 1.0;
102 LARC_TO_SSG[1][3] = 0.0;
104 LARC_TO_SSG[2][0] = 1.0;
105 LARC_TO_SSG[2][1] = -0.0;
106 LARC_TO_SSG[2][2] = 0.0;
107 LARC_TO_SSG[2][3] = 0.0;
109 LARC_TO_SSG[3][0] = 0.0;
110 LARC_TO_SSG[3][1] = 0.0;
111 LARC_TO_SSG[3][2] = 0.0;
112 LARC_TO_SSG[3][3] = 1.0;
113 #endif // USE_FAST_VIEWROT
115 force_update_fov_math();
119 #define USE_FAST_LOCAL
120 #ifdef USE_FAST_LOCAL
121 inline static void fgMakeLOCAL( sgMat4 dst, const double Theta,
122 const double Phi, const double Psi)
124 SGfloat cosTheta = (SGfloat) cos(Theta);
125 SGfloat sinTheta = (SGfloat) sin(Theta);
126 SGfloat cosPhi = (SGfloat) cos(Phi);
127 SGfloat sinPhi = (SGfloat) sin(Phi);
128 SGfloat sinPsi = (SGfloat) sin(Psi) ;
129 SGfloat cosPsi = (SGfloat) cos(Psi) ;
131 dst[0][0] = cosPhi * cosTheta;
132 dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
133 dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
136 dst[1][0] = -sinPhi * cosTheta;
137 dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
138 dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
139 dst[1][3] = SG_ZERO ;
141 dst[2][0] = sinTheta;
142 dst[2][1] = cosTheta * -sinPsi;
143 dst[2][2] = cosTheta * cosPsi;
154 // Update the view volume, position, and orientation
155 void FGViewer::UpdateViewParams( const FGInterface& f ) {
158 if ( ! fgPanelVisible() ) {
160 (GLint)(globals->get_options()->get_xsize()),
161 (GLint)(globals->get_options()->get_ysize()) );
164 int((current_panel->getViewHeight() - current_panel->getYOffset())
165 * (globals->get_options()->get_ysize() / 768.0));
166 glViewport( 0, (GLint)(globals->get_options()->get_ysize() - view_h),
167 (GLint)(globals->get_options()->get_xsize()),
173 // convert sgMat4 to MAT3 and print
174 static void print_sgMat4( sgMat4 &in) {
176 for ( i = 0; i < 4; i++ ) {
177 for ( j = 0; j < 4; j++ ) {
178 printf("%10.4f ", in[i][j]);
185 // Update the view parameters
186 void FGViewer::UpdateViewMath( const FGInterface& f ) {
189 sgVec3 v0, minus_z, sgvec, forward;
193 ssgSetFOV( globals->get_options()->get_fov(),
194 globals->get_options()->get_fov() * win_ratio );
198 scenery.center = scenery.next_center;
200 // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x,
201 // scenery.center.y, scenery.center.z);
203 // calculate the cartesion coords of the current lat/lon/0 elev
204 p = Point3D( f.get_Longitude(),
205 f.get_Lat_geocentric(),
206 f.get_Sea_level_radius() * FEET_TO_METER );
208 cur_zero_elev = sgPolarToCart3d(p) - scenery.center;
210 // calculate view position in current FG view coordinate system
211 // p.lon & p.lat are already defined earlier, p.radius was set to
212 // the sea level radius, so now we add in our altitude.
213 if ( f.get_Altitude() * FEET_TO_METER >
214 (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
215 p.setz( p.radius() + f.get_Altitude() * FEET_TO_METER );
217 p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
220 abs_view_pos = sgPolarToCart3d(p);
222 view_pos = abs_view_pos - scenery.center;
224 FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
225 FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
226 FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = " << view_pos );
228 // code to calculate LOCAL matrix calculated from Phi, Theta, and
229 // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
232 #ifdef USE_FAST_LOCAL
234 fgMakeLOCAL( LOCAL, f.get_Theta(), f.get_Phi(), -f.get_Psi() );
236 #else // USE_TEXT_BOOK_METHOD
239 sgSetVec3( rollvec, 0.0, 0.0, 1.0 );
241 sgMakeRotMat4( PHI, f.get_Phi() * RAD_TO_DEG, rollvec );
244 sgSetVec3( pitchvec, 0.0, 1.0, 0.0 );
245 sgMat4 THETA; // pitch
246 sgMakeRotMat4( THETA, f.get_Theta() * RAD_TO_DEG, pitchvec );
250 // sgMultMat4( ROT, PHI, THETA );
251 sgCopyMat4( ROT, PHI );
252 sgPostMultMat4( ROT, THETA );
255 sgSetVec3( yawvec, 1.0, 0.0, 0.0 );
257 sgMakeRotMat4( PSI, -f.get_Psi() * RAD_TO_DEG, yawvec );
260 // sgMultMat4( LOCAL, ROT, PSI );
261 sgCopyMat4( LOCAL, ROT );
262 sgPostMultMat4( LOCAL, PSI );
266 // cout << "LOCAL matrix" << endl;
267 // print_sgMat4( LOCAL );
270 f.get_Longitude() * RAD_TO_DEG,
272 -f.get_Latitude() * RAD_TO_DEG );
274 sgSetVec3( local_up, UP[0][0], UP[0][1], UP[0][2] );
275 // sgXformVec3( local_up, UP );
276 // cout << "Local Up = " << local_up[0] << "," << local_up[1] << ","
277 // << local_up[2] << endl;
279 // Alternative method to Derive local up vector based on
280 // *geodetic* coordinates
281 // alt_up = sgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
282 // printf( " Alt Up = (%.4f, %.4f, %.4f)\n",
283 // alt_up.x, alt_up.y, alt_up.z);
285 // VIEWo = LOCAL * UP
286 // sgMultMat4( VIEWo, LOCAL, UP );
287 sgCopyMat4( VIEWo, LOCAL );
288 sgPostMultMat4( VIEWo, UP );
289 // cout << "VIEWo matrix" << endl;
290 // print_sgMat4( VIEWo );
292 // generate the sg view up and forward vectors
293 sgSetVec3( view_up, VIEWo[0][0], VIEWo[0][1], VIEWo[0][2] );
294 // cout << "view = " << view[0] << ","
295 // << view[1] << "," << view[2] << endl;
296 sgSetVec3( forward, VIEWo[2][0], VIEWo[2][1], VIEWo[2][2] );
297 // cout << "forward = " << forward[0] << ","
298 // << forward[1] << "," << forward[2] << endl;
300 // generate the pilot offset vector in world coordinates
301 sgVec3 pilot_offset_world;
302 sgSetVec3( pilot_offset_world,
303 pilot_offset[2], pilot_offset[1], -pilot_offset[0] );
304 sgXformVec3( pilot_offset_world, pilot_offset_world, VIEWo );
306 // generate the view offset matrix
307 sgMakeRotMat4( VIEW_OFFSET, view_offset * RAD_TO_DEG, view_up );
308 // cout << "VIEW_OFFSET matrix" << endl;
309 // print_sgMat4( VIEW_OFFSET );
310 sgXformVec3( view_forward, forward, VIEW_OFFSET );
311 // cout << "view_forward = " << view_forward[0] << ","
312 // << view_forward[1] << "," << view_forward[2] << endl;
314 // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
315 #ifdef USE_FAST_VIEWROT
316 fgMakeViewRot( VIEW_ROT, VIEW_OFFSET, VIEWo );
318 // sgMultMat4( VIEW_ROT, VIEW_OFFSET, VIEWo );
319 // sgPreMultMat4( VIEW_ROT, LARC_TO_SSG );
320 sgCopyMat4( VIEW_ROT, VIEWo );
321 sgPostMultMat4( VIEW_ROT, VIEW_OFFSET );
322 sgPreMultMat4( VIEW_ROT, LARC_TO_SSG );
324 // cout << "VIEW_ROT matrix" << endl;
325 // print_sgMat4( VIEW_ROT );
328 sgSetVec3( trans_vec,
329 view_pos.x() + pilot_offset_world[0],
330 view_pos.y() + pilot_offset_world[1],
331 view_pos.z() + pilot_offset_world[2] );
333 // VIEW = VIEW_ROT * TRANS
334 sgCopyMat4( VIEW, VIEW_ROT );
335 sgPostMultMat4ByTransMat4( VIEW, trans_vec );
337 //!!!!!!!!!!!!!!!!!!!
338 // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
339 // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
340 // this in gui.cxx for now just testing
341 extern float quat_mat[4][4];
342 sgPreMultMat4( VIEW, quat_mat);
343 // !!!!!!!!!! testing
345 // make a vector to the current view position
346 sgSetVec3( v0, view_pos.x(), view_pos.y(), view_pos.z() );
348 // Given a vector pointing straight down (-Z), map into onto the
349 // local plane representing "horizontal". This should give us the
350 // local direction for moving "south".
351 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
353 sgmap_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
354 sgNormalizeVec3(surface_south);
355 // cout << "Surface direction directly south " << surface_south[0] << ","
356 // << surface_south[1] << "," << surface_south[2] << endl;
358 // now calculate the surface east vector
359 #define USE_FAST_SURFACE_EAST
360 #ifdef USE_FAST_SURFACE_EAST
362 sgNegateVec3(local_down, local_up);
363 sgVectorProductVec3(surface_east, surface_south, local_down);
367 sgMakeRotMat4( TMP, FG_PI_2 * RAD_TO_DEG, local_up );
369 sgMakeRotMat4( TMP, FG_PI_2 * RAD_TO_DEG, view_up );
370 #endif // USE_LOCAL_UP
371 // cout << "sgMat4 TMP" << endl;
372 // print_sgMat4( TMP );
373 sgXformVec3(surface_east, surface_south, TMP);
374 #endif // USE_FAST_SURFACE_EAST
375 // cout << "Surface direction directly east " << surface_east[0] << ","
376 // << surface_east[1] << "," << surface_east[2] << endl;
377 // cout << "Should be close to zero = "
378 // << sgScalarProductVec3(surface_south, surface_east) << endl;
382 void FGViewer::CurrentNormalInLocalPlane(sgVec3 dst, sgVec3 src) {
384 sgSetVec3(tmp, src[0], src[1], src[2] );
386 sgTransposeNegateMat4 ( TMP, UP ) ;
387 sgXformVec3(tmp, tmp, TMP);
388 sgSetVec3(dst, tmp[2], tmp[1], tmp[0] );
393 FGViewer::~FGViewer( void ) {