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@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.
29 #include <plib/ssg.h> // plib include
31 #include <simgear/constants.h>
32 #include <simgear/debug/logstream.hxx>
33 #include <simgear/math/point3d.hxx>
34 #include <simgear/math/polar3d.hxx>
35 #include <simgear/math/vector.hxx>
37 #include <Aircraft/aircraft.hxx>
38 #include <Cockpit/panel.hxx>
39 #include <Scenery/scenery.hxx>
41 #include "options.hxx"
45 // This is a record containing current view parameters for the current
49 // This is a record containing current view parameters for the current
55 FGView::FGView( void ) {
59 // Initialize a view structure
60 void FGView::Init( void ) {
61 FG_LOG( FG_VIEW, FG_INFO, "Initializing View parameters" );
64 goal_view_offset = 0.0;
65 sgSetVec3( pilot_offset, 0.0, 0.0, 0.0 );
67 winWidth = current_options.get_xsize();
68 winHeight = current_options.get_ysize();
70 if ( ! current_options.get_panel_status() ) {
71 set_win_ratio( winHeight / winWidth );
73 set_win_ratio( (winHeight*0.4232) / winWidth );
76 // This never changes -- NHV
77 LARC_TO_SSG[0][0] = 0.0;
78 LARC_TO_SSG[0][1] = 1.0;
79 LARC_TO_SSG[0][2] = -0.0;
80 LARC_TO_SSG[0][3] = 0.0;
82 LARC_TO_SSG[1][0] = 0.0;
83 LARC_TO_SSG[1][1] = 0.0;
84 LARC_TO_SSG[1][2] = 1.0;
85 LARC_TO_SSG[1][3] = 0.0;
87 LARC_TO_SSG[2][0] = 1.0;
88 LARC_TO_SSG[2][1] = -0.0;
89 LARC_TO_SSG[2][2] = 0.0;
90 LARC_TO_SSG[2][3] = 0.0;
92 LARC_TO_SSG[3][0] = 0.0;
93 LARC_TO_SSG[3][1] = 0.0;
94 LARC_TO_SSG[3][2] = 0.0;
95 LARC_TO_SSG[3][3] = 1.0;
97 force_update_fov_math();
101 #define USE_FAST_LOCAL
102 #ifdef USE_FAST_LOCAL
103 inline static void fgMakeLOCAL( sgMat4 dst, const double Theta,
104 const double Phi, const double Psi)
106 SGfloat cosTheta = (SGfloat) cos(Theta);
107 SGfloat sinTheta = (SGfloat) sin(Theta);
108 SGfloat cosPhi = (SGfloat) cos(Phi);
109 SGfloat sinPhi = (SGfloat) sin(Phi);
110 SGfloat sinPsi = (SGfloat) sin(Psi) ;
111 SGfloat cosPsi = (SGfloat) cos(Psi) ;
113 dst[0][0] = cosPhi * cosTheta;
114 dst[0][1] = sinPhi * cosPsi + cosPhi * -sinTheta * -sinPsi;
115 dst[0][2] = sinPhi * sinPsi + cosPhi * -sinTheta * cosPsi;
118 dst[1][0] = -sinPhi * cosTheta;
119 dst[1][1] = cosPhi * cosPsi + -sinPhi * -sinTheta * -sinPsi;
120 dst[1][2] = cosPhi * sinPsi + -sinPhi * -sinTheta * cosPsi;
121 dst[1][3] = SG_ZERO ;
123 dst[2][0] = sinTheta;
124 dst[2][1] = cosTheta * -sinPsi;
125 dst[2][2] = cosTheta * cosPsi;
136 // Update the view volume, position, and orientation
137 void FGView::UpdateViewParams( const FGInterface& f ) {
140 if ( ! current_options.get_panel_status() ) {
141 xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
143 xglViewport(0, (GLint)((winHeight)*0.5768), (GLint)(winWidth),
144 (GLint)((winHeight)*0.4232) );
149 // convert sgMat4 to MAT3 and print
150 static void print_sgMat4( sgMat4 &in) {
152 for ( i = 0; i < 4; i++ ) {
153 for ( j = 0; j < 4; j++ ) {
154 printf("%10.4f ", in[i][j]);
161 // Update the view parameters
162 void FGView::UpdateViewMath( const FGInterface& f ) {
165 sgVec3 v0, minus_z, sgvec, forward;
169 ssgSetFOV( current_options.get_fov(),
170 current_options.get_fov() * win_ratio );
174 scenery.center = scenery.next_center;
176 // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x,
177 // scenery.center.y, scenery.center.z);
179 // calculate the cartesion coords of the current lat/lon/0 elev
180 p = Point3D( f.get_Longitude(),
181 f.get_Lat_geocentric(),
182 f.get_Sea_level_radius() * FEET_TO_METER );
184 cur_zero_elev = fgPolarToCart3d(p) - scenery.center;
186 // calculate view position in current FG view coordinate system
187 // p.lon & p.lat are already defined earlier, p.radius was set to
188 // the sea level radius, so now we add in our altitude.
189 if ( f.get_Altitude() * FEET_TO_METER >
190 (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
191 p.setz( p.radius() + f.get_Altitude() * FEET_TO_METER );
193 p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
196 abs_view_pos = fgPolarToCart3d(p);
198 view_pos = abs_view_pos - scenery.center;
200 FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
201 FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
202 FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = " << view_pos );
204 // code to calculate LOCAL matrix calculated from Phi, Theta, and
205 // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
208 #ifdef USE_FAST_LOCAL
210 fgMakeLOCAL( LOCAL, f.get_Theta(), f.get_Phi(), -f.get_Psi() );
212 #else // USE_TEXT_BOOK_METHOD
215 sgSetVec3( rollvec, 0.0, 0.0, 1.0 );
217 sgMakeRotMat4( PHI, f.get_Phi() * RAD_TO_DEG, rollvec );
220 sgSetVec3( pitchvec, 0.0, 1.0, 0.0 );
221 sgMat4 THETA; // pitch
222 sgMakeRotMat4( THETA, f.get_Theta() * RAD_TO_DEG, pitchvec );
226 // sgMultMat4( ROT, PHI, THETA );
227 sgCopyMat4( ROT, PHI );
228 sgPostMultMat4( ROT, THETA );
231 sgSetVec3( yawvec, 1.0, 0.0, 0.0 );
233 sgMakeRotMat4( PSI, -f.get_Psi() * RAD_TO_DEG, yawvec );
236 // sgMultMat4( LOCAL, ROT, PSI );
237 sgCopyMat4( LOCAL, ROT );
238 sgPostMultMat4( LOCAL, PSI );
242 // cout << "LOCAL matrix" << endl;
243 // print_sgMat4( LOCAL );
246 f.get_Longitude() * RAD_TO_DEG,
248 -f.get_Latitude() * RAD_TO_DEG );
250 sgSetVec3( local_up, UP[0][0], UP[0][1], UP[0][2] );
251 // sgXformVec3( local_up, UP );
252 // cout << "Local Up = " << local_up[0] << "," << local_up[1] << ","
253 // << local_up[2] << endl;
255 // Alternative method to Derive local up vector based on
256 // *geodetic* coordinates
257 // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
258 // printf( " Alt Up = (%.4f, %.4f, %.4f)\n",
259 // alt_up.x, alt_up.y, alt_up.z);
261 // VIEWo = LOCAL * UP
262 // sgMultMat4( VIEWo, LOCAL, UP );
263 sgCopyMat4( VIEWo, LOCAL );
264 sgPostMultMat4( VIEWo, UP );
265 // cout << "VIEWo matrix" << endl;
266 // print_sgMat4( VIEWo );
268 // generate the sg view up and forward vectors
269 sgSetVec3( view_up, VIEWo[0][0], VIEWo[0][1], VIEWo[0][2] );
270 // cout << "view = " << view[0] << ","
271 // << view[1] << "," << view[2] << endl;
272 sgSetVec3( forward, VIEWo[2][0], VIEWo[2][1], VIEWo[2][2] );
273 // cout << "forward = " << forward[0] << ","
274 // << forward[1] << "," << forward[2] << endl;
276 // generate the pilot offset vector in world coordinates
277 sgVec3 pilot_offset_world;
278 sgSetVec3( pilot_offset_world,
279 pilot_offset[2], pilot_offset[1], -pilot_offset[0] );
280 sgXformVec3( pilot_offset_world, pilot_offset_world, VIEWo );
282 // generate the view offset matrix
283 sgMakeRotMat4( VIEW_OFFSET, view_offset * RAD_TO_DEG, view_up );
284 // cout << "VIEW_OFFSET matrix" << endl;
285 // print_sgMat4( VIEW_OFFSET );
287 // VIEW_ROT = LARC_TO_SSG * ( VIEWo * VIEW_OFFSET )
289 // sgMultMat4( TMP2, VIEWo, VIEW_OFFSET );
290 // sgMultMat4( VIEW_ROT, LARC_TO_SSG, TMP2 );
291 sgCopyMat4( VIEW_ROT, VIEWo );
292 sgPostMultMat4( VIEW_ROT, VIEW_OFFSET );
293 sgPreMultMat4( VIEW_ROT, LARC_TO_SSG );
295 // cout << "VIEW_ROT matrix" << endl;
296 // print_sgMat4( VIEW_ROT );
300 sgSetVec3( trans_vec,
301 view_pos.x() + pilot_offset_world[0],
302 view_pos.y() + pilot_offset_world[1],
303 view_pos.z() + pilot_offset_world[2] );
305 // VIEW = VIEW_ROT * TRANS
306 sgCopyMat4( VIEW, VIEW_ROT );
307 sgPostMultMat4ByTransMat4( VIEW, trans_vec );
309 //!!!!!!!!!!!!!!!!!!!
310 // THIS IS THE EXPERIMENTAL VIEWING ANGLE SHIFTER
311 // THE MAJORITY OF THE WORK IS DONE IN GUI.CXX
312 // this in gui.cxx for now just testing
313 extern float quat_mat[4][4];
314 sgPreMultMat4( VIEW, quat_mat);
315 // !!!!!!!!!! testing
317 sgMakeRotMat4( TMP, view_offset * RAD_TO_DEG, view_up );
318 sgXformVec3( view_forward, forward, TMP );
319 // cout << "view_forward = " << view_forward[0] << ","
320 // << view_forward[1] << "," << view_forward[2] << endl;
322 // make a vector to the current view position
323 sgSetVec3( v0, view_pos.x(), view_pos.y(), view_pos.z() );
325 // Given a vector pointing straight down (-Z), map into onto the
326 // local plane representing "horizontal". This should give us the
327 // local direction for moving "south".
328 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
330 sgmap_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
331 sgNormalizeVec3(surface_south);
332 // cout << "Surface direction directly south " << surface_south[0] << ","
333 // << surface_south[1] << "," << surface_south[2] << endl;
335 // now calculate the surface east vector
336 sgMakeRotMat4( TMP, FG_PI_2 * RAD_TO_DEG, view_up );
337 // cout << "sgMat4 TMP" << endl;
338 // print_sgMat4( TMP );
339 sgXformVec3(surface_east, surface_south, TMP);
340 // cout << "Surface direction directly east" << surface_east[0] << ","
341 // << surface_east[1] << "," << surface_east[2] << endl;
342 // cout << "Should be close to zero = "
343 // << sgScalarProductVec3(surface_south, surface_east) << endl;
348 FGView::~FGView( void ) {