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/point3d.hxx>
36 #include <Math/polar3d.hxx>
37 #include <Math/vector.hxx>
38 #include <Scenery/scenery.hxx>
39 #include <Time/fg_time.hxx>
41 #include "options.hxx"
45 // temporary (hopefully) hack
46 static int panel_hist = 0;
49 // This is a record containing current view parameters for the current
53 // This is a record containing current view parameters for the current
59 FGView::FGView( void ) {
63 // Initialize a view structure
64 void FGView::Init( void ) {
65 FG_LOG( FG_VIEW, FG_INFO, "Initializing View parameters" );
68 goal_view_offset = 0.0;
69 sgSetVec3( pilot_offset, 0.0, 0.0, 0.0 );
71 winWidth = current_options.get_xsize();
72 winHeight = current_options.get_ysize();
74 if ( ! current_options.get_panel_status() ) {
75 current_view.set_win_ratio( (GLfloat) winWidth / (GLfloat) winHeight );
77 current_view.set_win_ratio( (GLfloat) winWidth /
78 ((GLfloat) (winHeight)*0.4232) );
81 // This never changes -- NHV
82 LARC_TO_SSG[0][0] = 0.0;
83 LARC_TO_SSG[0][1] = 1.0;
84 LARC_TO_SSG[0][2] = -0.0;
85 LARC_TO_SSG[0][3] = 0.0;
87 LARC_TO_SSG[1][0] = 0.0;
88 LARC_TO_SSG[1][1] = 0.0;
89 LARC_TO_SSG[1][2] = 1.0;
90 LARC_TO_SSG[1][3] = 0.0;
92 LARC_TO_SSG[2][0] = 1.0;
93 LARC_TO_SSG[2][1] = -0.0;
94 LARC_TO_SSG[2][2] = 0.0;
95 LARC_TO_SSG[2][3] = 0.0;
97 LARC_TO_SSG[3][0] = 0.0;
98 LARC_TO_SSG[3][1] = 0.0;
99 LARC_TO_SSG[3][2] = 0.0;
100 LARC_TO_SSG[3][3] = 1.0;
102 force_update_fov_math();
105 // Update the view volume, position, and orientation
106 void FGView::UpdateViewParams( const FGInterface& f ) {
109 if ((current_options.get_panel_status() != panel_hist) && (current_options.get_panel_status()))
111 FGPanel::OurPanel->ReInit( 0, 0, 1024, 768);
114 if ( ! current_options.get_panel_status() ) {
115 xglViewport(0, 0 , (GLint)(winWidth), (GLint)(winHeight) );
117 xglViewport(0, (GLint)((winHeight)*0.5768), (GLint)(winWidth),
118 (GLint)((winHeight)*0.4232) );
121 panel_hist = current_options.get_panel_status();
125 // convert sgMat4 to MAT3 and print
126 static void print_sgMat4( sgMat4 &in) {
128 for ( i = 0; i < 4; i++ ) {
129 for ( j = 0; j < 4; j++ ) {
130 printf("%10.4f ", in[i][j]);
137 // Update the view parameters
138 void FGView::UpdateViewMath( const FGInterface& f ) {
140 sgVec3 v0, minus_z, sgvec, forward;
144 ssgSetFOV( current_options.get_fov(),
145 current_options.get_fov() * win_ratio );
149 scenery.center = scenery.next_center;
151 // printf("scenery center = %.2f %.2f %.2f\n", scenery.center.x,
152 // scenery.center.y, scenery.center.z);
154 // calculate the cartesion coords of the current lat/lon/0 elev
155 p = Point3D( f.get_Longitude(),
156 f.get_Lat_geocentric(),
157 f.get_Sea_level_radius() * FEET_TO_METER );
159 cur_zero_elev = fgPolarToCart3d(p) - scenery.center;
161 // calculate view position in current FG view coordinate system
162 // p.lon & p.lat are already defined earlier, p.radius was set to
163 // the sea level radius, so now we add in our altitude.
164 if ( f.get_Altitude() * FEET_TO_METER >
165 (scenery.cur_elev + 0.5 * METER_TO_FEET) ) {
166 p.setz( p.radius() + f.get_Altitude() * FEET_TO_METER );
168 p.setz( p.radius() + scenery.cur_elev + 0.5 * METER_TO_FEET );
171 abs_view_pos = fgPolarToCart3d(p);
173 view_pos = abs_view_pos - scenery.center;
175 FG_LOG( FG_VIEW, FG_DEBUG, "Polar view pos = " << p );
176 FG_LOG( FG_VIEW, FG_DEBUG, "Absolute view pos = " << abs_view_pos );
177 FG_LOG( FG_VIEW, FG_DEBUG, "Relative view pos = " << view_pos );
179 // code to calculate LOCAL matrix calculated from Phi, Theta, and
180 // Psi (roll, pitch, yaw) in case we aren't running LaRCsim as our
184 sgSetVec3( rollvec, 0.0, 0.0, 1.0 );
186 sgMakeRotMat4( PHI, f.get_Phi() * RAD_TO_DEG, rollvec );
189 sgSetVec3( pitchvec, 0.0, 1.0, 0.0 );
190 sgMat4 THETA; // pitch
191 sgMakeRotMat4( THETA, f.get_Theta() * RAD_TO_DEG, pitchvec );
194 sgMultMat4( ROT, PHI, THETA );
197 sgSetVec3( yawvec, 1.0, 0.0, 0.0 );
199 sgMakeRotMat4( PSI, -f.get_Psi() * RAD_TO_DEG, yawvec );
201 sgMultMat4( LOCAL, ROT, PSI );
202 // cout << "LOCAL matrix" << endl;
203 // print_sgMat4( LOCAL );
206 f.get_Longitude() * RAD_TO_DEG,
208 -f.get_Latitude() * RAD_TO_DEG );
210 sgSetVec3( local_up, 1.0, 0.0, 0.0 );
211 sgXformVec3( local_up, UP );
212 // cout << "Local Up = " << local_up[0] << "," << local_up[1] << ","
213 // << local_up[2] << endl;
215 // Alternative method to Derive local up vector based on
216 // *geodetic* coordinates
217 // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
218 // printf( " Alt Up = (%.4f, %.4f, %.4f)\n",
219 // alt_up.x, alt_up.y, alt_up.z);
222 sgMultMat4( VIEWo, LOCAL, UP );
223 // cout << "VIEWo matrix" << endl;
224 // print_sgMat4( VIEWo );
226 // generate the sg view up vector
228 sgSetVec3( vec1, 1.0, 0.0, 0.0 );
229 sgXformVec3( view_up, vec1, VIEWo );
231 // generate the pilot offset vector in world coordinates
232 sgVec3 pilot_offset_world;
234 pilot_offset[2], pilot_offset[1], -pilot_offset[0] );
235 sgXformVec3( pilot_offset_world, vec1, VIEWo );
237 // generate the view offset matrix
238 sgMakeRotMat4( VIEW_OFFSET, view_offset * RAD_TO_DEG, view_up );
239 // cout << "VIEW_OFFSET matrix" << endl;
240 // print_sgMat4( VIEW_OFFSET );
242 sgMultMat4( TMP2, VIEWo, VIEW_OFFSET );
243 sgMultMat4( VIEW_ROT, LARC_TO_SSG, TMP2 );
244 // cout << "VIEW_ROT matrix" << endl;
245 // print_sgMat4( VIEW_ROT );
247 sgMakeTransMat4( TRANS,
248 view_pos.x() + pilot_offset_world[0],
249 view_pos.y() + pilot_offset_world[1],
250 view_pos.z() + pilot_offset_world[2] );
252 sgMultMat4( VIEW, VIEW_ROT, TRANS );
254 sgSetVec3( sgvec, 0.0, 0.0, 1.0 );
255 sgXformVec3( forward, sgvec, VIEWo );
256 // cout << "forward = " << forward[0] << ","
257 // << forward[1] << "," << forward[2] << endl;
259 sgMakeRotMat4( TMP, view_offset * RAD_TO_DEG, view_up );
260 sgXformVec3( view_forward, forward, TMP );
261 // cout << "view_forward = " << view_forward[0] << ","
262 // << view_forward[1] << "," << view_forward[2] << endl;
264 // make a vector to the current view position
265 sgSetVec3( v0, view_pos.x(), view_pos.y(), view_pos.z() );
267 // Given a vector pointing straight down (-Z), map into onto the
268 // local plane representing "horizontal". This should give us the
269 // local direction for moving "south".
270 sgSetVec3( minus_z, 0.0, 0.0, -1.0 );
272 sgmap_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
273 sgNormalizeVec3(surface_south);
274 // cout << "Surface direction directly south " << surface_south[0] << ","
275 // << surface_south[1] << "," << surface_south[2] << endl;
277 // now calculate the surface east vector
278 sgMakeRotMat4( TMP, FG_PI_2 * RAD_TO_DEG, view_up );
279 // cout << "sgMat4 TMP" << endl;
280 // print_sgMat4( TMP );
281 sgXformVec3(surface_east, surface_south, TMP);
282 // cout << "Surface direction directly east" << surface_east[0] << ","
283 // << surface_east[1] << "," << surface_east[2] << endl;
284 // cout << "Should be close to zero = "
285 // << sgScalarProductVec3(surface_south, surface_east) << endl;
290 FGView::~FGView( void ) {