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.
23 // (Log is kept at end of this file)
30 #include <Debug/fg_debug.h>
31 #include <Flight/flight.h>
32 #include <Include/fg_constants.h>
33 #include <Math/mat3.h>
34 #include <Math/polar3d.h>
35 #include <Math/vector.h>
36 #include <Scenery/scenery.hxx>
37 #include <Time/fg_time.hxx>
39 #include "options.hxx"
43 // This is a record containing current view parameters
48 fgVIEW::fgVIEW( void ) {
52 // Initialize a view structure
53 void fgVIEW::Init( void ) {
54 fgPrintf( FG_VIEW, FG_INFO, "Initializing View parameters\n");
57 goal_view_offset = 0.0;
61 // Update the view parameters
62 void fgVIEW::Update( fgFLIGHT *f ) {
65 MAT3vec vec, forward, v0, minus_z;
66 MAT3mat R, TMP, UP, LOCAL, VIEW;
67 double theta_x, theta_y, ntmp;
71 scenery.center.x = scenery.next_center.x;
72 scenery.center.y = scenery.next_center.y;
73 scenery.center.z = scenery.next_center.z;
75 // printf("win_ratio = %.2f\n", win_ratio);
77 // calculate sin() and cos() of fov / 2 in X direction;
78 theta_x = (o->fov * win_ratio * DEG_TO_RAD) / 2.0;
79 // printf("theta_x = %.2f\n", theta_x);
80 sin_fov_x = sin(theta_x);
81 cos_fov_x = cos(theta_x);
82 slope_x = sin_fov_x / cos_fov_x;
83 // printf("slope_x = %.2f\n", slope_x);
85 // calculate sin() and cos() of fov / 2 in Y direction;
86 theta_y = (o->fov * DEG_TO_RAD) / 2.0;
87 // printf("theta_y = %.2f\n", theta_y);
88 sin_fov_y = sin(theta_y);
89 cos_fov_y = cos(theta_y);
90 slope_y = sin_fov_y / cos_fov_y;
91 // printf("slope_y = %.2f\n", slope_y);
93 // calculate the cartesion coords of the current lat/lon/0 elev
95 p.lat = FG_Lat_geocentric;
96 p.radius = FG_Sea_level_radius * FEET_TO_METER;
98 cur_zero_elev = fgPolarToCart3d(p);
100 cur_zero_elev.x -= scenery.center.x;
101 cur_zero_elev.y -= scenery.center.y;
102 cur_zero_elev.z -= scenery.center.z;
104 // calculate view position in current FG view coordinate system
105 // p.lon & p.lat are already defined earlier
106 p.radius = FG_Radius_to_vehicle * FEET_TO_METER + 1.0;
108 abs_view_pos = fgPolarToCart3d(p);
110 view_pos.x = abs_view_pos.x - scenery.center.x;
111 view_pos.y = abs_view_pos.y - scenery.center.y;
112 view_pos.z = abs_view_pos.z - scenery.center.z;
114 fgPrintf( FG_VIEW, FG_DEBUG, "Absolute view pos = %.4f, %.4f, %.4f\n",
115 abs_view_pos.x, abs_view_pos.y, abs_view_pos.z);
116 fgPrintf( FG_VIEW, FG_DEBUG, "Relative view pos = %.4f, %.4f, %.4f\n",
117 view_pos.x, view_pos.y, view_pos.z);
119 // Derive the LOCAL aircraft rotation matrix (roll, pitch, yaw)
120 // from FG_T_local_to_body[3][3]
122 // Question: Why is the LaRCsim matrix arranged so differently
123 // than the one we need???
124 LOCAL[0][0] = FG_T_local_to_body_33;
125 LOCAL[0][1] = -FG_T_local_to_body_32;
126 LOCAL[0][2] = -FG_T_local_to_body_31;
128 LOCAL[1][0] = -FG_T_local_to_body_23;
129 LOCAL[1][1] = FG_T_local_to_body_22;
130 LOCAL[1][2] = FG_T_local_to_body_21;
132 LOCAL[2][0] = -FG_T_local_to_body_13;
133 LOCAL[2][1] = FG_T_local_to_body_12;
134 LOCAL[2][2] = FG_T_local_to_body_11;
136 LOCAL[3][0] = LOCAL[3][1] = LOCAL[3][2] = LOCAL[3][3] = 0.0;
138 // printf("LaRCsim LOCAL matrix\n");
139 // MAT3print(LOCAL, stdout);
141 #ifdef OLD_LOCAL_TO_BODY_CODE
142 // old code to calculate LOCAL matrix calculated from Phi,
143 // Theta, and Psi (roll, pitch, yaw)
145 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
146 MAT3rotate(R, vec, FG_Phi);
147 /* printf("Roll matrix\n"); */
148 /* MAT3print(R, stdout); */
150 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
151 /* MAT3mult_vec(vec, vec, R); */
152 MAT3rotate(TMP, vec, FG_Theta);
153 /* printf("Pitch matrix\n"); */
154 /* MAT3print(TMP, stdout); */
157 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
158 /* MAT3mult_vec(vec, vec, R); */
159 /* MAT3rotate(TMP, vec, FG_Psi - FG_PI_2); */
160 MAT3rotate(TMP, vec, -FG_Psi);
161 /* printf("Yaw matrix\n");
162 MAT3print(TMP, stdout); */
163 MAT3mult(LOCAL, R, TMP);
164 // printf("FG derived LOCAL matrix\n");
165 // MAT3print(LOCAL, stdout);
166 #endif // OLD_LOCAL_TO_BODY_CODE
168 // Derive the local UP transformation matrix based on *geodetic*
170 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
171 MAT3rotate(R, vec, FG_Longitude); // R = rotate about Z axis
172 // printf("Longitude matrix\n");
173 // MAT3print(R, stdout);
175 MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
176 MAT3mult_vec(vec, vec, R);
177 MAT3rotate(TMP, vec, -FG_Latitude); // TMP = rotate about X axis
178 // printf("Latitude matrix\n");
179 // MAT3print(TMP, stdout);
181 MAT3mult(UP, R, TMP);
182 // printf("Local up matrix\n");
183 // MAT3print(UP, stdout);
185 MAT3_SET_VEC(local_up, 1.0, 0.0, 0.0);
186 MAT3mult_vec(local_up, local_up, UP);
188 // printf( "Local Up = (%.4f, %.4f, %.4f)\n",
189 // local_up[0], local_up[1], local_up[2]);
191 // Alternative method to Derive local up vector based on
192 // *geodetic* coordinates
193 // alt_up = fgPolarToCart(FG_Longitude, FG_Latitude, 1.0);
194 // printf( " Alt Up = (%.4f, %.4f, %.4f)\n",
195 // alt_up.x, alt_up.y, alt_up.z);
197 // Calculate the VIEW matrix
198 MAT3mult(VIEW, LOCAL, UP);
199 // printf("VIEW matrix\n");
200 // MAT3print(VIEW, stdout);
202 // generate the current up, forward, and fwrd-view vectors
203 MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
204 MAT3mult_vec(view_up, vec, VIEW);
206 MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
207 MAT3mult_vec(forward, vec, VIEW);
208 // printf( "Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1],
211 MAT3rotate(TMP, view_up, view_offset);
212 MAT3mult_vec(view_forward, forward, TMP);
214 // make a vector to the current view position
215 MAT3_SET_VEC(v0, view_pos.x, view_pos.y, view_pos.z);
217 // Given a vector pointing straight down (-Z), map into onto the
218 // local plane representing "horizontal". This should give us the
219 // local direction for moving "south".
220 MAT3_SET_VEC(minus_z, 0.0, 0.0, -1.0);
221 map_vec_onto_cur_surface_plane(local_up, v0, minus_z, surface_south);
222 MAT3_NORMALIZE_VEC(surface_south, ntmp);
223 // printf( "Surface direction directly south %.2f %.2f %.2f\n",
224 // surface_south[0], surface_south[1], surface_south[2]);
226 // now calculate the surface east vector
227 MAT3rotate(TMP, view_up, FG_PI_2);
228 MAT3mult_vec(surface_east, surface_south, TMP);
229 // printf( "Surface direction directly east %.2f %.2f %.2f\n",
230 // surface_east[0], surface_east[1], surface_east[2]);
231 // printf( "Should be close to zero = %.2f\n",
232 // MAT3_DOT_PRODUCT(surface_south, surface_east));
236 // Update the "World to Eye" transformation matrix
237 // This is most useful for view frustum culling
238 void fgVIEW::UpdateWorldToEye( fgFLIGHT *f ) {
239 MAT3mat R_Phi, R_Theta, R_Psi, R_Lat, R_Lon, T_view;
244 MAT3_SET_HVEC(vec, 0.0, 0.0, -1.0, 1.0);
245 MAT3rotate(R_Phi, vec, FG_Phi);
246 // printf("Roll matrix (Phi)\n");
247 // MAT3print(R_Phi, stdout);
250 MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
251 MAT3rotate(R_Theta, vec, FG_Theta);
252 // printf("\nPitch matrix (Theta)\n");
253 // MAT3print(R_Theta, stdout);
256 MAT3_SET_HVEC(vec, 0.0, -1.0, 0.0, 1.0);
257 MAT3rotate(R_Psi, vec, FG_Psi + FG_PI - view_offset );
258 // printf("\nYaw matrix (Psi)\n");
259 // MAT3print(R_Psi, stdout);
262 MAT3_SET_HVEC(vec, 1.0, 0.0, 0.0, 1.0);
263 // R_Lat = rotate about X axis
264 MAT3rotate(R_Lat, vec, FG_Latitude);
265 // printf("\nLatitude matrix\n");
266 // MAT3print(R_Lat, stdout);
269 MAT3_SET_HVEC(vec, 0.0, 0.0, 1.0, 1.0);
270 // R_Lon = rotate about Z axis
271 MAT3rotate(R_Lon, vec, FG_Longitude - FG_PI_2 );
272 // printf("\nLongitude matrix\n");
273 // MAT3print(R_Lon, stdout);
275 // View position in scenery centered coordinates
276 MAT3_SET_HVEC(vec, view_pos.x, view_pos.y, view_pos.z, 1.0);
277 MAT3translate(T_view, vec);
278 // printf("\nTranslation matrix\n");
279 // MAT3print(T_view, stdout);
281 // aircraft roll/pitch/yaw
282 MAT3mult(TMP, R_Phi, R_Theta);
283 MAT3mult(AIRCRAFT, TMP, R_Psi);
284 // printf("\naircraft roll pitch yaw\n");
285 // MAT3print(AIRCRAFT, stdout);
288 MAT3mult(WORLD, R_Lat, R_Lon);
289 // printf("\nworld\n");
290 // MAT3print(WORLD, stdout);
292 MAT3mult(EYE_TO_WORLD, AIRCRAFT, WORLD);
293 MAT3mult(EYE_TO_WORLD, EYE_TO_WORLD, T_view);
294 // printf("\nEye to world\n");
295 // MAT3print(EYE_TO_WORLD, stdout);
297 MAT3invert(WORLD_TO_EYE, EYE_TO_WORLD);
298 // printf("\nWorld to eye\n");
299 // MAT3print(WORLD_TO_EYE, stdout);
301 // printf( "\nview_pos = %.2f %.2f %.2f\n",
302 // view_pos.x, view_pos.y, view_pos.z );
304 // MAT3_SET_HVEC(eye, 0.0, 0.0, 0.0, 1.0);
305 // MAT3mult_vec(vec, eye, EYE_TO_WORLD);
306 // printf("\neye -> world = %.2f %.2f %.2f\n", vec[0], vec[1], vec[2]);
308 // MAT3_SET_HVEC(vec1, view_pos.x, view_pos.y, view_pos.z, 1.0);
309 // MAT3mult_vec(vec, vec1, WORLD_TO_EYE);
310 // printf( "\nabs_view_pos -> eye = %.2f %.2f %.2f\n",
311 // vec[0], vec[1], vec[2]);
316 fgVIEW::~fgVIEW( void ) {
321 // Revision 1.10 1998/05/17 16:59:03 curt
322 // First pass at view frustum culling now operational.
324 // Revision 1.9 1998/05/16 13:08:37 curt
325 // C++ - ified views.[ch]xx
326 // Shuffled some additional view parameters into the fgVIEW class.
327 // Changed tile-radius to tile-diameter because it is a much better
329 // Added a WORLD_TO_EYE transformation to views.cxx. This allows us
330 // to transform world space to eye space for view frustum culling.
332 // Revision 1.8 1998/05/02 01:51:01 curt
333 // Updated polartocart conversion routine.
335 // Revision 1.7 1998/04/30 12:34:20 curt
336 // Added command line rendering options:
337 // enable/disable fog/haze
338 // specify smooth/flat shading
339 // disable sky blending and just use a solid color
340 // enable wireframe drawing mode
342 // Revision 1.6 1998/04/28 01:20:23 curt
343 // Type-ified fgTIME and fgVIEW.
344 // Added a command line option to disable textures.
346 // Revision 1.5 1998/04/26 05:10:04 curt
347 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
349 // Revision 1.4 1998/04/25 22:04:53 curt
350 // Use already calculated LaRCsim values to create the roll/pitch/yaw
351 // transformation matrix (we call it LOCAL)
353 // Revision 1.3 1998/04/25 20:24:02 curt
354 // Cleaned up initialization sequence to eliminate interdependencies
355 // between sun position, lighting, and view position. This creates a
356 // valid single pass initialization path.
358 // Revision 1.2 1998/04/24 00:49:22 curt
359 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
360 // Trying out some different option parsing code.
361 // Some code reorganization.
363 // Revision 1.1 1998/04/22 13:25:45 curt
364 // C++ - ifing the code.
365 // Starting a bit of reorganization of lighting code.
367 // Revision 1.16 1998/04/18 04:11:29 curt
368 // Moved fg_debug to it's own library, added zlib support.
370 // Revision 1.15 1998/02/20 00:16:24 curt
371 // Thursday's tweaks.
373 // Revision 1.14 1998/02/09 15:07:50 curt
376 // Revision 1.13 1998/02/07 15:29:45 curt
377 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
378 // <chotchkiss@namg.us.anritsu.com>
380 // Revision 1.12 1998/01/29 00:50:28 curt
381 // Added a view record field for absolute x, y, z position.
383 // Revision 1.11 1998/01/27 00:47:58 curt
384 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
385 // system and commandline/config file processing code.
387 // Revision 1.10 1998/01/19 19:27:09 curt
388 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
389 // This should simplify things tremendously.
391 // Revision 1.9 1998/01/13 00:23:09 curt
392 // Initial changes to support loading and management of scenery tiles. Note,
393 // there's still a fair amount of work left to be done.
395 // Revision 1.8 1997/12/30 22:22:33 curt
396 // Further integration of event manager.
398 // Revision 1.7 1997/12/30 20:47:45 curt
399 // Integrated new event manager with subsystem initializations.
401 // Revision 1.6 1997/12/22 04:14:32 curt
402 // Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
404 // Revision 1.5 1997/12/18 04:07:02 curt
405 // Worked on properly translating and positioning the sky dome.
407 // Revision 1.4 1997/12/17 23:13:36 curt
408 // Began working on rendering a sky.
410 // Revision 1.3 1997/12/15 23:54:50 curt
411 // Add xgl wrappers for debugging.
412 // Generate terrain normals on the fly.
414 // Revision 1.2 1997/12/10 22:37:48 curt
415 // Prepended "fg" on the name of all global structures that didn't have it yet.
416 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
418 // Revision 1.1 1997/08/27 21:31:17 curt